-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (72 loc) · 2.11 KB
/
index.js
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
81
82
83
84
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require("node-fetch");
const knownServices = require("./src/services");
const getServers = async () => {
return new Promise((resolve, reject) => {
fetch("http://eureka.aliperongeluk.eu/eureka/apps", {
headers: { Accept: "application/json" }
})
.then(res => res.json())
.then(body => {
const applications = body.applications.application;
const services = [];
pushInstancesInServices(applications, services);
pushOfflineServicesInServices(applications, services);
const tableContent = [];
services.forEach(service => {
tableContent.push([
service.title,
service.online ? "Online" : "Offline"
]);
});
resolve(tableContent);
})
.catch(e => reject(e));
});
};
const pushInstancesInServices = (applications, services) => {
applications.forEach(application => {
application.instance.forEach(instance => {
const known =
knownServices[
knownServices.findIndex(known => known.key === instance.app)
];
services.push({
...instance,
...known,
online: instance.status === "UP"
});
});
});
};
const pushOfflineServicesInServices = services => {
knownServices.forEach(known => {
if (services.findIndex(service => service.app === known.key) === -1) {
services.push({
...known,
online: false
});
}
});
};
// Google Assistant deps
const { dialogflow, Table } = require("actions-on-google");
const app = dialogflow({ debug: true });
app.intent("Get the service status", async conv => {
const tableContent = await getServers();
conv.ask("Here are the services:");
conv.close(
new Table({
title: "All services",
columns: ["Service", "status"],
rows: tableContent
})
);
});
const expressApp = express().use(bodyParser.json());
expressApp.get("*", (req, res) => {
res.send("This is a Google Assistant app!");
});
expressApp.post("*", app);
module.exports = expressApp;