Last active
March 29, 2019 10:55
-
-
Save ilyam8/0c8f290532be5c2f56df7c02a24e8cc3 to your computer and use it in GitHub Desktop.
This file contains 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
import datetime | |
import json | |
from collections import defaultdict | |
from bases.FrameworkServices.UrlService import UrlService | |
update_every = 5 | |
REPOS = ( | |
'netdata/netdata', | |
'titpetric/netdata', | |
'firehol/netdata', | |
) | |
ORDER = [ | |
'pulls_sum', | |
'pulls_rate', | |
'pulls', | |
'stars', | |
'status', | |
'last_updated', | |
] | |
def charts(): | |
return { | |
'pulls_rate': { | |
'options': [None, 'Pulls Rate', 'pulls/s', 'pulls', 'docker_hub.pulls_rate', 'stacked'], | |
'lines': [['pulls_{0}'.format(repo), repo.replace("/", "_"), 'incremental'] for repo in REPOS], | |
}, | |
'pulls': { | |
'options': [None, 'Pulls', 'pulls', 'pulls', 'docker_hub.pulls', 'stacked'], | |
'lines': [['pulls_{0}'.format(repo), repo.replace("/", "_")] for repo in REPOS], | |
}, | |
'pulls_sum': { | |
'options': [ | |
None, 'Pulls Summary', 'pulls', 'pulls', 'docker_hub.pulls_sum', 'line'], | |
'lines': [ | |
['pulls_sum', 'sum'], | |
], | |
}, | |
'stars': { | |
'options': [None, 'Stars', 'stars', 'stars', 'docker_hub.stars', 'stacked'], | |
'lines': [['stars_{0}'.format(repo), repo.replace("/", "_")] for repo in REPOS], | |
}, | |
'status': { | |
'options': [None, 'Current Status', 'status', 'status', 'docker_hub.status', 'line'], | |
'lines': [['status_{0}'.format(repo), repo.replace("/", "_")] for repo in REPOS], | |
}, | |
'last_updated': { | |
'options': [None, 'Time Since Last Update', 'seconds', 'last updated', 'docker_hub.last_updated', 'line'], | |
'lines': [['last_updated_{0}'.format(repo), repo.replace("/", "_")] for repo in REPOS], | |
} | |
} | |
URL = 'https://hub.docker.com/v2/repositories/{repo}' | |
TIME_FTM = '%Y-%m-%dT%H:%M:%S.%fZ' | |
class Service(UrlService): | |
def __init__(self, configuration=None, name=None): | |
UrlService.__init__(self, configuration=configuration, name=name) | |
self.order = ORDER | |
self.definitions = charts() | |
self.url = URL | |
def get_data_once(self, url): | |
raw = self._get_raw_data(url=url) | |
if not raw: | |
return None | |
try: | |
return json.loads(raw) | |
except json.JSONDecodeError as error: | |
self.error('error on json parse : {0}'.format(error)) | |
return None | |
def _get_data(self): | |
data = dict() | |
pulls_sum = list() | |
for repo in REPOS: | |
url = self.url.format(repo=repo) | |
raw_json = self.get_data_once(url) | |
if not raw_json: | |
self.error('unable get data from {0}'.format(url)) | |
continue | |
parsed = parse_json(raw_json) | |
if not parsed: | |
self.error('unable to parse response from {0} : {1}'.format(url, raw_json)) | |
continue | |
pulls_sum.append(parsed['pulls']) | |
data.update(('{0}_{1}'.format(k, repo), v) for k, v in parsed.items()) | |
if len(pulls_sum) == len(REPOS): | |
data['pulls_sum'] = sum(pulls_sum) | |
return data or None | |
def seconds_since_last_updated(last_updated_string): | |
last_updated_date = datetime.datetime.strptime(last_updated_string, TIME_FTM) | |
current_date = datetime.datetime.utcnow() | |
delta = current_date - last_updated_date | |
return ((delta.days * 86400 + delta.seconds) * 10 ** 6 + delta.microseconds) / 10 ** 6 | |
def parse_json(raw_json): | |
try: | |
return { | |
'pulls': raw_json['pull_count'], | |
'stars': raw_json['star_count'], | |
'status': raw_json['status'], | |
'last_updated': seconds_since_last_updated(raw_json['last_updated']), | |
} | |
except KeyError: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment