Skip to content

Commit

Permalink
Merge pull request #318 from harmony-one/perf-fix
Browse files Browse the repository at this point in the history
Fix time measurement issues and type issues
  • Loading branch information
theofandrich authored Sep 29, 2023
2 parents 413cb8d + f620453 commit 7abfd19
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 114 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"prettier": "2.8.7",
"ts-jest": "^29.1.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.4"
"typescript": "^5.2.2"
},
"dependencies": {
"@elastic/elasticsearch": "^8.9.0",
Expand Down
13 changes: 7 additions & 6 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { hydrateFiles } from '@grammyjs/files'
import { VoiceTranslateBot } from './modules/voice-translate'
import { TextToSpeechBot } from './modules/text-to-speech'
import { VoiceToTextBot } from './modules/voice-to-text'
import { now } from './utils/perf'

Events.EventEmitter.defaultMaxListeners = 30

Expand Down Expand Up @@ -105,7 +106,7 @@ ES.init()
bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
const transaction = Sentry.startTransaction({ name: 'bot-command' })
const entities = ctx.entities()
const startTime = process.hrtime.bigint()
const startTime = now()
let command = ''
for (const ent of entities) {
if (ent.type === 'bot_command') {
Expand All @@ -128,14 +129,14 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
const userId = Number(ctx.message?.from?.id ?? '0')
const username = ctx.message?.from?.username ?? ''
if (!ctx.session.analytics.actualResponseTime) {
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
if (!ctx.session.analytics.firstResponseTime) {
ctx.session.analytics.firstResponseTime = ctx.session.analytics.actualResponseTime
}
const firstResponseTime = ((ctx.session.analytics.firstResponseTime - startTime) / 1000n).toString()
const actualResponseTime = ((ctx.session.analytics.actualResponseTime - startTime) / 1000n).toString()
const totalProcessingTime = (process.hrtime.bigint() - startTime).toString()
const totalProcessingTime = (now() - startTime).toString()
const firstResponseTime = (ctx.session.analytics.firstResponseTime - startTime).toString()
const actualResponseTime = (ctx.session.analytics.actualResponseTime - startTime).toString()
ES.add({
command,
text: ctx.message?.text ?? '',
Expand Down Expand Up @@ -226,7 +227,7 @@ const voiceToTextBot = new VoiceToTextBot(payments)

bot.on('message:new_chat_members:me', async (ctx) => {
try {
const accountId = payments.getAccountId(ctx as OnMessageContext)
const accountId = payments.getAccountId(ctx)

const chat = await chatService.getAccountById(accountId)

Expand Down
59 changes: 30 additions & 29 deletions src/modules/1country/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import config from '../../config'
import { MAX_TRIES, sendMessage } from '../open-ai/helpers'
import { sleep } from '../sd-images/utils'
import { isValidUrl } from '../open-ai/utils/web-crawler'
import { now } from '../../utils/perf'

export const SupportedCommands = {
register: { name: 'rent' },
Expand Down Expand Up @@ -139,7 +140,7 @@ export class OneCountryBot implements PayableBot {

this.logger.warn('### unsupported command')
await ctx.reply('### unsupported command', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
}

Expand All @@ -149,12 +150,12 @@ export class OneCountryBot implements PayableBot {
await sendMessage(ctx, 'The bot is suspended').catch(async (e) => {
await this.onError(ctx, e)
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
return
}
if (!ctx.match) {
await ctx.reply('Error: Missing 1.country domain', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
return
}
Expand All @@ -166,7 +167,7 @@ export class OneCountryBot implements PayableBot {
reply_markup: keyboard,
message_thread_id: ctx.message?.message_thread_id
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Success
}

Expand All @@ -177,7 +178,7 @@ export class OneCountryBot implements PayableBot {
await sendMessage(ctx, 'The bot is suspended').catch(async (e) => {
await this.onError(ctx, e)
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
return
}
if (!ctx.match) {
Expand All @@ -200,7 +201,7 @@ export class OneCountryBot implements PayableBot {
}).catch(async (e) => {
await this.onError(ctx, e)
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
return
}
Expand All @@ -211,14 +212,14 @@ export class OneCountryBot implements PayableBot {
}).catch(async (e) => {
await this.onError(ctx, e)
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
return
}
// ************** dc set process ********************
await ctx.reply('Subdomain created')
console.log(subdomain)
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Success
// ****** ////
} else {
Expand All @@ -231,7 +232,7 @@ export class OneCountryBot implements PayableBot {
}
} catch (e) {
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
await this.onError(ctx, e)
}
}
Expand All @@ -243,15 +244,15 @@ export class OneCountryBot implements PayableBot {
await sendMessage(ctx, 'The bot is suspended').catch(async (e) => {
await this.onError(ctx, e)
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
return
}
const { prompt } = getCommandNamePrompt(ctx, SupportedCommands)
const lastDomain = ctx.session.oneCountry.lastDomain
let msgId = 0
if (!prompt && !lastDomain) {
await ctx.reply('Write a domain name', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
return
}
Expand All @@ -262,7 +263,7 @@ export class OneCountryBot implements PayableBot {
) {
await this.onNotBalanceMessage(ctx)
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
} else {
const fullUrl = getUrl(lastDomain, true)
await ctx.reply(`The Domain ${fullUrl} was registered`, {
Expand All @@ -275,7 +276,7 @@ export class OneCountryBot implements PayableBot {
// disable_web_page_preview: false
// })
ctx.session.analytics.sessionState = SessionState.Success
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
return
}
}
Expand All @@ -288,13 +289,13 @@ export class OneCountryBot implements PayableBot {
parse_mode: 'Markdown',
message_thread_id: ctx.message?.message_thread_id
})
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
ctx.session.analytics.sessionState = SessionState.Error
return
}
ctx.session.oneCountry.lastDomain = domain
msgId = (await ctx.reply('Checking name...')).message_id
ctx.session.analytics.firstResponseTime = process.hrtime.bigint()
ctx.session.analytics.firstResponseTime = now()
const response = await isDomainAvailable(domain)
const domainAvailable = response.isAvailable
let msg = `The name *${domain}* `
Expand All @@ -311,22 +312,22 @@ export class OneCountryBot implements PayableBot {
}
msg += `${appText.registerConfirmation}, or ${appText.registerKeepWriting}`
ctx.session.analytics.sessionState = SessionState.Success
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
if (ctx.chat?.id) {
await ctx.api.editMessageText(ctx.chat.id, msgId, msg, { parse_mode: 'Markdown' })
}
ctx.session.analytics.sessionState = SessionState.Success
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
} catch (e) {
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
await this.onError(ctx, e)
}
}

async onNotBalanceMessage (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> {
const accountId = this.payments.getAccountId(ctx as OnMessageContext)
const accountId = this.payments.getAccountId(ctx)
const account = this.payments.getUserAccount(accountId)
const addressBalance = await this.payments.getUserBalance(accountId)
const { totalCreditsAmount } = await chatService.getUserCredits(accountId)
Expand All @@ -337,7 +338,7 @@ export class OneCountryBot implements PayableBot {
.replaceAll('$WALLET_ADDRESS', account?.address ?? '')
ctx.session.analytics.sessionState = SessionState.Error
await sendMessage(ctx, balanceMessage, { parseMode: 'Markdown' }).catch(async (e) => { await this.onError(ctx, e) })
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}

onRenewCmd = async (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> => {
Expand Down Expand Up @@ -399,7 +400,7 @@ export class OneCountryBot implements PayableBot {
if (!ctx.match) {
await ctx.reply('Error: Missing 1.country domain', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
return
}
const url = getUrl(ctx.match as string)
Expand All @@ -422,12 +423,12 @@ export class OneCountryBot implements PayableBot {
)
ctx.session.analytics.sessionState = SessionState.Error
} finally {
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
} else {
await ctx.reply('This command is reserved', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
}

Expand All @@ -452,12 +453,12 @@ export class OneCountryBot implements PayableBot {
{ message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.sessionState = SessionState.Error
} finally {
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
} else {
await ctx.reply('This command is reserved', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.sessionState = SessionState.Error
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
}

Expand Down Expand Up @@ -488,7 +489,7 @@ export class OneCountryBot implements PayableBot {
await ctx.reply('This command is reserved', { message_thread_id: ctx.message?.message_thread_id })
ctx.session.analytics.sessionState = SessionState.Error
}
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}

onEnableSubomain = async (ctx: OnMessageContext): Promise<void> => {
Expand Down Expand Up @@ -544,7 +545,7 @@ export class OneCountryBot implements PayableBot {
ctx,
'Error: The bot does not have permission to send photos in chat'
)
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
} else if (ex.error_code === 429) {
this.botSuspended = true
const retryAfter = ex.parameters.retry_after
Expand All @@ -561,7 +562,7 @@ export class OneCountryBot implements PayableBot {
ctx.from.username ? ctx.from.username : ''
} Bot has reached limit, wait ${retryAfter} seconds`
).catch(async (e) => { await this.onError(ctx, e, retryCount - 1) })
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
if (method === 'editMessageText') {
ctx.session.openAi.chatGpt.chatConversation.pop() // deletes last prompt
}
Expand All @@ -577,7 +578,7 @@ export class OneCountryBot implements PayableBot {
await sendMessage(ctx, 'Error handling your request')
.catch(async (e) => { await this.onError(ctx, e, retryCount - 1) }
)
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
}
}
3 changes: 2 additions & 1 deletion src/modules/document-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type OnMessageContext, type PayableBot, type RefundCallback, SessionState } from '../types'
import * as Sentry from '@sentry/node'
import { now } from '../../utils/perf'

const SupportedDocuments = { PDF: 'application/pdf' }

Expand All @@ -21,7 +22,7 @@ export class DocumentHandler implements PayableBot {
await ctx.reply('you failed kid')
ctx.session.analytics.sessionState = SessionState.Error
} finally {
ctx.session.analytics.actualResponseTime = process.hrtime.bigint()
ctx.session.analytics.actualResponseTime = now()
}
}

Expand Down
Loading

0 comments on commit 7abfd19

Please sign in to comment.