diff --git a/.github/workflows/automate-metrics.yaml b/.github/workflows/automate-metrics.yaml index 58c291f7..cc6fe279 100644 --- a/.github/workflows/automate-metrics.yaml +++ b/.github/workflows/automate-metrics.yaml @@ -2,9 +2,6 @@ name: Update User Metrics on: workflow_dispatch: - schedule: - # Weekly Every Monday at 1PM UTC - - cron: '0 13 * * 1' env: portal_id: ${{ secrets.GA4_PORTAL_ID }} @@ -32,17 +29,9 @@ jobs: pip install google-analytics-data - name: Get and Write Metrics to JSON - run: python get-metrics.py + run: | + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/metrics-api/.github/workflows/get-metrics.py + python get_metrics.py - name: Write Markdown File run: python write-metrics-md.py - - - name: Push Changes - if: steps.Get-and-Write-Metrics.outputs == 1 # Conditional step execution - run: | - git config --global user.name "${{ github.actor }}" - git config --global user.email "${{ github.actor }}@users.noreply.github.com" - git add user_metrics.json - git add ../portal/metrics.md - git commit -m "Update user metrics" - git push diff --git a/.github/workflows/get-metrics.py b/.github/workflows/get-metrics.py index 2e74be38..32ed0d8e 100644 --- a/.github/workflows/get-metrics.py +++ b/.github/workflows/get-metrics.py @@ -10,14 +10,6 @@ def _run_total_users_report(property_id): - """Fetches total users for a given property ID - - Args: - property_id: The Google Analytics 4 property ID - - Returns: - int: The total number of active users - """ client = BetaAnalyticsDataClient() @@ -38,48 +30,14 @@ def _run_total_users_report(property_id): def get_metrics(portal_id, foundations_id, cookbooks_id): - """Retrieves total users for specified GA4 properties and writes to file if changes are significant.""" - metrics_dict = {} metrics_dict['Portal'] = _run_total_users_report(str(portal_id)) metrics_dict['Foundations'] = _run_total_users_report(str(foundations_id)) metrics_dict['Cookbooks'] = _run_total_users_report(str(cookbooks_id)) - return metrics_dict # Return the metrics dictionary - - -def write_metrics(metrics_dict): - """Reads existing metrics, compares for significant change, and writes to file if necessary.""" - - # Read existing user counts (handle potential file absence) - try: - with open('user_metrics.json') as f: - user_data = json.load(f) - except FileNotFoundError: - user_data = {} - - # Define a threshold for significant change (adjust as needed) - threshold = 100 - has_significant_change = False - for property, user_count in metrics_dict.items(): - existing_count = user_data.get(property, 0) - if abs(existing_count - user_count) > threshold: - user_data[property] = user_count - has_significant_change = True - - # Write to file if significant change detected - if has_significant_change: - with open('user_metrics.json', 'w') as outfile: - json.dump(metrics_dict, outfile) - return 1 # Signals significant change - else: - return 0 # Signals no significant change + with open('user_metrics.json', 'w') as outfile: + json.dump(metrics_dict, outfile) if __name__ == '__main__': - metrics = get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) - exit_code = write_metrics(metrics) - if exit_code == 1: - print('Significant change detected in user metrics.') - else: - print('No significant change in user metrics.') + get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID) diff --git a/.github/workflows/nightly-build.yaml b/.github/workflows/nightly-build.yaml index f2ab7753..4fe75d41 100644 --- a/.github/workflows/nightly-build.yaml +++ b/.github/workflows/nightly-build.yaml @@ -6,6 +6,9 @@ on: - cron: '0 0 * * *' # Daily “At 00:00” jobs: + update-metrics: + uses: automate-metrics.yaml@main + build: if: ${{ github.repository_owner == 'ProjectPythia' }} uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main @@ -20,3 +23,10 @@ jobs: uses: ./.github/workflows/sphinx-link-checker.yaml with: path_to_source: 'portal' + + deploy: + needs: build + uses: ProjectPythia/cookbook-actions/.github/workflows/deploy-book.yaml@main + with: + cname: projectpythia.org + publish_dir: 'portal/_build/html' diff --git a/.github/workflows/publish-site.yaml b/.github/workflows/publish-site.yaml index a8cd22f3..1d52a773 100644 --- a/.github/workflows/publish-site.yaml +++ b/.github/workflows/publish-site.yaml @@ -8,6 +8,9 @@ on: workflow_dispatch: jobs: + update-metrics: + uses: automate-metrics.yaml@main + build: uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main with: diff --git a/.github/workflows/trigger-site-build.yaml b/.github/workflows/trigger-site-build.yaml index ed8488cd..03363aaf 100644 --- a/.github/workflows/trigger-site-build.yaml +++ b/.github/workflows/trigger-site-build.yaml @@ -3,6 +3,9 @@ on: pull_request: jobs: + update-metrics: + uses: automate-metrics.yaml@main + build: uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main with: diff --git a/.github/workflows/write-metrics-md.py b/.github/workflows/write-metrics-md.py index 63fe11a2..7c4c5082 100644 --- a/.github/workflows/write-metrics-md.py +++ b/.github/workflows/write-metrics-md.py @@ -1,3 +1,4 @@ +import datetime import json @@ -9,20 +10,19 @@ def process_user_data(user_data_file, markdown_file): user_data_file: Path to the JSON file containing user data. markdown_file: Path to the output markdown file. """ + now = datetime.datetime.now() with open(user_data_file, 'r') as f: user_data = json.load(f) - # table_header = '| Portal | Foundations | Cookbooks |\n' - # table_row = f"| {' | '.join([str(user_data[key]) for key in user_data])} |\n" - # table = table_header + table_row - # Write processed data to markdown file with open(markdown_file, 'w') as f: f.write('# Metrics \n\n') f.write('Total Users:\n\n') for key in user_data: f.write(f'{key}: {user_data[key]}\n') + f.write('\n') + f.write(f'Last Updated: {now}\n') f.close() @@ -30,4 +30,3 @@ def process_user_data(user_data_file, markdown_file): user_data_file = 'user_metrics.json' markdown_file = '../../portal/metrics.md' process_user_data(user_data_file, markdown_file) - print('User data report generated: ', markdown_file) diff --git a/portal/metrics.md b/portal/metrics.md index a4c0e8a6..b05321b8 100644 --- a/portal/metrics.md +++ b/portal/metrics.md @@ -1,7 +1 @@ # Metrics - -Total Users: - -Portal: 0 -Foundations: 0 -Cookbooks: 0