Skip to content

Commit

Permalink
chore: more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Nov 19, 2024
1 parent e95e5a9 commit 640ad09
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
3 changes: 2 additions & 1 deletion server/api/[version]/scores/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default defineEventHandler(async (event) => {
availability: tables.scores.availability,
total: tables.scores.total,
dominance: tables.scores.dominance,
dominanceRatio: tables.activity.dominanceRatio,
dominanceRatioViaBalance: tables.activity.dominanceRatioViaBalance,
dominanceRatioViaSlots: tables.activity.dominanceRatioViaSlots,
reliability: tables.scores.reliability,
reason: tables.scores.reason,
})
Expand Down
9 changes: 9 additions & 0 deletions server/api/[version]/validators/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Validator } from 'nimiq-rpc-client-ts'
import { getRpcClient } from '~~/server/lib/client'
import { mainQuerySchema } from '~~/server/utils/schemas'
import { fetchValidators } from '~~/server/utils/validators'
import { consola } from 'consola'
import { getRange } from 'nimiq-validators-trustscore'

export default defineCachedEventHandler(async (event) => {
const params = await getValidatedQuery(event, mainQuerySchema.parse)
Expand All @@ -16,6 +18,13 @@ export default defineCachedEventHandler(async (event) => {
addresses = activeValidators.map(v => v.address)
}

const range = await getRange(getRpcClient())
if (!(await checkIfScoreExistsInDb(range))) {
const { data, error } = await calculateScores(range)
if (!data || error)
consola.warn(`Error calculating scores for range ${range}`, error)
}

const { data: validators, error: errorValidators } = await fetchValidators({ ...params, addresses })
if (errorValidators || !validators)
throw createError(errorValidators)
Expand Down
2 changes: 1 addition & 1 deletion server/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const rangeQuerySchema = z.object({
export const validatorSchema = z.object({
name: z.string().optional(),
address: z.string().regex(/^NQ\d{2}(\s\w{4}){8}$/, 'Invalid Nimiq address format'),
fee: z.literal(-1).or(z.number().min(0).max(1)).default(-1),
fee: z.literal(null).or(z.number().min(0).max(1)).default(null),
payoutType: z.nativeEnum(PayoutType).default(PayoutType.None),
payoutSchedule: z.string().optional().default(''),
isMaintainedByNimiq: z.boolean().optional(),
Expand Down
29 changes: 15 additions & 14 deletions server/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result<Fe
icon: tables.validators.icon,
hasDefaultIcon: tables.validators.hasDefaultIcon,
accentColor: tables.validators.accentColor,
// score: {
// total: tables.scores.total,
// dominance: tables.scores.dominance,
// availability: tables.scores.availability,
// reliability: tables.scores.reliability,
// },
score: {
total: tables.scores.total,
dominance: tables.scores.dominance,
availability: tables.scores.availability,
reliability: tables.scores.reliability,
},
// score: sql<Score>`
// CASE
// WHEN ${tables.scores.total} IS NOT NULL THEN json_object(
Expand All @@ -189,14 +189,15 @@ export async function fetchValidators(params: FetchValidatorsOptions): Result<Fe
)`,
})
.from(tables.validators)
// .where(and(...filters))
// .leftJoin(
// tables.scores,
// and(
// eq(tables.validators.id, tables.scores.validatorId),
// isNotNull(tables.scores.total),
// ),
// )
.where(and(...filters))
.leftJoin(
tables.scores,
and(
eq(tables.validators.id, tables.scores.validatorId),
eq(tables.scores.epochNumber, latestEpoch),
isNotNull(tables.scores.total),
),
)
.leftJoin(
tables.activity,
and(
Expand Down

0 comments on commit 640ad09

Please sign in to comment.