Commit 5ee60c43 authored by Dominik Rosiek's avatar Dominik Rosiek

models

parent 8c35dc58
......@@ -152,14 +152,14 @@ class DDBTable(object):
def create_table(self):
try:
message = self._dynamodb(operation='DescribeTable').call(
self._dynamodb(operation='DescribeTable').call(
TableName=self._get_table_name())
except AmazonException as e:
if e.code != 'ResourceNotFoundException':
raise e
logger.warning('Creation {table_name} table ...'.format(
table_name=self._get_table_name()))
message = self._dynamodb(operation='CreateTable').call(
self._dynamodb(operation='CreateTable').call(
**self._get_table_kwargs())
else:
logger.warning('{table_name} table already exists.'.format(
......@@ -218,7 +218,7 @@ class DDBstations(DDBTable):
}
FIELDS = {
'station_id': DDBUUIDField,
'api_staion_id': DDBIntField,
'api_station_id': DDBIntField,
'city': DDBStringField,
'longitude': DDBIntField,
'latitude': DDBIntField,
......@@ -226,19 +226,18 @@ class DDBstations(DDBTable):
}
@gen.coroutine
def add(self, city, api_staion_id, longitude, latitude, name):
station_id = uuid.uuid4()
def add(self, station):
data = {
'city': city,
'api_staion_id': api_staion_id,
'longitude': longitude,
'latitude': latitude,
'name':name
'city': station.city,
'api_station_id': station.api_id,
'longitude': station.longitude,
'latitude': station.latitude,
'name': station.name
}
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'station_id': station_id}),
Key=self.encode_item(data={'station_id': station.id}),
AttributeUpdates=self.encode_item(data=data, update=True))
raise gen.Return(message)
......@@ -268,23 +267,22 @@ class DDBtypes(DDBTable):
'unit': DDBStringField,
'norm': DDBIntField,
'longname': DDBStringField,
'descriptions': DDBStringField
'description': DDBStringField
}
@gen.coroutine
def add(self, shortname, unit, norm, longname, descriptions):
type_id = uuid.uuid4()
def add(self, pollution_type):
data = {
'shortname': shortname,
'unit': unit,
'norm': norm,
'longname': longname,
'descriptions':descriptions
'shortname': pollution_type.shortname,
'unit': pollution_type.unit,
'norm': pollution_type.norm,
'longname': pollution_type.longname,
'description': pollution_type.description
}
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'type_id': type_id}),
Key=self.encode_item(data={'type_id': pollution_type.id}),
AttributeUpdates=self.encode_item(data=data, update=True))
raise gen.Return(message)
......@@ -296,7 +294,6 @@ class DDBtypes(DDBTable):
)
data = self.decode_item(item=message['Item'])
raise gen.Return(data)
raise gen.Return(data)
class DDBmeasurements(DDBTable):
......@@ -318,18 +315,17 @@ class DDBmeasurements(DDBTable):
}
@gen.coroutine
def add(self, station_id, type_id, value, measurement_time):
measurement_id = uuid.uuid4()
def add(self, measurement):
data = {
'station_id': uuid.uuid4(),
'type_id': uuid.uuid4(),
'value': value,
'time': measurement_time
'station_id': measurement.station.id,
'type_id': measurement.type.id,
'value': measurement.value,
'time': measurement.time
}
message = yield gen.Task(self._dynamodb(operation='UpdateItem').call,
TableName=self._get_table_name(),
Key=self.encode_item(data={'measurement_id': measurement_id}),
Key=self.encode_item(data={'measurement_id': measurement.id}),
AttributeUpdates=self.encode_item(data=data, update=True))
raise gen.Return(message)
......
import uuid
from db import DDBtypes, DDBmeasurements, DDBstations
from functools import partial
from tornado.ioloop import IOLoop
class DDBobject(object):
@classmethod
def from_dict(cls, data):
return cls(**data)
class Station(DDBobject):
stored = {}
def __init__(self, api_station_id, city, longitude, latitude, name, station_id=None):
self.id = station_id or str(uuid.uuid4())
self.api_id = api_station_id
self.city = city
self.longitude = longitude
self.latitude = latitude
self.name = name
@classmethod
def get(cls, station_id):
if cls.stored.get(station_id, None):
return cls.stored.get(station_id)
return cls.from_dict(IOLoop.instance().run_sync(partial(DDBstations().get, station_id)))
def save(self):
return IOLoop.instance().run_sync(partial(DDBstations().add, station=self))
class Type(DDBobject):
stored = {}
def __init__(self, shortname, unit, norm, longname, description, type_id=None):
self.id = type_id or str(uuid.uuid4())
self.shortname = shortname
self.unit = unit
self.norm = norm
self.longname = longname
self.description = description
@classmethod
def get(cls, type_id):
if cls.stored.get(type_id, None):
return cls.stored.get(type_id)
return cls.from_dict(IOLoop.instance().run_sync(partial(DDBtypes().get, type_id)))
def save(self):
return IOLoop.instance().run_sync(partial(DDBtypes().add, pollution_type=self))
class Measurement(DDBobject):
stored = {}
def __init__(self, station, pollution_type, value, time, measurement_id=None):
self.id = measurement_id or str(uuid.uuid4())
self.station = station
self.type = pollution_type
self.value = value
self.time = time
@classmethod
def get(cls, measurement_id):
if cls.stored.get(measurement_id, None):
return cls.stored.get(measurement_id)
data = IOLoop.instance().run_sync(partial(DDBmeasurements().get, measurement_id))
data['station'] = Station.get(data['station_id'])
del data['station_id']
data['pollution_type'] = Type.get(data['type_id'])
del data['type_id']
return cls.from_dict(data)
def save(self):
return IOLoop.instance().run_sync(partial(DDBmeasurements().add, measurement=self))
import models
station = models.Station(1, "Kraków", 12, 13, "Aleje")
station.save()
station2 = models.Station.get(station.id)
assert station.id == station2.id
pollution_type = models.Type(shortname="pm2", unit="mg", norm=120, longname="long_pm2", description="description pm2")
pollution_type.save()
pollution_type2 = models.Type.get(pollution_type.id)
assert pollution_type.id == pollution_type2.id
measurement = models.Measurement(station=station, pollution_type=pollution_type, value=13, time=124)
measurement.save()
measurement2 = models.Measurement.get(measurement.id)
assert measurement.id == measurement2.id
\ No newline at end of file
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