Collection stats (#166)

* Add endpoint to retrieve collection stats report

* Create separate test for statistics

* Add test for collectionStats

* Fixed typo

* Change endpoint name to use a verb

* Change name to getCollectionStatsHTML
This commit is contained in:
Scott Noyes 2020-05-03 14:06:43 -05:00 committed by GitHub
parent 457e76e720
commit faba701251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 24 deletions

View File

@ -14,6 +14,7 @@ with the latest stable (2.1.x) releases of Anki; older versions (2.0.x and below
* [Sample Invocation](#sample-invocation) * [Sample Invocation](#sample-invocation)
* [Supported Actions](#supported-actions) * [Supported Actions](#supported-actions)
* [Miscellaneous](#miscellaneous) * [Miscellaneous](#miscellaneous)
* [Statistics](#statistics)
* [Decks](#decks) * [Decks](#decks)
* [Models](#models) * [Models](#models)
* [Notes](#notes) * [Notes](#notes)
@ -297,26 +298,6 @@ guarantee that your application continues to function properly in the future.
} }
``` ```
* **getNumCardsReviewedToday**
Gets the count of cards that have been reviewed in the current day (with day start time as configured by user in anki)
*Sample request*:
```json
{
"action": "getNumCardsReviewedToday",
"version": 6
}
```
*Sample result*:
```json
{
"result": 0,
"error": null
}
```
* **exportPackage** * **exportPackage**
Exports a given deck in `.apkg` format. Returns `true` if successful or `false` otherwise. The optional property Exports a given deck in `.apkg` format. Returns `true` if successful or `false` otherwise. The optional property
@ -367,6 +348,48 @@ guarantee that your application continues to function properly in the future.
} }
``` ```
#### Statistics
* **getNumCardsReviewedToday**
Gets the count of cards that have been reviewed in the current day (with day start time as configured by user in anki)
*Sample request*:
```json
{
"action": "getNumCardsReviewedToday",
"version": 6
}
```
*Sample result*:
```json
{
"result": 0,
"error": null
}
```
* **getCollectionStatsHTML**
Gets the collection statistics report
*Sample request*:
```json
{
"action": "getCollectionStatsHTML",
"version": 6
}
```
*Sample result*:
```json
{
"error": null,
"result": "<center> lots of HTML here </center>",
}
```
#### Decks #### Decks
* **deckNames** * **deckNames**

View File

@ -298,6 +298,11 @@ class AnkiConnect:
return self.database().scalar('select count() from revlog where id > ?', (self.scheduler().dayCutoff - 86400) * 1000) return self.database().scalar('select count() from revlog where id > ?', (self.scheduler().dayCutoff - 86400) * 1000)
@util.api()
def getCollectionStatsHTML(self):
return self.collection().stats().report()
# #
# Decks # Decks
# #

View File

@ -30,10 +30,6 @@ class TestMisc(unittest.TestCase):
self.assertIsNone(result['error']) self.assertIsNone(result['error'])
self.assertEqual(result['result'], 6) self.assertEqual(result['result'], 6)
# getNumCardsReviewedToday
result = util.invoke('getNumCardsReviewedToday')
self.assertIsInstance(result, int)
# exportPackage # exportPackage
fd, newname = tempfile.mkstemp(prefix='testexport', suffix='.apkg') fd, newname = tempfile.mkstemp(prefix='testexport', suffix='.apkg')
os.close(fd) os.close(fd)

21
tests/test_stats.py Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
import os
import tempfile
import unittest
import util
class TestMisc(unittest.TestCase):
def runTest(self):
# getNumCardsReviewedToday
result = util.invoke('getNumCardsReviewedToday')
self.assertIsInstance(result, int)
# collectionStats
result = util.invoke('getCollectionStatsHTML')
self.assertIsInstance(result, str)
if __name__ == '__main__':
unittest.main()