ora/src/dashboard/lib.js

36 lines
999 B
JavaScript
Raw Normal View History

2020-09-19 01:16:43 +02:00
import { each, groupBy, orderBy } from 'lodash'
import dj from 'dayjs'
2020-09-18 21:00:59 +02:00
import { Logs } from '../shared/db'
export async function data({ start, end }) {
const logs = await getLogsBetweenDates({ start, end })
console.log('Found', logs.length)
return groupBy(logs, 'host')
}
export async function getLogsBetweenDates({ start, end }) {
return await Logs.find({
$and: [{ timestamp: { $gt: start } }, { timestamp: { $lt: end } }],
})
}
export function countInGroup(grouped) {
const counted = Object.entries(grouped).map(([key, data]) => {
2020-09-19 01:16:43 +02:00
const total = data.reduce((acc, cur) => acc + cur.seconds, 0)
const human = dj.duration(total, 'seconds').humanize()
2020-09-18 21:00:59 +02:00
return {
host: key,
total,
human,
}
})
return orderBy(counted, 'total', 'desc')
}
2020-09-20 17:28:09 +02:00
export function longPress(node, fn) {
let timeout
node.addEventListener('mousedown', () => (timeout = setTimeout(fn, 500)), false)
node.addEventListener('mouseup', () => clearTimeout(timeout), false)
}