Example #1
0
def latest():
    with mysql.purp_db_session() as session:
        query = session.query(Measurement).order_by(
            Measurement.__table__.c.id.desc())
        record = query.first()
        output = record.to_dict(include_stats=True, format_date=False)
        return output
Example #2
0
def get_top(param, count):
    with mysql.purp_db_session() as session:
        print(param)
        sort_field = Measurement.__table__.c.get(param)
        print(sort_field)
        query = session.query(Measurement).order_by(sort_field.desc())
        query = query.limit(count)
        rows = query.all()
        items = [row.to_dict(False, False) for row in rows]
        return items
Example #3
0
def get_by_date(day):
    with mysql.purp_db_session() as session:
        select_params = {'day': day}
        sql_query = """
        SELECT *
        FROM measurement 
        WHERE timestamp >= UNIX_TIMESTAMP(:day) - 60*60*24 AND timestamp < (UNIX_TIMESTAMP(:day))
        ORDER BY timestamp ASC;
        """
        rows = session.execute(sql_query, select_params).fetchall()
        return [Measurement(**dict(row)).to_dict(False, False) for row in rows]
Example #4
0
def create_measurement(data):
    with mysql.purp_db_session() as session:
        query = session.query(Measurement).order_by(
            Measurement.__table__.c.id.desc())
        record = query.first()
        if record:
            latest = record.to_dict()
            if latest['timestamp'] == data['timestamp']:
                return latest
        measurement = Measurement(**data)
        session.add(measurement)
        session.flush()
        return measurement.to_dict()
Example #5
0
def get_trends(count):
    with mysql.purp_db_session() as session:
        # query = session.query(Measurement).options(defer(Measurement.stats))
        # sort_field = Measurement.__table__.c.id.desc()
        # query = query.order_by(sort_field)
        # query = query.limit(count)
        select_params = {'hours': 60 * 60 * int(count)}
        print(count)
        sql_query = """
        SELECT *
        FROM measurement 
        WHERE timestamp >= UNIX_TIMESTAMP(NOW()) - :hours
        ORDER BY timestamp ASC;
        """
        rows = session.execute(sql_query, select_params).fetchall()
        items = [
            Measurement(**dict(row)).to_dict(False, False) for row in rows
        ]
        return items
Example #6
0
def get_media():
    with mysql.purp_db_session() as session:
        query = session.query(Fanmedia).order_by(
            Fanmedia.__table__.c.id.desc())
        records = query.all()
        return [record.to_dict() for record in records]
Example #7
0
def get_songs():
    with mysql.purp_db_session() as session:
        query = session.query(Song).order_by(
            Song.__table__.c.track_number.asc())
        records = query.all()
        return [record.to_dict() for record in records]
Example #8
0
def get_fiction():
    with mysql.purp_db_session() as session:
        query = session.query(Fanfic).order_by(Fanfic.__table__.c.id.asc())
        records = query.all()
        return [record.to_dict() for record in records]
Example #9
0
def get_videos():
    with mysql.purp_db_session() as session:
        query = session.query(Video)
        records = query.all()
        return [record.to_dict() for record in records]