From c078f3150ec6227d20ab22b26bd7bcd8134b794b Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sun, 20 Sep 2020 18:37:02 +0200 Subject: [PATCH] fixed the duration --- src/dashboard/components/RangeChooser.svelte | 7 ++++--- src/dashboard/components/Rules.svelte | 20 ++------------------ src/dashboard/lib.js | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/dashboard/components/RangeChooser.svelte b/src/dashboard/components/RangeChooser.svelte index 5bac26f..f853084 100644 --- a/src/dashboard/components/RangeChooser.svelte +++ b/src/dashboard/components/RangeChooser.svelte @@ -32,9 +32,10 @@
- - - + + + +
diff --git a/src/dashboard/components/Rules.svelte b/src/dashboard/components/Rules.svelte index 6c26758..4cf25b3 100644 --- a/src/dashboard/components/Rules.svelte +++ b/src/dashboard/components/Rules.svelte @@ -1,28 +1,12 @@ {#each rules as rule, i} diff --git a/src/dashboard/lib.js b/src/dashboard/lib.js index 8ac8ae2..d9907ee 100644 --- a/src/dashboard/lib.js +++ b/src/dashboard/lib.js @@ -1,4 +1,4 @@ -import { each, groupBy, orderBy } from 'lodash' +import { groupBy, orderBy, sum } from 'lodash' import dj from 'dayjs' import { Logs } from '../shared/db' @@ -34,3 +34,20 @@ export function longPress(node, fn) { node.addEventListener('mousedown', () => (timeout = setTimeout(fn, 500)), false) node.addEventListener('mouseup', () => clearTimeout(timeout), false) } + +export function getUsageForRules(host, rules) { + return rules.map(async ({ every, limit }) => { + const limitAsSeconds = dj.duration(...limit).asSeconds() + const everyAsSeconds = dj.duration(...every).asSeconds() + + const logs = await getLogsBetweenDates({ + start: dj().subtract(everyAsSeconds, 's').toDate(), + end: new Date(), + host, + }) + + // Calculate usage in percentage 0-100 + const consumed = sum(logs.map((log) => log.seconds)) + return (consumed / limitAsSeconds) * 100 + }) +}