1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var types_1 = require("@sentry/types");
|
3 | var utils_1 = require("@sentry/utils");
|
4 | var hub_1 = require("./hub");
|
5 |
|
6 |
|
7 |
|
8 | var SessionFlusher = (function () {
|
9 | function SessionFlusher(transport, attrs) {
|
10 | var _this = this;
|
11 | this.flushTimeout = 60;
|
12 | this._pendingAggregates = {};
|
13 | this._isEnabled = true;
|
14 | this._transport = transport;
|
15 |
|
16 | this._intervalId = setInterval(function () { return _this.flush(); }, this.flushTimeout * 1000);
|
17 | this._sessionAttrs = attrs;
|
18 | }
|
19 |
|
20 | SessionFlusher.prototype.sendSessionAggregates = function (sessionAggregates) {
|
21 | if (!this._transport.sendSession) {
|
22 | utils_1.logger.warn("Dropping session because custom transport doesn't implement sendSession");
|
23 | return;
|
24 | }
|
25 | void this._transport.sendSession(sessionAggregates).then(null, function (reason) {
|
26 | utils_1.logger.error("Error while sending session: " + reason);
|
27 | });
|
28 | };
|
29 |
|
30 | SessionFlusher.prototype.flush = function () {
|
31 | var sessionAggregates = this.getSessionAggregates();
|
32 | if (sessionAggregates.aggregates.length === 0) {
|
33 | return;
|
34 | }
|
35 | this._pendingAggregates = {};
|
36 | this.sendSessionAggregates(sessionAggregates);
|
37 | };
|
38 |
|
39 | SessionFlusher.prototype.getSessionAggregates = function () {
|
40 | var _this = this;
|
41 | var aggregates = Object.keys(this._pendingAggregates).map(function (key) {
|
42 | return _this._pendingAggregates[parseInt(key)];
|
43 | });
|
44 | var sessionAggregates = {
|
45 | attrs: this._sessionAttrs,
|
46 | aggregates: aggregates,
|
47 | };
|
48 | return utils_1.dropUndefinedKeys(sessionAggregates);
|
49 | };
|
50 |
|
51 | SessionFlusher.prototype.close = function () {
|
52 | clearInterval(this._intervalId);
|
53 | this._isEnabled = false;
|
54 | this.flush();
|
55 | };
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | SessionFlusher.prototype.incrementSessionStatusCount = function () {
|
62 | var _a, _b;
|
63 | if (!this._isEnabled) {
|
64 | return;
|
65 | }
|
66 | var scope = hub_1.getCurrentHub().getScope();
|
67 | var requestSession = (_a = scope) === null || _a === void 0 ? void 0 : _a.getRequestSession();
|
68 | if (requestSession && requestSession.status) {
|
69 | this._incrementSessionStatusCount(requestSession.status, new Date());
|
70 |
|
71 |
|
72 | (_b = scope) === null || _b === void 0 ? void 0 : _b.setRequestSession(undefined);
|
73 |
|
74 | }
|
75 | };
|
76 | |
77 |
|
78 |
|
79 |
|
80 | SessionFlusher.prototype._incrementSessionStatusCount = function (status, date) {
|
81 |
|
82 | var sessionStartedTrunc = new Date(date).setSeconds(0, 0);
|
83 | this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {};
|
84 |
|
85 |
|
86 | var aggregationCounts = this._pendingAggregates[sessionStartedTrunc];
|
87 | if (!aggregationCounts.started) {
|
88 | aggregationCounts.started = new Date(sessionStartedTrunc).toISOString();
|
89 | }
|
90 | switch (status) {
|
91 | case types_1.RequestSessionStatus.Errored:
|
92 | aggregationCounts.errored = (aggregationCounts.errored || 0) + 1;
|
93 | return aggregationCounts.errored;
|
94 | case types_1.RequestSessionStatus.Ok:
|
95 | aggregationCounts.exited = (aggregationCounts.exited || 0) + 1;
|
96 | return aggregationCounts.exited;
|
97 | case types_1.RequestSessionStatus.Crashed:
|
98 | aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1;
|
99 | return aggregationCounts.crashed;
|
100 | }
|
101 | };
|
102 | return SessionFlusher;
|
103 | }());
|
104 | exports.SessionFlusher = SessionFlusher;
|
105 |
|
\ | No newline at end of file |