Repository navigation
[build] Add weekly CI check for Plausible pageview usage #17900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| name: Plausible Usage | ||
| on: | ||
| schedule: | ||
| - cron: 30 12 * * 3 | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| check: | ||
| name: Check Weekly Pageviews | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
| if: github.event.repository.fork == false | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| with: | ||
| persist-credentials: false | ||
| - name: Check Plausible Pageviews | ||
| id: check | ||
| env: | ||
| PLAUSIBLE_STATS_KEY: ${{ secrets.PLAUSIBLE_STATS_KEY }} | ||
| PLAUSIBLE_THRESHOLD: ${{ vars.PLAUSIBLE_THRESHOLD }} | ||
| PLAUSIBLE_MONTHLY_LIMIT: ${{ vars.PLAUSIBLE_MONTHLY_LIMIT }} | ||
| run: ./scripts/github-actions/check-plausible-usage.sh | ||
| - name: Slack Notification | ||
| if: failure() | ||
| uses: rtCamp/action-slack-notify@v2 | ||
| env: | ||
| SLACK_ICON_EMOJI: ":rotating_light:" | ||
| SLACK_COLOR: failure | ||
| SLACK_CHANNEL: selenium-tlc | ||
| SLACK_USERNAME: GitHub Workflows | ||
| SLACK_MESSAGE: ${{ steps.check.outputs.report || 'Plausible stats could not be read for last week' }} | ||
| MSG_MINIMAL: actions url | ||
| SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Fail when Plausible pageviews for the last seven complete days exceed a threshold. | ||
| # | ||
| # Selenium Manager telemetry is billed against a Plausible plan, and sustained | ||
| # overage locks the dashboard, so we watch the trailing week and raise an alarm | ||
| # with enough headroom to react. | ||
| # | ||
| # Requires PLAUSIBLE_STATS_KEY. PLAUSIBLE_SITE_ID, PLAUSIBLE_THRESHOLD and | ||
| # PLAUSIBLE_MONTHLY_LIMIT may be set to override the defaults. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SITE_ID="${PLAUSIBLE_SITE_ID:-manager.selenium.dev}" | ||
| THRESHOLD="${PLAUSIBLE_THRESHOLD:-17000000}" | ||
| MONTHLY_LIMIT="${PLAUSIBLE_MONTHLY_LIMIT:-75000000}" | ||
|
|
||
| : "${PLAUSIBLE_STATS_KEY:?PLAUSIBLE_STATS_KEY is required}" | ||
|
|
||
| for value in "$THRESHOLD" "$MONTHLY_LIMIT"; do | ||
| if [[ ! "$value" =~ ^[1-9][0-9]*$ ]]; then | ||
| echo "::error::Threshold and monthly limit must be positive integers, got '${value}'" | ||
| exit 1 | ||
| fi | ||
| done | ||
|
|
||
| millions() { | ||
| awk -v n="$1" 'BEGIN { v = n / 1000000; printf (v == int(v) ? "%d" : "%.1f"), v }' | ||
| } | ||
|
|
||
| # Yesterday back six days; Plausible's period=7d rolls into today's partial data. | ||
| if date -u -d 'yesterday' +%F >/dev/null 2>&1; then | ||
| END="$(date -u -d 'yesterday' +%F)" | ||
| START="$(date -u -d '7 days ago' +%F)" | ||
| else # BSD/macOS date | ||
| END="$(date -u -v-1d +%F)" | ||
| START="$(date -u -v-7d +%F)" | ||
| fi | ||
|
|
||
| echo "Site: ${SITE_ID}" | ||
| echo "Window: ${START} to ${END}" | ||
| echo "Threshold: $(millions "$THRESHOLD")M per week, $(millions "$MONTHLY_LIMIT")M per month" | ||
|
|
||
| # The status code is appended so a 4xx body reaches the log instead of being | ||
| # swallowed by the failing assignment. | ||
| if ! response="$(curl --silent --show-error --write-out '\n%{http_code}' \ | ||
| --connect-timeout 10 --max-time 60 --retry 3 --retry-all-errors \ | ||
| --get "https://plausible.io/api/v1/stats/aggregate" \ | ||
| --header "Authorization: Bearer ${PLAUSIBLE_STATS_KEY}" \ | ||
| --data-urlencode "site_id=${SITE_ID}" \ | ||
|
qodo-code-review[bot] marked this conversation as resolved.
Resolved
|
||
| --data-urlencode "period=custom" \ | ||
| --data-urlencode "date=${START},${END}" \ | ||
| --data-urlencode "metrics=pageviews")"; then | ||
| echo "::error::Could not reach the Plausible API" | ||
| exit 1 | ||
| fi | ||
|
|
||
| http_code="${response##*$'\n'}" | ||
| body="${response%$'\n'*}" | ||
|
|
||
| echo "Response: HTTP ${http_code} ${body}" | ||
|
|
||
| if [[ "$http_code" != 200 ]]; then | ||
| echo "::error::Plausible API returned HTTP ${http_code}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| pageviews="$(jq -r '.results.pageviews.value // empty' <<<"$body" 2>/dev/null || true)" | ||
|
|
||
| if [[ ! "$pageviews" =~ ^[0-9]+$ ]]; then | ||
| echo "::error::No pageviews value in the Plausible response" | ||
|
diemol marked this conversation as resolved.
Resolved
|
||
| exit 1 | ||
| fi | ||
|
|
||
| over=false | ||
| direction=under | ||
| if ((pageviews > THRESHOLD)); then | ||
| over=true | ||
| direction=over | ||
| fi | ||
|
|
||
| monthly="$(awk -v p="$pageviews" 'BEGIN { printf "%d", p / 7 * 30 }')" | ||
| percent="$(awk -v p="$pageviews" -v t="$THRESHOLD" 'BEGIN { printf "%.0f", (p - t) / t * 100 }')" | ||
|
|
||
| report="Plausible: $(millions "$pageviews")M pageviews from ${START} to ${END}, ${percent#-}% ${direction} the $(millions "$THRESHOLD")M weekly budget" | ||
|
|
||
| echo "$report" | ||
| echo "Projected monthly (30-day pace): $(millions "$monthly")M, against a $(millions "$MONTHLY_LIMIT")M limit" | ||
|
|
||
| if [[ -n "${GITHUB_OUTPUT:-}" ]]; then | ||
| { | ||
| echo "pageviews=${pageviews}" | ||
| echo "percent=${percent#-}" | ||
| echo "monthly=${monthly}" | ||
| echo "start=${START}" | ||
| echo "end=${END}" | ||
| echo "over=${over}" | ||
| echo "report=${report}" | ||
| } >>"$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then | ||
| echo "$report" >>"$GITHUB_STEP_SUMMARY" | ||
| fi | ||
|
|
||
| if [[ "$over" == true ]]; then | ||
| echo "::error::${report}" | ||
| exit 1 | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.