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CAAC,mBAAmB,CAAC,GAAG,IAAI,CAAC,kBAAkB,CAAC,mBAAmB,CAAC,IAAI,EAAE,CAAC;QAElG,mEAAmE;QACnE,+EAA+E;QAC/E,IAAM,iBAAiB,GAAsB,IAAI,CAAC,kBAAkB,CAAC,mBAAmB,CAAC,CAAC;QAC1F,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE;YAC9B,iBAAiB,CAAC,OAAO,GAAG,IAAI,IAAI,CAAC,mBAAmB,CAAC,CAAC,WAAW,EAAE,CAAC;SACzE;QAED,QAAQ,MAAM,EAAE;YACd,KAAK,4BAAoB,CAAC,OAAO;gBAC/B,iBAAiB,CAAC,OAAO,GAAG,CAAC,iBAAiB,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;gBACjE,OAAO,iBAAiB,CAAC,OAAO,CAAC;YACnC,KAAK,4BAAoB,CAAC,EAAE;gBAC1B,iBAAiB,CAAC,MAAM,GAAG,CAAC,iBAAiB,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;gBAC/D,OAAO,iBAAiB,CAAC,MAAM,CAAC;YAClC,KAAK,4BAAoB,CAAC,OAAO;gBAC/B,iBAAiB,CAAC,OAAO,GAAG,CAAC,iBAAiB,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;gBACjE,OAAO,iBAAiB,CAAC,OAAO,CAAC;SACpC;IACH,CAAC;IACH,qBAAC;AAAD,CAAC,AA1GD,IA0GC;AA1GY,wCAAc","sourcesContent":["import {\n AggregationCounts,\n RequestSessionStatus,\n SessionAggregates,\n SessionFlusherLike,\n Transport,\n} from '@sentry/types';\nimport { dropUndefinedKeys, logger } from '@sentry/utils';\n\nimport { getCurrentHub } from './hub';\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 = 60;\n private _pendingAggregates: Record<number, AggregationCounts> = {};\n private _sessionAttrs: ReleaseHealthAttributes;\n private _intervalId: ReturnType<typeof setInterval>;\n private _isEnabled: boolean = true;\n private _transport: Transport;\n\n public constructor(transport: Transport, attrs: ReleaseHealthAttributes) {\n this._transport = transport;\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 /** Sends session aggregates to Transport */\n public sendSessionAggregates(sessionAggregates: SessionAggregates): void {\n if (!this._transport.sendSession) {\n logger.warn(\"Dropping session because custom transport doesn't implement sendSession\");\n return;\n }\n void this._transport.sendSession(sessionAggregates).then(null, reason => {\n logger.error(`Error while sending session: ${reason}`);\n });\n }\n\n /** Checks if `pendingAggregates` has entries, and if it does flushes them by calling `sendSessions` */\n public flush(): void {\n const sessionAggregates = this.getSessionAggregates();\n if (sessionAggregates.aggregates.length === 0) {\n return;\n }\n this._pendingAggregates = {};\n this.sendSessionAggregates(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 = getCurrentHub().getScope();\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\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 RequestSessionStatus.Errored:\n aggregationCounts.errored = (aggregationCounts.errored || 0) + 1;\n return aggregationCounts.errored;\n case RequestSessionStatus.Ok:\n aggregationCounts.exited = (aggregationCounts.exited || 0) + 1;\n return aggregationCounts.exited;\n case RequestSessionStatus.Crashed:\n aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1;\n return aggregationCounts.crashed;\n }\n }\n}\n"]}
\No newline at end of file