@abtnode/core 1.16.13-beta-0086a35d → 1.16.13-beta-714bec78

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/lib/api/node.js CHANGED
@@ -8,7 +8,7 @@ const isGitpod = require('@abtnode/util/lib/is-gitpod');
8
8
  const getFolderSize = require('@abtnode/util/lib/get-folder-size');
9
9
  const canPackageReadWrite = require('@abtnode/util/lib/can-pkg-rw');
10
10
  const { toDelegateAddress } = require('@arcblock/did-util');
11
- const { SERVER_CACHE_TTL } = require('@abtnode/constant');
11
+ const { SERVER_CACHE_TTL, MONITOR_RECORD_INTERVAL_SEC } = require('@abtnode/constant');
12
12
 
13
13
  const logger = require('@abtnode/logger')('@abtnode/core:api:node');
14
14
 
@@ -19,23 +19,18 @@ const { getDelegateState } = require('../util');
19
19
  const { NodeRuntimeMonitor } = require('../monitor/node-runtime-monitor');
20
20
  const getHistoryList = require('../monitor/get-history-list');
21
21
 
22
- // 10s 上报统计一次
23
- const MONITOR_RECORD_INTERVAL_SEC = 10;
24
-
25
- // 保存当天数据, 每天上报 8640 次
26
- const MONITOR_HISTORY_LENGTH = 86400 / MONITOR_RECORD_INTERVAL_SEC;
27
-
28
22
  class NodeAPI {
29
23
  /**
30
- *
31
- * @param {object} state Node StateDB
24
+ * @param {object} states StateDB
25
+ * @param {string} nodeDid
32
26
  */
33
- constructor(state) {
34
- assert.notStrictEqual(state, undefined, 'argument state can not be undefined');
35
- assert.notStrictEqual(state, null, 'argument state can not be null');
27
+ constructor(states, nodeDid) {
28
+ assert.notStrictEqual(states, undefined, 'argument states can not be undefined');
29
+ assert.notStrictEqual(states, null, 'argument states can not be null');
36
30
 
37
31
  this.runtimeMonitor = new NodeRuntimeMonitor({
38
- historyLength: MONITOR_HISTORY_LENGTH,
32
+ state: states.runtimeInsight,
33
+ did: nodeDid,
39
34
  });
40
35
 
41
36
  this.cache = new LRU({
@@ -43,7 +38,7 @@ class NodeAPI {
43
38
  maxAge: SERVER_CACHE_TTL,
44
39
  });
45
40
 
46
- this.state = state;
41
+ this.state = states.node;
47
42
  }
48
43
 
49
44
  async updateNodeInfo(entity = {}, context) {
@@ -147,14 +142,13 @@ class NodeAPI {
147
142
  }
148
143
 
149
144
  // eslint-disable-next-line no-unused-vars
150
- getHistory({ hours = 1 } = {}, context) {
151
- const history = this.runtimeMonitor.getHistory();
152
-
145
+ async getHistory({ hours = 1 } = {}, context) {
146
+ const history = await this.runtimeMonitor.getHistory();
153
147
  return getHistoryList({
154
148
  history,
155
149
  hours,
156
150
  recordIntervalSec: MONITOR_RECORD_INTERVAL_SEC,
157
- props: ['date', 'cpu', 'mem', 'daemonMem', 'serviceMem', 'dbMem'],
151
+ props: ['date', 'cpu', 'mem', 'daemonMem', 'serviceMem', 'hubMem'],
158
152
  });
159
153
  }
160
154
 
@@ -169,6 +163,11 @@ class NodeAPI {
169
163
  time: `*/${MONITOR_RECORD_INTERVAL_SEC} * * * * *`,
170
164
  fn: () => this.runtimeMonitor.monit(),
171
165
  },
166
+ {
167
+ name: 'cleanup-runtime-info',
168
+ time: '0 5 */2 * * *', // every 2 hours, eg: 00:05, 02:05, 04:05, 06:05
169
+ fn: () => this.runtimeMonitor.cleanup(),
170
+ },
172
171
  ];
173
172
  }
174
173
  }
@@ -24,6 +24,7 @@ const {
24
24
  NODE_MODES,
25
25
  APP_STRUCT_VERSION,
26
26
  BLOCKLET_CACHE_TTL,
27
+ MONITOR_RECORD_INTERVAL_SEC,
27
28
  } = require('@abtnode/constant');
28
29
 
29
30
  const getBlockletEngine = require('@blocklet/meta/lib/engine');
@@ -204,12 +205,6 @@ const getComponentChangedInfoForUpgrade = ({ newBlocklet, oldBlocklet, context =
204
205
  return { skippedProcessIds, installedComponentNames };
205
206
  };
206
207
 
207
- // 10s 上报统计一次
208
- const MONITOR_RECORD_INTERVAL_SEC = 10;
209
-
210
- // 保存当天数据, 每天上报 8640 次
211
- const MONITOR_HISTORY_LENGTH = 86400 / MONITOR_RECORD_INTERVAL_SEC;
212
-
213
208
  class BlockletManager extends BaseBlockletManager {
214
209
  /**
215
210
  * Creates an instance of BlockletManager.
@@ -241,7 +236,7 @@ class BlockletManager extends BaseBlockletManager {
241
236
  maxAge: BLOCKLET_CACHE_TTL,
242
237
  });
243
238
 
244
- this.runtimeMonitor = new BlockletRuntimeMonitor({ historyLength: MONITOR_HISTORY_LENGTH, states });
239
+ this.runtimeMonitor = new BlockletRuntimeMonitor({ states });
245
240
 
246
241
  this.blockletDownloader = new BlockletDownloader({
247
242
  installDir: this.installDir,
@@ -1445,8 +1440,7 @@ class BlockletManager extends BaseBlockletManager {
1445
1440
  // eslint-disable-next-line no-unused-vars
1446
1441
  async getRuntimeHistory({ did, hours }, context) {
1447
1442
  const metaDid = await states.blocklet.getBlockletMetaDid(did);
1448
-
1449
- const history = this.runtimeMonitor.getHistory(metaDid);
1443
+ const history = await this.runtimeMonitor.getHistory(metaDid);
1450
1444
 
1451
1445
  return getHistoryList({
1452
1446
  history,
@@ -1,6 +1,7 @@
1
1
  const logger = require('@abtnode/logger')('@abtnode/core:install-component-dev');
2
2
 
3
3
  const { BlockletStatus, BlockletSource, BLOCKLET_MODES, fromBlockletStatus } = require('@blocklet/constant');
4
+ const { isInProgress } = require('@blocklet/meta/lib/util');
4
5
  const {
5
6
  parseComponents,
6
7
  filterDuplicateComponents,
@@ -20,13 +21,9 @@ const installComponentFromDev = async ({ folder, meta, rootDid, mountPoint, mana
20
21
 
21
22
  const exist = existRoot.children.find((x) => x.meta.did === meta.did);
22
23
  if (exist) {
23
- if (exist.mode === BLOCKLET_MODES.PRODUCTION) {
24
- throw new Error('The blocklet component of production mode already exists, please remove it before developing');
25
- }
26
-
27
24
  const status = fromBlockletStatus(exist.status);
28
- if (['starting', 'running'].includes(status)) {
29
- throw new Error(`The blocklet component is already on ${status}, please stop it before developing`);
25
+ if (isInProgress(status)) {
26
+ throw new Error(`The blocklet component is on ${status}, please stop it before developing`);
30
27
  }
31
28
 
32
29
  logger.info('remove blocklet component for dev', { did, version });
@@ -49,7 +46,10 @@ const installComponentFromDev = async ({ folder, meta, rootDid, mountPoint, mana
49
46
  };
50
47
  const { dynamicComponents } = await parseComponents(component);
51
48
  dynamicComponents.unshift(component);
52
- const children = filterDuplicateComponents(dynamicComponents, existRoot.children);
49
+ const children = filterDuplicateComponents(
50
+ dynamicComponents,
51
+ existRoot.children.filter((x) => x.meta.did !== component.meta.did)
52
+ );
53
53
  checkVersionCompatibility([...children, ...existRoot.children]);
54
54
  await states.blocklet.addChildren(rootDid, children);
55
55
 
package/lib/index.js CHANGED
@@ -208,7 +208,7 @@ function ABTNode(options) {
208
208
  deleteDomainAlias,
209
209
  } = getRouterHelpers({ dataDirs, routingSnapshot, routerManager, blockletManager, certManager });
210
210
 
211
- const nodeAPI = new NodeAPI(states.node);
211
+ const nodeAPI = new NodeAPI(states, options.nodeDid);
212
212
  const teamAPI = new TeamAPI({ states, teamManager, dataDirs });
213
213
 
214
214
  blockletManager.resetSiteByDid = resetSiteByDid;
@@ -44,25 +44,18 @@ const { getRuntimeInfo } = require('../util/blocklet');
44
44
  // }
45
45
 
46
46
  class BlockletRuntimeMonitor extends EventEmitter {
47
- constructor({ historyLength = 100, states, logger = defaultLogger } = {}) {
47
+ constructor({ states, logger = defaultLogger } = {}) {
48
48
  super();
49
49
 
50
- /**
51
- * private
52
- */
53
- this.historyLength = historyLength;
54
50
  this.states = states;
55
51
  this.data = {};
56
52
  this.logger = logger;
57
53
  this.inProgress = false;
58
54
  }
59
55
 
60
- getHistory(blockletDid) {
61
- if (!this.data[blockletDid]) {
62
- return [];
63
- }
64
-
65
- return this.data[blockletDid].app.history || [];
56
+ async getHistory(blockletDid) {
57
+ const result = await this.states.runtimeInsight.findPaginated({ did: blockletDid });
58
+ return (result.list || []).reverse();
66
59
  }
67
60
 
68
61
  getRuntimeInfo(blockletDid, componentId = 'app') {
@@ -142,6 +135,8 @@ class BlockletRuntimeMonitor extends EventEmitter {
142
135
  { parallel: true }
143
136
  );
144
137
 
138
+ appCpu = Number(appCpu.toFixed(2));
139
+
145
140
  this.data[blockletDid].app.runtimeInfo.cpuUsage = appCpu;
146
141
  this.data[blockletDid].app.runtimeInfo.memoryUsage = appMem;
147
142
 
@@ -194,11 +189,7 @@ class BlockletRuntimeMonitor extends EventEmitter {
194
189
  }
195
190
 
196
191
  _push(blockletDid, value) {
197
- const arr = this.data[blockletDid].app.history;
198
- arr.push(value);
199
- if (arr.length > this.historyLength) {
200
- arr.shift();
201
- }
192
+ return this.states.runtimeInsight.insert({ did: blockletDid, ...value });
202
193
  }
203
194
  }
204
195
 
@@ -59,7 +59,7 @@ const getProcessInfo = (processId, { returnList } = {}) =>
59
59
 
60
60
  // type MonitorData = {
61
61
  // realtime: Realtime;
62
- // history: History;
62
+ // history: History; // saved in sqlite database
63
63
  // }
64
64
 
65
65
  const DEFAULT_DATA = {
@@ -75,16 +75,14 @@ const DEFAULT_DATA = {
75
75
  };
76
76
 
77
77
  class NodeRuntimeMonitor extends EventEmitter {
78
- constructor({ historyLength = 100, logger = defaultLogger } = {}) {
78
+ constructor({ did, state, logger = defaultLogger } = {}) {
79
79
  super();
80
80
 
81
- /**
82
- * private
83
- */
84
- this.historyLength = historyLength;
85
81
  this.logger = logger;
86
82
  this.data = cloneDeep(DEFAULT_DATA);
87
83
  this.inProgress = false;
84
+ this.did = did;
85
+ this.state = state;
88
86
  }
89
87
 
90
88
  emitRealtimeData() {
@@ -95,8 +93,9 @@ class NodeRuntimeMonitor extends EventEmitter {
95
93
  return this.data.realtime;
96
94
  }
97
95
 
98
- getHistory() {
99
- return this.data.history;
96
+ async getHistory() {
97
+ const result = await this.state.findPaginated({ did: this.did });
98
+ return (result.list || []).reverse();
100
99
  }
101
100
 
102
101
  async monit() {
@@ -189,12 +188,12 @@ class NodeRuntimeMonitor extends EventEmitter {
189
188
  });
190
189
  }
191
190
 
191
+ cleanup() {
192
+ return this.state.remove({ date: { $lt: Date.now() - 1000 * 60 * 60 * 24 } });
193
+ }
194
+
192
195
  _push(value) {
193
- const arr = this.data.history;
194
- arr.push(value);
195
- if (arr.length > this.historyLength) {
196
- arr.shift();
197
- }
196
+ return this.state.insert({ did: this.did, ...value });
198
197
  }
199
198
  }
200
199
 
@@ -16,6 +16,7 @@ const AuditLogState = require('./audit-log');
16
16
  const JobState = require('./job');
17
17
  const BackupState = require('./backup');
18
18
  const TrafficInsightState = require('./traffic-insight');
19
+ const RuntimeInsightState = require('./runtime-insight');
19
20
 
20
21
  const { getDbFilePath } = require('../util');
21
22
 
@@ -41,6 +42,7 @@ const init = (dataDirs, config = {}) => {
41
42
  const jobState = new JobState(models.Job, config);
42
43
  const backupState = new BackupState(models.Backup, config);
43
44
  const trafficInsight = new TrafficInsightState(models.TrafficInsight, config);
45
+ const runtimeInsight = new RuntimeInsightState(models.RuntimeInsight, config);
44
46
 
45
47
  return {
46
48
  node: nodeState,
@@ -57,6 +59,7 @@ const init = (dataDirs, config = {}) => {
57
59
  job: jobState,
58
60
  backup: backupState,
59
61
  trafficInsight,
62
+ runtimeInsight,
60
63
  };
61
64
  };
62
65
 
@@ -0,0 +1,26 @@
1
+ const { Op } = require('sequelize');
2
+ const BaseState = require('./base');
3
+
4
+ /**
5
+ * @extends BaseState<import('@abtnode/models').RuntimeInsightState>
6
+ */
7
+ class RuntimeInsight extends BaseState {
8
+ // fetch data for recent hour
9
+ findPaginated({ did = '', startDate = '', endDate = '', paging = { pageSize: 360 } } = {}) {
10
+ const where = {};
11
+ if (did) {
12
+ where.did = did;
13
+ }
14
+ if (startDate) {
15
+ where.date = { [Op.gte]: startDate };
16
+ }
17
+ if (endDate) {
18
+ where.date = where.date || {};
19
+ where.date[Op.lte] = endDate;
20
+ }
21
+
22
+ return super.paginate({ where }, { date: -1 }, paging);
23
+ }
24
+ }
25
+
26
+ module.exports = RuntimeInsight;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.13-beta-0086a35d",
6
+ "version": "1.16.13-beta-714bec78",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,19 +19,19 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/analytics": "1.16.13-beta-0086a35d",
23
- "@abtnode/auth": "1.16.13-beta-0086a35d",
24
- "@abtnode/certificate-manager": "1.16.13-beta-0086a35d",
25
- "@abtnode/constant": "1.16.13-beta-0086a35d",
26
- "@abtnode/cron": "1.16.13-beta-0086a35d",
27
- "@abtnode/logger": "1.16.13-beta-0086a35d",
28
- "@abtnode/models": "1.16.13-beta-0086a35d",
29
- "@abtnode/queue": "1.16.13-beta-0086a35d",
30
- "@abtnode/rbac": "1.16.13-beta-0086a35d",
31
- "@abtnode/router-provider": "1.16.13-beta-0086a35d",
32
- "@abtnode/static-server": "1.16.13-beta-0086a35d",
33
- "@abtnode/timemachine": "1.16.13-beta-0086a35d",
34
- "@abtnode/util": "1.16.13-beta-0086a35d",
22
+ "@abtnode/analytics": "1.16.13-beta-714bec78",
23
+ "@abtnode/auth": "1.16.13-beta-714bec78",
24
+ "@abtnode/certificate-manager": "1.16.13-beta-714bec78",
25
+ "@abtnode/constant": "1.16.13-beta-714bec78",
26
+ "@abtnode/cron": "1.16.13-beta-714bec78",
27
+ "@abtnode/logger": "1.16.13-beta-714bec78",
28
+ "@abtnode/models": "1.16.13-beta-714bec78",
29
+ "@abtnode/queue": "1.16.13-beta-714bec78",
30
+ "@abtnode/rbac": "1.16.13-beta-714bec78",
31
+ "@abtnode/router-provider": "1.16.13-beta-714bec78",
32
+ "@abtnode/static-server": "1.16.13-beta-714bec78",
33
+ "@abtnode/timemachine": "1.16.13-beta-714bec78",
34
+ "@abtnode/util": "1.16.13-beta-714bec78",
35
35
  "@arcblock/did": "1.18.84",
36
36
  "@arcblock/did-auth": "1.18.84",
37
37
  "@arcblock/did-ext": "^1.18.84",
@@ -42,10 +42,10 @@
42
42
  "@arcblock/pm2-events": "^0.0.5",
43
43
  "@arcblock/validator": "^1.18.84",
44
44
  "@arcblock/vc": "1.18.84",
45
- "@blocklet/constant": "1.16.13-beta-0086a35d",
46
- "@blocklet/meta": "1.16.13-beta-0086a35d",
47
- "@blocklet/resolver": "1.16.13-beta-0086a35d",
48
- "@blocklet/sdk": "1.16.13-beta-0086a35d",
45
+ "@blocklet/constant": "1.16.13-beta-714bec78",
46
+ "@blocklet/meta": "1.16.13-beta-714bec78",
47
+ "@blocklet/resolver": "1.16.13-beta-714bec78",
48
+ "@blocklet/sdk": "1.16.13-beta-714bec78",
49
49
  "@did-space/client": "^0.2.124",
50
50
  "@fidm/x509": "^1.2.1",
51
51
  "@ocap/mcrypto": "1.18.84",
@@ -96,5 +96,5 @@
96
96
  "express": "^4.18.2",
97
97
  "jest": "^27.5.1"
98
98
  },
99
- "gitHead": "cbf5c99e7cadf9fef4d77673cb45b44ef26436cf"
99
+ "gitHead": "6f447ce64c37e20b871c4b2cbb81085bd509b84f"
100
100
  }