Commit 274b5ab8 authored by Dominik Rosiek's avatar Dominik Rosiek

successful adds to database

parent 90106a46
import logging
import time
import re
import six
import uuid
......@@ -81,8 +84,8 @@ class DDBUUIDField(DDBField):
class DDBTable(object):
TABLE_NAME = ''
REGION_NAME = 'us-west-2'
TABLE_NAME = 'measurements'
REGION_NAME = 'eu-central-1'
KEY_SCHEMA = []
LOCAL_SECONDARY_INDEXES = []
GLOBAL_SECONDARY_INDEXES = []
......@@ -190,32 +193,35 @@ class DDBTable(object):
class DDBmeasurements(DDBTable):
TABLE_NAME = 'measurements'
KEY_SCHEMA = [{
'AttributeName': 'measurement_id',
'KeyType': 'HASH',
'AttributeName': 'sid',
'KeyType': 'Number',
},{
'AttributeName': 'tid',
'KeyType': 'Number',
}]
PROVISIONED_THROUGHPUT = {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
FIELDS = {
'measurement_id': DDBUUIDField,
'type_id': DDBIntField,
'station_id': DDBIntField,
'sid': DDBIntField,
'tid': DDBIntField,
'time': DDBIntField
}
@gen.coroutine
def update(self, measurement_id, type_id, station_id):
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'measurement_id': measurement_id}),
AttributeUpdates=self.encode_item(data={'type_id': type_id, 'station_id': station_id}, update=True))
Key=self.encode_item(data={'tid': type_id, 'sid': station_id}),
AttributeUpdates=self.encode_item(data={'time': time.time()}, update=True))
raise gen.Return(message)
@gen.coroutine
def get(self, measurement_id, type_id, station_id):
def get(self, tid=1, sid=1):
message = yield gen.Task(self._dynamodb(operation='GetItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'measurement_id': measurement_id}))
Key=self.encode_item(data={'tid': tid, 'sid': sid}))
data = self.decode_item(item=message['Item'])
raise gen.Return(data)
......@@ -227,12 +233,12 @@ if __name__ == '__main__':
measurements.create_table()
# You still can run code synchronous if required
measurements.update_(measurement_id=measurement_id, station_id=1, type_id=1)
measurements.update(measurement_id=measurement_id, station_id=1, type_id=7)
# run asynchronous with callback
measurements.get_(measurement_id=measurement_id, callback=print)
measurements.get()
# You even can run methods wrapped with @coroutine synchronously
ioloop = IOLoop.instance()
result = ioloop.run_sync(partial(measurements.get, measurement_id=measurement_id))
result = ioloop.run_sync(partial(measurements.get, sid=1, tid=7))
print(result)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment