Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move transient analytics out of session #321

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
type BotSessionData,
type OnCallBackQueryData,
type OnMessageContext, type PayableBot, type PayableBotConfig,
SessionState, type UtilityBot
RequestState, type UtilityBot
} from './modules/types'
import { mainMenu } from './pages'
import { TranslateBot } from './modules/translate/TranslateBot'
Expand Down Expand Up @@ -104,6 +104,15 @@ Sentry.setTags({ botName: config.botName })
ES.init()

bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
ctx.transient = {
refunded: false,
analytics: {
module: '',
firstResponseTime: 0n,
actualResponseTime: 0n,
sessionState: RequestState.Initial
}
}
const transaction = Sentry.startTransaction({ name: 'bot-command' })
const entities = ctx.entities()
const startTime = now()
Expand All @@ -125,28 +134,28 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
}
await next()
transaction.finish()
if (ctx.session.analytics.module) {
if (ctx.transient.analytics.module) {
const userId = Number(ctx.message?.from?.id ?? '0')
const username = ctx.message?.from?.username ?? ''
if (!ctx.session.analytics.actualResponseTime) {
ctx.session.analytics.actualResponseTime = now()
if (!ctx.transient.analytics.actualResponseTime) {
ctx.transient.analytics.actualResponseTime = now()
}
if (!ctx.session.analytics.firstResponseTime) {
ctx.session.analytics.firstResponseTime = ctx.session.analytics.actualResponseTime
if (!ctx.transient.analytics.firstResponseTime) {
ctx.transient.analytics.firstResponseTime = ctx.transient.analytics.actualResponseTime
}
const totalProcessingTime = (now() - startTime).toString()
const firstResponseTime = (ctx.session.analytics.firstResponseTime - startTime).toString()
const actualResponseTime = (ctx.session.analytics.actualResponseTime - startTime).toString()
const firstResponseTime = (ctx.transient.analytics.firstResponseTime - startTime).toString()
const actualResponseTime = (ctx.transient.analytics.actualResponseTime - startTime).toString()
ES.add({
command,
text: ctx.message?.text ?? '',
module: ctx.session.analytics.module,
module: ctx.transient.analytics.module,
userId,
username,
firstResponseTime,
actualResponseTime,
refunded: ctx.session.refunded,
sessionState: ctx.session.analytics.sessionState,
refunded: ctx.transient.refunded,
sessionState: ctx.transient.analytics.sessionState,
totalProcessingTime
}).catch((ex: any) => {
logger.error({ errorMsg: ex.message }, 'Failed to add data to ES')
Expand Down Expand Up @@ -186,13 +195,6 @@ function createInitialSessionData (): BotSessionData {
usage: 0,
isProcessingQueue: false,
requestQueue: []
},
refunded: false,
analytics: {
module: '',
firstResponseTime: 0n,
actualResponseTime: 0n,
sessionState: SessionState.Initial
}
}
}
Expand Down
Loading