Commit a5ec0bbd authored by Dominik Rosiek's avatar Dominik Rosiek

:)

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