From 9b52c8cbac2ebece517b37fd38f1497cbea1951b Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 17 Dec 2024 21:44:40 +0100 Subject: [PATCH] fix(folderstats): fixes folder stats not being populated --- API/download_metrics.py | 8 +++- src/app.py | 81 ++++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 27 deletions(-) diff --git a/API/download_metrics.py b/API/download_metrics.py index d33645d0..eba15bae 100644 --- a/API/download_metrics.py +++ b/API/download_metrics.py @@ -1,5 +1,6 @@ # Standard library imports from datetime import datetime +from typing import Optional # Third party imports from fastapi import APIRouter, Depends, HTTPException, Query @@ -33,6 +34,11 @@ def get_stats( description="Group by: day, month, or quarter", regex=r"^(day|month|quarter|year)$", ), + folder: Optional[str] = Query( + None, + description="Folder to filter metrics by", + example="TM", + ), _: bool = Depends(staff_required), ): """ @@ -59,4 +65,4 @@ def get_stats( ) metrics = DownloadMetrics() - return metrics.get_summary_stats(start_date, end_date, group_by) + return metrics.get_summary_stats(start_date, end_date, group_by, folder) diff --git a/src/app.py b/src/app.py index fb5e82d5..c9b2662e 100644 --- a/src/app.py +++ b/src/app.py @@ -29,7 +29,7 @@ import sys import time import uuid -from collections import namedtuple +from collections import Counter, namedtuple from datetime import datetime, timedelta, timezone from json import dumps from json import loads as json_loads @@ -2226,32 +2226,61 @@ def __init__(self) -> None: self.d_b = Database(dbdict) self.con, self.cur = self.d_b.connect() - def get_summary_stats(self, start_date, end_date, group_by): - """ - Get summary metrics for raw-data-api downlaods - """ - - select_query = f""" - SELECT - date_trunc('{group_by}', date) as kwdate, - SUM((summary->>'downloads_count')::numeric) as total_downloads_count, - SUM((summary->>'uploads_count')::numeric) as total_uploads_count, - SUM((summary->>'unique_users')::numeric) as total_unique_users, - SUM((summary->>'unique_downloads')::numeric) as total_unique_downloads, - SUM((summary->>'interactions_count')::numeric) as total_interactions_count, - SUM((summary->>'upload_size')::numeric) as total_upload_size, - SUM((summary->>'download_size')::numeric) as total_download_size - FROM - metrics - WHERE - date BETWEEN '{start_date}' AND '{end_date}' - GROUP BY - kwdate - ORDER BY - kwdate - """ + def get_summary_stats(self, start_date, end_date, group_by, folder=None): + """ + Get summary metrics for raw-data-api downloads + """ + if folder: + select_query = f""" + SELECT + date_trunc('{group_by}', date) as kwdate, + SUM((folders->'{folder}'->>'downloads_count')::numeric) as total_downloads_count, + SUM((folders->'{folder}'->>'uploads_count')::numeric) as total_uploads_count, + SUM((folders->'{folder}'->>'unique_users')::numeric) as total_unique_users, + SUM((folders->'{folder}'->>'unique_downloads')::numeric) as total_unique_downloads, + SUM((folders->'{folder}'->>'interactions_count')::numeric) as total_interactions_count, + SUM((folders->'{folder}'->>'upload_size')::numeric) as total_upload_size, + SUM((folders->'{folder}'->>'download_size')::numeric) as total_download_size, + JSONB_AGG((folders->'{folder}'->>'locations')::json) as total_locations + FROM + metrics + WHERE + date BETWEEN '{start_date}' AND '{end_date}' + GROUP BY + kwdate + ORDER BY + kwdate + """ + else: + select_query = f""" + SELECT + date_trunc('{group_by}', date) as kwdate, + SUM((summary->>'downloads_count')::numeric) as total_downloads_count, + SUM((summary->>'uploads_count')::numeric) as total_uploads_count, + SUM((summary->>'unique_users')::numeric) as total_unique_users, + SUM((summary->>'unique_downloads')::numeric) as total_unique_downloads, + SUM((summary->>'interactions_count')::numeric) as total_interactions_count, + SUM((summary->>'upload_size')::numeric) as total_upload_size, + SUM((summary->>'download_size')::numeric) as total_download_size, + JSONB_AGG((summary->>'locations')::json) as total_locations + FROM + metrics + WHERE + date BETWEEN '{start_date}' AND '{end_date}' + GROUP BY + kwdate + ORDER BY + kwdate + """ self.cur.execute(select_query) result = self.cur.fetchall() self.d_b.close_conn() - return [dict(item) for item in result] + result_lists = [] + for item in result: + item["total_locations"] = dict( + sum((Counter(loc) for loc in item["total_locations"]), Counter()) + ) + result_lists.append(dict(item)) + + return result_lists