@ejfdelgado/ejflab-common 1.7.3 → 1.8.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-common",
3
3
  "type": "commonjs",
4
- "version": "1.7.3",
4
+ "version": "1.8.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ejfdelgado/ejflab-common.git"
@@ -392,7 +392,7 @@ class FlowChartExec {
392
392
  let instance = this.createInstance(commandName, id, txt, args);
393
393
  index++;
394
394
  const response1 = `${commandName}${JSON.stringify(args)}`;
395
- const { canContinue, abort } = await instance.executeAsNode(args);
395
+ const { canContinue, abort } = await instance.executeAsNode(args, argsTxt);
396
396
  if (abort) {
397
397
  // Panic and halt all
398
398
  this.mustEnd = true;
@@ -429,7 +429,7 @@ class FlowChartExec {
429
429
  }
430
430
  index++;
431
431
  const response1 = `${commandName}${JSON.stringify(args)}`;
432
- const { canContinue, abort } = await instance.executeAsArrow(args);
432
+ const { canContinue, abort } = await instance.executeAsArrow(args, argsTxt);
433
433
  if (abort) {
434
434
  // Panic and halt all
435
435
  this.mustEnd = true;
@@ -13,8 +13,13 @@ class CommandPostgres extends CommandBasic {
13
13
  if (action == "execute") {
14
14
  //console.log("Starting execute...");
15
15
  const dbIdData = SimpleObj.getValue(this.context.data, dbIdPath, null);
16
- let bodyData = SimpleObj.getValue(this.context.data, bodyPath, null);
17
- bodyData = JSON.parse(JSON.stringify(bodyData));
16
+ let bodyData = {};
17
+ if (typeof bodyPath == "string") {
18
+ bodyData = SimpleObj.getValue(this.context.data, bodyPath, {});
19
+ bodyData = JSON.parse(JSON.stringify(bodyData));
20
+ } else if (typeof bodyPath == "object" && bodyPath !== null) {
21
+ bodyData = bodyPath;
22
+ }
18
23
  if (!(bodyData instanceof Array)) {
19
24
  bodyData = [bodyData]
20
25
  }
@@ -15,7 +15,11 @@ class StepBasic {
15
15
  nextTry = null;
16
16
  commandName;
17
17
  argsTxt;
18
+ argsTxtIndividual;
18
19
  fireTime;
20
+ startTime = null;
21
+ endTime = null;
22
+ duration = null;
19
23
  constructor(context, id, commandName, argsTxt) {
20
24
  this.context = context;
21
25
  this.id = id;
@@ -28,7 +32,39 @@ class StepBasic {
28
32
  setMaxTries(value) {
29
33
  this.maxErrorCount = value;
30
34
  }
31
- async executeAsNode(args) {
35
+ tic() {
36
+ this.startTime = new Date().getTime();
37
+ }
38
+ toc() {
39
+ const MAX_TIME_DATA = 10;
40
+ this.endTime = new Date().getTime();
41
+ this.duration = this.endTime - this.startTime;
42
+ // Read the performance object
43
+ let performance = SimpleObj.getValue(this.context.data, "performance", {});
44
+ if (performance.track !== true) {
45
+ return;
46
+ }
47
+ // Notify
48
+ const idTxt = `${this.commandName}«${this.argsTxtIndividual}»`;
49
+ const id = "d" + Buffer.from(idTxt).toString('base64');
50
+ if (!(id in performance)) {
51
+ const val = {
52
+ txt: idTxt,
53
+ time: [this.duration],
54
+ }
55
+ SimpleObj.recreate(this.context.data, `performance.${id}`, val);
56
+ } else {
57
+ const val = performance[id];
58
+ val.time.push(this.duration);
59
+ const overflow = val.time.length - MAX_TIME_DATA;
60
+ if (overflow > 0) {
61
+ val.time.splice(0, overflow);
62
+ }
63
+ SimpleObj.recreate(this.context.data, `performance.${id}.time`, val.time);
64
+ }
65
+ }
66
+ async executeAsNode(args, argsTxtIndividual) {
67
+ this.argsTxtIndividual = argsTxtIndividual;
32
68
  const answer = {
33
69
  canContinue: false,
34
70
  abort: false,
@@ -48,8 +84,10 @@ class StepBasic {
48
84
  }
49
85
  try {
50
86
  this.args = args;
87
+ this.tic();
51
88
  await this.execFirst();
52
89
  await this.execLast({ isCommand: true });
90
+ this.toc();
53
91
  answer.canContinue = true;
54
92
  this.resetErrorState();
55
93
  return answer;
@@ -59,7 +97,8 @@ class StepBasic {
59
97
  return answer;
60
98
  }
61
99
  }
62
- async executeAsArrow(args) {
100
+ async executeAsArrow(args, argsTxtIndividual) {
101
+ this.argsTxtIndividual = argsTxtIndividual;
63
102
  const answer = {
64
103
  canContinue: false,
65
104
  abort: false,
@@ -86,6 +125,7 @@ class StepBasic {
86
125
  }
87
126
  }
88
127
  if (this.firstTime) {
128
+ this.tic();
89
129
  const done = await this.execFirst();
90
130
  if (done === false) {
91
131
  return answer;
@@ -96,6 +136,7 @@ class StepBasic {
96
136
  answer.canContinue = await this.canContinue();
97
137
  if (answer.canContinue) {
98
138
  await this.execLast();
139
+ this.toc();
99
140
  this.fired = true;
100
141
  this.resetErrorState();
101
142
  } else {
@@ -2,18 +2,25 @@ const { StepBasic } = require("./StepBasic");
2
2
 
3
3
  class StepSleep extends StepBasic {
4
4
  startTime = null;
5
+ duration = null;
5
6
  constructor(context, id, commandName, argsTxt) {
6
7
  super(context, id, commandName, argsTxt);
7
8
  super.alwaysEvaluateCanContinue = false;
8
9
  }
9
10
  async execFirst() {
10
11
  this.startTime = new Date().getTime();
12
+ const minTime = this.args[0];
13
+ const maxTime = this.args[1];
14
+ if (typeof maxTime == "number") {
15
+ this.duration = minTime + Math.floor(Math.random() * (maxTime - minTime));
16
+ } else {
17
+ this.duration = minTime;
18
+ }
11
19
  }
12
20
 
13
21
  async canContinue() {
14
- const minTime = this.args[0];
15
22
  const diff = new Date().getTime() - this.startTime;
16
- return diff >= minTime;
23
+ return diff >= this.duration;
17
24
  }
18
25
  }
19
26