From 27a8678f888d0f0ba46048108c84c3179c65b820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Harsco=C3=ABt?= Date: Sat, 6 Mar 2021 18:58:40 +0100 Subject: [PATCH] added getNumCardsReviewedByDay action (#239) * added getNumCardsReviewedByDay action * added sample in statistics.md * fix bad quotes and add basic test --- actions/statistics.md | 23 +++++++++++++++++++++++ plugin/__init__.py | 5 +++++ tests/test_stats.py | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/actions/statistics.md b/actions/statistics.md index 0ff29b0..c02dad5 100644 --- a/actions/statistics.md +++ b/actions/statistics.md @@ -20,6 +20,29 @@ } ``` +* **getNumCardsReviewedByDay** + + Gets the number of cards reviewed as a list of pairs of `(dateString, number)` + + *Sample request*: + ```json + { + "action": "getNumCardsReviewedByDay", + "version": 6, + } + ``` + + *Sample result*: + ```json + { + "result": [ + ["2021-02-28", 124], + ["2021-02-27", 261] + ], + "error": null + } + ``` + * **getCollectionStatsHTML** Gets the collection statistics report diff --git a/plugin/__init__.py b/plugin/__init__.py index 0da6d44..5a4bac9 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -351,6 +351,11 @@ class AnkiConnect: def getNumCardsReviewedToday(self): return self.database().scalar('select count() from revlog where id > ?', (self.scheduler().dayCutoff - 86400) * 1000) + @util.api() + def getNumCardsReviewedByDay(self): + return self.database().all('select date(id/1000 - ?, "unixepoch", "localtime") as day, count() from revlog group by day order by day desc', + int(time.strftime("%H", time.localtime(self.scheduler().dayCutoff))) * 3600) + @util.api() def getCollectionStatsHTML(self, wholeCollection=True): diff --git a/tests/test_stats.py b/tests/test_stats.py index 0aa1475..3b80ac7 100755 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -28,6 +28,10 @@ class TestStats(unittest.TestCase): result = util.invoke('getNumCardsReviewedToday') self.assertIsInstance(result, int) + # getNumCardsReviewedByDay + result = util.invoke('getNumCardsReviewedByDay') + self.assertIsInstance(result, list) + # collectionStats result = util.invoke('getCollectionStatsHTML') self.assertIsInstance(result, str)