This commit is contained in:
boufni95 2025-02-05 18:12:23 +00:00
parent ea28cbc1b5
commit d2c3eca1f6

View file

@ -31,6 +31,7 @@ export type TxPointSettings = {
export class StateBundler { export class StateBundler {
tlvStorage: TlvFilesStorage tlvStorage: TlvFilesStorage
reportLog = getLogger({ component: 'stateBundlerReport' }) reportLog = getLogger({ component: 'stateBundlerReport' })
prevValues: Record<string, number> = {}
constructor(settings: StorageSettings) { constructor(settings: StorageSettings) {
const bundlerPath = [settings.dataDir, "bundler_events"].filter(s => !!s).join("/") const bundlerPath = [settings.dataDir, "bundler_events"].filter(s => !!s).join("/")
this.tlvStorage = new TlvFilesStorage(bundlerPath) this.tlvStorage = new TlvFilesStorage(bundlerPath)
@ -53,45 +54,38 @@ export class StateBundler {
}) })
return metrics return metrics
} }
/* increment = (key: string, value: number) => { AddValue = (appId: string, key: string, v: number) => {
this.sinceStart[key] = (this.sinceStart[key] || 0) + value const prevValueKey = `${appId}_${key}`
this.sinceLatestReport[key] = (this.sinceLatestReport[key] || 0) + value if (this.prevValues[prevValueKey] === v) {
this.triggerReportCheck() return
} }
set = (key: string, value: number) => { this.prevValues[prevValueKey] = v
this.sinceStart[key] = value this.tlvStorage.AddTlv(appId, key, this.serializeNow(v))
this.sinceLatestReport[key] = value }
this.triggerReportCheck()
}
max = (key: string, value: number) => {
this.sinceStart[key] = Math.max(this.sinceStart[key] || 0, value)
this.sinceLatestReport[key] = Math.max(this.sinceLatestReport[key] || 0, value)
this.triggerReportCheck()
} */
AddTxPoint = (actionName: TransactionStatePointType, v: number, settings: TxPointSettings, appId = '__root') => { AddTxPoint = (actionName: TransactionStatePointType, v: number, settings: TxPointSettings, appId = '_root') => {
const { used, from } = settings const { used, from } = settings
const meta = settings.meta || [] const meta = settings.meta || []
const key = [actionName, from, used, ...meta].join('_') const key = [actionName, from, used, ...meta].join('_')
this.tlvStorage.AddTlv(appId, key, this.serializeNow(v)) this.AddValue(appId, key, v)
} }
AddTxPointFailed = (actionName: TransactionStatePointType, v: number, settings: TxPointSettings, appId = '__root') => { AddTxPointFailed = (actionName: TransactionStatePointType, v: number, settings: TxPointSettings, appId = '_root') => {
const { used, from } = settings const { used, from } = settings
const meta = settings.meta || [] const meta = settings.meta || []
const key = [actionName, from, used, ...meta, 'failed'].join('_') const key = [actionName, from, used, ...meta, 'failed'].join('_')
this.tlvStorage.AddTlv(appId, key, this.serializeNow(v)) this.AddValue(appId, key, v)
} }
AddBalancePoint = (actionName: BalanceStatePointType, v: number, meta = [], appId = '__root') => { AddBalancePoint = (actionName: BalanceStatePointType, v: number, meta = [], appId = '_root') => {
const key = [actionName, ...meta].join('_') const key = [actionName, ...meta].join('_')
this.tlvStorage.AddTlv(appId, key, this.serializeNow(v)) this.AddValue(appId, key, v)
} }
AddMaxPoint = (actionName: MaxStatePointType, v: number, meta = [], appId = '__root') => { AddMaxPoint = (actionName: MaxStatePointType, v: number, meta = [], appId = '_root') => {
const key = [actionName, ...meta].join('_') const key = [actionName, ...meta].join('_')
this.tlvStorage.AddTlv(appId, key, this.serializeNow(v)) this.AddValue(appId, key, v)
} }
serializeNow = (v: number) => { serializeNow = (v: number) => {