{"size": 1855, "ext": "js", "max_stars_repo_path": "HttpGetData/index.js", "max_stars_repo_name": "GP2020-Sierra/azure-functions", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "HttpGetData/index.js", "max_issues_repo_name": "GP2020-Sierra/azure-functions", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "HttpGetData/index.js", "max_forks_repo_name": "GP2020-Sierra/azure-functions", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 39.4680851064, "max_line_length": 272, "alphanum_fraction": 0.5940700809}
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var DBFunctions = require("../src/DBFunctions");

const QUERY = `
SELECT TOP 1000 dateadd(mi, datediff(mi, 0, timestamp) / @perMinutes * @perMinutes, 0) as timestamp, AVG(temperatureDHT) as temperature, AVG(pressureLPS) as pressure, AVG(humidityDHT) as humidity, AVG(eco2) as co2, AVG(wifiDevices) as devices, COUNT(timestamp) as '_count'
FROM [iot].[messages] m
INNER JOIN [iot].[locations] l
ON m.locationID = l.id and l.nameID = @location
WHERE datediff(mi, '1970-01-01', timestamp) >= ((@since / 60 / @perMinutes) - 1) * @perMinutes
GROUP BY datediff(mi,0, timestamp) / @perMinutes
ORDER BY timestamp DESC`

module.exports = function (context, req) {
    context.log('HTTP Get Data');

    DBFunctions.dbFunction(context, (connection, callbacks) => {
        var request = new Request(QUERY, callbacks.ifErrorHttp);
        request.addParameter("location", TYPES.NVarChar, context.bindingData.locationID);
        request.addParameter("perMinutes", TYPES.Int, req.query.granularity || (req.body && req.body.granularity) || 5)
        request.addParameter("since", TYPES.Int, req.query.since || (req.body && req.body.since) || 1)

        let result = [];

        request.on('row', (columns) => {
            var o = {}
            columns.forEach(x => o[x.metadata.colName] = x.value);
            result.push(o);
        });

        request.on('requestCompleted', function() {
            if (result.length > 0) {
                context.res = {
                    body: JSON.stringify(result)
                }
            } else {
                context.res = {
                    status: 404,
                    body: "Location not found"
                }
            }
            callbacks.done();
        });

        connection.execSql(request);
    });
};