Skip to content

Create Summary (Week) #3

Create Summary (Week)

Create Summary (Week) #3

name: Create Summary (Week)
on:
schedule:
- cron: '0 7 * * 1'
workflow_dispatch:
inputs:
year:
description: 'Year to create the summary of'
required: true
week:
description: 'ISO week to create the summary of'
required: true
jobs:
publish:
runs-on: ubuntu-latest
name: Create Summary
steps:
- name: Generate access token
id: generate_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.SURVEY_SUMMARY_APP_ID }}
private_key: ${{ secrets.SURVEY_SUMMARY_APP_PRIVATE_KEY }}
installation_retrieval_mode: "repository"
installation_retrieval_payload: "OpenTTD/survey-web"
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.generate_token.outputs.token }}
- name: Install rclone
shell: bash
run: |
curl -sL https://rclone.org/install.sh | sudo bash
rclone config create --no-obscure openttd s3 \
provider Cloudflare \
access_key_id ${{ secrets.R2_SURVEY_ACCESS_KEY_ID }} \
secret_access_key ${{ secrets.R2_SURVEY_SECRET_ACCESS_KEY }} \
endpoint ${{ secrets.R2_SURVEY_ENDPOINT }} \
acl private \
no_check_bucket true
- name: Calculate dates
shell: bash
id: dates
run: |
year="${{ inputs.year }}"
week="${{ inputs.week }}"
# If no year/week is given, detect the week before today.
# This assumes this script is started on a Monday.
if [ -z "${year}" ] || [ -z "${week}" ]; then
year=$(date -d "yesterday" +%G)
week=$(date -d "yesterday" +%V)
fi
# Calculate the start and end of the week.
day_in_week=$(date -d "${year}-01-01" +%u)
if [ "${day_in_week}" -lt 5 ]; then
first_monday=$(date -d "${year}-01-01 -${day_in_week} days +1 day" +%Y-%m-%d)
else
first_monday=$(date -d "${year}-01-01 -${day_in_week} days +8 day" +%Y-%m-%d)
fi
start_date=$(date -d "${first_monday} +${week} weeks -1 week" +%Y-%m-%d)
end_date=$(date -d "${start_date} +6 days" +%Y-%m-%d)
if [ "$(echo -n ${week} | wc -c)" = "1" ]; then
week="0${week}"
fi
# Ensure the start date is actually in the week we want.
start_check=$(date -d "${start_date}" +%G-%V)
if [ "${start_check}" != "${year}-${week}" ]; then
echo "Start date ${start_date} is not in ${year}-${week}, but in ${start_check}"
exit 1
fi
# Ensure the end date is actually in the week we want.
end_check=$(date -d "${end_date}" +%G-%V)
if [ "${end_check}" != "${year}-${week}" ]; then
echo "End date ${end_date} is not in ${year}-${week}, but in ${end_check}"
exit 1
fi
echo "Week: ${week}"
echo "Year: ${year}"
echo "Start date: ${start_date}"
echo "End date: ${end_date}"
echo "week=${week}" >> "$GITHUB_OUTPUT"
echo "year=${year}" >> "$GITHUB_OUTPUT"
echo "start_date=${start_date}" >> "$GITHUB_OUTPUT"
echo "end_date=${end_date}" >> "$GITHUB_OUTPUT"
- name: Download packed results
shell: bash
run: |
echo "Downloading packs for week ${{ steps.dates.outputs.week }} in ${{ steps.dates.outputs.year }}: [${{ steps.dates.outputs.start_date }} .. ${{ steps.dates.outputs.end_date }}]"
mkdir -p packed
for i in $(seq 0 6); do
date=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%Y-%m-%d)
date_year=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%Y)
date_month=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%m)
echo "Downloading ${date}"
rclone copy -v openttd:survey-packed-prod/${date_year}/${date_month}/openttd-survey-pack.${date}.tar.xz packed
done
- name: Run analysis
shell: bash
run: |
mkdir -p _data/summaries/${{ steps.dates.outputs.year }}
python -m analysis wk packed/* > _data/summaries/${{ steps.dates.outputs.year }}/wk${{ steps.dates.outputs.week }}.json
- name: Create summary entry
shell: bash
run: |
python -m create_markdown wk ${{ steps.dates.outputs.year }} ${{ steps.dates.outputs.week }} ${{ steps.dates.outputs.start_date }} ${{ steps.dates.outputs.end_date }}
- name: Commit and push
shell: bash
run: |
git config --global user.name "OpenTTD Survey"
git config --global user.email "[email protected]"
git add _data/summaries/${{ steps.dates.outputs.year }}/wk${{ steps.dates.outputs.week }}.json
git add _summaries/${{ steps.dates.outputs.year }}/wk${{ steps.dates.outputs.week }}.md
git add _summaries/${{ steps.dates.outputs.year }}/wk${{ steps.dates.outputs.week }}
git commit -m "Add: summary for week ${{ steps.dates.outputs.week }} of ${{ steps.dates.outputs.year }}"
git push