From 4dd05b6fc244485da87e05542ca2ddf9920a9a11 Mon Sep 17 00:00:00 2001 From: Fuhu Xia Date: Mon, 25 Nov 2024 10:51:08 -0500 Subject: [PATCH] add date to S3 so receiving end knows report time --- metrics/datagov_metrics/ga.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/metrics/datagov_metrics/ga.py b/metrics/datagov_metrics/ga.py index 48b8db22a..44a90eb9d 100644 --- a/metrics/datagov_metrics/ga.py +++ b/metrics/datagov_metrics/ga.py @@ -2,6 +2,7 @@ import calendar import io import csv +from functools import lru_cache from datagov_metrics.s3_util import put_data_to_s3 import requests @@ -19,7 +20,7 @@ analytics = build("analyticsdata", "v1beta", credentials=credentials) properties = analytics.properties() - +@lru_cache() def date_range_last_month(): last_month = datetime.today().replace(day=1) - timedelta(days=1) last_day = calendar.monthrange(last_month.year, last_month.month)[1] @@ -187,11 +188,15 @@ def write_data_to_csv(response): def main(): reports = setup_reports() + end_date = date_range_last_month()[0]["endDate"] # for example, 2024-10-31 for report in reports: print(f"Fetching report: {report}") fetched_report = fetch_report(reports[report]) csv_data = write_data_to_csv(fetched_report) - put_data_to_s3(f"{report}.csv", csv_data) + put_data_to_s3(f"{report}.{end_date}.csv", csv_data) + + # This file get refreshed every time at the end of report generation + put_data_to_s3("report-end-date.txt", end_date) if __name__ == "__main__":