Add getDeckStats action

This commit is contained in:
郑佩茹 2022-05-03 15:59:33 -06:00
parent fe8b221a93
commit 137554f522
3 changed files with 72 additions and 0 deletions

View File

@ -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 #### Graphical Actions
* **guiBrowse** * **guiBrowse**

View File

@ -346,6 +346,21 @@ class AnkiConnect:
except NotFoundError: except NotFoundError:
raise NotFoundError('Note was not found: {}'.format(note_id)) 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 # Miscellaneous
# #
@ -617,6 +632,18 @@ class AnkiConnect:
collection.decks.remConf(configId) collection.decks.remConf(configId)
return True 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() @util.api()
def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None, deleteExisting=True): def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None, deleteExisting=True):

View File

@ -67,3 +67,8 @@ def test_removedDeckConfigId_fails_with_invalid_id(session_with_profile_loaded):
new_config_id = ac.cloneDeckConfigId(cloneFrom=1, name="test") 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 True
assert ac.removeDeckConfigId(configId=new_config_id) is False 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"