mirror of
https://github.com/cupcakearmy/ora.git
synced 2026-04-02 12:05:23 +00:00
move to dexie es nedb does not support multiple isntances
This commit is contained in:
@@ -1,25 +1,17 @@
|
||||
import NeDB from 'nedb-promises'
|
||||
import dj from 'dayjs'
|
||||
import Dexie from 'dexie'
|
||||
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',
|
||||
autoload: true,
|
||||
export const DB = new Dexie('ora')
|
||||
DB.version(2).stores({
|
||||
logs: `++id, host, timestamp`,
|
||||
limits: `++id, host`,
|
||||
})
|
||||
|
||||
export const Limits = NeDB.create({
|
||||
filename: 'limits.db',
|
||||
autoload: true,
|
||||
})
|
||||
|
||||
export function clear() {
|
||||
return Promise.all([Logs.remove({}, { multi: true }), Limits.remove({}, { multi: true })])
|
||||
}
|
||||
|
||||
export function normalizeTimestamp(timestamp) {
|
||||
// Normalize every dato to 15 minutes
|
||||
const t = dj(timestamp)
|
||||
@@ -32,12 +24,8 @@ export function normalizeTimestamp(timestamp) {
|
||||
}
|
||||
|
||||
export async function insertLog({ timestamp, host, seconds }) {
|
||||
Logs.update(
|
||||
{
|
||||
host,
|
||||
timestamp,
|
||||
},
|
||||
{ $inc: { seconds } },
|
||||
{ upsert: true }
|
||||
)
|
||||
const saved = await DB.logs.where({ host, timestamp }).first()
|
||||
const data = Object.assign({ host, timestamp, seconds: 0 }, saved)
|
||||
data.seconds += seconds
|
||||
await DB.logs.put(data)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { groupBy, orderBy, sum } from 'lodash'
|
||||
import dj from 'dayjs'
|
||||
|
||||
import { Limits, Logs } from './db.js'
|
||||
import { DB } from './db.js'
|
||||
|
||||
export async function data({ start, end }) {
|
||||
const logs = await getLogsBetweenDates({ start, end })
|
||||
@@ -9,11 +9,9 @@ export async function data({ start, end }) {
|
||||
}
|
||||
|
||||
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)
|
||||
let query = DB.logs.where('timestamp').inAnyRange([[start, end]])
|
||||
if (host) query = query.filter((x) => x.host === host)
|
||||
return await query.toArray()
|
||||
}
|
||||
|
||||
export function countInGroup(grouped) {
|
||||
@@ -53,12 +51,11 @@ export function getUsageForRules(host, rules) {
|
||||
}
|
||||
|
||||
export async function getUsageForHost(host) {
|
||||
const limit = await Limits.findOne({ host })
|
||||
const limit = await DB.limits.where({ host }).first()
|
||||
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