From faba70125199e6357858480ea40273db7100d147 Mon Sep 17 00:00:00 2001 From: Scott Noyes Date: Sun, 3 May 2020 14:06:43 -0500 Subject: [PATCH] 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 --- README.md | 63 +++++++++++++++++++++++++++++++-------------- plugin/__init__.py | 5 ++++ tests/test_misc.py | 4 --- tests/test_stats.py | 21 +++++++++++++++ 4 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 tests/test_stats.py diff --git a/README.md b/README.md index d953ee3..4902ddf 100644 --- a/README.md +++ b/README.md @@ -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) * [Supported Actions](#supported-actions) * [Miscellaneous](#miscellaneous) + * [Statistics](#statistics) * [Decks](#decks) * [Models](#models) * [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** 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": "
lots of HTML here
", + } + ``` + #### Decks * **deckNames** diff --git a/plugin/__init__.py b/plugin/__init__.py index f265b16..9e1b91e 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -298,6 +298,11 @@ class AnkiConnect: 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 # diff --git a/tests/test_misc.py b/tests/test_misc.py index 97c68c5..e0762bc 100755 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -30,10 +30,6 @@ class TestMisc(unittest.TestCase): self.assertIsNone(result['error']) self.assertEqual(result['result'], 6) - # getNumCardsReviewedToday - result = util.invoke('getNumCardsReviewedToday') - self.assertIsInstance(result, int) - # exportPackage fd, newname = tempfile.mkstemp(prefix='testexport', suffix='.apkg') os.close(fd) diff --git a/tests/test_stats.py b/tests/test_stats.py new file mode 100644 index 0000000..0f33487 --- /dev/null +++ b/tests/test_stats.py @@ -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()