@resolveio/server-lib 20.12.13 → 20.12.14

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.
@@ -0,0 +1,2 @@
1
+ import * as express from 'express';
2
+ export declare function setupSlowQueryPublicationRoutes(app: express.Application): void;
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.setupSlowQueryPublicationRoutes = setupSlowQueryPublicationRoutes;
40
+ var express = require("express");
41
+ var resolveio_server_app_1 = require("../resolveio-server-app");
42
+ function setupSlowQueryPublicationRoutes(app) {
43
+ var _this = this;
44
+ app.post('/api/slow-queries/run-publication', express.json(), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
45
+ var body, publication, subscriptionData, userId, manager, startTime, execution, durationMs, err_1;
46
+ return __generator(this, function (_a) {
47
+ switch (_a.label) {
48
+ case 0:
49
+ body = request.body || {};
50
+ publication = typeof body.publication === 'string' ? body.publication.trim() : '';
51
+ subscriptionData = Array.isArray(body.subscriptionData) ? body.subscriptionData : [];
52
+ userId = typeof body.userId === 'string' ? body.userId : undefined;
53
+ if (!publication) {
54
+ response.status(400).send(JSON.stringify({
55
+ error: true,
56
+ result: 'Publication name is required.'
57
+ }));
58
+ return [2 /*return*/];
59
+ }
60
+ manager = resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager();
61
+ if (!manager) {
62
+ response.status(503).send(JSON.stringify({
63
+ error: true,
64
+ result: 'Subscription manager is not available.'
65
+ }));
66
+ return [2 /*return*/];
67
+ }
68
+ _a.label = 1;
69
+ case 1:
70
+ _a.trys.push([1, 3, , 4]);
71
+ startTime = Date.now();
72
+ return [4 /*yield*/, manager.runPublicationOnce(publication, subscriptionData, { userId: userId })];
73
+ case 2:
74
+ execution = _a.sent();
75
+ durationMs = Date.now() - startTime;
76
+ response.send(JSON.stringify({
77
+ error: false,
78
+ result: {
79
+ durationMs: durationMs,
80
+ data: execution === null || execution === void 0 ? void 0 : execution.result,
81
+ snapshot: execution === null || execution === void 0 ? void 0 : execution.snapshot
82
+ }
83
+ }));
84
+ return [3 /*break*/, 4];
85
+ case 3:
86
+ err_1 = _a.sent();
87
+ console.error('Slow query publication run failed', err_1);
88
+ response.status(500).send(JSON.stringify({
89
+ error: true,
90
+ result: (err_1 === null || err_1 === void 0 ? void 0 : err_1.message) || 'Failed to run publication.'
91
+ }));
92
+ return [3 /*break*/, 4];
93
+ case 4: return [2 /*return*/];
94
+ }
95
+ });
96
+ }); });
97
+ }
98
+
99
+ //# sourceMappingURL=slow-query-publication.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/http/slow-query-publication.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,0EA6CC;AAhDD,iCAAmC;AACnC,gEAA0D;AAE1D,SAAgB,+BAA+B,CAAC,GAAwB;IAAxE,iBA6CC;IA5CA,GAAG,CAAC,IAAI,CAAC,mCAAmC,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,UAAO,OAAO,EAAE,QAAQ;;;;;oBAC/E,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;oBAC1B,WAAW,GAAG,OAAO,IAAI,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBAClF,gBAAgB,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC;oBACrF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AC;oBACvD,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;wBACxC,KAAK,EAAE,IAAI;wBACX,MAAM,EAAE,CAAA,KAAG,aAAH,KAAG,uBAAH,KAAG,CAAE,OAAO,KAAI,4BAA4B;qBACpD,CAAC,CAAC,CAAC;;;;;SAEL,CAAC,CAAC;AACJ,CAAC","file":"slow-query-publication.js","sourcesContent":["import * as express from 'express';\nimport { ResolveIOServer } from '../resolveio-server-app';\n\nexport function setupSlowQueryPublicationRoutes(app: express.Application) {\n\tapp.post('/api/slow-queries/run-publication', express.json(), async (request, response) => {\n\t\tconst body = request.body || {};\n\t\tconst publication = typeof body.publication === 'string' ? body.publication.trim() : '';\n\t\tconst subscriptionData = Array.isArray(body.subscriptionData) ? body.subscriptionData : [];\n\t\tconst userId = typeof body.userId === 'string' ? body.userId : undefined;\n\n\t\tif (!publication) {\n\t\t\tresponse.status(400).send(JSON.stringify({\n\t\t\t\terror: true,\n\t\t\t\tresult: 'Publication name is required.'\n\t\t\t}));\n\t\t\treturn;\n\t\t}\n\n\t\tconst manager = ResolveIOServer.getMainServer().getSubscriptionManager();\n\t\tif (!manager) {\n\t\t\tresponse.status(503).send(JSON.stringify({\n\t\t\t\terror: true,\n\t\t\t\tresult: 'Subscription manager is not available.'\n\t\t\t}));\n\t\t\treturn;\n\t\t}\n\n\ttry {\n\t\tconst startTime = Date.now();\n\t\tconst execution = await manager.runPublicationOnce(publication, subscriptionData, {userId});\n\t\tconst durationMs = Date.now() - startTime;\n\t\tresponse.send(JSON.stringify({\n\t\t\terror: false,\n\t\t\tresult: {\n\t\t\t\tdurationMs,\n\t\t\t\tdata: execution?.result,\n\t\t\t\tsnapshot: execution?.snapshot\n\t\t\t}\n\t\t}));\n\t}\n\tcatch (err) {\n\t\tconsole.error('Slow query publication run failed', err);\n\t\t\tresponse.status(500).send(JSON.stringify({\n\t\t\t\terror: true,\n\t\t\t\tresult: err?.message || 'Failed to run publication.'\n\t\t\t}));\n\t\t}\n\t});\n}\n"]}
@@ -395,12 +395,13 @@ var MongoManager = /** @class */ (function () {
395
395
  };
396
396
  MongoManager.prototype.executeFind = function (collectionName, filter, options, cacheKey) {
397
397
  return __awaiter(this, void 0, void 0, function () {
398
- var result, collection, _a, session, safeOptions, monitor, queryStart, err_1, durationMs;
398
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_1, durationMs;
399
399
  return __generator(this, function (_b) {
400
400
  switch (_b.label) {
401
401
  case 0:
402
402
  collection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(collectionName);
403
403
  _a = options || {}, session = _a.session, safeOptions = __rest(_a, ["session"]);
404
+ pubContext = (0, subscription_dependency_context_1.getPublicationContext)();
404
405
  monitor = monitor_manager_1.MonitorMongo.create('find', collectionName, JSON.stringify([filter, safeOptions]));
405
406
  queryStart = Date.now();
406
407
  _b.label = 1;
@@ -431,7 +432,10 @@ var MongoManager = /** @class */ (function () {
431
432
  options: safeOptions,
432
433
  durationMs: durationMs,
433
434
  queryType: 'find',
434
- notes: 'MongoManager executeFind'
435
+ notes: 'MongoManager executeFind',
436
+ publication: pubContext.publication,
437
+ subscriptionData: pubContext.subscriptionData,
438
+ userId: pubContext.userId
435
439
  });
436
440
  return [7 /*endfinally*/];
437
441
  case 6:
@@ -493,12 +497,13 @@ var MongoManager = /** @class */ (function () {
493
497
  };
494
498
  MongoManager.prototype.executeFindOne = function (collectionName, filter, options, cacheKey) {
495
499
  return __awaiter(this, void 0, void 0, function () {
496
- var result, collection, _a, session, safeOptions, monitor, queryStart, err_2, durationMs;
500
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_2, durationMs;
497
501
  return __generator(this, function (_b) {
498
502
  switch (_b.label) {
499
503
  case 0:
500
504
  collection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(collectionName);
501
505
  _a = options || {}, session = _a.session, safeOptions = __rest(_a, ["session"]);
506
+ pubContext = (0, subscription_dependency_context_1.getPublicationContext)();
502
507
  monitor = monitor_manager_1.MonitorMongo.create('findOne', collectionName, JSON.stringify([filter, safeOptions]));
503
508
  queryStart = Date.now();
504
509
  _b.label = 1;
@@ -529,7 +534,10 @@ var MongoManager = /** @class */ (function () {
529
534
  options: safeOptions,
530
535
  durationMs: durationMs,
531
536
  queryType: 'findOne',
532
- notes: 'MongoManager executeFindOne'
537
+ notes: 'MongoManager executeFindOne',
538
+ publication: pubContext.publication,
539
+ subscriptionData: pubContext.subscriptionData,
540
+ userId: pubContext.userId
533
541
  });
534
542
  return [7 /*endfinally*/];
535
543
  case 6:
@@ -591,12 +599,13 @@ var MongoManager = /** @class */ (function () {
591
599
  };
592
600
  MongoManager.prototype.executeAggregate = function (collectionName, pipeline, options, cacheKey, collections) {
593
601
  return __awaiter(this, void 0, void 0, function () {
594
- var result, collection, _a, session, safeOptions, monitor, queryStart, err_3, durationMs;
602
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_3, durationMs;
595
603
  return __generator(this, function (_b) {
596
604
  switch (_b.label) {
597
605
  case 0:
598
606
  collection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(collectionName);
599
607
  _a = options || {}, session = _a.session, safeOptions = __rest(_a, ["session"]);
608
+ pubContext = (0, subscription_dependency_context_1.getPublicationContext)();
600
609
  monitor = monitor_manager_1.MonitorMongo.create('aggregate', collectionName, JSON.stringify([pipeline, safeOptions]));
601
610
  queryStart = Date.now();
602
611
  _b.label = 1;
@@ -627,7 +636,10 @@ var MongoManager = /** @class */ (function () {
627
636
  options: safeOptions,
628
637
  durationMs: durationMs,
629
638
  queryType: 'aggregate',
630
- notes: 'MongoManager executeAggregate'
639
+ notes: 'MongoManager executeAggregate',
640
+ publication: pubContext.publication,
641
+ subscriptionData: pubContext.subscriptionData,
642
+ userId: pubContext.userId
631
643
  });
632
644
  return [7 /*endfinally*/];
633
645
  case 6:
@@ -674,12 +686,13 @@ var MongoManager = /** @class */ (function () {
674
686
  };
675
687
  MongoManager.prototype.executeCountDocuments = function (collectionName, filter, options, cacheKey) {
676
688
  return __awaiter(this, void 0, void 0, function () {
677
- var result, collection, _a, session, safeOptions, monitor, queryStart, err_4, durationMs;
689
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_4, durationMs;
678
690
  return __generator(this, function (_b) {
679
691
  switch (_b.label) {
680
692
  case 0:
681
693
  collection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(collectionName);
682
694
  _a = options || {}, session = _a.session, safeOptions = __rest(_a, ["session"]);
695
+ pubContext = (0, subscription_dependency_context_1.getPublicationContext)();
683
696
  monitor = monitor_manager_1.MonitorMongo.create('countDocuments', collectionName, JSON.stringify([filter, safeOptions]));
684
697
  queryStart = Date.now();
685
698
  _b.label = 1;
@@ -710,7 +723,10 @@ var MongoManager = /** @class */ (function () {
710
723
  options: safeOptions,
711
724
  durationMs: durationMs,
712
725
  queryType: 'countDocuments',
713
- notes: 'MongoManager executeCountDocuments'
726
+ notes: 'MongoManager executeCountDocuments',
727
+ publication: pubContext.publication,
728
+ subscriptionData: pubContext.subscriptionData,
729
+ userId: pubContext.userId
714
730
  });
715
731
  return [7 /*endfinally*/];
716
732
  case 6: