You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
import { getUnixTime, sub, format } from 'date-fns';
|
|
import { readable } from "svelte/store";
|
|
import { API_HOST } from './util';
|
|
|
|
export const SearchDateFormatStr = "yyyy-MM-dd HH:mm:ss.SSS";
|
|
export const formatDateForSearch = date => format(date, SearchDateFormatStr);
|
|
|
|
/*
|
|
take something like `clientIp:127.0.0.1 protocol:udp domain:google.com`
|
|
`(prop:expr)*
|
|
*/
|
|
export const parseTerms = (terms: string = "") :[string] | [] => {
|
|
|
|
const matches = terms.match(/([a-z]+[:>][\w\.]+)*/ig);
|
|
|
|
const exprs = matches.reduce((agg, term) => {
|
|
|
|
}, []);
|
|
return [];
|
|
};
|
|
|
|
export const fetchMetrics = async function ({
|
|
start = sub(new Date(), { hours: 8 }),
|
|
end = new Date(),
|
|
key = "domain",
|
|
interval = "30",
|
|
} = {}) {
|
|
try {
|
|
const response = await fetch(
|
|
API_HOST(`metrics/stats?start=${getUnixTime(
|
|
start
|
|
)}&end=${getUnixTime(end)}&key=${key}&interval=${interval}`),
|
|
{
|
|
method: "GET",
|
|
headers: { Accept: "application/json" }
|
|
}
|
|
);
|
|
|
|
const { success, payload } = await response.json();
|
|
|
|
if (!success) {
|
|
return { error: payload };
|
|
}
|
|
|
|
return { payload, interval, start, end, key };
|
|
} catch (e) {
|
|
console.error(e);
|
|
return { error: e };
|
|
}
|
|
};
|
|
|
|
export const metricsStore = readable([], (set) => {
|
|
const interval = setInterval(async () => {
|
|
const { error, payload } = await fetchMetrics({});
|
|
if (error) {
|
|
console.error(error);
|
|
return;
|
|
}
|
|
|
|
console.log(payload);
|
|
set(payload);
|
|
}, 15 * 1000);
|
|
return () => clearInterval(interval);
|
|
});
|