Skip to content

Commit

Permalink
chore: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 16, 2024
1 parent 6aea349 commit 64207c4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 57 deletions.
3 changes: 0 additions & 3 deletions app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
const colorMode = useColorMode()
const toggleDark = () => colorMode.value = colorMode.value === 'light' ? 'dark' : 'light'
const validatorsStore = useValidatorsStore()
await callOnce(validatorsStore.fetchValidators)
const route = useRoute()
const validatorDetail = computed(() => !!route.params.address)
Expand Down
2 changes: 1 addition & 1 deletion app/components/Donut.client.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { VisSingleContainer, VisDonut, VisTooltip } from '@unovis/vue'
import { Donut } from '@unovis/ts'
import type { Validator } from '~~/server/api/validators/index.get';
import type { Validator } from '~~/server/api/vts/index.get';
import ScorePies from './ScorePies.vue';
import { render, toDisplayString } from 'vue'
Expand Down
2 changes: 1 addition & 1 deletion app/pages/health.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
const { data: health, status, error } = useFetch('/api/health')
const { data: health, status, error } = useFetch('/api/vts/health')
const network = useRuntimeConfig().public.nimiqNetwork
</script>

Expand Down
20 changes: 8 additions & 12 deletions app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<script setup lang="ts">
const { validators, averageScore } = storeToRefs(useValidatorsStore())
const { data } = useFetch('/api/vts')
const validators = computed(() => data.value?.validators || [])
const { data: validatorsInEpoch } = useFetch('/api/_hub/database/query', {
method: 'POST',
body: { query: 'SELECT COUNT(*) AS total FROM scores' },
transform: (res) => res.results.at(0)?.total,
})
const { data: validatorsInDb } = useFetch('/api/_hub/database/query', {
method: 'POST',
body: { query: 'SELECT COUNT(*) AS total FROM validators' },
transform: (res) => res.results.at(0)?.total,
const averageScore = computed(() => {
if (!validators.value?.length) return 0
const scores = validators.value.map(validator => validator.total).filter(t => !!t) as number[]
const totalScore = scores?.reduce((acc, score) => acc + score, 0) || 0
return totalScore / validators.value.length
})
</script>

<template>
<div flex="~ col" pt-64 pb-128>
<div flex="~ wrap gap-96 justify-center" of-x-auto mx--32 px-32 pb-64>
<Stat text-green>
<template #value>{{ validatorsInEpoch }} <span text="18 neutral-700">/ {{ validatorsInDb }}</span></template>
<template #value>{{ validators?.length }}</template>
<template #description>Validators</template>
</Stat>
<Stat text-blue>
Expand Down
3 changes: 2 additions & 1 deletion app/pages/validator/[address].vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script setup lang="ts">
import { ValidatorTag } from '~~/server/utils/drizzle'
const { validators } = storeToRefs(useValidatorsStore())
const { data } = useFetch('/api/vts')
const validators = computed(() => data.value?.validators || [])
const route = useRoute()
const validator = computed(() => {
Expand Down
16 changes: 0 additions & 16 deletions app/stores/validators.ts

This file was deleted.

33 changes: 20 additions & 13 deletions pnpm-lock.yaml

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

11 changes: 5 additions & 6 deletions server/api/health.get.ts → server/api/vts/health.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,27 @@ export interface HealthStatus {


export default defineEventHandler(async (event) => {
const db = await useDrizzle();

const rpcClient = await getRpcClient();
const rpcClient = await getRpcClient()

// Get the latest epoch number in the activity table
const latestActivityBlock = await db
const latestActivityBlock = await useDrizzle()
.select({ epoch: max(tables.activity.epochBlockNumber) })
.from(tables.activity)
.get()
.then((row) => row?.epoch ?? -1);

const { data: latestFetchedEpoch, error: errorLatestFetchedEpoch } = await rpcClient.policy.getEpochAt(latestActivityBlock)
if (errorLatestFetchedEpoch)
throw errorLatestFetchedEpoch;

// Get the total number of validators
const totalValidators = await db
const totalValidators = await useDrizzle()
.select({ count: count(tables.validators.id) })
.from(tables.validators)
.get()
.then((row) => row?.count ?? 0);

const fetchedEpochs = await db
const fetchedEpochs = await useDrizzle()
.selectDistinct({ epoch: tables.activity.epochBlockNumber })
.from(tables.activity)
.orderBy(tables.activity.epochBlockNumber)
Expand Down
18 changes: 14 additions & 4 deletions server/api/validators/index.get.ts → server/api/vts/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { desc, eq, isNotNull, not } from 'drizzle-orm';
import { desc, eq, isNotNull, max } from 'drizzle-orm';

export interface Validator {
id: number
Expand All @@ -17,7 +17,7 @@ export interface Validator {
}

export default defineEventHandler(async (event) => {
const data = await useDrizzle()
const validators = await useDrizzle()
.select({
id: tables.validators.id,
name: tables.validators.name,
Expand All @@ -38,8 +38,18 @@ export default defineEventHandler(async (event) => {
.where(isNotNull(tables.scores.validatorId))
.groupBy(tables.validators.id)
.orderBy(desc(tables.scores.total))
.all()
.all() as Validator[]

const rpcClient = await getRpcClient()

const epochBlockNumber = await useDrizzle()
.select({ epoch: max(tables.activity.epochBlockNumber) })
.from(tables.activity)
.get().then(row => row?.epoch ?? -1)

const { data: epochNumber, error: epochNumberError } = await rpcClient.policy.getEpochAt(epochBlockNumber)
if (epochNumberError) throw epochNumberError

setResponseStatus(event, 200)
return data as Validator[]
return { validators, epochNumber } as const
})

0 comments on commit 64207c4

Please sign in to comment.