1 | import { dropUndefinedKeys } from '@sentry/utils';
|
2 | import { getCurrentHub } from './hub.js';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | class SessionFlusher {
|
8 | __init() {this.flushTimeout = 60;}
|
9 | __init2() {this._pendingAggregates = {};}
|
10 |
|
11 | __init3() {this._isEnabled = true;}
|
12 |
|
13 | constructor(client, attrs) {SessionFlusher.prototype.__init.call(this);SessionFlusher.prototype.__init2.call(this);SessionFlusher.prototype.__init3.call(this);
|
14 | this._client = client;
|
15 |
|
16 | this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000);
|
17 | this._sessionAttrs = attrs;
|
18 | }
|
19 |
|
20 |
|
21 | flush() {
|
22 | const sessionAggregates = this.getSessionAggregates();
|
23 | if (sessionAggregates.aggregates.length === 0) {
|
24 | return;
|
25 | }
|
26 | this._pendingAggregates = {};
|
27 | this._client.sendSession(sessionAggregates);
|
28 | }
|
29 |
|
30 |
|
31 | getSessionAggregates() {
|
32 | const aggregates = Object.keys(this._pendingAggregates).map((key) => {
|
33 | return this._pendingAggregates[parseInt(key)];
|
34 | });
|
35 |
|
36 | const sessionAggregates = {
|
37 | attrs: this._sessionAttrs,
|
38 | aggregates,
|
39 | };
|
40 | return dropUndefinedKeys(sessionAggregates);
|
41 | }
|
42 |
|
43 |
|
44 | close() {
|
45 | clearInterval(this._intervalId);
|
46 | this._isEnabled = false;
|
47 | this.flush();
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | incrementSessionStatusCount() {
|
56 | if (!this._isEnabled) {
|
57 | return;
|
58 | }
|
59 | const scope = getCurrentHub().getScope();
|
60 | const requestSession = scope && scope.getRequestSession();
|
61 |
|
62 | if (requestSession && requestSession.status) {
|
63 | this._incrementSessionStatusCount(requestSession.status, new Date());
|
64 |
|
65 |
|
66 | if (scope) {
|
67 | scope.setRequestSession(undefined);
|
68 | }
|
69 |
|
70 | }
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 | _incrementSessionStatusCount(status, date) {
|
78 |
|
79 | const sessionStartedTrunc = new Date(date).setSeconds(0, 0);
|
80 | this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {};
|
81 |
|
82 |
|
83 |
|
84 | const aggregationCounts = this._pendingAggregates[sessionStartedTrunc];
|
85 | if (!aggregationCounts.started) {
|
86 | aggregationCounts.started = new Date(sessionStartedTrunc).toISOString();
|
87 | }
|
88 |
|
89 | switch (status) {
|
90 | case 'errored':
|
91 | aggregationCounts.errored = (aggregationCounts.errored || 0) + 1;
|
92 | return aggregationCounts.errored;
|
93 | case 'ok':
|
94 | aggregationCounts.exited = (aggregationCounts.exited || 0) + 1;
|
95 | return aggregationCounts.exited;
|
96 | default:
|
97 | aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1;
|
98 | return aggregationCounts.crashed;
|
99 | }
|
100 | }
|
101 | }
|
102 |
|
103 | export { SessionFlusher };
|
104 |
|