Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 19, 2024
1 parent fe7ea4c commit 2c130cf
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions server/api/vts/health.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { max, count, } from 'drizzle-orm';
import { NimiqRPCClient } from 'nimiq-rpc-client-ts';
import { Range, getRange } from 'nimiq-vts';
import { getMissingEpochs } from '~~/server/database/utils';
import { consola } from 'consola'

export enum HealthFlag {
MissingEpochs = 'missing-epochs',
Expand All @@ -24,28 +25,30 @@ export interface HealthStatus {
flags: HealthFlag[]
}

function err(error: any) {
consola.error(error)
return createError(error)
}

export default defineEventHandler(async (event) => {
console.log('GET /vts/health')
consola.info('GET /vts/health')
const url = useRuntimeConfig().rpcUrl
if (!url)
return createError('Missing RPC URL in runtime config');
console.log('RPC URL:', url)
if (!url) return err('Missing RPC URL in runtime config');
consola.info('RPC URL:', url)
const rpcClient = new NimiqRPCClient(new URL(url))
console.log('RPC Client:', rpcClient)
consola.info('RPC Client:', rpcClient)

// Get the latest epoch number in the activity table
const latestActivityBlock = await useDrizzle()
.select({ epoch: max(tables.activity.epochBlockNumber) })
.from(tables.activity)
.get()
.then((row) => row?.epoch ?? -1);
console.log('Latest Activity Block:', latestActivityBlock)
consola.info('Latest Activity Block:', latestActivityBlock)

const { data: latestFetchedEpoch, error: errorLatestFetchedEpoch } = await rpcClient.policy.getEpochAt(latestActivityBlock)
if (errorLatestFetchedEpoch)
return createError(errorLatestFetchedEpoch);
console.log('Latest Fetched Epoch:', latestFetchedEpoch)
if (errorLatestFetchedEpoch) return err(errorLatestFetchedEpoch);
consola.info('Latest Fetched Epoch:', latestFetchedEpoch)
// const latestFetchedEpoch = latestActivityBlock

// Get the total number of validators
Expand All @@ -54,33 +57,31 @@ export default defineEventHandler(async (event) => {
.from(tables.validators)
.get()
.then((row) => row?.count ?? 0)
console.log('Total Validators:', totalValidators)
consola.info('Total Validators:', totalValidators)

const fetchedEpochs = await useDrizzle()
.selectDistinct({ epoch: tables.activity.epochBlockNumber })
.from(tables.activity)
.orderBy(tables.activity.epochBlockNumber)
.all()
.then((rows) => rows.map(row => row.epoch));
console.log('Fetched Epochs:', fetchedEpochs)
consola.info('Fetched Epochs:', fetchedEpochs)


const { data: headBlockNumber, error: errorHeadBlockNumber } = await rpcClient.blockchain.getBlockNumber()
if (errorHeadBlockNumber)
return createError(errorHeadBlockNumber)
console.log('Head Block Number:', headBlockNumber)
if (errorHeadBlockNumber) return err(errorHeadBlockNumber)
consola.info('Head Block Number:', headBlockNumber)
const { data: currentEpoch, error: errorCurrentEpoch } = await rpcClient.blockchain.getEpochNumber()
if (errorCurrentEpoch)
return createError(errorCurrentEpoch)
console.log('Current Epoch:', currentEpoch)
if (errorCurrentEpoch) return err(errorCurrentEpoch)
consola.info('Current Epoch:', currentEpoch)

const range = await getRange(rpcClient);
console.log('Range:', range)
consola.info('Range:', range)
const missingEpochs = await getMissingEpochs(range);
console.log('Missing Epochs:', missingEpochs)
consola.info('Missing Epochs:', missingEpochs)

const isSynced = missingEpochs.length === 0;
console.log('Is Synced:', isSynced)
consola.info('Is Synced:', isSynced)
const flags: HealthFlag[] = []
if (!isSynced) flags.push(HealthFlag.MissingEpochs)

Expand All @@ -96,7 +97,7 @@ export default defineEventHandler(async (event) => {
flags,
fetchedEpochs,
};
// console.log('Health Status:', healthStatus)
consola.info('Health Status:', healthStatus)

// Return the health status
setResponseStatus(event, 200);
Expand Down

0 comments on commit 2c130cf

Please sign in to comment.