class Source(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True) source_type_id = db.Column(db.Integer, db.ForeignKey('source_type.id')) source_type = db.relationship( 'SourceType', backref=db.backref('sources', lazy='dynamic')) params = db.Column(JsonType)
class MetricData(db.Model): __table_args__ = (sa.UniqueConstraint('metric_id', 'created'),) id = db.Column(db.Integer, primary_key=True) created = db.Column(db.DateTime, nullable=False) metric_id = db.Column(db.Integer, db.ForeignKey('metric.id')) metric = db.relationship( 'Metric', backref=db.backref('metric_data', lazy='dynamic')) int_value = db.Column(db.Integer)
class Log(db.Model): id = db.Column(db.Integer, primary_key=True) created = db.Column(db.DateTime) value = db.Column(db.String(255)) def __init__(self, value): self.created = datetime.datetime.now() self.value = value def __repr__(self): return '<Log %r>' % self.value
class Metric(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) source_id = db.Column(db.Integer, db.ForeignKey('source.id')) source = db.relationship( 'Source', backref=db.backref('metrics', lazy='dynamic')) last_run = db.Column(db.DateTime) cron_minute = db.Column(db.String(64)) cron_hour = db.Column(db.String(64)) cron_dow = db.Column(db.String(64)) cron_dom = db.Column(db.String(64)) cron_moy = db.Column(db.String(64)) #transform def batch_update(self, rows): if not rows: return row_list = [] for row in rows: row_list = [] row_list.append({ 'created': row[0], 'metric_id': self.id, 'int_value': int(row[1]) }) db.session.execute( MetricData.__table__.insert( append_string='ON DUPLICATE KEY UPDATE int_value=' + str(row[1]), ), row_list, ) print row_list # TODO: Using append_string here raises a SA warming because # it isn't part of the dialect. # t = sa.text('insert into metric_data (created, metric_id, int_value) values (:created, :metric_id, :int_value) on duplicate key update int_value=:int_value') # db.session.execute(t, row_list) # db.session.execute( # MetricData.__table__.insert( # append_string='ON DUPLICATE KEY UPDATE int_value=int_value', # ), # row_list, # ) db.session.expire_all()
class SourceType(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True)