mirror of
https://github.com/cupcakearmy/ora.git
synced 2026-04-02 12:05:23 +00:00
client for blocking
This commit is contained in:
@@ -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
64
src/shared/lib.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user