Skip to content

Commit

Permalink
add more data to aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Jan 24, 2025
1 parent 5f0590d commit 3bf9774
Show file tree
Hide file tree
Showing 38 changed files with 183 additions and 1,122 deletions.
2 changes: 1 addition & 1 deletion apps/gui/src/lib/components/lists/table/DataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
}
</script>
<!-- <pre>{JSON.stringify($tableData, null, 2)}</pre> -->
<!-- <pre>{JSON.stringify($config.filtersShow, null, 2)}</pre> -->

<!-- **UI Layout with Resizable Panes** -->
<Resizable.PaneGroup direction="horizontal" class="min-h-[100%]">
Expand Down
6 changes: 5 additions & 1 deletion apps/gui/src/lib/components/partials/Debugger.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import { route66 } from "$lib/stores";
import { Value } from "svelte-radix";
import { shouldSync as _shouldSync } from "$lib/stores/app";
import { eventsStoreMemoryRelay } from "$lib/stores/memory-relays/memory-relay-events";
import { calculateSize, type ObjectSizeType } from "$lib/utils/cache";
const debug: Writable<Map<string, any>> = writable(new Map());
Expand Down Expand Up @@ -104,10 +106,12 @@
const debugStores = () => {
const eventKeys = Array.from($events?.keys?.()) ?? [];
const eventsArray = Array.from(eventKeys);
const measure: ObjectSizeType = calculateSize($events)
addDebug('store:events', eventKeys?.length || 0);
addDebug('store:events:size', `${measure.size.toFixed(2)}${measure.unit}`);
[0,1,3,10002,10166,30166].forEach( kind => {
[0,1,3,10002,10166, 30166].forEach( kind => {
addDebug(`store:events:${kind}`, eventsArray.filter( (key: string) => {
const parts = key.split(':');
return parts[1] === kind.toString();
Expand Down
7 changes: 3 additions & 4 deletions apps/gui/src/lib/components/partials/MonitorActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@
onMount(() => {
toggleEnableMonitor = async () => {
disabled.set(true);
const { publishEventsToMemoryRelay } = await import('$lib/stores/events-helpers.js');
const resumer = await pauseLiveSync();
disabled.set(true);
if(monitor?.enabled) {
monitor.disable();
([...$eventsArray] as IEvent[]).filter( event => event.pubkey === monitor.pubkey).forEach( event => {
const key = eventKey(event);
$events.delete(key)
});
events.set($events);
disabled.set(false);
await resumer();
disabled.set(false);
}
else {
monitor.enable()
Expand All @@ -62,8 +61,8 @@
publishEventsToMemoryRelay(events)
}
await $route66?.services?.monitors?.sync(options, { onevents })
disabled.set(false);
await resumer();
disabled.set(false);
}
$route66?.services?.monitors?.manager?.updateMonitor?.(monitor)
Expand Down
9 changes: 7 additions & 2 deletions apps/gui/src/lib/config/dataTable/geocodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getCountryName } from '$lib/stores/iso3166';
export const columnsDisable: DataKeys = ['id']
export const filtersDisable: DataKeys = []

export const columnsShow: DataKeys = ['geocode', 'count', 'percent']
export const filtersShow: DataKeys = []
export const columnsShow: DataKeys = ['geocode', 'count', 'percent', 'softwaresCount']
export const filtersShow: DataKeys = ['count', 'softwaresCount', 'softwares']

export const humanReadableNames: NameFormatter = {};

Expand Down Expand Up @@ -46,6 +46,11 @@ export const filterFormatters: Formatters = {
if(typeof software !== 'string') return '-';
software = makeSoftwareReadable(software);
return truncateWithEllipsis(software, 33);
},
softwares: (software: string) => {
if(typeof software !== 'string') return '-';
software = makeSoftwareReadable(software);
return truncateWithEllipsis(software, 33);
}
}

Expand Down
4 changes: 2 additions & 2 deletions apps/gui/src/lib/config/dataTable/isps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getCountryName } from '$lib/stores/iso3166';
export const columnsDisable: DataKeys = ['id']
export const filtersDisable: DataKeys = []

export const columnsShow: DataKeys = ['prettyName', 'asname', 'as', 'count', 'percent']
export const filtersShow: DataKeys = []
export const columnsShow: DataKeys = ['prettyName', 'asname', 'as', 'count', 'percent', 'softwaresCount']
export const filtersShow: DataKeys = ['softwares']

export const humanReadableNames: NameFormatter = {};

Expand Down
25 changes: 0 additions & 25 deletions apps/gui/src/lib/config/dataTable/monitors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { relaySpeedGroupResolver, SpeedGroupBars, SpeedGroupColors, SpeedGroups } from '$lib/stores/checks.js';
import { inactiveDisabledMonitorChecksCount, monitors, monitorsMap } from '$lib/stores/monitors.js';
import type { Monitor } from "@nostrwatch/route66/models"
import { PFP } from '$lib/utils/pfp.js';
Expand All @@ -7,30 +6,6 @@ import { formatSeconds, timeAgo } from '$lib/utils/time.js';
import { validNip05s } from '$lib/stores/nip05s.js';
import { activeMonitorChecksCount } from '$lib/stores';

type Resolver = (input: any) => any

class SpeedGroupResolver {
private resolver: Resolver = () => SpeedGroups.Mid;
private unsubscribe: () => void;

constructor() {
this.unsubscribe = relaySpeedGroupResolver.subscribe((fn: Resolver) => {
this.resolver = fn;
});
}

resolve(input: number): SpeedGroups {
return this.resolver(input);
}

dispose() {
this.unsubscribe();
}
}

const speedGroupResolver = new SpeedGroupResolver();


export type NameFormatter = Record<string, string>;

export type Formatters = Record<string, Formatter>;
Expand Down
14 changes: 10 additions & 4 deletions apps/gui/src/lib/config/dataTable/operators.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { DataKeys, Formatters, NameFormatter } from '$lib/components/lists/table/DataTableTypes';
import { makeSoftwareReadable } from '$lib/synonyms/software';

export const columnsDisable: DataKeys = []
export const filtersDisable: DataKeys = []

export const columnsShow: DataKeys = ['name', 'about', 'reference', 'relaysCount', 'softwaresCount', 'ispsCount']
export const filtersShow: DataKeys = []
export const filtersShow: DataKeys = ['softwares', 'isps']

export const columnsDisable: DataKeys = []
export const filtersDisable: DataKeys = []

export const humanReadableNames: NameFormatter = {};

Expand Down Expand Up @@ -56,7 +57,12 @@ export const filterFormatters: Formatters = {
if(typeof software !== 'string') return '-';
software = makeSoftwareReadable(software);
return truncateWithEllipsis(software, 33);
}
},
softwares: (software: string) => {
if(typeof software !== 'string') return '-';
software = makeSoftwareReadable(software);
return truncateWithEllipsis(software, 33);
},
}

export const tableRowStyler = (row: Record<string, any>) => {
Expand Down
26 changes: 23 additions & 3 deletions apps/gui/src/lib/stores/geocodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StateManager } from '@nostrwatch/route66';
import { Nip66CheckEvent } from '@nostrwatch/route66/models';
import type { lte } from 'lodash';
import { doAggregateCache } from './app.js';
import softwares from '../config/dataTable/softwares.js';

export const geocodes: Readable<string[]> = derived(
relayCheckAggregates,
Expand Down Expand Up @@ -69,17 +70,36 @@ export const relaysByGeo: Readable<Map<string, string[]>> = derived(
return relaysByGeo;
})

export const softwaresByGeo: Readable<Map<string, string[]>> = derived(
[relayCheckAggregates],
([$relayCheckAggregates]) => {
const softwaresByGeo: Map<string, string[] | Set<string>> = new Map();
$relayCheckAggregates.forEach((relayCheck) => {
const geocode = relayCheck?.geocode || 'unknown';
const softwares = softwaresByGeo.get(geocode) || new Set();
if((softwares as Set<string>).has(relayCheck.software)) return;
(softwares as Set<string>).add(relayCheck.software);
softwaresByGeo.set(geocode, softwares);
});
softwaresByGeo.forEach((softwares, geocode) => {
softwaresByGeo.set(geocode, Array.from(softwares));
})
return softwaresByGeo as Map<string, string[]>;
})

export const geoRows = derived(
[geocodes, geocodeCounts, geocodePercentages, relaysByGeo],
([$geocodes, $geocodeCounts, $geocodePercentages, $relaysByGeo]) => {
[geocodes, geocodeCounts, geocodePercentages, relaysByGeo, softwaresByGeo],
([$geocodes, $geocodeCounts, $geocodePercentages, $relaysByGeo, $softwaresByGeo]) => {
const rows: Record<string, any> = [];
$geocodes.forEach((geocode: string) => {
const id = geocode;
const count = $geocodeCounts.get(geocode) || 0;
const percent = $geocodePercentages.get(geocode) || 0;
const relays = $relaysByGeo.get(geocode) || [];
const relaysCount = relays.length || 0;
rows.push({ id, geocode, count, percent, relays, relaysCount });
const softwares = $softwaresByGeo.get(geocode) || [];
const softwaresCount = softwares.length || 0;
rows.push({ id, geocode, count, percent, relays, relaysCount, softwares, softwaresCount });
});
return rows;
}
Expand Down
30 changes: 27 additions & 3 deletions apps/gui/src/lib/stores/isps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { eventsArray } from './events.js';
import { relayCheckAggregates, relayChecks } from './checks.js';
import { StateManager } from '@nostrwatch/route66';
import { doAggregateCache } from './app.js';
import softwares from '../config/dataTable/softwares.js';

export type StoreIsp = {
title: string;
Expand Down Expand Up @@ -69,20 +70,43 @@ export const ispPercentages = derived(ispCounts, ($ispCounts) => {
return percentages;
});

export const softwaresByIsp: Readable<Map<string, string[]>> = derived(
[relayCheckAggregates],
([$relayCheckAggregates]) => {
const softwaresByIsp: Map<string, string[] | Set<string>> = new Map();
$relayCheckAggregates.forEach((relayCheck) => {
const isp = relayCheck?.isp || 'unknown';
const softwares = softwaresByIsp.get(isp) || new Set();
if((softwares as Set<string>).has(relayCheck.software)) return;
(softwares as Set<string>).add(relayCheck.software);
softwaresByIsp.set(isp, softwares);
});
console.log(softwaresByIsp, 'softwaresByIsp')
softwaresByIsp.forEach((softwares, isp) => {
softwaresByIsp.set(isp, Array.from(softwares));
})
return softwaresByIsp as Map<string, string[]>;
}
);

export const ispRows = derived(
[isps, ispCounts, ispPercentages],
([$isps, $ispCounts, $ispPercentages]) => {
[isps, ispCounts, ispPercentages, softwaresByIsp],
([$isps, $ispCounts, $ispPercentages, $softwaresByIsp]) => {
const rows = $isps.map((isp) => {
const count = $ispCounts.get(isp.title) || 0;
const percent = $ispPercentages.get(isp.title) || 0;
const softwares = $softwaresByIsp.get(isp.title) || [];
const softwaresCount = softwares?.length;
const { title:prettyName, asname, as } = isp;
return {
id: as,
prettyName,
asname,
as,
count,
percent
percent,
softwares,
softwaresCount
};
});
return rows;
Expand Down
57 changes: 57 additions & 0 deletions apps/gui/src/lib/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,61 @@ export const getLocalStorageUsage = (maxSizeMB: number = 5): LocalStorageUsage =
maxSizeMB,
percentageUsed,
};
}

export type ObjectSizeType = { size: number, unit: 'bytes' | 'KB' | 'MB' }

export const calculateSize = (input: any): ObjectSizeType => {
function getSizeInBytes(value: any): number {
const objectList = new Set();
const stack = [value];
let bytes = 0;

while (stack.length) {
const currentValue = stack.pop();

if (currentValue === null || currentValue === undefined) {
bytes += 0;
} else if (typeof currentValue === 'boolean') {
bytes += 4;
} else if (typeof currentValue === 'string') {
bytes += currentValue.length * 2;
} else if (typeof currentValue === 'number') {
bytes += 8;
} else if (typeof currentValue === 'object') {
if (!objectList.has(currentValue)) {
objectList.add(currentValue);
for (const key in currentValue) {
if (currentValue.hasOwnProperty(key)) {
bytes += key.length * 2;
stack.push(currentValue[key]);
}
}

if (currentValue instanceof Map) {
currentValue.forEach((v, k) => {
stack.push(k);
stack.push(v);
});
} else if (currentValue instanceof Set) {
currentValue.forEach(v => stack.push(v));
} else if (Array.isArray(currentValue)) {
stack.push(...currentValue);
}
}
}
}

return bytes;
}

const bytes = getSizeInBytes(input);

if (bytes >= 1024 * 1024) {
return { size: bytes / (1024 * 1024), unit: 'MB' };
} else if (bytes >= 1024) {
return { size: bytes / 1024, unit: 'KB' };
} else {
return { size: bytes, unit: 'bytes' };
}
}
3 changes: 1 addition & 2 deletions apps/gui/src/lib/utils/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ export const bootstrapMonitorChecks = async () => {
export const bootstrap = async () => {
//console.log('bootstrap')
if(!$route66){
//console.log('bootstrap:no instance')
$route66 = await instance();
}
await $route66.ready();
//console.log('bootstrap:ready')
bindBootstrapEmitters();
const onevents = (events: IEvent[]) => {
for(const event of events){
Expand Down Expand Up @@ -339,6 +337,7 @@ export const seedMetaFromCache = async () => {
if(!$route66) {
$route66 = await instance();
}
await $route66.ready();
if(!$route66) return;
if(get(isSeeded)) return;
if(!hasBeenBoostrapped()) return;
Expand Down
6 changes: 3 additions & 3 deletions apps/gui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { doBootstrap } from '$lib/stores/routines.js';
import Header from '$lib/components/layout/Header.svelte';
import { instance, bootstrap, seedChecksFromCache } from '$lib/utils/lifecycle';
import { instance, bootstrap, seedFromCache } from '$lib/utils/lifecycle';
import { writable, type Writable, get } from 'svelte/store';
import { navigating } from '$app/stores';
import type Route66 from '@nostrwatch/route66';
Expand Down Expand Up @@ -77,7 +77,7 @@
setTabState('leader');
try {
await boot();
seedChecksFromCache()
seedFromCache()
//console.log('Leader tab: DB initialized.');
} catch (error) {
console.error('[Lifecycle] Error in onLeaderAcquired:', error);
Expand Down Expand Up @@ -128,7 +128,7 @@
if (!get(doBootstrap)) {
try {
(await instance()).monitorService.ensureMonitorsActive();
await seedChecksFromCache();
await seedFromCache();
appState.set('running');
//console.log('Data seeded from cache.');
} catch (error) {
Expand Down
Loading

0 comments on commit 3bf9774

Please sign in to comment.