Skip to content

Commit

Permalink
Merge pull request #5 from ascendcorp/feature/refactor-structure
Browse files Browse the repository at this point in the history
Feature/refactor structure
  • Loading branch information
ohmrefresh authored Aug 10, 2019
2 parents d810dbc + e6c248c commit ba5d58a
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 72 deletions.
15 changes: 0 additions & 15 deletions GrafanaSnapshot/Base.py

This file was deleted.

21 changes: 0 additions & 21 deletions GrafanaSnapshot/DeleteSnapshot.py

This file was deleted.

3 changes: 1 addition & 2 deletions GrafanaSnapshot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-
from .GenerateSnapshot import GenerateSnapshot
from .DeleteSnapshot import DeleteSnapshot
from .snapshot_face import SnapshotFace
2 changes: 2 additions & 0 deletions GrafanaSnapshot/feature/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .base import Base
from .snapshots import Snapshots
3 changes: 3 additions & 0 deletions GrafanaSnapshot/feature/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Base(object):
def __init__(self, api):
self.api = api
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from .Base import Base
from .base import Base
import urllib3
import datetime

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class GenerateSnapshot(Base):
class Snapshots(Base):

def generate(self, tags, time_from, time_to, expires=300):
def create_snapshot(self, tags, time_from, time_to, expires=300):

"""
Generate Grafana snapshot with expires
Expand All @@ -17,7 +16,7 @@ def generate(self, tags, time_from, time_to, expires=300):
:param expires:
:return:
"""

dashboards_info = self.api.search.search_dashboards(tag=tags)
dashboards = {}
for dashboard_info in dashboards_info:
Expand All @@ -40,6 +39,23 @@ def generate(self, tags, time_from, time_to, expires=300):

return snapshot_list

def delete(self, delete_key=None, key=None):

"""
Delete snapshot with delete_key or snapshot key
:param delete_key:
:param key:
:return:
"""

if delete_key:
return self.api.snapshots.delete_snapshot_by_delete_key(delete_key)
elif key:
return self.api.snapshots.delete_snapshot_by_key(key)
else:
return None

@staticmethod
def __time_str_from_unix_ms(unix_ms):
return datetime.datetime.utcfromtimestamp(int(unix_ms / 1000)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
36 changes: 36 additions & 0 deletions GrafanaSnapshot/snapshot_face.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from grafana_api.grafana_face import GrafanaFace

from .feature import (
Snapshots,
)


class SnapshotFace:
def __init__(
self,
auth,
host="localhost",
port=None,
url_path_prefix="",
protocol="https",
verify=False,
):
"""
Init auth
:param auth: API Token (https://grafana.com/docs/http_api/auth/#create-api-token)
:param host: Host of the API server Ex. 127.0.0.1
:param port: Ex. 3000 (default port)
:param protocol: http or https
.. code-block:: python
grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https")
"""
self.api = GrafanaFace(
auth,
host=host,
port=port,
url_path_prefix=url_path_prefix,
protocol=protocol,
verify=verify,
)

self.snapshots = Snapshots(self.api)
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ coverage = "~=4.5"
mock = {version = "*", markers = "python_version <= '2.7'"}
pylint = ">=1.9"
requests-mock = "~=1.6"
unittest-xml-reporting = "~=2.5"
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GrafanaSnapshot Package [![Build Status](https://travis-ci.com/ascendcorp/GrafanaSnapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/GrafanaSnapshot)
# grafana-snapshot Package [![Build Status](https://travis-ci.com/ascendcorp/grafana-snapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/grafana-snapshot)


## Install
Expand All @@ -12,12 +12,13 @@ You can test by creating a python file:
from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot

if __name__ == "__main__":
grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https")
result = grafana.generate(tags="tags", time_from=1563183710618, time_to=1563185212275)

grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='https')

## Create snaphot
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275)

## Delete snaphot by key
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.delete(delete_key='some_delete_key', key=None)
result = grafana.snapshots.delete(delete_key='some_delete_key', key=None)
```


Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
unittest-xml-reporting
urllib3
requests
pytest
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_version():
license="MIT",
packages=find_packages(),
install_requires=['requests', 'grafana_api'],
tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'xmlrunner', 'pytest', 'unittest-xml-reporting'],
tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'pytest'],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
19 changes: 9 additions & 10 deletions tests/test_deleteSnapshot.py → tests/test_delete_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

import requests_mock
from GrafanaSnapshot.DeleteSnapshot import DeleteSnapshot
from GrafanaSnapshot.snapshot_face import SnapshotFace


class TestDeleteSnapshot(unittest.TestCase):
Expand All @@ -13,8 +12,8 @@ def test_delete_snapshot_with_delete_key(self, m):
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
)

grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.delete(delete_key='some_delete_key', key=None)
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.snapshots.delete(delete_key='some_delete_key', key=None)

expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
self.assertEqual(result, expected)
Expand All @@ -26,8 +25,8 @@ def test_delete_snapshot_with_key(self, m):
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
)

grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.delete(delete_key=None, key='some_key')
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.snapshots.delete(delete_key=None, key='some_key')

expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
self.assertEqual(result, expected)
Expand All @@ -39,13 +38,13 @@ def test_delete_snapshot_with_both(self, m):
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
)

grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.delete(delete_key='some_delete_key', key='some_key')
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
result = grafana.snapshots.delete(delete_key='some_delete_key', key='some_key')

expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
self.assertEqual(result, expected)

def test_delete_snapshot_without_key_and_delete_key(self):
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')

self.assertIsNone(grafana.delete())
self.assertIsNone(grafana.snapshots.delete())
15 changes: 5 additions & 10 deletions tests/test_generateSnapshot.py → tests/test_generate_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

import requests_mock
from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot
from GrafanaSnapshot.snapshot_face import SnapshotFace


class TestGenerateSnapshot(unittest.TestCase):
Expand Down Expand Up @@ -57,8 +56,8 @@ def test_generate(self, m):
},
)

grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http")
results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275)
grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http")
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275)
self.assertEqual(len(results), 1)

@requests_mock.Mocker()
Expand Down Expand Up @@ -111,11 +110,7 @@ def test_generate_with_expire(self, m):
},
)

grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http")
results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500)
grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http")
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500)
self.assertEqual(len(results), 1)


if __name__ == "__main__":
import xmlrunner
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="test-reports"))

0 comments on commit ba5d58a

Please sign in to comment.