ora/src/shared/db.js

44 lines
891 B
JavaScript
Raw Normal View History

2020-09-18 21:00:59 +02:00
import NeDB from 'nedb-promises'
2020-09-20 22:24:57 +02:00
import dj from 'dayjs'
import RelativeTime from 'dayjs/plugin/relativeTime'
import Duration from 'dayjs/plugin/duration'
dj.extend(Duration)
dj.extend(RelativeTime)
2020-09-18 21:00:59 +02:00
export const Logs = NeDB.create({
filename: 'logs.db',
autoload: true,
})
2020-09-19 01:16:43 +02:00
2020-09-20 17:28:09 +02:00
export const Limits = NeDB.create({
filename: 'limits.db',
autoload: true,
})
export function clear() {
return Promise.all([Logs.remove({}, { multi: true }), Limits.remove({}, { multi: true })])
}
2020-09-19 01:16:43 +02:00
export function normalizeTimestamp(timestamp) {
// Normalize every dato to 15 minutes
2020-09-20 22:24:57 +02:00
const t = dj(timestamp)
2020-09-19 01:16:43 +02:00
const min = t.minute()
return t
.millisecond(0)
.second(0)
.minute(min - (min % 15))
.toDate()
}
export async function insertLog({ timestamp, host, seconds }) {
Logs.update(
{
host,
timestamp,
},
{ $inc: { seconds } },
{ upsert: true }
)
}