{"version":3,"file":"sessionflusher.js","sources":["../../src/sessionflusher.ts"],"sourcesContent":["import type {\n AggregationCounts,\n Client,\n RequestSessionStatus,\n SessionAggregates,\n SessionFlusherLike,\n} from '@sentry/types';\nimport { dropUndefinedKeys } from '@sentry/utils';\nimport { getCurrentScope } from './exports';\n\ntype ReleaseHealthAttributes = {\n environment?: string;\n release: string;\n};\n\n/**\n * @inheritdoc\n */\nexport class SessionFlusher implements SessionFlusherLike {\n public readonly flushTimeout: number;\n private _pendingAggregates: Record;\n private _sessionAttrs: ReleaseHealthAttributes;\n private _intervalId: ReturnType;\n private _isEnabled: boolean;\n private _client: Client;\n\n public constructor(client: Client, attrs: ReleaseHealthAttributes) {\n this._client = client;\n this.flushTimeout = 60;\n this._pendingAggregates = {};\n this._isEnabled = true;\n\n // Call to setInterval, so that flush is called every 60 seconds\n this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000);\n this._sessionAttrs = attrs;\n }\n\n /** Checks if `pendingAggregates` has entries, and if it does flushes them by calling `sendSession` */\n public flush(): void {\n const sessionAggregates = this.getSessionAggregates();\n if (sessionAggregates.aggregates.length === 0) {\n return;\n }\n this._pendingAggregates = {};\n this._client.sendSession(sessionAggregates);\n }\n\n /** Massages the entries in `pendingAggregates` and returns aggregated sessions */\n public getSessionAggregates(): SessionAggregates {\n const aggregates: AggregationCounts[] = Object.keys(this._pendingAggregates).map((key: string) => {\n return this._pendingAggregates[parseInt(key)];\n });\n\n const sessionAggregates: SessionAggregates = {\n attrs: this._sessionAttrs,\n aggregates,\n };\n return dropUndefinedKeys(sessionAggregates);\n }\n\n /** JSDoc */\n public close(): void {\n clearInterval(this._intervalId);\n this._isEnabled = false;\n this.flush();\n }\n\n /**\n * Wrapper function for _incrementSessionStatusCount that checks if the instance of SessionFlusher is enabled then\n * fetches the session status of the request from `Scope.getRequestSession().status` on the scope and passes them to\n * `_incrementSessionStatusCount` along with the start date\n */\n public incrementSessionStatusCount(): void {\n if (!this._isEnabled) {\n return;\n }\n const scope = getCurrentScope();\n const requestSession = scope.getRequestSession();\n\n if (requestSession && requestSession.status) {\n this._incrementSessionStatusCount(requestSession.status, new Date());\n // This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in\n // case captureRequestSession is called more than once to prevent double count\n scope.setRequestSession(undefined);\n /* eslint-enable @typescript-eslint/no-unsafe-member-access */\n }\n }\n\n /**\n * Increments status bucket in pendingAggregates buffer (internal state) corresponding to status of\n * the session received\n */\n private _incrementSessionStatusCount(status: RequestSessionStatus, date: Date): number {\n // Truncate minutes and seconds on Session Started attribute to have one minute bucket keys\n const sessionStartedTrunc = new Date(date).setSeconds(0, 0);\n this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {};\n\n // corresponds to aggregated sessions in one specific minute bucket\n // for example, {\"started\":\"2021-03-16T08:00:00.000Z\",\"exited\":4, \"errored\": 1}\n const aggregationCounts: AggregationCounts = this._pendingAggregates[sessionStartedTrunc];\n if (!aggregationCounts.started) {\n aggregationCounts.started = new Date(sessionStartedTrunc).toISOString();\n }\n\n switch (status) {\n case 'errored':\n aggregationCounts.errored = (aggregationCounts.errored || 0) + 1;\n return aggregationCounts.errored;\n case 'ok':\n aggregationCounts.exited = (aggregationCounts.exited || 0) + 1;\n return aggregationCounts.exited;\n default:\n aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1;\n return aggregationCounts.crashed;\n }\n }\n}\n"],"names":[],"mappings":";;;AAeA;AACA;AACA;AACO,MAAM,gBAA6C;;AAQ1D,GAAS,WAAW,CAAC,MAAM,EAAU,KAAK,EAA2B;AACrE,IAAI,IAAI,CAAC,OAAQ,GAAE,MAAM,CAAA;AACzB,IAAI,IAAI,CAAC,YAAa,GAAE,EAAE,CAAA;AAC1B,IAAI,IAAI,CAAC,kBAAmB,GAAE,EAAE,CAAA;AAChC,IAAI,IAAI,CAAC,UAAW,GAAE,IAAI,CAAA;AAC1B;AACA;AACA,IAAI,IAAI,CAAC,WAAY,GAAE,WAA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