diff --git a/.github/workflows/get-metrics.py b/.github/workflows/get-metrics.py new file mode 100644 index 00000000..4096a8e1 --- /dev/null +++ b/.github/workflows/get-metrics.py @@ -0,0 +1,31 @@ +import logging +import os + +import google +from google.analytics.data_v1beta import BetaAnalyticsDataClient + +PRIVATE_KEY_ID = os.environ.get('PRIVATE_KEY_ID') +PRIVATE_KEY = os.environ.get('PRIVATE_KEY') + +credentials_dict = { + 'type': 'service_account', + 'project_id': 'cisl-vast-pythia', + 'private_key_id': PRIVATE_KEY_ID, + 'private_key': PRIVATE_KEY, + 'client_email': 'pythia-metrics-api@cisl-vast-pythia.iam.gserviceaccount.com', + 'client_id': '113402578114110723940', + 'auth_uri': 'https://accounts.google.com/o/oauth2/auth', + 'token_uri': 'https://oauth2.googleapis.com/token', + 'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs', + 'client_x509_cert_url': 'https://www.googleapis.com/robot/v1/metadata/x509/pythia-metrics-api%40cisl-vast-pythia.iam.gserviceaccount.com', + 'universe_domain': 'googleapis.com', +} + +try: + client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict) +except google.auth.exceptions.MalformedError as e: + print('Malformed Error:', repr(e)) + logging.error('Malformed Error:', repr(e)) + logging.error('Credentials dict:', credentials_dict) + + print(credentials_dict) diff --git a/.github/workflows/nightly-build.yaml b/.github/workflows/nightly-build.yaml index f2ab7753..9a530144 100644 --- a/.github/workflows/nightly-build.yaml +++ b/.github/workflows/nightly-build.yaml @@ -6,7 +6,30 @@ on: - cron: '0 0 * * *' # Daily “At 00:00” jobs: + automate-metrics: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Automate Metrics + env: + PORTAL_ID: ${{ secrets.PORTAL_ID }} + FOUNDATIONS_ID: ${{ secrets.FOUNDATIONS_ID }} + COOKBOOKS_ID: ${{ secrets.COOKBOOKS_ID }} + PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} + PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }} + run: | + python -m venv analytics-api + source analytics-api/bin/activate + pip install google-analytics-data cartopy matplotlib + + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/get-metrics.py + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/write-metrics-md.py + + python get-metrics.py + python write-metrics-md.py + build: + needs: automate-metrics if: ${{ github.repository_owner == 'ProjectPythia' }} uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main with: @@ -20,3 +43,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..3798b151 100644 --- a/.github/workflows/publish-site.yaml +++ b/.github/workflows/publish-site.yaml @@ -8,7 +8,30 @@ on: workflow_dispatch: jobs: + automate-metrics: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Automate Metrics + env: + PORTAL_ID: ${{ secrets.PORTAL_ID }} + FOUNDATIONS_ID: ${{ secrets.FOUNDATIONS_ID }} + COOKBOOKS_ID: ${{ secrets.COOKBOOKS_ID }} + PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} + PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }} + run: | + python -m venv analytics-api + source analytics-api/bin/activate + pip install google-analytics-data cartopy matplotlib + + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/get-metrics.py + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/write-metrics-md.py + + python get-metrics.py + python write-metrics-md.py + build: + needs: automate-metrics uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main with: environment_file: 'environment.yml' diff --git a/.github/workflows/trigger-preview.yaml b/.github/workflows/trigger-preview.yaml index 05463fcf..5c4dcced 100644 --- a/.github/workflows/trigger-preview.yaml +++ b/.github/workflows/trigger-preview.yaml @@ -8,7 +8,30 @@ on: - completed jobs: + automate-metrics: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Automate Metrics + env: + PORTAL_ID: ${{ secrets.PORTAL_ID }} + FOUNDATIONS_ID: ${{ secrets.FOUNDATIONS_ID }} + COOKBOOKS_ID: ${{ secrets.COOKBOOKS_ID }} + PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} + PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }} + run: | + python -m venv analytics-api + source analytics-api/bin/activate + pip install google-analytics-data cartopy matplotlib + + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/get-metrics.py + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/write-metrics-md.py + + python get-metrics.py + python write-metrics-md.py + find-pull-request: + needs: automate-metrics uses: ProjectPythia/cookbook-actions/.github/workflows/find-pull-request.yaml@main deploy-preview: needs: find-pull-request diff --git a/.github/workflows/trigger-site-build.yaml b/.github/workflows/trigger-site-build.yaml index ed8488cd..eb3e272c 100644 --- a/.github/workflows/trigger-site-build.yaml +++ b/.github/workflows/trigger-site-build.yaml @@ -3,6 +3,28 @@ on: pull_request: jobs: + automate-metrics: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Automate Metrics + env: + PORTAL_ID: ${{ secrets.PORTAL_ID }} + FOUNDATIONS_ID: ${{ secrets.FOUNDATIONS_ID }} + COOKBOOKS_ID: ${{ secrets.COOKBOOKS_ID }} + PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} + PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }} + run: | + python -m venv analytics-api + source analytics-api/bin/activate + pip install google-analytics-data cartopy matplotlib + + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/get-metrics.py + curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/write-metrics-md.py + + python get-metrics.py + python write-metrics-md.py + 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 new file mode 100644 index 00000000..961feb10 --- /dev/null +++ b/.github/workflows/write-metrics-md.py @@ -0,0 +1,38 @@ +import datetime +import json + + +def process_user_data(user_data_file, bar_plot_file, map_plot_file, markdown_file): + """ + Reads user data from a JSON file and writes it to a markdown file. + + Args: + 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) + + # Write processed data to markdown file + with open(markdown_file, 'w') as f: + f.write('# Metrics \n\n') + f.write(f'Last Updated: {now}\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.write('\n\n') + f.write('') + f.write('\n') + f.close() + + +if __name__ == '__main__': + user_data_file = 'user_metrics.json' + bar_plot_file = '../.github/workflows/bypage.png' + map_plot_file = '../.github/workflows/bycountry.png' + markdown_file = 'portal/metrics.md' + process_user_data(user_data_file, bar_plot_file, map_plot_file, markdown_file) diff --git a/.gitignore b/.gitignore index 1babb552..4fb65167 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,8 @@ portal/code_of_conduct.md portal/resource-gallery.md portal/resource-gallery/*.md resource-gallery-submission-input.json + +# Analytics +.github/workflows/analytics-api/ +.github/workflows/*.json +.github/workflows/*.png diff --git a/portal/_templates/footer-menu.html b/portal/_templates/footer-menu.html index 3a17455b..919cc9c3 100644 --- a/portal/_templates/footer-menu.html +++ b/portal/_templates/footer-menu.html @@ -8,6 +8,7 @@