mirror of
https://github.com/cupcakearmy/ora.git
synced 2024-12-22 08:06:28 +00:00
fixed the duration
This commit is contained in:
parent
b404bd5d85
commit
c078f3150e
@ -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">
|
||||
|
@ -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}
|
||||
|
@ -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
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user