Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Add getDeckStats action #318

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,46 @@ corresponding to when the API was available for use.
}
```

* **getDeckStats**

Gets statistics such as total cards and cards due for the given decks.

*Sample request*:
```json
{
"action": "getDeckStats",
"version": 6,
"params": {
"decks": ["Japanese::JLPT N5", "Easy Spanish"]
}
}
```

*Sample result*:
```json
{
"result": {
"1651445861967": {
"deck_id": 1651445861967,
"name": "Japanese::JLPT N5",
"new_count": 20,
"learn_count": 0,
"review_count": 0,
"total_in_deck": 1506
},
"1651445861960": {
"deck_id": 1651445861960,
"name": "Easy Spanish",
"new_count": 26,
"learn_count": 10,
"review_count": 5,
"total_in_deck": 852
}
},
"error": null
}
```

#### Graphical Actions

* **guiBrowse**
Expand Down
27 changes: 27 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ def getNote(self, note_id: int) -> Note:
except NotFoundError:
raise NotFoundError('Note was not found: {}'.format(note_id))

def deckStatsToJson(self, due_tree):
return {'deck_id': due_tree.deck_id,
'name': due_tree.name,
'new_count': due_tree.new_count,
'learn_count': due_tree.learn_count,
'review_count': due_tree.review_count,
'total_in_deck': due_tree.total_in_deck}

def collectDeckTreeChildren(self, parent_node):
allNodes = {parent_node.deck_id: parent_node}
for child in parent_node.children:
for deckId, childNode in self.collectDeckTreeChildren(child).items():
allNodes[deckId] = childNode
return allNodes

#
# Miscellaneous
#
Expand Down Expand Up @@ -617,6 +632,18 @@ def removeDeckConfigId(self, configId):
collection.decks.remConf(configId)
return True

@util.api()
def getDeckStats(self, decks):
collection = self.collection()
scheduler = self.scheduler()
responseDict = {}
deckIds = list(map(lambda d: collection.decks.id(d), decks))

allDeckNodes = self.collectDeckTreeChildren(scheduler.deck_due_tree())
for deckId, deckNode in allDeckNodes.items():
if deckId in deckIds:
responseDict[deckId] = self.deckStatsToJson(deckNode)
return responseDict

@util.api()
def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None, deleteExisting=True):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_decks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ def test_removedDeckConfigId_fails_with_invalid_id(session_with_profile_loaded):
new_config_id = ac.cloneDeckConfigId(cloneFrom=1, name="test")
assert ac.removeDeckConfigId(configId=new_config_id) is True
assert ac.removeDeckConfigId(configId=new_config_id) is False


def test_getDeckStats(session_with_profile_loaded):
result = ac.getDeckStats(decks=["Default"])
assert result["name"] == "Default"