Example #1
0
def overview_devices_count():
    time_unit_models = {
        'hour': DeviceCountHour,
        'day': DeviceCountDay,
        'month': DeviceCountMonth
    }
    time_unit, time_format = _validate_time_unit()
    model = time_unit_models[time_unit]
    charts_config = get_charts_config(time_unit=time_unit)
    start_time = charts_config['start_time']
    x_data = charts_config['x_data']

    # Query different models according to time unit
    time_devices_count = db.session \
        .query(func.to_char(model.countTime, time_format),
               func.sum(model.deviceCount)) \
        .filter(model.countTime > start_time, model.tenantID == g.tenant_uid) \
        .group_by(func.to_char(model.countTime, time_format)) \
        .order_by(func.to_char(model.countTime, time_format)).all()
    devices_count_dict = dict(time_devices_count)
    records = {
        'time': x_data,
        'value': [devices_count_dict.get(date, 0) for date in x_data]
    }
    return jsonify(records)
Example #2
0
def overview_messages_count():
    time_unit_models = {
        'hour': EmqxBillHour, 'day': EmqxBillDay, 'month': EmqxBillMonth
    }
    time_unit, time_format = _validate_time_unit()
    model = time_unit_models[time_unit]
    charts_config = get_charts_config(time_unit=time_unit)
    start_time = charts_config['start_time']
    x_data = charts_config['x_data']
    # Query different models according to time unit
    time_messages_count = db.session \
        .query(func.to_char(model.countTime, time_format).label('msgTime'),
               model.msgType, func.sum(model.msgCount)) \
        .filter_tenant(tenant_uid=g.tenant_uid) \
        .filter(model.countTime > start_time) \
        .group_by(func.to_char(model.countTime, time_format), model.msgType) \
        .order_by(func.to_char(model.countTime, time_format)).all()
    records = _convert_query_message(time_messages_count, x_data)
    return jsonify(records)