mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
Merge pull request #3324 from gradido/speedup_transactionList
feat(backend): speedup listTransactions
This commit is contained in:
commit
1cb1dda690
@ -12,7 +12,7 @@ Decimal.set({
|
||||
})
|
||||
|
||||
const constants = {
|
||||
DB_VERSION: '0084-introduce_humhub_registration',
|
||||
DB_VERSION: '0085-add_index_transactions_user_id',
|
||||
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
|
||||
LOG4JS_CONFIG: 'log4js-config.json',
|
||||
// default log level on production should be info
|
||||
|
||||
@ -8,6 +8,8 @@ import { Resolver, Query, Ctx, Authorized } from 'type-graphql'
|
||||
import { Balance } from '@model/Balance'
|
||||
|
||||
import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { BalanceLoggingView } from '@/logging/BalanceLogging.view'
|
||||
import { DecayLoggingView } from '@/logging/DecayLogging.view'
|
||||
import { Context, getUser } from '@/server/context'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
import { calculateDecay } from '@/util/decay'
|
||||
@ -27,9 +29,15 @@ export class BalanceResolver {
|
||||
logger.addContext('user', user.id)
|
||||
logger.info(`balance(userId=${user.id})...`)
|
||||
|
||||
const gdtResolver = new GdtResolver()
|
||||
const balanceGDT = await gdtResolver.gdtBalance(context)
|
||||
logger.debug(`balanceGDT=${balanceGDT}`)
|
||||
let balanceGDT
|
||||
if (!context.balanceGDT) {
|
||||
const gdtResolver = new GdtResolver()
|
||||
balanceGDT = await gdtResolver.gdtBalance(context)
|
||||
} else {
|
||||
balanceGDT = context.balanceGDT
|
||||
}
|
||||
|
||||
logger.debug(`balanceGDT=${context.balanceGDT}`)
|
||||
|
||||
const lastTransaction = context.lastTransaction
|
||||
? context.lastTransaction
|
||||
@ -52,6 +60,7 @@ export class BalanceResolver {
|
||||
context.transactionCount || context.transactionCount === 0
|
||||
? context.transactionCount
|
||||
: await dbTransaction.count({ where: { userId: user.id } })
|
||||
|
||||
logger.debug(`transactionCount=${count}`)
|
||||
|
||||
const linkCount = await dbTransactionLink.count({
|
||||
@ -71,9 +80,9 @@ export class BalanceResolver {
|
||||
)
|
||||
logger.info(
|
||||
'calculatedDecay',
|
||||
lastTransaction.balance,
|
||||
lastTransaction.balanceDate,
|
||||
calculatedDecay,
|
||||
lastTransaction.balance.toString(),
|
||||
lastTransaction.balanceDate.toISOString(),
|
||||
new DecayLoggingView(calculatedDecay),
|
||||
)
|
||||
|
||||
// The final balance is reduced by the link amount withheld
|
||||
@ -98,7 +107,14 @@ export class BalanceResolver {
|
||||
count,
|
||||
linkCount,
|
||||
})
|
||||
logger.info('new Balance', balance, balanceGDT, count, linkCount, newBalance)
|
||||
logger.info(
|
||||
'new Balance',
|
||||
balance.toString(),
|
||||
balanceGDT?.toString(),
|
||||
count,
|
||||
linkCount,
|
||||
new BalanceLoggingView(newBalance),
|
||||
)
|
||||
|
||||
return newBalance
|
||||
}
|
||||
|
||||
@ -38,6 +38,7 @@ import { calculateBalance } from '@/util/validate'
|
||||
import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualTransactions'
|
||||
|
||||
import { BalanceResolver } from './BalanceResolver'
|
||||
import { GdtResolver } from './GdtResolver'
|
||||
import { getCommunityByIdentifier, getCommunityName, isHomeCommunity } from './util/communities'
|
||||
import { findUserByIdentifier } from './util/findUserByIdentifier'
|
||||
import { getLastTransaction } from './util/getLastTransaction'
|
||||
@ -229,8 +230,11 @@ export class TransactionResolver {
|
||||
logger.addContext('user', user.id)
|
||||
logger.info(`transactionList(user=${user.firstName}.${user.lastName}, ${user.emailId})`)
|
||||
|
||||
const gdtResolver = new GdtResolver()
|
||||
const balanceGDTPromise = gdtResolver.gdtBalance(context)
|
||||
|
||||
// find current balance
|
||||
const lastTransaction = await getLastTransaction(user.id, ['contribution'])
|
||||
const lastTransaction = await getLastTransaction(user.id)
|
||||
logger.debug(`lastTransaction=${lastTransaction}`)
|
||||
|
||||
const balanceResolver = new BalanceResolver()
|
||||
@ -319,6 +323,7 @@ export class TransactionResolver {
|
||||
|
||||
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate, transactionLinkcount } =
|
||||
await transactionLinkSummary(user.id, now)
|
||||
|
||||
context.linkCount = transactionLinkcount
|
||||
logger.debug(`transactionLinkcount=${transactionLinkcount}`)
|
||||
context.sumHoldAvailableAmount = sumHoldAvailableAmount
|
||||
@ -413,6 +418,8 @@ export class TransactionResolver {
|
||||
).toDecimalPlaces(2, Decimal.ROUND_HALF_UP)
|
||||
}
|
||||
})
|
||||
const balanceGDT = await balanceGDTPromise
|
||||
context.balanceGDT = balanceGDT
|
||||
|
||||
// Construct Result
|
||||
return new TransactionList(await balanceResolver.balance(context), transactions)
|
||||
|
||||
19
backend/src/logging/BalanceLogging.view.ts
Normal file
19
backend/src/logging/BalanceLogging.view.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { AbstractLoggingView } from '@logging/AbstractLogging.view'
|
||||
|
||||
import { Balance } from '@/graphql/model/Balance'
|
||||
|
||||
export class BalanceLoggingView extends AbstractLoggingView {
|
||||
public constructor(private self: Balance) {
|
||||
super()
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public toJSON(): any {
|
||||
return {
|
||||
balance: this.decimalToString(this.self.balance),
|
||||
balanceGDT: this.self.balanceGDT,
|
||||
count: this.self.count,
|
||||
linkCount: this.self.linkCount,
|
||||
}
|
||||
}
|
||||
}
|
||||
21
backend/src/logging/DecayLogging.view.ts
Normal file
21
backend/src/logging/DecayLogging.view.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { AbstractLoggingView } from '@logging/AbstractLogging.view'
|
||||
|
||||
import { Decay } from '@/graphql/model/Decay'
|
||||
|
||||
export class DecayLoggingView extends AbstractLoggingView {
|
||||
public constructor(private self: Decay) {
|
||||
super()
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public toJSON(): any {
|
||||
return {
|
||||
balance: this.decimalToString(this.self.balance),
|
||||
decay: this.decimalToString(this.self.decay),
|
||||
roundedDecay: this.decimalToString(this.self.roundedDecay),
|
||||
start: this.dateToString(this.self.start),
|
||||
end: this.dateToString(this.self.end),
|
||||
duration: this.self.duration,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,6 +16,7 @@ export interface Context {
|
||||
gradidoID?: string
|
||||
// hack to use less DB calls for Balance Resolver
|
||||
lastTransaction?: dbTransaction | null
|
||||
balanceGDT?: number | null
|
||||
transactionCount?: number
|
||||
linkCount?: number
|
||||
sumHoldAvailableAmount?: Decimal
|
||||
|
||||
@ -50,5 +50,5 @@ export async function downgrade(queryFn: (query: string, values?: any[]) => Prom
|
||||
await queryFn('ALTER TABLE `user_contacts` DROP COLUMN IF EXISTS `gms_publish_email`;')
|
||||
await queryFn('ALTER TABLE `user_contacts` DROP COLUMN IF EXISTS `country_code`;')
|
||||
await queryFn('ALTER TABLE `user_contacts` DROP COLUMN IF EXISTS `gms_publish_phone`;')
|
||||
await queryFn('ALTER TABLE `communities` DROP COLUMN IF EXISTS `gms_api_key`;')
|
||||
await queryFn('ALTER TABLE `communities` DROP COLUMN IF EXISTS `gms_api_key`;')
|
||||
}
|
||||
|
||||
10
database/migrations/0085-add_index_transactions_user_id.ts
Normal file
10
database/migrations/0085-add_index_transactions_user_id.ts
Normal file
@ -0,0 +1,10 @@
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||
await queryFn('CREATE INDEX user_id ON transactions (user_id);')
|
||||
}
|
||||
|
||||
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||
await queryFn('DROP INDEX user_id ON transactions;')
|
||||
}
|
||||
@ -4,7 +4,7 @@ import dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
|
||||
const constants = {
|
||||
DB_VERSION: '0084-introduce_humhub_registration',
|
||||
DB_VERSION: '0085-add_index_transactions_user_id',
|
||||
LOG4JS_CONFIG: 'log4js-config.json',
|
||||
// default log level on production should be info
|
||||
LOG_LEVEL: process.env.LOG_LEVEL ?? 'info',
|
||||
|
||||
@ -10,7 +10,7 @@ Decimal.set({
|
||||
})
|
||||
|
||||
const constants = {
|
||||
DB_VERSION: '0084-introduce_humhub_registration',
|
||||
DB_VERSION: '0085-add_index_transactions_user_id',
|
||||
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
|
||||
LOG4JS_CONFIG: 'log4js-config.json',
|
||||
// default log level on production should be info
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user