forked from lilsweetcaligula/weavy-co2-emission-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (30 loc) · 1.44 KB
/
app.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
const Assert = require('assert-plus')
const { prop, setProp } = require('./functools')
const GetCpuUsageByProcs = require('./get_cpu_usage_by_procs')
const CalculateElectricityUsageKwBasedOnCpuUsage = require('./calculate_electricity_usage_kw_based_on_cpu_usage.js')
const CalculateCo2EmissionLbsBasedOnElectricityUsageKw = require('./calculate_co2_emission_lbs_based_on_electricity_usage_kw')
const OutputAppResult = require('./output_app_result')
class App {
static async invoke(opts = {}) {
Assert.object(opts, 'opts')
const cpu_usage_infos = await GetCpuUsageByProcs.invoke({}) // [{ pid: 123, cpu: 0.31 }, ...]
const el_usage_infos = await (async () => {
const calculations = cpu_usage_infos.map(async (usage) => {
const cpu_usage = prop(usage, 'cpu')
const el_usage = await CalculateElectricityUsageKwBasedOnCpuUsage.invoke(cpu_usage)
return setProp(usage, 'electricity_usage_kw', el_usage)
})
return Promise.all(calculations)
})()
const co2_em_infos = await (async () => {
const calculations = el_usage_infos.map(async (usage) => {
const el_usage = prop(usage, 'electricity_usage_kw')
const co2_em = await CalculateCo2EmissionLbsBasedOnElectricityUsageKw.invoke(el_usage)
return setProp(usage, 'co2_emission_lbs', co2_em)
})
return Promise.all(calculations)
})()
await OutputAppResult.invoke(co2_em_infos, opts)
}
}
module.exports = App