fixed the duration

This commit is contained in:
cupcakearmy 2020-09-20 18:37:02 +02:00
parent b404bd5d85
commit c078f3150e
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
3 changed files with 24 additions and 22 deletions

View File

@ -32,9 +32,10 @@
<div class="flex items-center">
<div class="btn-group">
<button class="btn btn-sm" on:click={all}>All</button>
<button class="btn btn-sm" on:click={set('month')}>Month</button>
<button class="btn btn-sm" on:click={set('week')}>Week</button>
<button class="btn btn-sm" on:click={set('day')}>Day</button>
<button class="btn btn-sm" on:click={set('month', 1)}>Month</button>
<button class="btn btn-sm" on:click={set('week', 1)}>Week</button>
<button class="btn btn-sm" on:click={set('day', 3)}>3 Days</button>
<button class="btn btn-sm" on:click={set('day', 0)}>Today</button>
</div>
<div class="spacer" />
<div class="input-group">

View File

@ -1,28 +1,12 @@
<script>
import dj from 'dayjs'
import { sum } from 'lodash'
import { Logs } from '../../shared/db.js'
import { getLogsBetweenDates } from '../lib.js'
import { getUsageForRules } from '../lib.js'
export let host = ''
export let rules = []
$: percentages = rules.map(async ({ every }) => {
const durationAsSeconds = dj.duration(...every).asSeconds()
const start = dj().subtract(durationAsSeconds, 's').toDate()
console.log(start)
const logs = await getLogsBetweenDates({
start,
end: new Date(),
host,
})
console.log(logs)
const consumed = sum(logs.map((log) => log.seconds))
return (consumed / durationAsSeconds) * 100
console.log(total, dj.duration(total, 's').humanize())
})
$: percentages = getUsageForRules(host, rules)
</script>
{#each rules as rule, i}

View File

@ -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
})
}