2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-14 18:12:57 +00:00
cointop/meta/metrics/snapcraft_metrics.py
2020-11-15 18:11:31 -08:00

61 lines
1.9 KiB
Python

import configparser
import pymacaroons
import requests
import json
from datetime import date, timedelta
# Generate config with "package_metrics" ACL:
# $ snapcraft export-login --snaps cointop --acls package_metrics --expires '2020-12-01 00:00:00' snapcraft.cfg
config_path = 'snapcraft.cfg'
days = 30
today = date.today()
start = (today - timedelta(days=days)).strftime("%Y-%m-%d")
end = today.strftime("%Y-%m-%d")
config = configparser.ConfigParser()
config.read(config_path)
macaroon = config['login.ubuntu.com']['macaroon']
unbound_discharge = config['login.ubuntu.com']['unbound_discharge']
root_macaroon = pymacaroons.Macaroon.deserialize(macaroon)
unbound = pymacaroons.Macaroon.deserialize(unbound_discharge)
bound = root_macaroon.prepare_for_request(unbound)
discharge_macaroon_raw = bound.serialize()
auth = "Macaroon root={}, discharge={}".format(macaroon, discharge_macaroon_raw)
# https://dashboard.snapcraft.io/docs/api/snap.html#obtaining-information-about-a-snap
url = 'https://dashboard.snapcraft.io/dev/api/snaps/info/cointop'
r = requests.get(url, headers={'Authorization': auth, 'Content-Type': 'application/json'})
data = r.json()
snap_id = data['snap_id']
# https://dashboard.snapcraft.io/docs/api/snap.html#post--dev-api-snaps-metrics
url = 'https://dashboard.snapcraft.io/dev/api/snaps/metrics'
payload = {
'filters': [
{
'snap_id': snap_id,
'metric_name': 'installed_base_by_channel',
'start': start,
'end': end
}
]
}
r = requests.post(
url,
data=json.dumps(payload),
headers={
'Authorization': auth,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
)
data = r.json()
series = data['metrics'][0]['series']
values = [x for x in series if x['name'] == 'stable'][0]['values']
total = sum(values)
print(total)