def handle_graph_endpoint(board_id=None, graph_type=None, start=None, end=None, last_available=None): boards = database.get_board(app.config["db"]) board_desc = dict((board.board_id, board.board_desc) for board in boards) last_metrics = database.get_last_metric(app.config["db"], board_ids=board_id, sensor_type=graph_type) output = list() for last_metric in last_metrics: output.append({"id": last_metric.board_id, "desc": board_desc[last_metric.board_id], "data": []}) metrics = database.get_metric( app.config["db"], board_ids=last_metric.board_id, sensor_type=graph_type, start=start, end=end ) if not metrics and last_available: metrics = database.get_metric( app.config["db"], board_ids=last_metric.board_id, sensor_type=graph_type, last_available=last_available ) for metric in metrics: data = (metric.last_update, float(metric.sensor_data)) output[-1]["data"].append(data) return output
def aggregate(aggregate_details, db, start, end, sensor_type, execute): ids_to_delete = [] a_metrics = database.get_metric_aggregated(db, start=start, end=end, sensor_type=sensor_type) for a_metric in a_metrics: metric = { 'id': a_metric[0], 'board_id': a_metric[1], 'sensor_type': a_metric[2], 'total_sum': a_metric[3], 'count': a_metric[4] } for aggregate_detail in aggregate_details: if metric['sensor_type'] in aggregate_detail['sensor_type']: m_to_delete = database.get_metric(db, board_ids=metric['board_id'], sensor_type=metric['sensor_type'], start=start, end=end) ids_to_delete += [m.id for m in m_to_delete if m.id != metric['id']] new_value = compute_value(aggregate_detail, metric['total_sum'], metric['count']) LOG.info("Update record with id '%d' for sensor_type '%s' for board_id '%s' with value '%s'", metric['id'], metric['sensor_type'], metric['board_id'], new_value) if execute and new_value: database.update_metric(db, metric['id'], new_value) LOG.info("Delete %d metrics", len(ids_to_delete)) if execute: database.delete_row(db, database.Metric, ids_to_delete)
def _get_value_count(self, value_count, board_ids=None, sensor_type=None, start_offset=None, end_offset=None): db_params = { 'db': self.db, 'board_ids': board_ids, 'sensor_type': sensor_type, 'start': utils.time_offset(start_offset), 'end': utils.time_offset(end_offset) } if value_count['type'] == 'Metric': db_params['last_available'] = value_count['count'] metrics = database.get_metric(**db_params) elif value_count['type'] == 'LastMetric': metrics = database.get_last_metric(**db_params) else: metrics = [] return metrics