index.js 3.56 KB
Newer Older
Hanadi's avatar
Hanadi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const node_cron_1 = __importDefault(require("node-cron"));
const HereClient_1 = require("./src/here/HereClient");
const FrostClient_1 = require("./src/frost/FrostClient");
const logger_1 = require("./src/logger/logger");
const app = express_1.default();
const PORT = 8089;
const geoProximity = {
    latitude: 48.791348,
    longitude: 9.190342,
    distance: 3000
};
if (!process.env.HERE_API_KEY) {
    throw new Error("HERE_API_KEY environment variable should be present!");
}
if (!process.env.FROST_URL) {
    throw new Error("FROST_URL environment variable should be present!");
}
const hereClient = new HereClient_1.HereClient(process.env.HERE_API_KEY);
const frostClient = new FrostClient_1.FrostClient(process.env.FROST_URL);
function triggerJob(forceUpdateLocations = false) {
    return __awaiter(this, void 0, void 0, function* () {
        const now = new Date().getTime();
        return hereClient.flow(geoProximity)
            .then(flowResponse => frostClient.mapAndInsertHereFlowResponse(flowResponse, forceUpdateLocations))
            .then(insertedObservations => ({
            status: 200,
            success: true,
            millis: new Date().getTime() - now,
            insertedObservations
        }))
            .catch((err) => {
            logger_1.error(err.message, err);
            return Object.assign(Object.assign({}, err), { insertedObservations: "Unknown", status: 500, success: false, message: err.message, name: err.name, stack: err.stack, millis: new Date().getTime() - now });
        });
    });
}
// Routes
app.get("/", (req, res) => res.send("Healthy!"));
app.get("/here", (req, res) => {
    hereClient.flow(geoProximity)
        .then(body => res.send(body))
        .catch(err => console.error(err));
});
app.get("/trigger-job", (req, res) => {
    logger_1.info("Received trigger-job request");
    const forceUpdateLocations = !!req.query.forceUpdateLocations;
    triggerJob(forceUpdateLocations).then(result => {
        res.status(result.status).send(result);
        logger_1.info(`trigger-job request completed after ${result.millis} ms with status ${result.status}.`);
    });
});
node_cron_1.default.schedule("*/5 * * * *", () => {
    logger_1.info("Triggering job");
    triggerJob().then(result => {
        if (result.success)
            logger_1.info(`Job executed after ${result.millis} ms. Inserted observations: ${result.insertedObservations}.`);
        else
            logger_1.info(`Job execution failed after ${result.millis} ms.`);
    });
});
// Start Application
app.listen(PORT, () => {
    logger_1.info(`⚡️[server]: Server is running at http://localhost:${PORT}`);
});
//# sourceMappingURL=index.js.map