This is the fork of the sonatype-stats utility which has a goal of:
- migrating the tool from Ammonite to Scala CLI
- migrating from Scala 2.12 to Scala 3
- migrating from Circe to Jsoniter Scala
- migrating to newer version of STTP
- migrating charts to Chart.js
- migrating from Travis CI to GH Actions
Sonatype exposes projects stats only after you log in, and only for your projects (Central Statistics). Its UI also only shows last 12 months.
Through their API you can fetch data since the beginning of the project for every artifact. And this is what this script does:
- it fetches unique IPs in every month since project start
- it fetches both unique IPs and all downloads for every artifact since the project start
- it creates chart containing all of that data (using Chart.js)
- it saves both chart and values from Sonatype in
data
directory - it uses content of
data
directory as cache to not download the same data twice unnecessarily
You can then looks though the statistics, archive them or publish on GitHub pages.
Install Scala CLI. Then run:
SONATYPE_PROJECT=[project name] SONATYPE_USERNAME=[username] SONATYPE_PASSWORD='password' scala-cli run .
putting the right values for your project and Sonatype user. The data will be generated in data
directory.
We can create a GH Action which could publish Sonatype Stats on GitHub Pages in a dedicated repository:
- create a repository dedicated for Sonatype statistics - they will be visible on its GitHub Pages site
- obtain Sonatype credentials (instead of username you can create User Token)
- setup
SONATYPE_USERNAME
secret - setup
SONATYPE_PASSWORD
secret
- setup
- find which project ID you want to use (you can check what projects you see on e.g. a Central Statistics after you log in)
- setup
SONATYPE_PROJECT
variable
- setup
- create GitHub Access Token
with an access to the repository (we'll be using Publish to GitHub Pages action)
- in Fine-graned tokens you need to grant "Read access to metadata" and "Read and Write access to code and pages" just for the repository which will host stats on its GH Pages
- finally, create in your repository file
.github/workflows/sonatype-stats.yml
with the content like below:
name: Publish Sonatype Stats to GitHub Pages
on:
push:
branches:
# update index.html also if anything was modified in the workflow
- master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 0 7,15 * *'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v2
- name: Cache fetched data
uses: actions/cache@v4
with:
path: data
key: setup-1
- name: Fetch and render Sonatype Stats
uses: MateuszKubuszok/sonatype-stats@v1.0.0
with:
sonatype-project: ${{ vars.SONATYPE_PROJECT }}
sonatype-username: ${{ secrets.SONATYPE_USERNAME }}
sonatype-password: ${{ secrets.SONATYPE_PASSWORD }}
- name: Publish generated content to GitHub Pages
uses: tsunematsu21/actions-publish-gh-pages@v1.0.2
with:
dir: data
branch: gh-pages
token: ${{ secrets.ACCESS_TOKEN }}
This will create a job that is running every 1st and 15th of the month - once a month ends Sonatype triggers a workflow that computes stats for it. Usually it takes several days before they are avaiable (often around a week), so there is no point in running this action very often. (Stats once downloaded are cached, so we only have to add missing ones).
Once it finishes running, don't forget to enable GitHub Pages for your repository.
See example for Scalaland.io.
You can tweak this setup as it only shows the most basic use case.