示例#1
0
def query_lastweek_device():

    form = QueryAPI_lastweek_device(request.form)
    if form.validate_on_submit():
        device = form.sensors.data

        query = ''' 
            from(bucket:"{}")
                |> range(start: -7d, stop: now())
                |> filter(fn:(r) =>
                r._measurement == "sensorWizether" and
                r.device == "{}")
        '''.format(app.config['INFLUX_BUCKET'], device)

        #print(query)

        root_path = Path(current_app.root_path).parent
        upload_path = os.path.join(root_path, 'api_tmp')

        df_result = sendQueryInflux(query)

        filename = 'data_' + datetime.now().strftime("%Y-%m-%d") + '.csv'
        filename_path = os.path.join(upload_path, filename)

        if df_result.empty:
            flash('Error. No se han encontrado datos')
            return redirect(url_for('dashboard.open_api'))
        else:
            df_result = removeUselessColumns(df_result)
            df_result.to_csv(filename_path, index=False)

        return send_file(filename_path, as_attachment=True)
    else:
        flash('Error. Revisa los datos introducidos y vuelve a probar.')
        return redirect(url_for('dashboard.open_api'))
示例#2
0
def query_date_range():

    form = QueryAPI_DateRange_Field(request.form)
    if form.validate_on_submit():
        start = form.start_date.data.strftime("%Y-%m-%dT%H:%M:%SZ")
        stop = form.stop_date.data.strftime("%Y-%m-%dT%H:%M:%SZ")
        field = form.fields.data
        device = form.sensors.data

        # Check if start date its wrong
        if (start > stop):
            flash(
                'Error. La fecha de inicio no puede ser mayor a la fecha de fin.'
            )
            return redirect(url_for('dashboard.open_api'))

        query = ''' 
        from(bucket:"{}")
            |> range(start: {}, stop: {})
            |> filter(fn:(r) =>
            r._measurement == "sensorWizether" and
            r._field == "{}" and
            r.device == "{}")
        '''.format(app.config['INFLUX_BUCKET'], start, stop, field, device)

        #print(query)

        root_path = Path(current_app.root_path).parent
        upload_path = os.path.join(root_path, 'api_tmp')

        df_result = sendQueryInflux(query)

        filename = 'data_' + datetime.now().strftime("%Y-%m-%d") + '.csv'
        filename_path = os.path.join(upload_path, filename)

        if df_result.empty:
            flash('Error. No se han encontrado datos')
            return redirect(url_for('dashboard.open_api'))
        else:
            df_result = removeUselessColumns(df_result)
            df_result.to_csv(filename_path, index=False)

        return send_file(filename_path, as_attachment=True)
    else:
        flash('Error. Revisa los datos introducidos y vuelve a probar.')
        return redirect(url_for('dashboard.open_api'))