@etainabl/nodejs-sdk 1.2.34 → 1.2.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +4 -0
- package/package.json +3 -2
- package/src/index.ts +1 -2
- package/dist/cjs/api.d.ts +0 -111
- package/dist/cjs/api.js +0 -318
- package/dist/cjs/consumption.d.ts +0 -14
- package/dist/cjs/consumption.js +0 -32
- package/dist/cjs/db.d.ts +0 -6
- package/dist/cjs/db.js +0 -69
- package/dist/cjs/etainabl.d.ts +0 -10
- package/dist/cjs/etainabl.js +0 -2
- package/dist/cjs/index.d.ts +0 -11
- package/dist/cjs/logger.d.ts +0 -3
- package/dist/cjs/logger.js +0 -14
- package/dist/cjs/monitoring.d.ts +0 -1
- package/dist/cjs/monitoring.js +0 -31
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/reporting.d.ts +0 -2
- package/dist/cjs/reporting.js +0 -69
- package/dist/cjs/slack.d.ts +0 -4
- package/dist/cjs/slack.js +0 -28
- package/dist/cjs/types/index.d.ts +0 -20
- package/dist/cjs/types/index.js +0 -2
- package/dist/cjs/units.d.ts +0 -22
- package/dist/cjs/units.js +0 -94
- package/dist/mjs/api.d.ts +0 -111
- package/dist/mjs/api.js +0 -308
- package/dist/mjs/consumption.d.ts +0 -14
- package/dist/mjs/consumption.js +0 -22
- package/dist/mjs/db.d.ts +0 -6
- package/dist/mjs/db.js +0 -53
- package/dist/mjs/etainabl.d.ts +0 -10
- package/dist/mjs/etainabl.js +0 -1
- package/dist/mjs/index.d.ts +0 -11
- package/dist/mjs/index.js +0 -9
- package/dist/mjs/logger.d.ts +0 -3
- package/dist/mjs/logger.js +0 -9
- package/dist/mjs/monitoring.d.ts +0 -1
- package/dist/mjs/monitoring.js +0 -15
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/reporting.d.ts +0 -2
- package/dist/mjs/reporting.js +0 -62
- package/dist/mjs/slack.d.ts +0 -4
- package/dist/mjs/slack.js +0 -14
- package/dist/mjs/types/index.d.ts +0 -20
- package/dist/mjs/types/index.js +0 -1
- package/dist/mjs/units.d.ts +0 -22
- package/dist/mjs/units.js +0 -89
package/dist/mjs/api.js
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import https from 'https';
|
|
3
|
-
import logger from './logger.js';
|
|
4
|
-
const log = logger('etainablApi');
|
|
5
|
-
function _handleResponse(req, res, isPaged = false) {
|
|
6
|
-
if (!res) {
|
|
7
|
-
throw new Error(`No response from API (${req.method} ${req.url})`);
|
|
8
|
-
}
|
|
9
|
-
if (res.status !== 200) {
|
|
10
|
-
throw new Error(`${res.status} ${res.statusText} response from API (${req.method} ${req.url})`);
|
|
11
|
-
}
|
|
12
|
-
if (!res.data) {
|
|
13
|
-
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
14
|
-
}
|
|
15
|
-
if (isPaged && !res.data.data) {
|
|
16
|
-
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
17
|
-
}
|
|
18
|
-
return res;
|
|
19
|
-
}
|
|
20
|
-
const factory = {
|
|
21
|
-
getWithId: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
22
|
-
const req = {
|
|
23
|
-
method: 'GET',
|
|
24
|
-
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
25
|
-
};
|
|
26
|
-
log.info(`API Request: ${req.method} ${process.env.ETAsINABL_API_URL}/${req.url}`);
|
|
27
|
-
let res;
|
|
28
|
-
try {
|
|
29
|
-
res = await etainablApi.get(req.url, options);
|
|
30
|
-
}
|
|
31
|
-
catch (e) {
|
|
32
|
-
if (e.response?.data)
|
|
33
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
34
|
-
throw e;
|
|
35
|
-
}
|
|
36
|
-
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
37
|
-
_handleResponse(req, res);
|
|
38
|
-
return res.data;
|
|
39
|
-
},
|
|
40
|
-
get: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
|
|
41
|
-
const req = {
|
|
42
|
-
method: 'GET',
|
|
43
|
-
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
44
|
-
};
|
|
45
|
-
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
46
|
-
let res;
|
|
47
|
-
try {
|
|
48
|
-
res = await etainablApi.get(req.url, options);
|
|
49
|
-
}
|
|
50
|
-
catch (e) {
|
|
51
|
-
if (e.response?.data)
|
|
52
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
56
|
-
_handleResponse(req, res);
|
|
57
|
-
return res.data;
|
|
58
|
-
},
|
|
59
|
-
list: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
|
|
60
|
-
const req = {
|
|
61
|
-
method: 'GET',
|
|
62
|
-
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
63
|
-
};
|
|
64
|
-
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
65
|
-
let res;
|
|
66
|
-
try {
|
|
67
|
-
res = await etainablApi.get(req.url, options);
|
|
68
|
-
}
|
|
69
|
-
catch (e) {
|
|
70
|
-
if (e.response?.data)
|
|
71
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
72
|
-
throw e;
|
|
73
|
-
}
|
|
74
|
-
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
75
|
-
_handleResponse(req, res, true);
|
|
76
|
-
return res.data;
|
|
77
|
-
},
|
|
78
|
-
update: (etainablApi, endpoint, postEndpoint) => async (id, data, options = {}) => {
|
|
79
|
-
const req = {
|
|
80
|
-
method: 'PATCH',
|
|
81
|
-
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
82
|
-
};
|
|
83
|
-
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
84
|
-
let res;
|
|
85
|
-
try {
|
|
86
|
-
res = await etainablApi.patch(req.url, data, options);
|
|
87
|
-
}
|
|
88
|
-
catch (e) {
|
|
89
|
-
if (e.response?.data)
|
|
90
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
91
|
-
throw e;
|
|
92
|
-
}
|
|
93
|
-
_handleResponse(req, res);
|
|
94
|
-
return res.data;
|
|
95
|
-
},
|
|
96
|
-
create: (etainablApi, endpoint, postEndpoint) => async (data, options = {}) => {
|
|
97
|
-
const req = {
|
|
98
|
-
method: 'POST',
|
|
99
|
-
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
100
|
-
};
|
|
101
|
-
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
102
|
-
let res;
|
|
103
|
-
try {
|
|
104
|
-
res = await etainablApi.post(req.url, data, options);
|
|
105
|
-
}
|
|
106
|
-
catch (e) {
|
|
107
|
-
if (e.response?.data)
|
|
108
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
109
|
-
throw e;
|
|
110
|
-
}
|
|
111
|
-
_handleResponse(req, res);
|
|
112
|
-
return res.data;
|
|
113
|
-
},
|
|
114
|
-
remove: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
115
|
-
const req = {
|
|
116
|
-
method: 'DELETE',
|
|
117
|
-
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
118
|
-
};
|
|
119
|
-
let res;
|
|
120
|
-
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
121
|
-
try {
|
|
122
|
-
res = await etainablApi.delete(req.url, options);
|
|
123
|
-
}
|
|
124
|
-
catch (e) {
|
|
125
|
-
if (e.response?.data)
|
|
126
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
127
|
-
throw e;
|
|
128
|
-
}
|
|
129
|
-
_handleResponse(req, res);
|
|
130
|
-
return res.data;
|
|
131
|
-
},
|
|
132
|
-
customWithId: (etainablApi, method, endpoint, postEndpoint) => async (id, data, options = {}) => {
|
|
133
|
-
const req = {
|
|
134
|
-
method: method,
|
|
135
|
-
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`,
|
|
136
|
-
data,
|
|
137
|
-
...options
|
|
138
|
-
};
|
|
139
|
-
log.info(`API Request (Custom): ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
140
|
-
let res;
|
|
141
|
-
try {
|
|
142
|
-
res = await etainablApi.request(req);
|
|
143
|
-
}
|
|
144
|
-
catch (e) {
|
|
145
|
-
if (e.response?.data)
|
|
146
|
-
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
147
|
-
throw e;
|
|
148
|
-
}
|
|
149
|
-
_handleResponse(req, res);
|
|
150
|
-
return res.data;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
// ETN Sub Endpoints
|
|
154
|
-
// e.g. /assets/:id/documents/:documentId
|
|
155
|
-
const subFactory = {
|
|
156
|
-
// e.g. POST /assets/:id/documents
|
|
157
|
-
create: (etainablApi, endpoint, subEndpoint) => async (id, data, options = {}) => {
|
|
158
|
-
const subUrl = `${id}/${subEndpoint}`;
|
|
159
|
-
return factory.create(etainablApi, endpoint, subUrl)(data, options);
|
|
160
|
-
},
|
|
161
|
-
// e.g. PATCH /assets/:id/documents/:documentId
|
|
162
|
-
update: (etainablApi, endpoint, subEndpoint) => async (id, subId, data, options = {}) => {
|
|
163
|
-
const subUrl = `${subEndpoint}/${subId}`;
|
|
164
|
-
return factory.update(etainablApi, endpoint, subUrl)(id, data, options);
|
|
165
|
-
},
|
|
166
|
-
// e.g. DELETE /assets/:id/documents/:documentId
|
|
167
|
-
remove: (etainablApi, endpoint, subEndpoint) => async (id, subId, options = {}) => {
|
|
168
|
-
const subUrl = `${subEndpoint}/${subId}`;
|
|
169
|
-
return factory.remove(etainablApi, endpoint, subUrl)(id, options);
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
export default (auth, instanceOptions = {}) => {
|
|
173
|
-
try {
|
|
174
|
-
const headers = {};
|
|
175
|
-
if (auth.key) {
|
|
176
|
-
headers['x-key'] = auth.key;
|
|
177
|
-
}
|
|
178
|
-
else if (auth.token) {
|
|
179
|
-
headers['Authorization'] = auth.token;
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
headers['x-key'] = process.env.ETAINABL_API_KEY;
|
|
183
|
-
}
|
|
184
|
-
const etainablApi = axios.create({
|
|
185
|
-
baseURL: process.env.ETAINABL_API_URL,
|
|
186
|
-
timeout: 300000,
|
|
187
|
-
httpsAgent: new https.Agent({ keepAlive: true }),
|
|
188
|
-
headers,
|
|
189
|
-
...instanceOptions
|
|
190
|
-
});
|
|
191
|
-
return {
|
|
192
|
-
instance: etainablApi,
|
|
193
|
-
// accounts
|
|
194
|
-
getAccount: factory.getWithId(etainablApi, 'accounts'),
|
|
195
|
-
listAccounts: factory.list(etainablApi, 'accounts'),
|
|
196
|
-
updateAccount: factory.update(etainablApi, 'accounts'),
|
|
197
|
-
createAccount: factory.create(etainablApi, 'accounts'),
|
|
198
|
-
removeAccount: factory.remove(etainablApi, 'accounts'),
|
|
199
|
-
getAccountSchema: factory.get(etainablApi, 'accounts', 'schema'),
|
|
200
|
-
invalidateAccountCache: factory.customWithId(etainablApi, 'put', 'accounts', 'invalidate-cache'),
|
|
201
|
-
// assets
|
|
202
|
-
getAsset: factory.getWithId(etainablApi, 'assets'),
|
|
203
|
-
listAssets: factory.list(etainablApi, 'assets'),
|
|
204
|
-
updateAsset: factory.update(etainablApi, 'assets'),
|
|
205
|
-
createAsset: factory.create(etainablApi, 'assets'),
|
|
206
|
-
removeAsset: factory.remove(etainablApi, 'assets'),
|
|
207
|
-
getAssetSchema: factory.get(etainablApi, 'assets', 'schema'),
|
|
208
|
-
// assetGroups
|
|
209
|
-
getAssetGroup: factory.getWithId(etainablApi, 'asset-groups'),
|
|
210
|
-
listAssetGroups: factory.list(etainablApi, 'asset-groups'),
|
|
211
|
-
updateAssetGroup: factory.update(etainablApi, 'asset-groups'),
|
|
212
|
-
createAssetGroup: factory.create(etainablApi, 'asset-groups'),
|
|
213
|
-
removeAssetGroup: factory.remove(etainablApi, 'asset-groups'),
|
|
214
|
-
getAssetGroupAssets: factory.getWithId(etainablApi, 'asset-groups', 'assets'),
|
|
215
|
-
getAssetGroupSchema: factory.get(etainablApi, 'asset-groups', 'schema'),
|
|
216
|
-
// automation
|
|
217
|
-
getAutomation: factory.getWithId(etainablApi, 'automation'),
|
|
218
|
-
listAutomations: factory.list(etainablApi, 'automation'),
|
|
219
|
-
updateAutomation: factory.update(etainablApi, 'automation'),
|
|
220
|
-
createAutomation: factory.create(etainablApi, 'automation'),
|
|
221
|
-
removeAutomation: factory.remove(etainablApi, 'automation'),
|
|
222
|
-
createAutomationLog: subFactory.create(etainablApi, 'automation', 'logs'),
|
|
223
|
-
updateAutomationLog: subFactory.update(etainablApi, 'automation', 'logs'),
|
|
224
|
-
removeAutomationLog: subFactory.remove(etainablApi, 'automation', 'logs'),
|
|
225
|
-
// company
|
|
226
|
-
getCompany: factory.getWithId(etainablApi, 'companies'),
|
|
227
|
-
// consumption
|
|
228
|
-
getConsumption: factory.getWithId(etainablApi, 'consumptions'),
|
|
229
|
-
listConsumptions: factory.list(etainablApi, 'consumptions'),
|
|
230
|
-
updateConsumption: factory.update(etainablApi, 'consumptions'),
|
|
231
|
-
createConsumption: factory.create(etainablApi, 'consumptions'),
|
|
232
|
-
removeConsumption: factory.remove(etainablApi, 'consumptions'),
|
|
233
|
-
getConsumptionSchema: factory.get(etainablApi, 'consumptions', 'schema'),
|
|
234
|
-
// emails
|
|
235
|
-
getEmail: factory.getWithId(etainablApi, 'emails'),
|
|
236
|
-
listEmails: factory.list(etainablApi, 'emails'),
|
|
237
|
-
updateEmail: factory.update(etainablApi, 'emails'),
|
|
238
|
-
createEmail: factory.create(etainablApi, 'emails'),
|
|
239
|
-
removeEmail: factory.remove(etainablApi, 'emails'),
|
|
240
|
-
// emission factors
|
|
241
|
-
getEmissionFactor: factory.getWithId(etainablApi, 'emission-factors'),
|
|
242
|
-
listEmissionFactors: factory.list(etainablApi, 'emission-factors'),
|
|
243
|
-
updateEmissionFactor: factory.update(etainablApi, 'emission-factors'),
|
|
244
|
-
createEmissionFactor: factory.create(etainablApi, 'emission-factors'),
|
|
245
|
-
removeEmissionFactor: factory.remove(etainablApi, 'emission-factors'),
|
|
246
|
-
// entity
|
|
247
|
-
getEntity: factory.getWithId(etainablApi, 'entities'),
|
|
248
|
-
listEntities: factory.list(etainablApi, 'entities'),
|
|
249
|
-
updateEntity: factory.update(etainablApi, 'entities'),
|
|
250
|
-
createEntity: factory.create(etainablApi, 'entities'),
|
|
251
|
-
removeEntity: factory.remove(etainablApi, 'entities'),
|
|
252
|
-
getEntitiesSchema: factory.get(etainablApi, 'entities', 'schema'),
|
|
253
|
-
// logs
|
|
254
|
-
getLog: factory.getWithId(etainablApi, 'logs'),
|
|
255
|
-
listLogs: factory.list(etainablApi, 'logs'),
|
|
256
|
-
updateLog: factory.update(etainablApi, 'logs'),
|
|
257
|
-
createLog: factory.create(etainablApi, 'logs'),
|
|
258
|
-
removeLog: factory.remove(etainablApi, 'logs'),
|
|
259
|
-
// readings
|
|
260
|
-
getReading: factory.getWithId(etainablApi, 'readings'),
|
|
261
|
-
listReadings: factory.list(etainablApi, 'readings'),
|
|
262
|
-
updateReading: factory.update(etainablApi, 'readings'),
|
|
263
|
-
createReading: factory.create(etainablApi, 'readings'),
|
|
264
|
-
removeReading: factory.remove(etainablApi, 'readings'),
|
|
265
|
-
getReadingSchema: factory.get(etainablApi, 'readings', 'schema'),
|
|
266
|
-
// reports
|
|
267
|
-
getReport: factory.getWithId(etainablApi, 'reports'),
|
|
268
|
-
listReports: factory.list(etainablApi, 'reports'),
|
|
269
|
-
updateReport: factory.update(etainablApi, 'reports'),
|
|
270
|
-
createReport: factory.create(etainablApi, 'reports'),
|
|
271
|
-
removeReport: factory.remove(etainablApi, 'reports'),
|
|
272
|
-
sendReport: factory.customWithId(etainablApi, 'post', 'reports', 'send'),
|
|
273
|
-
// report templates
|
|
274
|
-
getReportTemplate: factory.getWithId(etainablApi, 'report-templates'),
|
|
275
|
-
listReportTemplates: factory.list(etainablApi, 'report-templates'),
|
|
276
|
-
updateReportTemplate: factory.update(etainablApi, 'report-templates'),
|
|
277
|
-
createReportTemplate: factory.create(etainablApi, 'report-templates'),
|
|
278
|
-
removeReportTemplate: factory.remove(etainablApi, 'report-templates'),
|
|
279
|
-
// scheduled reports
|
|
280
|
-
getScheduledReport: factory.getWithId(etainablApi, 'scheduled-reports'),
|
|
281
|
-
listScheduledReports: factory.list(etainablApi, 'scheduled-reports'),
|
|
282
|
-
updateScheduledReport: factory.update(etainablApi, 'scheduled-reports'),
|
|
283
|
-
createScheduledReport: factory.create(etainablApi, 'scheduled-reports'),
|
|
284
|
-
removeScheduledReport: factory.remove(etainablApi, 'scheduled-reports'),
|
|
285
|
-
sendScheduledReport: factory.customWithId(etainablApi, 'post', 'scheduled-reports', 'send'),
|
|
286
|
-
// invoices
|
|
287
|
-
getInvoice: factory.getWithId(etainablApi, 'invoices'),
|
|
288
|
-
listInvoices: factory.list(etainablApi, 'invoices'),
|
|
289
|
-
updateInvoice: factory.update(etainablApi, 'invoices'),
|
|
290
|
-
createInvoice: factory.create(etainablApi, 'invoices'),
|
|
291
|
-
removeInvoice: factory.remove(etainablApi, 'invoices'),
|
|
292
|
-
getInvoiceSchema: factory.get(etainablApi, 'invoices', 'schema'),
|
|
293
|
-
//suppliers
|
|
294
|
-
listSuppliers: factory.list(etainablApi, 'suppliers'),
|
|
295
|
-
getSupplierSchema: factory.get(etainablApi, 'suppliers', 'schema'),
|
|
296
|
-
// import templates
|
|
297
|
-
getImportTemplate: factory.getWithId(etainablApi, 'import-templates'),
|
|
298
|
-
//data imports
|
|
299
|
-
updateDataIngest: factory.update(etainablApi, 'data-ingests'),
|
|
300
|
-
createDataIngest: factory.create(etainablApi, 'data-ingests'),
|
|
301
|
-
listDataIngest: factory.list(etainablApi, 'data-ingests'),
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
catch (e) {
|
|
305
|
-
log.error(e);
|
|
306
|
-
throw e;
|
|
307
|
-
}
|
|
308
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import moment from 'moment';
|
|
2
|
-
interface ConsumptionData {
|
|
3
|
-
date: Date;
|
|
4
|
-
consumption: number;
|
|
5
|
-
}
|
|
6
|
-
export declare const dayNightConsumption: (data: ConsumptionData[]) => {
|
|
7
|
-
dayConsumption: number;
|
|
8
|
-
nightConsumption: number;
|
|
9
|
-
consumption: number;
|
|
10
|
-
};
|
|
11
|
-
export declare const calcMaxConsumptionValue: (data: ConsumptionData[]) => number;
|
|
12
|
-
export declare const calcMaxDemand: (data: ConsumptionData[]) => number;
|
|
13
|
-
export declare const calcPeakLoad: (consumption: number, maxDemand: number, startDate: string | moment.Moment, endDate: string | moment.Moment) => number;
|
|
14
|
-
export {};
|
package/dist/mjs/consumption.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import moment from 'moment';
|
|
2
|
-
export const dayNightConsumption = (data) => data.reduce((acc, item) => {
|
|
3
|
-
const hour = moment.utc(item.date).hour(); // End Time of HH consumption period
|
|
4
|
-
if (hour >= 0 && hour < 7) {
|
|
5
|
-
acc.nightConsumption += item.consumption;
|
|
6
|
-
}
|
|
7
|
-
else {
|
|
8
|
-
acc.dayConsumption += item.consumption;
|
|
9
|
-
}
|
|
10
|
-
acc.consumption += item.consumption;
|
|
11
|
-
return acc;
|
|
12
|
-
}, {
|
|
13
|
-
dayConsumption: 0,
|
|
14
|
-
nightConsumption: 0,
|
|
15
|
-
consumption: 0
|
|
16
|
-
});
|
|
17
|
-
export const calcMaxConsumptionValue = (data) => Math.max(...data.map((item) => item.consumption));
|
|
18
|
-
export const calcMaxDemand = (data) => calcMaxConsumptionValue(data) * 2;
|
|
19
|
-
export const calcPeakLoad = (consumption, maxDemand, startDate, endDate) => {
|
|
20
|
-
const days = Math.ceil(moment(endDate).diff(moment(startDate), 'days', true));
|
|
21
|
-
return maxDemand === 0 ? 0 : ((consumption / (maxDemand * 24 * days)) * 100);
|
|
22
|
-
};
|
package/dist/mjs/db.d.ts
DELETED
package/dist/mjs/db.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { MongoClient } from 'mongodb';
|
|
2
|
-
import logger from './logger.js';
|
|
3
|
-
const log = logger('dbHelpers');
|
|
4
|
-
let cachedDb;
|
|
5
|
-
async function connectToDatabase(retryAttempt = 1) {
|
|
6
|
-
if (!process.env.ETAINABL_DB_URL)
|
|
7
|
-
throw new Error("ETAINABL_DB_URL is not set");
|
|
8
|
-
if (!process.env.AWS_ACCESS_KEY_ID)
|
|
9
|
-
throw new Error("AWS_ACCESS_KEY_ID is not set");
|
|
10
|
-
if (!process.env.AWS_SECRET_ACCESS_KEY)
|
|
11
|
-
throw new Error("AWS_SECRET_ACCESS_KEY is not set");
|
|
12
|
-
if (cachedDb) {
|
|
13
|
-
log.debug('Using cached MongoDB connection.');
|
|
14
|
-
return Promise.resolve(cachedDb);
|
|
15
|
-
}
|
|
16
|
-
const uri = `mongodb+srv://${process.env.ETAINABL_DB_URL}`;
|
|
17
|
-
try {
|
|
18
|
-
if (process.env.DB_BASIC_AUTH === 'true') {
|
|
19
|
-
log.debug('Connecting to MongoDB server... (Auth: Basic)');
|
|
20
|
-
const client = new MongoClient(uri);
|
|
21
|
-
await client.connect();
|
|
22
|
-
log.debug('Connected successfully to MongoDB server! (Auth: Basic)');
|
|
23
|
-
cachedDb = client.db('etainabl');
|
|
24
|
-
return cachedDb;
|
|
25
|
-
}
|
|
26
|
-
log.debug('Connecting to MongoDB server... (Auth: AWS)');
|
|
27
|
-
const client = new MongoClient(uri, {
|
|
28
|
-
auth: {
|
|
29
|
-
username: process.env.AWS_ACCESS_KEY_ID,
|
|
30
|
-
password: process.env.AWS_SECRET_ACCESS_KEY
|
|
31
|
-
},
|
|
32
|
-
authSource: '$external',
|
|
33
|
-
authMechanism: 'MONGODB-AWS'
|
|
34
|
-
});
|
|
35
|
-
await client.connect();
|
|
36
|
-
log.debug('Connected successfully to MongoDB server! (Auth: AWS)');
|
|
37
|
-
cachedDb = client.db('etainabl');
|
|
38
|
-
return cachedDb;
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
// Retry
|
|
42
|
-
if (retryAttempt > 5) {
|
|
43
|
-
console.log(`Error connecting to MongoDB server after 5 attempts...`);
|
|
44
|
-
throw e;
|
|
45
|
-
}
|
|
46
|
-
console.log(`MongoDB Connection error: ${e.message}`);
|
|
47
|
-
console.log(`Error connecting to MongoDB server... Retrying in 3 seconds... (Attempt ${retryAttempt})`);
|
|
48
|
-
return connectToDatabase(retryAttempt + 1);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
export default {
|
|
52
|
-
connectToDatabase
|
|
53
|
-
};
|
package/dist/mjs/etainabl.d.ts
DELETED
package/dist/mjs/etainabl.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/mjs/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import api from './api.js';
|
|
2
|
-
import logger from './logger.js';
|
|
3
|
-
import db from './db.js';
|
|
4
|
-
import slack from './slack.js';
|
|
5
|
-
import * as units from './units.js';
|
|
6
|
-
import * as consumption from './consumption.js';
|
|
7
|
-
import * as monitoring from './monitoring.js';
|
|
8
|
-
import * as reporting from './reporting.js';
|
|
9
|
-
import type { Account, Asset, Automation, Company, DataIngest, Invoice, Log, Reading, Supplier } from './types/index.js';
|
|
10
|
-
export { api, logger, consumption, monitoring, db, slack, units, reporting };
|
|
11
|
-
export type { Account, Asset, Automation, Company, DataIngest, Invoice, Log, Reading, Supplier };
|
package/dist/mjs/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import api from './api.js';
|
|
2
|
-
import logger from './logger.js';
|
|
3
|
-
import db from './db.js';
|
|
4
|
-
import slack from './slack.js';
|
|
5
|
-
import * as units from './units.js';
|
|
6
|
-
import * as consumption from './consumption.js';
|
|
7
|
-
import * as monitoring from './monitoring.js';
|
|
8
|
-
import * as reporting from './reporting.js';
|
|
9
|
-
export { api, logger, consumption, monitoring, db, slack, units, reporting };
|
package/dist/mjs/logger.d.ts
DELETED
package/dist/mjs/logger.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import winston from 'winston';
|
|
2
|
-
export default (namespace) => winston.createLogger({
|
|
3
|
-
level: 'debug',
|
|
4
|
-
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
|
|
5
|
-
defaultMeta: { service: process.env.AWS_LAMBDA_FUNCTION_NAME, script: namespace },
|
|
6
|
-
transports: [
|
|
7
|
-
new winston.transports.Console()
|
|
8
|
-
]
|
|
9
|
-
});
|
package/dist/mjs/monitoring.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const sendHeartbeat: () => Promise<boolean>;
|
package/dist/mjs/monitoring.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import logger from './logger.js';
|
|
3
|
-
const log = logger('monitoring');
|
|
4
|
-
export const sendHeartbeat = async () => {
|
|
5
|
-
if (!process.env.HEARTBEAT_URL || process.env.HEARTBEAT_URL.endsWith('/'))
|
|
6
|
-
return false;
|
|
7
|
-
try {
|
|
8
|
-
await axios.post(process.env.HEARTBEAT_URL);
|
|
9
|
-
return true;
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
log.warn(`Failed to send heartbeat: ${e.message || e}`);
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
};
|
package/dist/mjs/package.json
DELETED
package/dist/mjs/reporting.d.ts
DELETED
package/dist/mjs/reporting.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import moment from 'moment';
|
|
2
|
-
moment.locale('en', {
|
|
3
|
-
week: {
|
|
4
|
-
dow: 1
|
|
5
|
-
}
|
|
6
|
-
});
|
|
7
|
-
const getNextRunTime = (startDate, schedule, taskTime) => {
|
|
8
|
-
const [num, freq] = schedule.frequency.split('|');
|
|
9
|
-
const targetDate = moment(startDate).add(num, freq);
|
|
10
|
-
if (schedule.frequencyPeriod === 'first') {
|
|
11
|
-
targetDate.startOf(freq);
|
|
12
|
-
}
|
|
13
|
-
else if (schedule.frequencyPeriod === 'last') {
|
|
14
|
-
targetDate.endOf(freq);
|
|
15
|
-
}
|
|
16
|
-
const isWeekday = targetDate.isoWeekday() > 0 && targetDate.isoWeekday() < 6;
|
|
17
|
-
const isSaturday = targetDate.isoWeekday() === 6;
|
|
18
|
-
// The weekday or weekend chosen should be within the same month as the target date
|
|
19
|
-
if (schedule.frequencyDay === 'weekdays' && !isWeekday) {
|
|
20
|
-
if ((targetDate.date() / 7) < 2) {
|
|
21
|
-
targetDate.add(isSaturday ? 2 : 1, 'days');
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
targetDate.subtract(isSaturday ? 1 : 2, 'days');
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
else if (schedule.frequencyDay === 'weekends' && isWeekday) {
|
|
28
|
-
if ((targetDate.date() / 7) < 2) {
|
|
29
|
-
targetDate.isoWeekday(6);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
targetDate.isoWeekday(0);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (taskTime.isAfter(targetDate, 'minute')) {
|
|
36
|
-
return getNextRunTime(targetDate, schedule, taskTime);
|
|
37
|
-
}
|
|
38
|
-
return targetDate;
|
|
39
|
-
};
|
|
40
|
-
export const getScheduledReportRunTimes = (schedule, limit = 1, taskTime = moment()) => {
|
|
41
|
-
if (!schedule.startDate || !schedule.enabled)
|
|
42
|
-
return [];
|
|
43
|
-
const originalStartDate = moment.utc(schedule.startDate);
|
|
44
|
-
let startDate = originalStartDate;
|
|
45
|
-
const includeStartDate = taskTime.isSameOrBefore(originalStartDate, 'minute');
|
|
46
|
-
let runTimes = [];
|
|
47
|
-
if (includeStartDate) {
|
|
48
|
-
runTimes = [originalStartDate];
|
|
49
|
-
}
|
|
50
|
-
const [, freq] = schedule.frequency.split('|');
|
|
51
|
-
if (freq === 'once') {
|
|
52
|
-
const nextRunTime = runTimes[0];
|
|
53
|
-
// If this is now beyond the start date, return an empty array
|
|
54
|
-
return taskTime.isAfter(nextRunTime, 'minute') ? [] : runTimes;
|
|
55
|
-
}
|
|
56
|
-
const scheduleRunTimes = Array.from(Array(includeStartDate ? limit - 1 : limit).keys()).map(() => {
|
|
57
|
-
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
|
58
|
-
startDate = nextRunTime.hour(originalStartDate.hour()).minute(originalStartDate.minute());
|
|
59
|
-
return nextRunTime;
|
|
60
|
-
});
|
|
61
|
-
return [...runTimes, ...scheduleRunTimes];
|
|
62
|
-
};
|
package/dist/mjs/slack.d.ts
DELETED
package/dist/mjs/slack.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
const postMessage = async (message) => {
|
|
3
|
-
const url = 'https://hooks.slack.com/services/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';
|
|
4
|
-
const data = {
|
|
5
|
-
text: `[${(process.env.ENV || '').toUpperCase()}][${process.env.AWS_LAMBDA_FUNCTION_NAME}] ${message}`
|
|
6
|
-
};
|
|
7
|
-
const headers = {
|
|
8
|
-
'Content-Type': 'application/json'
|
|
9
|
-
};
|
|
10
|
-
return axios.post(url, data, { headers });
|
|
11
|
-
};
|
|
12
|
-
export default {
|
|
13
|
-
postMessage
|
|
14
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Account } from './account.js';
|
|
2
|
-
import type { Asset } from './asset.js';
|
|
3
|
-
import type { Automation } from './automation.js';
|
|
4
|
-
import type { Company } from './company.js';
|
|
5
|
-
import type { CreateScraperRunParams, ScraperRun } from './scraperRun.js';
|
|
6
|
-
import type { DataIngest } from './dataIngest.js';
|
|
7
|
-
import type { Entity } from './entity.js';
|
|
8
|
-
import type { Email } from './email.js';
|
|
9
|
-
import type { Invoice } from './invoice.js';
|
|
10
|
-
import type { Log } from './log.js';
|
|
11
|
-
import type { Reading } from './reading.js';
|
|
12
|
-
import type { Report } from './report.js';
|
|
13
|
-
import type { Supplier } from './supplier.js';
|
|
14
|
-
export type { Account, Asset, Automation, Company, CreateScraperRunParams, DataIngest, Email, Entity, Invoice, Log, Reading, Report, ScraperRun, Supplier };
|
|
15
|
-
export interface ETNPagedResponse<T = any> {
|
|
16
|
-
data: T[];
|
|
17
|
-
total: number;
|
|
18
|
-
limit: number;
|
|
19
|
-
skip: number;
|
|
20
|
-
}
|
package/dist/mjs/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/mjs/units.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
interface Item {
|
|
2
|
-
units?: string | null;
|
|
3
|
-
unit?: string | null;
|
|
4
|
-
factor?: number | null;
|
|
5
|
-
value: number;
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
}
|
|
8
|
-
export declare type AccountType = 'electricity' | 'gas' | 'water' | 'waste' | 'solar' | 'heating' | 'flow' | 'cooling' | 'temperature' | 'oil' | 'other';
|
|
9
|
-
export declare type ETNUnit = 'kwh' | 'kg' | 'm3' | 'lbs' | 'tonnes' | 'wh' | 'mwh' | 'ft3' | 'hcf' | 'm3/h' | 'qty' | 'l' | 'C' | 'mcuf' | 'hcuf' | 'tcuf' | 'ocuf' | 'hm3' | 'tm3' | 'nm3';
|
|
10
|
-
export declare type BaseUnit = 'kwh' | 'm3' | 'C' | 'kg' | 'm3/h' | 'l';
|
|
11
|
-
export declare const accountTypeMap: {
|
|
12
|
-
[key: string]: BaseUnit;
|
|
13
|
-
};
|
|
14
|
-
export declare const accountTypeUnitMap: {
|
|
15
|
-
[key: string]: ETNUnit[];
|
|
16
|
-
};
|
|
17
|
-
export declare const convertItems: (items: Item[], type: AccountType, defaultUnits: ETNUnit | undefined, accountFactor: number | undefined) => any;
|
|
18
|
-
export declare const checkAccountTypeVsUnits: (type: string, unit: string, additionalLog?: number | '') => {
|
|
19
|
-
type: AccountType;
|
|
20
|
-
unit: ETNUnit;
|
|
21
|
-
};
|
|
22
|
-
export {};
|