Created
March 11, 2014 17:42
-
-
Save fridgei/9490979 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 requests | |
import furl | |
import json | |
from operator import itemgetter | |
class BaseApi(object): | |
def __init__(self, host): | |
self.host = host | |
self.session = requests.Session() | |
@classmethod | |
def _check_response(cls, response): | |
if not 200 <= response.status_code < 300: | |
raise Exception( | |
"{url} returned {status} with payload" | |
"\n{data}\n and query params\n{params}".format( | |
url=response.url, | |
status=response.status_code, | |
data=json.dumps( | |
response.request.data, | |
indent=4 | |
), | |
params=json.dumps( | |
response.requst.params, | |
indent=4 | |
) | |
) | |
) | |
return response.json() | |
def _post(self, endpoint, payload={}): | |
url = furl.urljoin(self.host, endpoint) | |
response = self.session.post(url, data=payload) | |
return BaseApi._check_response(response) | |
def _get(self, endpoint, payload={}): | |
url = furl.urljoin(self.host, endpoint) | |
response = self.session.get(url, params=payload) | |
return BaseApi._check_response(response) | |
def _delete(self, endpoint): | |
url = furl.urljoin(self.host, endpoint) | |
response = self.session.delete(url) | |
return BaseApi._check_response(response) | |
def _put(self, endpoint, payload={}): | |
url = furl.urljoin(self.host, endpoint) | |
response = self.session.put(url, data=payload) | |
return BaseApi._check_response(response) | |
class DescartesApi(BaseApi): | |
def __init__(self, host, descartes_api): | |
self.host = host | |
self.session = requests.Session() | |
self.session.headers.update( | |
{ | |
'X-DESCARTES-API-TOKEN': descartes_api, | |
'Accept': 'application/json' | |
} | |
) | |
def get_dash_list(self): | |
return self._get('graphs') | |
def get_graph(self, uuid): | |
return self._get("graphs/{uuid}".format(uuid=uuid)) | |
def update_graph(self, uuid, updated_options={}): | |
return self._post( | |
"graphs/{uuid}".format(uuid=uuid), | |
payload=updated_options | |
) | |
def get_tags_for_graph(self, uuid): | |
return self._get( | |
"graphs/{uuid}/tags".format(uuid=uuid) | |
) | |
def add_tag_to_graph(self, uuid, tag): | |
return self._post( | |
"graphs/{uuid}/tags".format(uuid=uuid), | |
payload={'name': tag} | |
) | |
def delete_tag_from_graph(self, uuid, tag_id): | |
return self._delete("graphs/{uuid}/tags/{tag_id}".format(**locals())) | |
def delete_graph(self, uuid): | |
return self._delete("graphs/{uuid}".format(uuid=uuid)) | |
def search_for_graph(self, search_term): | |
return self._get( | |
'graphs/', | |
payload={'search': search_term} | |
) | |
def list_dashboards(self): | |
return self._get('dashboards') | |
def edit_dashboard(self, uuid, updated_options): | |
return self._put( | |
"dashboards/{uuid}".format(uuid=uuid), | |
payload=updated_options | |
) | |
def delete_graph_from_dashboard(self, dash_uuid, graph_uuid): | |
return self._delete( | |
"dashboards/{dash_uid}/graphs/{graph_uuid}".format(**locals()) | |
) | |
def delete_dashboard(self, uuid): | |
return self._delete( | |
"dashboards/{dash_uid}".format(uuid=uuid) | |
) | |
def list_metrics(self): | |
return self._get('metrics') | |
def search_metrics(self, pattern): | |
return self._get('metrics/search', payload={'pattern': pattern}) | |
def create_descartes_graph(self, url, name, tags=[]): | |
payload = { | |
'name': name, | |
'node': url, | |
'tag': ",".join(tags) | |
} | |
return self._post( | |
"graphs", | |
payload=payload | |
) | |
def create_descartes_dash(self, name, description='', uuids=[]): | |
payload = { | |
'name': name, | |
'description': description, | |
'uuids': ",".join(uuids) | |
} | |
return self._post( | |
'dashboards', | |
payload=payload | |
) | |
class GraphiteApi(BaseApi): | |
def get_dashboard_names(self): | |
return map( | |
itemgetter('name'), | |
self._post( | |
'dashboard/find/?query=' | |
)['dashboards'] | |
) | |
def get_dashboard(self, dashboard): | |
return self._get( | |
"dashboard/load/{dash}/".format(dash=dashboard) | |
)['state'] | |
@classmethod | |
def prepare_for_descartes(cls, url): | |
wrapped_url = furl(url) | |
title = url.args.get('title') | |
for k in 'width height from until tz _uniq title'.split(): | |
try: | |
del url.args[k] | |
except KeyError: | |
pass | |
return title, wrapped_url.url | |
class NativeGraphiteDashImporter(object): | |
def __init__(self, graphite_host, descartes_host, descartes_api_key): | |
self.graphite_api = GraphiteApi(graphite_host) | |
self.descartes_api = DescartesApi(descartes_host, descartes_api_key) | |
def import_all(self): | |
for dash_name in self.graphite_api.get_dashboard_names(): | |
dash_data = self.graphite_api.get_dashboard(dash_name) | |
dash_title = dash_data['name'] | |
included_graphs = [ | |
GraphiteApi.prepare_for_descartes(graph[-1]) | |
for graph in dash_data['graphs'] | |
] | |
uuids = [ | |
self.descartes_api.create_descartes_graph(url, title)['uuid'] | |
for title, url in included_graphs | |
] | |
self.descartes_api.create_descartes_dash(dash_title, uuids=uuids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment