mirror of https://github.com/cupcakearmy/ora.git
42 lines
998 B
JavaScript
42 lines
998 B
JavaScript
import dj from 'dayjs'
|
|
import RelativeTime from 'dayjs/plugin/relativeTime'
|
|
import Duration from 'dayjs/plugin/duration'
|
|
import Dexie from 'dexie'
|
|
import 'dexie-export-import'
|
|
|
|
dj.extend(Duration)
|
|
dj.extend(RelativeTime)
|
|
|
|
export const DB = new Dexie('ora')
|
|
|
|
DB.version(2).stores({
|
|
logs: `++id, host, timestamp`,
|
|
limits: `++id, host`,
|
|
})
|
|
|
|
DB.version(3).stores({
|
|
settings: `key, value`,
|
|
})
|
|
|
|
export function normalizeTimestamp(timestamp) {
|
|
// Normalize every dato to 15 minutes
|
|
const t = dj(timestamp)
|
|
const min = t.minute()
|
|
return t
|
|
.millisecond(0)
|
|
.second(0)
|
|
.minute(min - (min % 15))
|
|
.toDate()
|
|
}
|
|
|
|
export async function insertLog({ timestamp, host, seconds }) {
|
|
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)
|
|
}
|
|
|
|
export async function clear() {
|
|
return Promise.allSettled(DB.tables.map((table) => DB.table(table.name).clear()))
|
|
}
|