Commit a5ec0bbd authored by Dominik Rosiek's avatar Dominik Rosiek

:)

parent c6dcfd09
...@@ -203,7 +203,6 @@ class DDBTable(object): ...@@ -203,7 +203,6 @@ class DDBTable(object):
message = yield gen.Task(self._dynamodb(operation='Scan').call, message = yield gen.Task(self._dynamodb(operation='Scan').call,
TableName=self._get_table_name()) TableName=self._get_table_name())
data = [self.decode_item(item=item) for item in message['Items']] data = [self.decode_item(item=item) for item in message['Items']]
raise gen.Return(data) raise gen.Return(data)
...@@ -339,24 +338,44 @@ class DDBmeasurements(DDBTable): ...@@ -339,24 +338,44 @@ class DDBmeasurements(DDBTable):
data = self.decode_item(item=message['Item']) data = self.decode_item(item=message['Item'])
raise gen.Return(data) raise gen.Return(data)
if __name__ == '__main__':
types = DDBtypes() class DDBlastmeasurements(DDBTable):
types.create_table() TABLE_NAME = 'lastmeasurements'
types.add(1, 2, 3, 4, 5) KEY_SCHEMA = [{
ioloop = IOLoop.instance() 'AttributeName': 'station_id',
result = ioloop.run_sync(partial(types.get_all)) 'KeyType': 'HASH',
print(result) },
{
measurements = DDBmeasurements() 'AttributeName': 'pollution_id',
measurements.create_table() 'KeyType': 'HASH',
measurements.add(1, 2, 3, 4) }]
ioloop = IOLoop.instance() PROVISIONED_THROUGHPUT = {
result = ioloop.run_sync(partial(measurements.get_all)) 'ReadCapacityUnits': 1,
print(result) 'WriteCapacityUnits': 1
}
stations = DDBstations() FIELDS = {
stations.create_table() 'station_id': DDBUUIDField,
stations.add(1, 2, 3, 4) 'pollution_id': DDBUUIDField,
ioloop = IOLoop.instance() 'measurement_id': DDBUUIDField,
result = ioloop.run_sync(partial(stations.get_all)) }
print(result)
@gen.coroutine
def add(self, measurement):
data = {
'measurement_id': measurement.id,
}
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'station_id': measurement.station.id, 'pollution_id': measurement.type.id}),
AttributeUpdates=self.encode_item(data=data, update=True))
raise gen.Return(message)
@gen.coroutine
def get(self, station, pollution_type):
message = yield gen.Task(self._dynamodb(operation='GetItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'station_id': station.id, 'pollution_id': pollution_type.id})
)
data = self.decode_item(item=message['Item'])
raise gen.Return(data)
\ No newline at end of file
...@@ -19,10 +19,12 @@ class MainHandler(tornado.web.RequestHandler): ...@@ -19,10 +19,12 @@ class MainHandler(tornado.web.RequestHandler):
#stations = models.Station.get_all() #stations = models.Station.get_all()
#stations = models.Station.getall() #stations = models.Station.getall()
stations = yield models.Station.get_all() stations = yield models.Station.get_all()
pollutions = yield models.Type.get_all()
measurement = yield models.Measurement.get("5c2be10b-5d33-47d7-95fe-b3f6fc29a3af") for station in stations.values():
for s in stations: for pollution in pollutions.values():
self.write("{0} {1} {2} {3} {4}<br />".format(s.id, s.city, s.longitude, s.latitude, s.name)) measurement = yield models.Measurement.get_last(station, pollution)
self.write(str(measurement))
# measurement = models.Measurement.getlast(station=s.id) # measurement = models.Measurement.getlast(station=s.id)
# self.write("".format()) # self.write("".format())
......
import uuid import uuid
from db import DDBtypes, DDBmeasurements, DDBstations from db import DDBtypes, DDBmeasurements, DDBstations, DDBlastmeasurements
from functools import partial from functools import partial
from tornado.gen import coroutine from tornado.gen import coroutine
...@@ -41,12 +41,14 @@ class Station(DDBobject): ...@@ -41,12 +41,14 @@ class Station(DDBobject):
@classmethod @classmethod
@coroutine @coroutine
def get_all(cls): def get_all(cls):
data = yield DDBstations().get_all() if not cls.stored:
return_value = [] data = yield DDBstations().get_all()
for station in data: return_value = []
return_value.append(cls.from_dict(station)) for station in data:
return_value.append(cls.from_dict(station))
cls.stored = {i.id: i for i in return_value}
return return_value return cls.stored
class Type(DDBobject): class Type(DDBobject):
...@@ -69,10 +71,23 @@ class Type(DDBobject): ...@@ -69,10 +71,23 @@ class Type(DDBobject):
result = yield DDBtypes().get(type_id) result = yield DDBtypes().get(type_id)
return cls.from_dict(result) return cls.from_dict(result)
@coroutine
def save(self): def save(self):
result = yield (DDBtypes().add(pollution_type=self)) result = yield DDBtypes().add(pollution_type=self)
return result return result
@classmethod
@coroutine
def get_all(cls):
if not cls.stored:
data = yield DDBtypes().get_all()
return_value = []
for pollution_type in data:
return_value.append(cls.from_dict(pollution_type))
cls.stored = {i.id: i for i in return_value}
return cls.stored
class Measurement(DDBobject): class Measurement(DDBobject):
stored = {} stored = {}
...@@ -105,9 +120,15 @@ class Measurement(DDBobject): ...@@ -105,9 +120,15 @@ class Measurement(DDBobject):
@coroutine @coroutine
def save(self): def save(self):
result = yield DDBmeasurements().add(measurement=self) result = yield DDBmeasurements().add(measurement=self)
result = yield DDBlastmeasurements().add(measurement=self)
return result return result
@classmethod
@coroutine @coroutine
def get_last(self, station_id): def get_last(cls, station, pollution_type):
# TODO data = yield DDBlastmeasurements().get(station, pollution_type)
pass return_value = yield cls.get(measurement_id=data['measurement_id'])
return return_value
def __str__(self):
return "{0} {1} {2} {3} {4}".format(self.id, self.station.id, self.type.id, self.value, self.time)
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