akindices/app/main/models.py
Matthew Dillon 2ec63cc0e3 WIP
2015-10-10 07:55:17 -07:00

108 lines
3.8 KiB
Python

from app import db
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import text
from flask import abort
class Dataset(db.Model):
__tablename__ = 'datasets'
id = db.Column(db.Integer, primary_key=True)
datatype = db.Column(db.String(15), nullable=False)
model = db.Column(db.String(15), nullable=False)
modelname = db.Column(db.String(50), nullable=False)
scenario = db.Column(db.String(15), nullable=False)
resolution = db.Column(db.String(15), nullable=False)
temperatures = db.relationship('Temperature', backref='datasets')
@hybrid_property
def type(self):
return self.datatype.lower().capitalize()
class Temperature(db.Model):
__tablename__ = 'temperatures'
id = db.Column(db.Integer, primary_key=True)
dataset_id = db.Column(db.Integer, db.ForeignKey('datasets.id'))
community_id = db.Column(db.Integer, db.ForeignKey('communities.id'))
year = db.Column(db.Integer, nullable=False)
january = db.Column(db.Float, nullable=False)
february = db.Column(db.Float, nullable=False)
march = db.Column(db.Float, nullable=False)
april = db.Column(db.Float, nullable=False)
may = db.Column(db.Float, nullable=False)
june = db.Column(db.Float, nullable=False)
july = db.Column(db.Float, nullable=False)
august = db.Column(db.Float, nullable=False)
september = db.Column(db.Float, nullable=False)
october = db.Column(db.Float, nullable=False)
november = db.Column(db.Float, nullable=False)
december = db.Column(db.Float, nullable=False)
updated = db.Column(db.DateTime, nullable=True)
class DB:
@classmethod
def getCommunity(cls, id):
cmd = """
SELECT id, name, latitude, longitude, northing, easting
FROM new_communities
WHERE id=:id;
"""
result = db.engine.execute(text(cmd), id=id).fetchone()
return result or abort(500)
@classmethod
def getCommunities(cls):
cmd = """
SELECT id, name
FROM new_communities
ORDER BY name ASC;
"""
result = db.engine.execute(text(cmd), id=id).fetchall()
return result or abort(500)
@classmethod
def getDatasets(cls):
cmd = """
SELECT DISTINCT ON (
doc->'model',
doc->'datatype',
doc->'resolution',
doc->'modelname',
doc->'scenario'
)
doc->'model' AS model,
doc->'datatype' AS datatype,
doc->'resolution' AS resolution,
doc->'modelname' AS modelname,
doc->'scenario' AS scenario
FROM new_communities c,
jsonb_array_elements(c.data)
WITH ORDINALITY t1(doc, rn)
ORDER BY datatype ASC, modelname ASC, scenario ASC;
"""
result = db.engine.execute(text(cmd)).fetchall()
return result or abort(500)
@classmethod
def getTemps(cls, start, end, community_id, model, scenario):
years = [str(x) for x in range(int(start), int(end)+1)]
cmd = """
WITH x AS (
SELECT name, jsonb_array_elements(data) AS data
FROM new_communities
WHERE id=:community_id)
SELECT d.key::INTEGER AS year, d.value AS temperatures
FROM x, jsonb_each(data) d
WHERE data->>'model'=:model
AND data->>'scenario'=:scenario
AND d.key IN :years;
"""
result = db.engine.execute(text(cmd),
community_id=community_id,
model=model,
scenario=scenario,
years=tuple(years)).fetchall()
return result or abort(500)