Permalink
Browse files

Added Event helper

  • Loading branch information...
1 parent feaf936 commit 348ede2dbdc0b801d73f95a8620417197f170f2f @tsileo committed Sep 30, 2013
Showing with 47 additions and 4 deletions.
  1. +27 −4 README.rst
  2. +20 −0 cube.py
View
@@ -52,7 +52,7 @@ Overview
Requirements
============
-* `Requests <http://docs.python-requests.org/en/latest/>`_
+ * `Requests <http://docs.python-requests.org/en/latest/>`_
Installation
@@ -106,6 +106,27 @@ Metric resolutions shortcut
from cube import ONE_HOUR, FIVE_MINUTE
+Event helper
+------------
+
+::
+
+ from cube import Cube, Event
+
+ cube = Cube()
+
+ my_event = cube.new_event('myevent')
+ # or
+ my_event = Event(cube, 'myevent')
+
+ my_event.put({'temp': 10})
+ my_event.put({'temp': 20})
+
+ my_event.event()
+ # [{u'time': u'2012-10-01T13:04:04.453Z'}, {u'time': u'2012-10-01T13:04:39.725Z'}]
+
+ my_event.event('myevent(temp)')
+
Changelog
=========
@@ -115,9 +136,11 @@ Changelog
**September 30 2013**
-- Cleaned code (PEP8)
-- Compatible with requests 2.0
-- Added metric resolution shortcut
+ * Cleaned code (PEP8)
+ * Compatible with requests 2.0
+ * Added metric resolution shortcut
+ * Added a ``Event`` helper
+
License (MIT)
View
20 cube.py
@@ -18,6 +18,23 @@
ONE_DAY = '864e5'
+class Event(object):
+ def __init__(self, cube, event_type):
+ self.cube = cube
+ self.event_type = event_type
+
+ def put(self, event_data={}, **kwargs):
+ return self.cube.put(self.event_type, event_data, **kwargs)
+
+ def event(self, expression=None, **kwargs):
+ if expression is None:
+ expression = self.event_type
+ return self.cube.event(expression, **kwargs)
+
+ def metric(self, expression, **kwargs):
+ return self.cube.metric(self, expression, **kwargs)
+
+
class Cube(object):
def __init__(self, hostname="localhost", **kwargs):
self.collector_url = 'http://{0}:{1}/{2}/'.format(hostname,
@@ -75,3 +92,6 @@ def types(self):
r = requests.get(self.evaluator_url + 'types')
r.raise_for_status()
return r.json()
+
+ def new_event(self, event_type):
+ return Event(self, event_type)

0 comments on commit 348ede2

Please sign in to comment.