Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(folderstats): fixes folder stats not being populated #286

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion API/download_metrics.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
):
"""
Expand All @@ -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)
81 changes: 55 additions & 26 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading