@sdflc/backend-helpers 1.0.51 → 1.0.52

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.
@@ -1,7 +1,8 @@
1
- import { Logger } from '@sdflc/utils';
1
+ import { Logger, MeasureTool } from '@sdflc/utils';
2
2
  import { Database } from './Database';
3
3
  import { BaseCorePropsInterface, BaseCoreActionsInterface, CoreActionInterface, JwtTokenSecretAndSettings, ApiContext } from './interfaces';
4
4
  declare class BaseCore {
5
+ protected measureTool: MeasureTool;
5
6
  protected config: BaseCorePropsInterface;
6
7
  protected logger: Logger;
7
8
  constructor(props: BaseCorePropsInterface);
package/build/BaseCore.js CHANGED
@@ -30,6 +30,7 @@ const defaultValidators = {
30
30
  };
31
31
  class BaseCore {
32
32
  constructor(props) {
33
+ this.measureTool = new utils_1.MeasureTool();
33
34
  this.config = props;
34
35
  this.logger = props.logger;
35
36
  }
@@ -121,9 +122,19 @@ class BaseCore {
121
122
  // If we process action with the same requestId as requestId passed to the constructor it means
122
123
  // we are processing the same request from FE and can skip some of the operations like transactions and etc
123
124
  const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
125
+ // TODO: Calculate total thread block time between async calls withing the request
126
+ // this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
127
+ // this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
128
+ // });
129
+ this.logger.log(`The ${doingWhat} started:`, args);
130
+ this.measureTool.start();
124
131
  let step = doingWhat || '';
125
132
  const setStep = (stepName) => {
126
- step = stepName;
133
+ step = stepName || step;
134
+ const time = this.measureTool.addCheckpoint(stepName);
135
+ if (stepName) {
136
+ this.logger.debug(`${time.name}. Since last step: ${time.durationMs} ms`);
137
+ }
127
138
  };
128
139
  const actionOpt = {
129
140
  core: this,
@@ -137,13 +148,17 @@ class BaseCore {
137
148
  yield this.getDb().startTransaction();
138
149
  }
139
150
  if (doAuth === true || (doAuth == null && this.config.doAuth)) {
151
+ this.measureTool.addCheckpoint('Authenticate request: start');
140
152
  const [authResult, errMsg] = yield this.authenticate(opt, actionOpt);
153
+ this.measureTool.addCheckpoint('Authenticate request: end');
141
154
  if (!authResult) {
142
155
  return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
143
156
  }
144
157
  }
145
158
  else {
159
+ this.measureTool.addCheckpoint('Find space: start');
146
160
  const [spaceResult, errMsg] = yield this.findSpace();
161
+ this.measureTool.addCheckpoint('Find space: end');
147
162
  if (spaceResult !== true) {
148
163
  return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
149
164
  }
@@ -151,6 +166,7 @@ class BaseCore {
151
166
  }
152
167
  let validatedItems = {};
153
168
  if (typeof validate === 'function') {
169
+ this.measureTool.addCheckpoint('Validate parameters: start');
154
170
  // If we have validator provided, use it to validate data before running action
155
171
  // validator is expected to return an array:
156
172
  // [0] - true or validation error object,
@@ -163,14 +179,19 @@ class BaseCore {
163
179
  }
164
180
  // If validator returned null then consider the validation passed
165
181
  result = result != null ? result : true;
182
+ this.measureTool.addCheckpoint('Validate parameters: end');
166
183
  if (result !== true) {
167
184
  // if there was an error then `result` is expected to be an object with the same structure as the checkit library does
168
185
  yield this.checkResult({ hasTransaction, result, sameRequest });
169
186
  return result.code != null ? result : new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
170
187
  }
171
188
  }
189
+ let time = this.measureTool.stop();
190
+ this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
172
191
  const result = yield action(args, Object.assign(Object.assign({}, actionOpt), { validatedItems }));
173
192
  yield this.checkResult({ hasTransaction, result, sameRequest });
193
+ time = this.measureTool.stop(doingWhat);
194
+ this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
174
195
  return result;
175
196
  }
176
197
  catch (ex) {
@@ -184,6 +205,8 @@ class BaseCore {
184
205
  message: `${step} failed on ${doingWhat}`,
185
206
  };
186
207
  }
208
+ const time = this.measureTool.stop(doingWhat);
209
+ this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
187
210
  return this.exceptionFailure(ex, what);
188
211
  }
189
212
  });
@@ -197,7 +220,6 @@ class BaseCore {
197
220
  accessRights: [utils_1.ACCESS_RIGHTS.LIST],
198
221
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
199
222
  var _d;
200
- this.logger.log(`Requesting a list for ${this.config.name}:`, args);
201
223
  const object = yield this.beforeList(args, opt);
202
224
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.list(object));
203
225
  const result = yield this.afterList(items, opt);
@@ -219,7 +241,6 @@ class BaseCore {
219
241
  action: (args) => __awaiter(this, void 0, void 0, function* () {
220
242
  var _d;
221
243
  const { id } = args || {};
222
- this.logger.log(`Requesting an item for ${this.config.name}:`, args);
223
244
  const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.get(id));
224
245
  return this.success([item], item ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
225
246
  }),
@@ -238,7 +259,6 @@ class BaseCore {
238
259
  action: (args) => __awaiter(this, void 0, void 0, function* () {
239
260
  var _d;
240
261
  const { ids } = args || {};
241
- this.logger.log(`Requesting multiple items for ${this.config.name}:`, args);
242
262
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.getMany(ids));
243
263
  return this.success(items, items.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
244
264
  }),
@@ -257,7 +277,6 @@ class BaseCore {
257
277
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
258
278
  var _d;
259
279
  const { params } = args || {};
260
- this.logger.log(`Requesting creation of an item for ${this.config.name}:`, args);
261
280
  const object = yield this.beforeCreate(params, opt);
262
281
  const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(object));
263
282
  const result = yield this.afterCreate(item, opt);
@@ -278,7 +297,6 @@ class BaseCore {
278
297
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
279
298
  var _d;
280
299
  const { params } = args || {};
281
- this.logger.log(`Requesting creation of multiple items for ${this.config.name}:`, args);
282
300
  const objects = yield this.beforeCreateMany(params, opt);
283
301
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(objects));
284
302
  const result = yield this.afterCreateMany(items, opt);
@@ -299,7 +317,6 @@ class BaseCore {
299
317
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
300
318
  var _d;
301
319
  const { where, params } = args || {};
302
- this.logger.log(`Requesting update of an item for ${this.config.name}:`, args);
303
320
  const object = yield this.beforeUpdate(params, opt);
304
321
  const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.update(where, object));
305
322
  const result = yield this.afterUpdate(item, opt);
@@ -320,7 +337,6 @@ class BaseCore {
320
337
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
321
338
  var _d;
322
339
  const { params } = args || {};
323
- this.logger.log(`Requesting update of an item for ${this.config.name}:`, args);
324
340
  const object = yield this.beforeUpdateMany(params, opt);
325
341
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.updateMany(object));
326
342
  const result = yield this.afterUpdateMany(items, opt);
@@ -341,7 +357,6 @@ class BaseCore {
341
357
  action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
342
358
  var _d;
343
359
  const { params } = args || {};
344
- this.logger.log(`Requesting setting of item(s) for ${this.config.name}:`, args);
345
360
  const object = yield this.beforeSet(params, opt);
346
361
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.set(object));
347
362
  const result = yield this.afterSet(items, opt);
@@ -362,7 +377,6 @@ class BaseCore {
362
377
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
363
378
  var _d;
364
379
  const { where } = args || {};
365
- this.logger.log(`Requesting removal of an item for ${this.config.name}:`, args);
366
380
  const object = yield this.beforeRemove(where, opt);
367
381
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
368
382
  const result = yield this.afterRemove(items, opt);
@@ -383,7 +397,6 @@ class BaseCore {
383
397
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
384
398
  var _d;
385
399
  const { where } = args || {};
386
- this.logger.log(`Requesting removal of multiple items for ${this.config.name}:`, args);
387
400
  const object = yield this.beforeRemoveMany(where, opt);
388
401
  const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
389
402
  const result = yield this.afterRemoveMany(items, opt);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.51",
3
+ "version": "1.0.52",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",