dl-stats.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. name: Report Download Stats
  2. on:
  3. schedule:
  4. - cron: '0/15 * * * *' # run every 15 min - don't forget to adjust the "interval" in the json sent to the metrics endpoint
  5. jobs:
  6. report-download-stats:
  7. runs-on: ubuntu-latest
  8. steps:
  9. - name: Get download count of latest releases
  10. id: get-stats
  11. uses: actions/github-script@v6
  12. with:
  13. script: |
  14. const query = `query($owner:String!, $name:String!) {
  15. repository(owner:$owner, name:$name){
  16. releases(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
  17. nodes {
  18. isPrerelease
  19. tagName
  20. releaseAssets(first: 20) {
  21. nodes {
  22. name
  23. downloadCount
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }`;
  30. const variables = {
  31. owner: context.repo.owner,
  32. name: context.repo.repo
  33. }
  34. return await github.graphql(query, variables)
  35. - name: Transform Results
  36. id: transform-stats
  37. run: |
  38. TIME=$(($(date +%s) / $INTERVAL * $INTERVAL))
  39. echo ${JSON_DATA} | jq --arg TIME "$TIME" --arg INTERVAL "$INTERVAL" -c '.repository.releases.nodes[] | select(.isPrerelease == false) | .tagName as $tagName | .releaseAssets.nodes[] | {filename: .name, downloads: .downloadCount, release: $tagName, time: ($TIME|tonumber), interval: ($INTERVAL|tonumber)}' > input.json
  40. jq -c 'select(.filename|endswith("-x86_64.AppImage")) | {name: "github.releases.downloads", tags: ["file=AppImage", "version=\(.release)", "arch=amd64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  41. jq -c 'select(.filename|endswith("_amd64.deb")) | {name: "github.releases.downloads", tags: ["file=deb", "version=\(.release)", "arch=amd64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  42. jq -c 'select(.filename|endswith("-x64.msi")) | {name: "github.releases.downloads", tags: ["file=msi", "version=\(.release)", "arch=amd64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  43. jq -c 'select(.filename|endswith("-x64.exe")) | {name: "github.releases.downloads", tags: ["file=exe", "version=\(.release)", "arch=amd64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  44. jq -c 'select(.filename|endswith("-arm64.dmg")) | {name: "github.releases.downloads", tags: ["file=dmg", "version=\(.release)", "arch=arm64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  45. jq -c 'select(.filename|endswith(".dmg")) | select(.filename|endswith("-arm64.dmg")|not) | {name: "github.releases.downloads", tags: ["file=dmg", "version=\(.release)", "arch=amd64"], value: .downloads, interval: .interval, time: .time}' input.json >> output.json
  46. RESULT=$(jq -s -c "." output.json)
  47. echo "::set-output name=result::${RESULT}"
  48. env:
  49. INTERVAL: 900
  50. JSON_DATA: ${{ steps.get-stats.outputs.result }}
  51. - name: Upload Results
  52. uses: fjogeleit/http-request-action@v1
  53. with:
  54. url: 'https://graphite-us-central1.grafana.net/metrics'
  55. method: 'POST'
  56. contentType: 'application/json'
  57. bearerToken: ${{ secrets.GRAFANA_GRAPHITE_TOKEN }}
  58. data: ${{ steps.transform-stats.outputs.result }}
  59. continue-on-error: true # currently there seems to be a problem with the metrics endpoint, failing every now and then