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

successful adds to database

parent 90106a46
import logging import logging
import time
import re import re
import six import six
import uuid import uuid
...@@ -81,8 +84,8 @@ class DDBUUIDField(DDBField): ...@@ -81,8 +84,8 @@ class DDBUUIDField(DDBField):
class DDBTable(object): class DDBTable(object):
TABLE_NAME = '' TABLE_NAME = 'measurements'
REGION_NAME = 'us-west-2' REGION_NAME = 'eu-central-1'
KEY_SCHEMA = [] KEY_SCHEMA = []
LOCAL_SECONDARY_INDEXES = [] LOCAL_SECONDARY_INDEXES = []
GLOBAL_SECONDARY_INDEXES = [] GLOBAL_SECONDARY_INDEXES = []
...@@ -190,32 +193,35 @@ class DDBTable(object): ...@@ -190,32 +193,35 @@ class DDBTable(object):
class DDBmeasurements(DDBTable): class DDBmeasurements(DDBTable):
TABLE_NAME = 'measurements' TABLE_NAME = 'measurements'
KEY_SCHEMA = [{ KEY_SCHEMA = [{
'AttributeName': 'measurement_id', 'AttributeName': 'sid',
'KeyType': 'HASH', 'KeyType': 'Number',
},{
'AttributeName': 'tid',
'KeyType': 'Number',
}] }]
PROVISIONED_THROUGHPUT = { PROVISIONED_THROUGHPUT = {
'ReadCapacityUnits': 1, 'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1 'WriteCapacityUnits': 1
} }
FIELDS = { FIELDS = {
'measurement_id': DDBUUIDField, 'sid': DDBIntField,
'type_id': DDBIntField, 'tid': DDBIntField,
'station_id': DDBIntField, 'time': DDBIntField
} }
@gen.coroutine @gen.coroutine
def update(self, measurement_id, type_id, station_id): def update(self, measurement_id, type_id, station_id):
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call, message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(), TableName=self._get_table_name(),
Key=self.encode_item(data={'measurement_id': measurement_id}), Key=self.encode_item(data={'tid': type_id, 'sid': station_id}),
AttributeUpdates=self.encode_item(data={'type_id': type_id, 'station_id': station_id}, update=True)) AttributeUpdates=self.encode_item(data={'time': time.time()}, update=True))
raise gen.Return(message) raise gen.Return(message)
@gen.coroutine @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, message = yield gen.Task(self._dynamodb(operation='GetItem').call,
TableName=self._get_table_name(), 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']) data = self.decode_item(item=message['Item'])
raise gen.Return(data) raise gen.Return(data)
...@@ -227,12 +233,12 @@ if __name__ == '__main__': ...@@ -227,12 +233,12 @@ if __name__ == '__main__':
measurements.create_table() measurements.create_table()
# You still can run code synchronous if required # 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 # run asynchronous with callback
measurements.get_(measurement_id=measurement_id, callback=print) measurements.get()
# You even can run methods wrapped with @coroutine synchronously # You even can run methods wrapped with @coroutine synchronously
ioloop = IOLoop.instance() 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) 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