|
| 1 | +name: download-stats |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # run every 5 minutes |
| 6 | + - cron: '*/5 * * * *' |
| 7 | + |
| 8 | +jobs: |
| 9 | + push-stats: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Fetch downloads count |
| 14 | + id: fetch |
| 15 | + |
| 16 | + with: |
| 17 | + github-token: ${{github.token}} |
| 18 | + script: | |
| 19 | + let metrics = [] |
| 20 | +
|
| 21 | + // Get a list of releases |
| 22 | + const opts = github.repos.listReleases.endpoint.merge({ |
| 23 | + ...context.repo |
| 24 | + }) |
| 25 | + const releases = await github.paginate(opts) |
| 26 | +
|
| 27 | + // Get download stats for every release |
| 28 | + for (const rel of releases) { |
| 29 | + // Names for assets are like `arduino-cli_0.4.0_Linux_32bit.tar.gz`, |
| 30 | + // we'll use this later to split the asset file name more easily |
| 31 | + const baseName = `arduino-cli_${rel.name}_` |
| 32 | +
|
| 33 | + // Get a list of assets for this release |
| 34 | + const opts = github.repos.listAssetsForRelease.endpoint.merge({ |
| 35 | + ...context.repo, |
| 36 | + release_id: rel.id |
| 37 | + }) |
| 38 | + const assets = await github.paginate(opts) |
| 39 | +
|
| 40 | + for (const asset of assets) { |
| 41 | + // Ignore files that are not arduino-cli packages |
| 42 | + if (!asset.name.startsWith(baseName)) { |
| 43 | + continue |
| 44 | + } |
| 45 | +
|
| 46 | + // Strip the base and remove file extension to get `Linux_32bit` |
| 47 | + systemArch = asset.name.replace(baseName, "").split(".")[0].split("_") |
| 48 | +
|
| 49 | + // Add a metric object to the list of gathered metrics |
| 50 | + metrics.push({ |
| 51 | + "type": "gauge", |
| 52 | + "name": "arduino.downloads.total", |
| 53 | + "value": asset.download_count, |
| 54 | + "host": "${{ github.repository }}", |
| 55 | + "tags": [ |
| 56 | + `version:${rel.name}`, |
| 57 | + `os:${systemArch[0]}`, |
| 58 | + `arch:${systemArch[1]}`, |
| 59 | + "cdn:github.com", |
| 60 | + "project:arduino-cli" |
| 61 | + ] |
| 62 | + }) |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + // The action will put whatever we return from this function in |
| 67 | + // `outputs.result`, JSON encoded. So we just return the array |
| 68 | + // of objects and GitHub will do the rest. |
| 69 | + return metrics |
| 70 | +
|
| 71 | + - name: Send metrics |
| 72 | + uses: masci/datadog@v1 |
| 73 | + with: |
| 74 | + api-key: ${{ secrets.DD_API_KEY }} |
| 75 | + # Metrics input expects YAML but JSON will work just right. |
| 76 | + metrics: ${{steps.fetch.outputs.result}} |
| 77 | + |
| 78 | + - name: Report failure |
| 79 | + if: failure() |
| 80 | + uses: masci/datadog@v1 |
| 81 | + with: |
| 82 | + api-key: ${{ secrets.DD_API_KEY }} |
| 83 | + events: | |
| 84 | + - title: "Arduino CLI stats failing" |
| 85 | + text: "Stats collection failed" |
| 86 | + alert_type: "error" |
| 87 | + host: ${{ github.repository }} |
| 88 | + tags: |
| 89 | + - "project:arduino-cli" |
| 90 | + - "cdn:github.com" |
0 commit comments