-
-
Notifications
You must be signed in to change notification settings - Fork 403
[skip changelog] Add stats fetching from Arduino CDN using AWS Athena #666
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
11 commits
Select commit
Hold shift + click to select a range
4d9339b
[skip changelog] Add stats fetching from Arduino CDN using AWS Athena
4755779
Fix path typo for athena stats
49ec7bf
Fix path typo for athena stats again
f679301
Add sh
8a0f37e
Add checkout step
dc5ee84
Add STATS_ prefix to stats secret and env vars
697d197
Use latest version of jq
6820110
Use latest version of jq inside fetch action
1d9b062
Use PATH override to use latest version of jq inside fetch action
866d63f
Fix path typo
fde9a7b
Remove push event
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,93 @@ | ||
#!/usr/bin/env bash | ||
|
||
# This script performs the following: | ||
# 1. Run the query, use jq to capture the QueryExecutionId, and then capture that into bash variable | ||
# 2. Wait for the query to finish running (240 seconds). | ||
# 3. Get the results. | ||
# 4. Json data points struct build | ||
|
||
# Expected env variables are: | ||
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for accessing AWS resources | ||
# AWS_ATHENA_SOURCE_TABLE | ||
# AWS_ATHENA_OUTPUT_LOCATION | ||
# GITHUB_REPOSITORY | ||
|
||
set -euo pipefail | ||
|
||
! read -r -d '' query << EOM | ||
select | ||
replace(url_extract_path("d.url"), '/arduino-cli/arduino-cli_', '') as flavor, | ||
count("id") as gauge | ||
from ${AWS_ATHENA_SOURCE_TABLE} | ||
where "d.url" like 'https://downloads.arduino.cc/arduino-cli/arduino-cli_%' | ||
and "d.url" not like '%latest%' -- exclude latest redirect | ||
and "d.url" not like '%alpha%' -- exclude early alpha releases | ||
and "d.url" not like '%.tar.bz2%' -- exclude very old releases archive formats | ||
group by 1 | ||
EOM | ||
|
||
queryExecutionId=$( | ||
aws athena start-query-execution \ | ||
--query-string "${query}" \ | ||
--query-execution-context "Database=demo_books" \ | ||
--result-configuration "OutputLocation=${AWS_ATHENA_OUTPUT_LOCATION}" \ | ||
--region us-east-1 | jq -r ".QueryExecutionId" | ||
) | ||
|
||
echo "QueryExecutionId is ${queryExecutionId}" | ||
for i in $(seq 1 120); do | ||
queryState=$( aws athena get-query-execution \ | ||
--query-execution-id "${queryExecutionId}" \ | ||
--region us-east-1 | jq -r ".QueryExecution.Status.State" | ||
); | ||
|
||
if [[ "${queryState}" == "SUCCEEDED" ]]; then | ||
break; | ||
fi; | ||
|
||
echo "QueryExecutionId ${queryExecutionId} - state is ${queryState}" | ||
|
||
if [[ "${queryState}" == "FAILED" ]]; then | ||
exit 1; | ||
fi; | ||
|
||
sleep 2 | ||
done | ||
|
||
echo "Query succeeded. Processing data" | ||
queryResult=$( aws athena get-query-results \ | ||
--query-execution-id "${queryExecutionId}" \ | ||
--region us-east-1 | jq --compact-output | ||
); | ||
|
||
! read -r -d '' jsonTemplate << EOM | ||
{ | ||
"type": "gauge", | ||
"name": "arduino.downloads.total", | ||
"value": "%s", | ||
"host": "${GITHUB_REPOSITORY}", | ||
"tags": [ | ||
"version:%s", | ||
"os:%s", | ||
"arch:%s", | ||
"cdn:downloads.arduino.cc", | ||
"project:arduino-cli" | ||
] | ||
}, | ||
EOM | ||
|
||
datapoints="[" | ||
for row in $(echo "${queryResult}" | jq 'del(.ResultSet.Rows[0])' | jq -r '.ResultSet.Rows[] | .Data' --compact-output); do | ||
value=$(jq -r ".[1].VarCharValue" <<< "${row}") | ||
tag=$(jq -r ".[0].VarCharValue" <<< "${row}") | ||
# Some splitting to obtain 0.6.0, Windows, 32bit elements from string 0.6.0_Windows_32bit.zip | ||
split=($(echo "$tag" | tr '_' '\n')) | ||
if [[ ${#split[@]} -ne 3 ]]; then | ||
continue | ||
fi | ||
archSplit=($(echo "${split[2]}" | tr '.' '\n')) | ||
datapoints+=$(printf "${jsonTemplate}" "${value}" "${split[0]}" "${split[1]}" "${archSplit[0]}") | ||
done | ||
datapoints="${datapoints::-1}]" | ||
|
||
echo "::set-output name=result::$(jq --compact-output <<< "${datapoints}")" |
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,50 @@ | ||
name: download-stats | ||
|
||
on: | ||
schedule: | ||
# run every day at 12:00:00 | ||
- cron: '* 12 * * *' | ||
|
||
jobs: | ||
push-stats: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v1 | ||
|
||
- name: Fetch downloads count form Arduino CDN using AWS Athena | ||
id: fetch | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.STATS_AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.STATS_AWS_SECRET_ACCESS_KEY }} | ||
AWS_ATHENA_SOURCE_TABLE: ${{ secrets.STATS_AWS_ATHENA_SOURCE_TABLE }} | ||
AWS_ATHENA_OUTPUT_LOCATION: ${{ secrets.STATS_AWS_ATHENA_OUTPUT_LOCATION }} | ||
GITHUB_REPOSITORY: ${{ github.repository }} | ||
run: | | ||
# Fetch jq 1.6 as VM has only 1.5 ATM | ||
wget -q https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O jq | ||
chmod +x jq | ||
PATH=${{ github.workspace }}:$PATH | ||
.github/tools/fetch_athena_stats.sh | ||
|
||
- name: Send metrics | ||
uses: masci/datadog@v1 | ||
with: | ||
api-key: ${{ secrets.DD_API_KEY }} | ||
# Metrics input expects YAML but JSON will work just right. | ||
metrics: ${{steps.fetch.outputs.result}} | ||
|
||
- name: Report failure | ||
if: failure() | ||
uses: masci/datadog@v1 | ||
with: | ||
api-key: ${{ secrets.DD_API_KEY }} | ||
events: | | ||
- title: "Arduino CLI stats failing" | ||
text: "Stats collection failed" | ||
alert_type: "error" | ||
host: ${{ github.repository }} | ||
tags: | ||
- "project:arduino-cli" | ||
- "cdn:downloads.arduino.cc" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😿