From f133200c7f2ce6198fb9649aa7254cf222943d6d Mon Sep 17 00:00:00 2001 From: Sam Keen Date: Fri, 16 Dec 2022 12:25:20 +0000 Subject: [PATCH] fix(2380): fixed block duration field to account for the new api format (#2423) * fix(2380): fixed block duration field to account for the new api format * fix(2380): removed unneeded import * fix(2380): a little more hardened against duration being undefined and different environments returning different data * fix(2380): improved comment --- apps/stats/.env.stagnet3 | 2 +- libs/network-stats/src/config/stats-fields.ts | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/apps/stats/.env.stagnet3 b/apps/stats/.env.stagnet3 index 44d96d55f..e0f1b7159 100644 --- a/apps/stats/.env.stagnet3 +++ b/apps/stats/.env.stagnet3 @@ -1,3 +1,3 @@ # App configuration variables NX_VEGA_URL=https://api.stagnet3.vega.xyz/graphql -NX_VEGA_ENV=STAGNET3; +NX_VEGA_ENV=STAGNET3 diff --git a/libs/network-stats/src/config/stats-fields.ts b/libs/network-stats/src/config/stats-fields.ts index 55cb003f7..62e10189e 100644 --- a/libs/network-stats/src/config/stats-fields.ts +++ b/libs/network-stats/src/config/stats-fields.ts @@ -110,9 +110,30 @@ export const statsFields: { [key in keyof Stats]: StatFields[] } = { blockDuration: [ { title: t('Block time'), - formatter: (duration: number) => (duration / 1000000000).toFixed(3), - goodThreshold: (blockDuration: number) => - blockDuration > 0 && blockDuration <= 2000000000, + formatter: (duration: string) => { + const dp = 3; + if (duration?.includes('ms')) { + return (parseFloat(duration) / 1000).toFixed(dp).toString(); + } else if (duration?.includes('s')) { + return parseFloat(duration).toFixed(dp).toString(); + } + return duration + ? (Number(duration) / 1000000000).toFixed(dp) + : undefined; + }, + goodThreshold: (blockDuration: string) => { + if (blockDuration?.includes('ms')) { + // we only get ms from the api if duration is less than 1s, so this + // automatically passes + return true; + } else if (blockDuration?.includes('s')) { + return parseFloat(blockDuration) <= 2; + } else { + return ( + Number(blockDuration) > 0 && Number(blockDuration) <= 2000000000 + ); + } + }, description: t('Seconds between the two most recent blocks'), }, ],