Skip to content

Commit

Permalink
Merge pull request #4983 from GSA/metrics-file-with-date
Browse files Browse the repository at this point in the history
add date to S3 so receiving end knows report time
  • Loading branch information
FuhuXia authored Nov 25, 2024
2 parents 36f85d8 + 4dd05b6 commit 6463553
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions metrics/datagov_metrics/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 6463553

Please sign in to comment.