Fix federation summary (#1180)

* Fix federation summary

* Fix federation summary

* Make it dynamic

* Make it dynamic
This commit is contained in:
KoalaSat 2024-03-15 21:06:54 +01:00 committed by GitHub
parent b8fd2e21cd
commit 0e049eec43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 92 deletions

View File

@ -1,4 +1,4 @@
import React, { useContext, useMemo } from 'react';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
@ -34,11 +34,14 @@ interface Props {
const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => {
const { t } = useTranslation();
const { federation } = useContext(FederationContext);
const { federation, coordinatorUpdatedAt, federationUpdatedAt } = useContext(FederationContext);
const [loadingProgress, setLoadingProgress] = useState<number>(0);
const loadingProgress = useMemo(() => {
return (federation.exchange.onlineCoordinators / federation.exchange.totalCoordinators) * 100;
}, [federation.exchange.onlineCoordinators, federation.exchange.totalCoordinators]);
useEffect(() => {
const loadedCoordinators =
federation.exchange.enabledCoordinators - federation.exchange.loadingCoordinators;
setLoadingProgress((loadedCoordinators / federation.exchange.enabledCoordinators) * 100);
}, [open, coordinatorUpdatedAt, federationUpdatedAt]);
return (
<Dialog open={open} onClose={onClose}>
@ -69,7 +72,7 @@ const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => {
</ListItemIcon>
<ListItemText
primary={federation.exchange.totalCoordinators}
primary={federation.exchange.enabledCoordinators}
secondary={t('Enabled RoboSats coordinators')}
/>
</ListItem>

View File

@ -12,19 +12,17 @@ interface ExchangeInfo {
version: Version;
}
const defaultExchangeInfo: ExchangeInfo = {
num_public_buy_orders: 0,
num_public_sell_orders: 0,
book_liquidity: 0,
active_robots_today: 0,
last_day_nonkyc_btc_premium: 0,
last_day_volume: 0,
lifetime_volume: 0,
version: { major: 0, minor: 0, patch: 0 },
};
export const updateExchangeInfo = (federation: Federation): ExchangeInfo => {
const info: ExchangeInfo = defaultExchangeInfo;
const info: ExchangeInfo = {
num_public_buy_orders: 0,
num_public_sell_orders: 0,
book_liquidity: 0,
active_robots_today: 0,
last_day_nonkyc_btc_premium: 0,
last_day_volume: 0,
lifetime_volume: 0,
version: { major: 0, minor: 0, patch: 0 },
};
const premiums: number[] = [];
const volumes: number[] = [];
let highestVersion: Version = { major: 0, minor: 0, patch: 0 };
@ -38,19 +36,20 @@ export const updateExchangeInfo = (federation: Federation): ExchangeInfo => {
'lifetime_volume',
];
Object.values(federation.coordinators).forEach((coordinator, index) => {
if (coordinator.info !== undefined && coordinator.enabled === true) {
premiums[index] = coordinator.info.last_day_nonkyc_btc_premium;
volumes[index] = coordinator.info.last_day_volume;
highestVersion = getHigherVer(highestVersion, coordinator.info.version);
active_robots_today = Math.max(active_robots_today, coordinator.info.active_robots_today);
Object.values(federation.coordinators)
.filter((coor) => coor.isUpdated())
.forEach((coordinator, index) => {
if (coordinator.info !== undefined) {
premiums[index] = coordinator.info.last_day_nonkyc_btc_premium;
volumes[index] = coordinator.info.last_day_volume;
highestVersion = getHigherVer(highestVersion, coordinator.info.version);
active_robots_today = Math.max(active_robots_today, coordinator.info.active_robots_today);
aggregations.forEach((key: any) => {
info[key] = Number(info[key]) + Number(coordinator.info[key]);
});
}
return null;
});
aggregations.forEach((key: any) => {
info[key] = Number(info[key]) + Number(coordinator.info[key]);
});
}
});
info.last_day_nonkyc_btc_premium = weightedMean(premiums, volumes);
info.version = highestVersion;
@ -68,7 +67,16 @@ export interface Exchange {
}
export const defaultExchange: Exchange = {
info: defaultExchangeInfo,
info: {
num_public_buy_orders: 0,
num_public_sell_orders: 0,
book_liquidity: 0,
active_robots_today: 0,
last_day_nonkyc_btc_premium: 0,
last_day_volume: 0,
lifetime_volume: 0,
version: { major: 0, minor: 0, patch: 0 },
},
enabledCoordinators: 0,
onlineCoordinators: 0,
loadingCoordinators: 0,

View File

@ -65,10 +65,8 @@ export class Federation {
this.exchange.loadingCoordinators =
this.exchange.loadingCoordinators < 1 ? 0 : this.exchange.loadingCoordinators - 1;
this.loading = this.exchange.loadingCoordinators > 0;
if (Object.values(this.coordinators).every((coor) => coor.isUpdated())) {
this.updateExchange();
this.triggerHook('onFederationUpdate');
}
this.updateExchange();
this.triggerHook('onFederationUpdate');
};
// Setup
@ -98,6 +96,16 @@ export class Federation {
update = async (): Promise<void> => {
this.loading = true;
this.exchange.info = {
num_public_buy_orders: 0,
num_public_sell_orders: 0,
book_liquidity: 0,
active_robots_today: 0,
last_day_nonkyc_btc_premium: 0,
last_day_volume: 0,
lifetime_volume: 0,
version: { major: 0, minor: 0, patch: 0 },
};
this.exchange.loadingCoordinators = Object.keys(this.coordinators).length;
for (const coor of Object.values(this.coordinators)) {
await coor.update(() => {
@ -151,6 +159,7 @@ export class Federation {
this.exchange.enabledCoordinators = Object.values(this.coordinators).filter(
(c) => c.enabled,
).length;
this.triggerHook('onFederationUpdate');
};
}

View File

@ -1,5 +1,3 @@
import { type Coordinator } from '../models';
interface Version {
major: number | null;
minor: number | null;
@ -61,58 +59,4 @@ const getHigherVer = (ver0: Version, ver1: Version): Version => {
}
};
export const aggregateInfo = (federation: Coordinator[]): AggregatedInfo => {
const info = {
onlineCoordinators: 0,
totalCoordinators: 0,
num_public_buy_orders: 0,
num_public_sell_orders: 0,
book_liquidity: 0,
active_robots_today: 0,
last_day_nonkyc_btc_premium: 0,
last_day_volume: 0,
lifetime_volume: 0,
version: { major: 0, minor: 0, patch: 0 },
};
info.totalCoordinators = federation.length;
const addUp: toAdd[] = [
'num_public_buy_orders',
'num_public_sell_orders',
'book_liquidity',
'active_robots_today',
'last_day_volume',
'lifetime_volume',
];
addUp.map((key) => {
let value = 0;
federation.map((coordinator) => {
if (coordinator.info != null) {
value = value + coordinator.info[key];
}
return null;
});
info[key] = value;
return null;
});
const premiums: number[] = [];
const volumes: number[] = [];
let highestVersion = { major: 0, minor: 0, patch: 0 };
federation.map((coordinator, index) => {
if (coordinator.info != null) {
info.onlineCoordinators = info.onlineCoordinators + 1;
premiums[index] = coordinator.info.last_day_nonkyc_btc_premium;
volumes[index] = coordinator.info.last_day_volume;
highestVersion = getHigherVer(highestVersion, coordinator.info.version);
}
return null;
});
info.last_day_nonkyc_btc_premium = weightedMean(premiums, volumes);
info.version = highestVersion;
return info;
};
export default aggregateInfo;
export default getHigherVer;