This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (115 loc) · 3.05 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
const _ = require("lodash");
const capitano = require("capitano");
const semver = require("resin-semver");
const moment = require("moment");
const config = require("config");
var jsonfile = require("jsonfile");
const env = require("get-env")({
staging: "staging",
production: "production",
devenv: "devenv"
});
const authToken = config.get("authToken");
const resin = require("resin-sdk")({
apiUrl: config.get("apiEndpoint")
});
var help = function() {
console.log("Fleet Score Util\n");
_.forEach(capitano.state.commands, function(command) {
if (!command.isWildcard()) {
console.log(`\t${command.signature}\t\t\t${command.description}`);
}
});
};
var getVersion = function(device) {
var version = {};
if (device.os_version) {
var parsed_semver = semver.parse(device.os_version);
version.os = parsed_semver ? parsed_semver.version : "Unknown";
} else {
version.os = "1.0.0-pre";
}
version.supervisor = device.supervisor_version;
version.combined = `${version.os}%${version.supervisor}`;
return version.combined;
};
var replaceToken = async function() {
const file = `./config/${env}.json`;
resin.request
.send({
url: `/user/v1/refresh-token`,
baseUrl: config.get("apiEndpoint")
})
.then(function(response) {
if (response.status === 200) {
var obj = {
apiEndpoint: config.get("apiEndpoint"),
authToken: response.body
};
jsonfile.writeFile(file, obj, function(err) {
if (err) {
console.error(err);
}
});
}
});
};
var getDevices = async function() {
await resin.auth.loginWithToken(authToken);
replaceToken();
const before = moment()
.subtract(28, "days")
.startOf("day");
const device_filter = {
filter: {
$or: [
{ is_online: true },
{ last_connectivity_event: { $ge: before.toISOString() } }
]
},
select: ["supervisor_version", "os_version"]
};
const devices = await resin.models.device.getAll(device_filter);
var filtered_devices = _.filter(devices, function(o) {
return o.supervisor_version !== null;
});
var fleet = _.countBy(filtered_devices, getVersion);
var fleet_list = [];
_.forEach(fleet, function(value, key) {
var vers = key.split("%");
var combo = { os: vers[0], supervisor: vers[1], count: value };
fleet_list.push(combo);
});
var fleet_sorted_list = fleet_list
.sort(function(a, b) {
if (a.os === b.os) {
return semver.compare(a.supervisor, b.supervisor);
} else {
return semver.compare(a.os, b.os);
}
})
.reverse();
_.forEach(fleet_sorted_list, function(o) {
console.log(`${o.os}\t${o.supervisor}\t${o.count}`);
});
};
capitano.command({
signature: "get",
description: "A test command",
action: getDevices
});
capitano.command({
signature: "help",
description: "Print this help",
action: help
});
capitano.command({
signature: "*",
action: help
});
capitano.run(process.argv, function(error) {
if (error) {
throw error;
}
});