You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by hu...@apache.org on 2021/09/18 22:35:01 UTC

[incubator-ponymail-foal] 01/01: refactor get_years, add activity stats and rename

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch humbedooh/monthly_stats
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git

commit f16ead4003fcb4417e4e8cd01252c76832feb0f3
Author: Daniel Gruno <hu...@apache.org>
AuthorDate: Sat Sep 18 17:34:54 2021 -0500

    refactor get_years, add activity stats and rename
---
 server/plugins/messages.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/server/plugins/messages.py b/server/plugins/messages.py
index cd3ed7b..e1077ba 100644
--- a/server/plugins/messages.py
+++ b/server/plugins/messages.py
@@ -411,8 +411,8 @@ async def get_list_stats(session, maxage="90d", admin=False):
     return lists
 
 
-async def get_years(session, query_defuzzed):
-    """ Fetches the oldest and youngest email, returns the years between them """
+async def get_activity_span(session, query_defuzzed):
+    """ Fetches the activity span of a search as well as active months within that span """
 
     # Fetch any private lists included in search results
     fuzz_private_only = dict(query_defuzzed)
@@ -445,7 +445,7 @@ async def get_years(session, query_defuzzed):
             {"bool": {"should": [{"term": {"private": False}}, {"terms": {"list_raw": private_lists_accessible}}]}}
         ]
 
-    # Get oldest and youngest doc in single scan
+    # Get oldest and youngest doc in single scan, as well as a monthly histogram
     res = await session.database.search(
         index=session.database.dbs.mbox,
         size=0,
@@ -453,18 +453,28 @@ async def get_years(session, query_defuzzed):
             "aggs": {
                 "first": {"min": {"field": "epoch"}},
                 "last": {"max": {"field": "epoch"}},
+                "active_months": {
+                    "date_histogram": {
+                        "field": "date",
+                        "calendar_interval": "month",
+                        "format": "yyyy-MM"
+                    }
+                }
             },
         }
     )
 
     oldest = datetime.datetime.fromtimestamp(0)
     youngest = datetime.datetime.fromtimestamp(0)
+    monthly_activity = {}
     if res["aggregations"]:
         aggs = res["aggregations"]
         oldest = datetime.datetime.fromtimestamp(aggs["first"]["value"] or 0)
         youngest = datetime.datetime.fromtimestamp(aggs["last"]["value"] or 0)
+        for bucket in aggs["active_months"].get("buckets", []):
+            monthly_activity[bucket["key_as_string"]] = bucket["doc_count"]
 
-    return oldest.year, youngest.year, oldest.month, youngest.month
+    return oldest, youngest, monthly_activity
 
 
 class ThreadConstructor: