Permalink
Browse files

Initial import

  • Loading branch information...
tsileo committed Oct 1, 2012
0 parents commit e9b346715cc39b1ee8b220f47e0328db71720c01
Showing with 208 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +1 −0 MANIFEST.in
  3. +98 −0 README.rst
  4. +69 −0 cube.py
  5. +31 −0 setup.py
@@ -0,0 +1,9 @@
+*.pyc
+build/
+dist/
+cube_client.egg-info/
+.sass-cache
+.DS_Store
+*tar.gz
+*.zip
+.pypirc
@@ -0,0 +1 @@
+include README.rst
@@ -0,0 +1,98 @@
+===========
+Cube-client
+===========
+
+A Python client for `Cube: Time Series Data Collection & Analysis <http://square.github.com/cube/>`_.
+
+
+Features
+========
+
+You can post (`Collector <https://github.com/square/cube/wiki/Collector>`_) and request (`Evaluator <https://github.com/square/cube/wiki/Evaluator>`_) data to/from Cube.
+
+ * Post events
+ * Request events
+ * Request metrics
+ * Request known event types
+
+
+Overview
+========
+
+::
+
+ >>> from cube import Cube
+ >>> c = Cube()
+ >>>
+ >>> c.put("sample_data", {"myval": 10})
+ [{'data': {'myval': 10}, 'type': 'sample_data', 'time': '2012-10-01T13:04:04.453929'}]
+
+ >>> c.event("sample_data")
+ [{u'time': u'2012-10-01T13:04:04.453Z'}]
+
+ >>> c.put("sample_data", {"myval": 20})
+ [{'data': {'myval': 20}, 'type': 'sample_data', 'time': '2012-10-01T13:04:39.725676'}]
+
+ >>> c.event("sample_data")
+ [{u'time': u'2012-10-01T13:04:04.453Z'}, {u'time': u'2012-10-01T13:04:39.725Z'}]
+
+ >>> c.event("sample_data(myval)")
+ [{u'data': {u'myval': 10}, u'time': u'2012-10-01T13:04:04.453Z'}, {u'data': {u'myval': 20}, u'time': u'2012-10-01T13:04:39.725Z'}]
+
+ >>> c.metric("sum(sample_data)", step="36e5", start="2012-10-01")
+ [{u'value': 0, u'time': u'2012-10-01T00:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T01:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T02:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T03:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T04:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T05:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T06:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T07:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T08:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T09:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T10:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T11:00:00.000Z'}, {u'value': 0, u'time': u'2012-10-01T12:00:00.000Z'}, {u'value': 2, u'time': u'2012-10-01T13:00:00.000Z'}]
+
+
+Requirements
+============
+
+* `Requests <http://docs.python-requests.org/en/latest/>`_
+
+
+Installation
+============
+
+::
+
+ $ pip install cube-client
+
+
+Usage
+=====
+
+::
+
+ from cube import Cube
+ from datetime import datetime
+
+ cube = Cube()
+ # or
+ cube = Cube("localhost")
+
+ # Create an event
+ cube.put("myevent", {"temp": 30})
+ # or
+ cube.put("myvent", {"temp": 30}, time=datatime.now().isoformat())

This comment has been minimized.

Show comment
Hide comment
@aaronsw

aaronsw Nov 1, 2012

Contributor

This should be myevent instead of myvent.

@aaronsw

aaronsw Nov 1, 2012

Contributor

This should be myevent instead of myvent.

+
+ # Request events data
+ # See Cube queries:
+ # https://github.com/square/cube/wiki/Queries#wiki-metric
+ cube.event("myevent(temp)")
+
+ # Request metrics
+ cube.metric(c.metric("sum(myevent)", step="36e5", start="2012-10-01"))
+
+ # Request known event types
+ cube.types()
+
+
+License (MIT)
+=============
+
+Copyright (c) 2012 Thomas Sileo
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69 cube.py
@@ -0,0 +1,69 @@
+import requests
+import json
+from datetime import datetime
+
+API_VERSION = "1.0"
+
+class Cube:
+ def __init__(self, hostname="localhost", **kwargs):
+ self.collector_url = "http://%s:%s/%s/" % (hostname, \
+ kwargs.get("collector_port", 1080), API_VERSION)
+ self.evaluator_url = "http://%s:%s/%s/" % (hostname, \
+ kwargs.get("evaluator_port", 1081), API_VERSION)
+
+ def put(self, event_type, event_data={}, **kwargs):
+ """
+ Create/update an event.
+ """
+ event = dict(type=event_type, data=event_data)
+
+ event["time"] = kwargs.get("time", datetime.now().isoformat())
+
+ if kwargs.get("id"):
+ event["id"] = kwargs.get("id")
+
+ data = json.dumps([event])
+
+ r = requests.post(self.collector_url + "event/put", data=data,
+ headers={'content-type': 'application/json'})
+ r.raise_for_status()
+
+ return [event]
+
+ def make_query(self, query_type, expression, **kwargs):
+ data = dict(expression=expression,
+ stop=kwargs.get("stop", datetime.now().isoformat()))
+
+ if kwargs.get("start"):
+ data["start"] = kwargs.get("start")
+
+ if kwargs.get("limit"):
+ data["limit"] = kwargs.get("limit")
+
+ if query_type == "metric" and kwargs.get("step"):
+ data["step"] = kwargs.get("step")
+
+ r = requests.get(self.evaluator_url + query_type, params=data)
+ r.raise_for_status()
+
+ return r.json
+
+ def event(self, expression, **kwargs):
+ """
+ Query with an event expression
+ """
+ return self.make_query("event", expression, **kwargs)
+
+ def metric(self, expression, **kwargs):
+ """
+ Query with a metric expression
+ """
+ return self.make_query("metric", expression, **kwargs)
+
+ def types(self):
+ """
+ List of the known event types
+ """
+ r = requests.get(self.evaluator_url + "types")
+ r.raise_for_status()
+ return r.json
@@ -0,0 +1,31 @@
+import os
+import sys
+from setuptools import setup
+
+def read(fname):
+ return open(os.path.join(os.path.dirname(__file__), fname)).read()
+
+setup(
+ name = "cube-client",
+ version = "0.1",
+ author = "Thomas Sileo",
+ author_email = "thomas.sileo@gmail.com",
+ description = "A Python client for Cube: Time Series Data Collection & Analysis",
+ license = "MIT",
+ keywords = "cube time series",
+ url = "https://github.com/tsileo/cube-client",
+ py_modules=['cube'],
+ long_description= read('README.rst'),
+ install_requires=[
+ "requests"
+ ],
+ classifiers=[
+ "Development Status :: 4 - Beta",
+ "Intended Audience :: Developers",
+ "Topic :: Database",
+ "License :: OSI Approved :: MIT License",
+ "Programming Language :: Python",
+ ],
+ scripts = ["cube.py"],
+ zip_safe=False,
+)

0 comments on commit e9b3467

Please sign in to comment.