Refactors services with defaultConfig and polled data.

Much more simpler code base. Each service requires a poll functioon to poll its
status. If type is defined, and the registered '+service.ts' exports a poll
ServicePoller, it will be used. Otherwise, a generic poll mechanism is used.
This commit is contained in:
2023-09-22 10:34:51 +02:00
parent d9d2d5c5af
commit 8450226211
15 changed files with 156 additions and 166 deletions

View File

@@ -1,28 +1,29 @@
import type { ServiceConfig } from '$lib/config';
import { type ServiceData, type ServiceHandler } from '../service';
import type { ServicePoller } from '../service';
export const handle: ServiceHandler = async (config: ServiceConfig) => {
const res: ServiceData = {
logo: 'https://cdn.rawgit.com/pi-hole/graphics/master/Vortex/Vortex.svg',
subtitle: 'Sends ads into a black hole'
};
export const config: Partial<ServiceConfig> = {
title: 'Pi-hole',
logo: 'https://cdn.rawgit.com/pi-hole/graphics/master/Vortex/Vortex.svg',
subtitle: 'Sends ads into a black hole'
};
export const poll: ServicePoller = async (config: ServiceConfig) => {
try {
const resp: Response = await fetch(
config.url + '/api.php?summaryRaw&auth=' + config.api_token
);
res.status = resp.ok ? 'online' : 'offline';
const raw = await resp.json();
res.data = {
queries_today: raw.dns_queries_today,
ads_percentage: raw.ads_percentage_today,
status: raw.status,
client: raw.unique_clients
return {
status: resp.ok ? 'online' : 'offline',
data: {
queries_today: raw.dns_queries_today,
ads_percentage: raw.ads_percentage_today,
status: raw.status,
client: raw.unique_clients
}
};
} catch (error) {
res.status = 'offline';
console.warn('could not fetch pihole status: ' + error);
return { status: 'offline' };
}
return res;
};