client for blocking

This commit is contained in:
2020-09-20 22:24:57 +02:00
parent c078f3150e
commit 77564060e2
12 changed files with 81 additions and 22 deletions

View File

@@ -1,5 +1,10 @@
import NeDB from 'nedb-promises'
import day from 'dayjs'
import dj from 'dayjs'
import RelativeTime from 'dayjs/plugin/relativeTime'
import Duration from 'dayjs/plugin/duration'
dj.extend(Duration)
dj.extend(RelativeTime)
export const Logs = NeDB.create({
filename: 'logs.db',
@@ -17,7 +22,7 @@ export function clear() {
export function normalizeTimestamp(timestamp) {
// Normalize every dato to 15 minutes
const t = day(timestamp)
const t = dj(timestamp)
const min = t.minute()
return t
.millisecond(0)

64
src/shared/lib.js Normal file
View File

@@ -0,0 +1,64 @@
import { groupBy, orderBy, sum } from 'lodash'
import dj from 'dayjs'
import { Limits, Logs } from './db.js'
export async function data({ start, end }) {
const logs = await getLogsBetweenDates({ start, end })
return groupBy(logs, 'host')
}
export async function getLogsBetweenDates({ start, end, host }) {
const where = {
$and: [{ timestamp: { $gt: start } }, { timestamp: { $lt: end } }],
}
if (host) where.host = host
return await Logs.find(where)
}
export function countInGroup(grouped) {
const counted = Object.entries(grouped).map(([key, data]) => {
const total = data.reduce((acc, cur) => acc + cur.seconds, 0)
const human = dj.duration(total, 'seconds').humanize()
return {
host: key,
total,
human,
}
})
return orderBy(counted, 'total', 'desc')
}
export function longPress(node, fn) {
let timeout
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
})
}
export async function getUsageForHost(host) {
const limit = await Limits.findOne({ host })
return await Promise.all(getUsageForRules(host, limit.rules))
}
export function percentagesToBool(percentages) {
const blocked = percentages.map((p) => p >= 100).includes(true)
console.log(percentages, blocked)
return blocked
}