def push(self, database, series, value): """ Pushes a single value with current timestamp to the given database/series """ db = self._databases[database] client = Client(db['api_key'], db['api_secret']) data = [DataPoint(datetime.now(), float(value))] client.write_key(series, data)
def push(self, database, series, value): """ Pushes a single value with current timestamp to the given database/series """ try: db = self._databases[database] client = Client(db['api_key'], db['api_secret']) data = [DataPoint(datetime.now(), float(value))] client.write_key(series, data) self.last_response = 'OK' return True except Exception as e: self.last_response = e return False
def push(self, database, series, value): """ Pushes a single value with current timestamp to the given database/series """ try: db = self._databases[database] client = Client(db['api_key'], db['api_secret']) data = [DataPoint(datetime.now(), float(value))] client.write_key(series, data) self.last_response = 'OK' return True except Exception as e: self.last_response = e return False
class TempoDBOutput(Output): def __init__(self, **tempo_kwargs): self.client = Client(**tempo_kwargs) def __repr__(self): return '<%s>' % self.__class__.__name__ def send(self, key, value): if isinstance(value, bool): value = 1 if value else 0 self.client.write_key( key, [DataPoint(datetime.now(), value)] )
def import_samples(): sin = [math.sin(math.radians(d)) for d in range(0,3600)] cos = [math.cos(math.radians(d)) for d in range(0,3600)] sin_data = [] cos_data = [] start_time = datetime.datetime(2013,07,26) for i in range(len(sin)): current_time = start_time + datetime.timedelta(minutes=i) sin_data.append(DataPoint(current_time, sin[i])) cos_data.append(DataPoint(current_time, cos[i])) client = Client(API_KEY, API_SECRET) client.write_key('type:trig.function:sin.1',sin_data) client.write_key('type:trig.function:cos.1', cos_data)
temp = 1 / (1/THERMISTOR_REF_TEMP + math.log(resistance / THERMISTOR_REF_RESISTANCE) / THERMISTOR_B_VALUE) print "Temperature is: %f K, (%f degC)" % (temp, temp-273.15) return temp-273.15 ser = serial.Serial('/dev/tty.usbserial-A800etDk', 9600) temperature_array = [] while 1: r = ser.readline() split = r.split(",") if(len(split) == 3): # Deal with the temperature pin first value = split[0].strip() temp = calculate_temp(value) temperature_array.append(temp) if(len(temperature_array) >= 20): mean = sum(temperature_array) / float(len(temperature_array)) print "Saving off this minute's mean: %f" % mean client.write_key('temperature', [DataPoint(datetime.datetime.utcnow(), mean)]) temperature_array = [] # And now the LDR resistance value = split[1].strip() voltage = float(value) / 1024 * 5 resistance = POTENTIAL_DIVIDER_RESISTOR / (5 / voltage - 1) print "LDR resistance is: %f Ohms" % resistance # And finally, the humidity value = split[2].strip() print "Humidity reading is: %d" % float(value)
class ClientTest(TestCase): def setUp(self): self.client = Client('key', 'secret', 'example.com', 443, True) self.client.session = mock.Mock() self.headers = { 'User-Agent': 'tempodb-python/%s' % tempodb.get_version(), 'Accept-Encoding': 'gzip', } self.get_headers = self.headers self.delete_headers = self.headers self.put_headers = dict({ 'Content-Type': 'application/json', }, **self.headers) self.post_headers = self.put_headers def test_init(self): client = Client('key', 'secret', 'example.com', 80, False) self.assertEqual(client.key, 'key') self.assertEqual(client.secret, 'secret') self.assertEqual(client.host, 'example.com') self.assertEqual(client.port, 80) self.assertEqual(client.secure, False) def test_defaults(self): client = Client('key', 'secret') self.assertEqual(client.host, 'api.tempo-db.com') self.assertEqual(client.port, 443) self.assertEqual(client.secure, True) def test_port_defaults(self): """ 80 is the default port for HTTP, 443 is the default for HTTPS """ client = Client('key', 'secret', 'example.com', 80, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com/v1/etc') client = Client('key', 'secret', 'example.com', 88, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com:88/v1/etc') client = Client('key', 'secret', 'example.com', 443, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com:443/v1/etc') client = Client('key', 'secret', 'example.com', 443, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com/v1/etc') client = Client('key', 'secret', 'example.com', 88, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com:88/v1/etc') client = Client('key', 'secret', 'example.com', 80, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com:80/v1/etc') def test_get_series(self): self.client.session.get.return_value = MockResponse(200, """[{ "id": "id", "key": "key", "name": "name", "tags": ["tag1", "tag2"], "attributes": {"key1": "value1"} }]""") series = self.client.get_series() self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/', auth=('key', 'secret'), headers=self.get_headers ) expected = [Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2'])] self.assertEqual(series, expected) def test_create_series(self): self.client.session.post.return_value = MockResponse(200, """{ "id": "id", "key": "my-key.tag1.1", "name": "", "tags": ["my-key", "tag1"], "attributes": {} }""") series = self.client.create_series("my-key.tag1.1") self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/', data="""{"key": "my-key.tag1.1"}""", auth=('key', 'secret'), headers=self.post_headers ) expected = Series('id', 'my-key.tag1.1', '', {}, ['my-key', 'tag1']) self.assertEqual(series, expected) def test_create_series_validity_error(self): with self.assertRaises(ValueError): series = self.client.create_series('key.b%^.test') def test_update_series(self): update = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1']) self.client.session.put.return_value = MockResponse(200, simplejson.dumps(update.to_json())) updated = self.client.update_series(update) self.client.session.put.assert_called_once_with( 'https://example.com/v1/series/id/id/', auth=('key', 'secret'), data=simplejson.dumps(update.to_json()), headers=self.put_headers ) self.assertEqual(update, updated) def test_read_id(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "key", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000", "end": "2012-03-28T00:00:00.000", "data": [{"t": "2012-03-27T00:00:00.000", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_id('id', start, end) expected = DataSet(Series('id', 'key'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/id/id/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read_key(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "key1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000", "end": "2012-03-28T00:00:00.000", "data": [{"t": "2012-03-27T00:00:00.000", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_key('key1', start, end) expected = DataSet(Series('id', 'key1'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read_key_escape(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "ke:y/1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000", "end": "2012-03-28T00:00:00.000", "data": [{"t": "2012-03-27T00:00:00.000", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_key('ke:y/1', start, end) expected = DataSet(Series('id', 'ke:y/1'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read(self): self.client.session.get.return_value = MockResponse(200, """[{ "series": { "id": "id", "key": "key1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000", "end": "2012-03-28T00:00:00.000", "data": [{"t": "2012-03-27T00:00:00.000", "v": 12.34}], "summary": {} }]""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) datasets = self.client.read(start, end, keys=['key1']) expected = [DataSet(Series('id', 'key1'), start, end, [DataPoint(start, 12.34)], Summary())] self.client.session.get.assert_called_once_with( 'https://example.com/v1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00&key=key1', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(datasets, expected) def test_delete_id(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_id("id1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/id/id1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_delete_key(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_key("key1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_delete_key_escape(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_key("ke:y/1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_write_id(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_id("id1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/id/id1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_key(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_key("key1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_key_escape(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_key("ke:y/1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_id(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_id("id1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/id/id1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_key(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_key("key1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/key1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_key_escape(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_key("ke:y/1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_bulk(self): self.client.session.post.return_value = MockResponse(200, "") data = [ { 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4.164 }, { 'id': '38268c3b231f1266a392931e15e99231', 'v': 73.13 }, { 'key': 'your-custom-key', 'v': 55.423 }, { 'key': 'foo', 'v': 324.991 }, ] ts = datetime.datetime(2012, 3, 27) result = self.client.write_bulk(ts, data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/data/', auth=('key', 'secret'), data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data), headers=self.post_headers ) self.assertEqual(result, '') def test_increment_bulk(self): self.client.session.post.return_value = MockResponse(200, "") data = [ { 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4 }, { 'id': '38268c3b231f1266a392931e15e99231', 'v': 2 }, { 'key': 'your-custom-key', 'v': 1 }, { 'key': 'foo', 'v': 1 }, ] ts = datetime.datetime(2012, 3, 27) result = self.client.increment_bulk(ts, data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/increment/', auth=('key', 'secret'), data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data), headers=self.post_headers ) self.assertEqual(result, '')
""" http://tempo-db.com/api/write-series/#write-series-by-key """ import datetime import random from tempodb import Client, DataPoint client = Client('your-api-key', 'your-api-secret') date = datetime.datetime(2012, 1, 1) for day in range(1, 10): # print out the current day we are sending data for print date data = [] # 1440 minutes in one day for min in range (1, 1441): data.append(DataPoint(date, random.random() * 50.0)) date = date + datetime.timedelta(minutes=1) client.write_key('your-custom-key', data)
from datetime import datetime from time import sleep import random from tempodb import Client, DataPoint import tempodb from os import getenv API_KEY = getenv('API_KEY') assert API_KEY, "API_KEY is required" API_SECRET = getenv('API_SECRET') assert API_SECRET, "API_SECRET is required" SERIES_KEY = getenv('SERIES_KEY', 'prng') API_HOST = getenv('API_HOST', tempodb.client.API_HOST) API_PORT = int(getenv('API_PORT', tempodb.client.API_PORT)) API_SECURE = bool(getenv('API_SECURE', True)) client = Client(API_KEY, API_SECRET, API_HOST, API_PORT, API_SECURE) while True: client.write_key(SERIES_KEY, [DataPoint(datetime.now(), random.random() * 50.0)]) sleep(1)
date = datetime.datetime.now() # 1: Pressure # correct pressure for height above sea level # see http://de.wikipedia.org/wiki/Barometrische_H%C3%B6henformel#Reduktion_auf_Meeresh.C3.B6he # baro[0] is the current temperature of the sensor # baro[1] is the absolute atmospheric pressure # p0 is the corrected atmospheric pressure adjusted to HEIGHT temperature = baro[0] temperature = 6 efactor=5.6402 * (-0.0916 + math.exp(0.06 * temperature)) xfactor = (9.80665 / (287.05 * ((273.15 + temperature) + .12 * efactor + 0.0065 * (HEIGHT/2)))) * HEIGHT p0 = baro[1] * math.exp(xfactor) # upload to tempoDB data = [] data.append(DataPoint(date, p0)) tmp=tempodbclient.write_key(SERIES_KEY1, data) # 2. Temperature data = [] data.append(DataPoint(date, temperature)) tmp=tempodbclient.write_key(SERIES_KEY2, data) print tmp sys.stdout.flush() # wait 5minutes time.sleep(300.0) tag.disconnect() del tag
""" http://tempo-db.com/api/write-series/#write-series-by-key """ import datetime import random from tempodb import Client, DataPoint # Modify these with your credentials found at: http://tempo-db.com/manage/ API_KEY = 'a68ffbe8f6fe4fb3bbda2782002680f0' API_SECRET = '3fe37f49b1bb4ae481dec13932c9bb92' SERIES_KEY = 'paul-python-1' client = Client(API_KEY, API_SECRET) date = datetime.datetime(2012, 1, 1) for day in range(1, 10): # print out the current day we are sending data for print date data = [] # 1440 minutes in one day for min in range(1, 1441): data.append(DataPoint(date, random.random() * 50.0)) date = date + datetime.timedelta(minutes=1) client.write_key(SERIES_KEY, data)
class ClientTest(TestCase): def setUp(self): self.client = Client('key', 'secret', 'example.com', 443, True) self.client.session = mock.Mock() self.headers = { 'User-Agent': 'tempodb-python/%s' % tempodb.get_version(), 'Accept-Encoding': 'gzip', } self.get_headers = self.headers self.delete_headers = self.headers self.put_headers = dict({ 'Content-Type': 'application/json', }, **self.headers) self.post_headers = self.put_headers def test_init(self): client = Client('key', 'secret', 'example.com', 80, False) self.assertEqual(client.key, 'key') self.assertEqual(client.secret, 'secret') self.assertEqual(client.host, 'example.com') self.assertEqual(client.port, 80) self.assertEqual(client.secure, False) def test_defaults(self): client = Client('key', 'secret') self.assertEqual(client.host, 'api.tempo-db.com') self.assertEqual(client.port, 443) self.assertEqual(client.secure, True) def test_port_defaults(self): """ 80 is the default port for HTTP, 443 is the default for HTTPS """ client = Client('key', 'secret', 'example.com', 80, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com/v1/etc') client = Client('key', 'secret', 'example.com', 88, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com:88/v1/etc') client = Client('key', 'secret', 'example.com', 443, False) self.assertEqual(client.build_full_url('/etc'), 'http://example.com:443/v1/etc') client = Client('key', 'secret', 'example.com', 443, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com/v1/etc') client = Client('key', 'secret', 'example.com', 88, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com:88/v1/etc') client = Client('key', 'secret', 'example.com', 80, True) self.assertEqual(client.build_full_url('/etc'), 'https://example.com:80/v1/etc') def test_get_series(self): self.client.session.get.return_value = MockResponse(200, """[{ "id": "id", "key": "key", "name": "name", "tags": ["tag1", "tag2"], "attributes": {"key1": "value1"} }]""") series = self.client.get_series() self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/', auth=('key', 'secret'), headers=self.get_headers ) expected = [ Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2'])] self.assertEqual(series, expected) def test_delete_series(self): self.client.session.delete.return_value = MockResponse( 200, """{"deleted":2}""") summary = self.client.delete_series( [], [], [], {'key': 'one', 'key2': 'two'}) self.assertEqual(summary.deleted, 2) def test_create_series(self): self.client.session.post.return_value = MockResponse(200, """{ "id": "id", "key": "my-key.tag1.1", "name": "", "tags": ["my-key", "tag1"], "attributes": {} }""") series = self.client.create_series("my-key.tag1.1") self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/', data="""{"key": "my-key.tag1.1"}""", auth=('key', 'secret'), headers=self.post_headers ) expected = Series('id', 'my-key.tag1.1', '', {}, ['my-key', 'tag1']) self.assertEqual(series, expected) def test_create_series_validity_error(self): with self.assertRaises(ValueError): series = self.client.create_series('key.b%^.test') def test_update_series(self): update = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1']) self.client.session.put.return_value = MockResponse( 200, simplejson.dumps(update.to_json())) updated = self.client.update_series(update) self.client.session.put.assert_called_once_with( 'https://example.com/v1/series/id/id/', auth=('key', 'secret'), data=simplejson.dumps(update.to_json()), headers=self.put_headers ) self.assertEqual(update, updated) def test_read_id(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "key", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000Z", "end": "2012-03-28T00:00:00.000Z", "data": [{"t": "2012-03-27T00:00:00.000Z", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_id('id', start, end) expected = DataSet(Series('id', 'key'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/id/id/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read_key(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "key1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000Z", "end": "2012-03-28T00:00:00.000Z", "data": [{"t": "2012-03-27T00:00:00.000Z", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_key('key1', start, end) expected = DataSet(Series('id', 'key1'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read_key_escape(self): self.client.session.get.return_value = MockResponse(200, """{ "series": { "id": "id", "key": "ke:y/1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000Z", "end": "2012-03-28T00:00:00.000Z", "data": [{"t": "2012-03-27T00:00:00.000Z", "v": 12.34}], "summary": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_key('ke:y/1', start, end) expected = DataSet(Series('id', 'ke:y/1'), start, end, [DataPoint(start, 12.34)], Summary()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(dataset, expected) def test_read(self): self.client.session.get.return_value = MockResponse(200, """[{ "series": { "id": "id", "key": "key1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000Z", "end": "2012-03-28T00:00:00.000Z", "data": [{"t": "2012-03-27T00:00:00.000Z", "v": 12.34}], "summary": {} }]""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) datasets = self.client.read(start, end, keys=['key1']) expected = [ DataSet(Series('id', 'key1'), start, end, [DataPoint(start, 12.34)], Summary())] self.client.session.get.assert_called_once_with( 'https://example.com/v1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00&key=key1', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(datasets, expected) def test_delete_id(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_id("id1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/id/id1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_delete_key(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_key("key1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_delete_key_escape(self): self.client.session.delete.return_value = MockResponse(200, "") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) result = self.client.delete_key("ke:y/1", start, end) self.client.session.delete.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.delete_headers ) self.assertEquals(result, '') def test_write_id(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_id("id1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/id/id1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_key(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_key("key1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/key1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_key_escape(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 12.34)] result = self.client.write_key("ke:y/1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 12.34}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_id(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_id("id1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/id/id1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_key(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_key("key1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/key1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_increment_key_escape(self): self.client.session.post.return_value = MockResponse(200, "") data = [DataPoint(datetime.datetime(2012, 3, 27), 1)] result = self.client.increment_key("ke:y/1", data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/increment/', auth=('key', 'secret'), data="""[{"t": "2012-03-27T00:00:00", "v": 1}]""", headers=self.post_headers ) self.assertEquals(result, '') def test_write_bulk(self): self.client.session.post.return_value = MockResponse(200, "") data = [ {'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4.164}, {'id': '38268c3b231f1266a392931e15e99231', 'v': 73.13}, {'key': 'your-custom-key', 'v': 55.423}, {'key': 'foo', 'v': 324.991}, ] ts = datetime.datetime(2012, 3, 27) result = self.client.write_bulk(ts, data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/data/', auth=('key', 'secret'), data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data), headers=self.post_headers ) self.assertEqual(result, '') def test_increment_bulk(self): self.client.session.post.return_value = MockResponse(200, "") data = [ {'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4}, {'id': '38268c3b231f1266a392931e15e99231', 'v': 2}, {'key': 'your-custom-key', 'v': 1}, {'key': 'foo', 'v': 1}, ] ts = datetime.datetime(2012, 3, 27) result = self.client.increment_bulk(ts, data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/increment/', auth=('key', 'secret'), data="""{"data": %s, "t": "2012-03-27T00:00:00"}""" % simplejson.dumps(data), headers=self.post_headers ) self.assertEqual(result, '') def test_write_multi(self): self.client.session.post.return_value = MockResponse(200, "") data = [ {'t': datetime.datetime(2013, 8, 21), 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4.164}, {'t': datetime.datetime(2013, 8, 22), 'id': '38268c3b231f1266a392931e15e99231', 'v': 73.13}, {'t': datetime.datetime(2013, 8, 23), 'key': 'your-custom-key', 'v': 55.423}, {'t': datetime.datetime(2013, 8, 24), 'key': 'foo', 'v': 324.991}, ] result = self.client.write_multi(data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/multi/', auth=('key', 'secret'), data=simplejson.dumps(data, default=tempodb.DATETIME_HANDLER), headers=self.post_headers ) self.assertEqual(result, '') def test_write_multi_207(self): expected_response = """{ "{'error': {"multistatus": [ { "status": "422", "messages": [ "Must provide a series ID or key" ] }, { "status": "200", "messages": [] }, { "status": "422", "messages": [ "Must provide a numeric value", "Must provide a series ID or key" ] } ]}}""" self.client.session.post.return_value = MockResponse( 207, expected_response) data = [ {'t': datetime.datetime(2013, 8, 21), 'v': 4.164}, {'t': datetime.datetime(2013, 8, 22), 'id': '38268c3b231f1266a392931e15e99231'}, {} ] result = self.client.write_multi(data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/multi/', auth=('key', 'secret'), data=simplejson.dumps(data, default=tempodb.DATETIME_HANDLER), headers=self.post_headers ) self.assertEqual(result["error"], expected_response) def test_write_multi(self): self.client.session.post.return_value = MockResponse(200, "") data = [ {'t': datetime.datetime(2013, 8, 21), 'id': '01868c1a2aaf416ea6cd8edd65e7a4b8', 'v': 4164}, {'t': datetime.datetime(2013, 8, 22), 'id': '38268c3b231f1266a392931e15e99231', 'v': 7313}, {'t': datetime.datetime(2013, 8, 23), 'key': 'your-custom-key', 'v': 55423}, {'t': datetime.datetime(2013, 8, 24), 'key': 'foo', 'v': 324991}, ] result = self.client.write_multi(data) self.client.session.post.assert_called_once_with( 'https://example.com/v1/multi/', auth=('key', 'secret'), data=simplejson.dumps(data, default=tempodb.DATETIME_HANDLER), headers=self.post_headers ) self.assertEqual(result, '')
import datetime, time from tempodb import Client, DataPoint from Temperature import Temperature from config import USER, PWD, API_KEY, API_SECRET UPDATE_INTERVAL = 60 # seconds tempoDB = Client(API_KEY, API_SECRET) core = Temperature(USER, PWD, 'mutant_jetpack') core.setInterval(UPDATE_INTERVAL) core.setRGB(0) print core while core.isConnected(): tempc = core.temperature() date = datetime.datetime.utcnow() if tempc: tempoDB.write_key("TEMPERATURE", [DataPoint(date, tempc)]) print "%s, %1.2f" % (datetime.datetime.now(), tempc) time.sleep(UPDATE_INTERVAL)
""" http://tempo-db.com/api/write-series/#write-series-by-key """ from datetime import datetime, timedelta import random from tempodb import Client, DataPoint client = Client('a93feb1d6a7e49919331b09eab299967', '67fd4ee803df456b845cdeb5e675d465') # for id in range(1,33): # station='station_'+str(id) # series = client.create_series(station) date = datetime(2012, 1, 1) for day in range(1, 10): # print out the current day we are sending data for print date for id in range(1,33): data = [] # 1440 minutes in one day for min in range (1, 1441): data.append(DataPoint(date, random.random() * 50.0)) date = date + timedelta(minutes=1) station='station_'+str(id) client.write_key(station, data)
import math, datetime from tempodb import Client, DataPoint client = Client("a755539a9e124278b04f988d39bc5ef9", "43f97dc4dbbc46499bd6694a3455210c") sin = [math.sin(math.radians(d)) for d in range(0,3600)] cos = [math.cos(math.radians(d)) for d in range(0,3600)] start = datetime.datetime(2013,01,01) sin_data = [] cos_data = [] for i in range(len(sin)): sin_data.append(DataPoint(start + datetime.timedelta(minutes=i), sin[i])) cos_data.append(DataPoint(start + datetime.timedelta(minutes=i), cos[i])) client.write_key('type:sin.1',sin_data) client.write_key('type:cos.1', cos_data) client.read_key('type:sin.1', start, datetime.datetime(2013,01,05)) attributes={ "type": "sin" } client.read(start, datetime.datetime(2013,01,05), attributes= attributes)
""" http://tempo-db.com/api/write-series/#write-series-by-key """ import datetime import random from tempodb import Client, DataPoint client = Client('your-api-key', 'your-api-secret') date = datetime.datetime(2012, 1, 1) for day in range(1, 10): # print out the current day we are sending data for print date data = [] # 1440 minutes in one day for min in range(1, 1441): data.append(DataPoint(date, random.random() * 50.0)) date = date + datetime.timedelta(minutes=1) client.write_key('your-custom-key', data)
""" http://tempo-db.com/api/write-series/#write-series-by-key """ import datetime import random from tempodb import Client, DataPoint # Modify these with your credentials found at: http://tempo-db.com/manage/ API_KEY = '680d1adbbb0c42a398b5846c8e1517dd' API_SECRET = 'f2db65d178634a36b4c25467f8bc2099' SERIES_KEY = 'your-custom-key' client = Client(API_KEY, API_SECRET) date = datetime.datetime(2012, 1, 1) for day in range(1, 10): # print out the current day we are sending data for print date data = [] # 1440 minutes in one day for min in range (1, 1441): data.append(DataPoint(date, random.random() * 50.0)) date = date + datetime.timedelta(minutes=1) client.write_key(SERIES_KEY, data)
sys.exit() volstep = (int(vollimitup) - int(vollimitdown)) / 10 print "[INIT]: ",AMIXER," limits from ",vollimitdown," to ",vollimitup,". Current volume is ",volvalue,". Volume Step is ",volstep i=0 tempArray = [] lightArray = [] softArray = [] client = Client(API_KEY, API_SECRET) while 1: sensors = ser.readline() #Temp: 26.66. Light: 125. softPot: 99 sensorsRegEx = re.compile("Temp: ([0-9.]*). Light: (\d{0,3}). softPot: (\d{0,3})") sensorsLine = sensorsRegEx.search(sensors) if i == 1000: sys.stderr.write("Sending data to tempodb...\n") client.write_key("light", lightArray) client.write_key("soft", softArray) client.write_key("temp", tempArray) tempArray = [] lightArray = [] softArray = [] i = 0 i+=1 if sensorsLine: temp = sensorsLine.group(1) light = sensorsLine.group(2) slide = int(sensorsLine.group(3)) curtime = datetime.datetime.now() tempArray.append(DataPoint(curtime,float(temp))) lightArray.append(DataPoint(curtime,float(light))) softArray.append(DataPoint(curtime,float(slide)))