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 })
|
|
|
|
return groupBy(logs, 'host')
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:43:39 +02:00
|
|
|
export async function getLogsBetweenDates({ start, end, host }) {
|
|
|
|
const where = {
|
2020-09-18 21:00:59 +02:00
|
|
|
$and: [{ timestamp: { $gt: start } }, { timestamp: { $lt: end } }],
|
2020-09-20 17:43:39 +02:00
|
|
|
}
|
|
|
|
if (host) where.host = host
|
|
|
|
return await Logs.find(where)
|
2020-09-18 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|