move to dexie es nedb does not support multiple isntances

This commit is contained in:
2020-10-08 20:03:09 +02:00
parent 56cd2a3e7c
commit 3f77bcf1e0
9 changed files with 40 additions and 101 deletions

View File

@@ -39,7 +39,6 @@ setInterval(() => {
getAllTabs()
}, frequency)
browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
// await Limits.
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
return getUsageForHost(message).then((percentages) => percentagesToBool(percentages))
})

View File

@@ -34,7 +34,6 @@ function init() {
async function check() {
if (window.document.hidden) return
console.log('Checking')
const isBlocked = await browser.runtime.sendMessage(window.location.host)
wrapper.style.display = isBlocked ? 'initial' : 'none'
}

View File

@@ -3,7 +3,7 @@
import day from 'dayjs'
import { range, random } from 'lodash'
import { insertLog, normalizeTimestamp, clear as clearDB } from '../../shared/db'
import { insertLog, normalizeTimestamp, DB } from '../../shared/db'
let loading = false
@@ -29,7 +29,8 @@
async function clear() {
try {
loading = true
await clearDB()
await DB.limits.clear()
await DB.logs.clear()
} finally {
loading = false
}

View File

@@ -2,7 +2,7 @@
import { createEventDispatcher } from 'svelte'
import { cloneDeep } from 'lodash'
import { Limits } from '../../shared/db'
import { DB } from '../../shared/db'
const dispatch = createEventDispatcher()
const init = { limit: ['1', 'h'], every: [1, 'd'] }
@@ -23,8 +23,7 @@
}
async function save() {
const { _id, ...rest } = limit
await Limits.update({ _id }, rest, { upsert: true })
await DB.limits.put(limit)
dispatch('update')
close()
}
@@ -41,7 +40,8 @@
<div class="content">
{#if limit}
<label class="form-label">
Host <input type="text" class="form-input" placeholder="google.com" bind:value={limit.host} />
Host
<input type="text" class="form-input" placeholder="google.com" bind:value={limit.host} />
</label>
<div class="form-label">Rules</div>

View File

@@ -4,7 +4,7 @@
import RulesEditor from '../components/RulesEditor.svelte'
import Rules from '../components/Rules.svelte'
import { Limits } from '../../shared/db.js'
import { DB } from '../../shared/db.js'
import { longPress } from '../../shared/lib'
let limits = null
@@ -15,15 +15,15 @@
}
function edit(id) {
limit = limits.find((limit) => limit._id === id)
limit = limits.find((limit) => limit.id === id)
}
async function load() {
limits = await Limits.find()
limits = await DB.limits.toArray()
}
async function del(id) {
await Limits.remove({ _id: id })
await DB.limits.delete(id)
await load()
}
@@ -50,7 +50,7 @@
<th>Rules</th>
<th class="text-right w-32">Actions</th>
</tr>
{#each limits as { host, rules, _id }}
{#each limits as { host, rules, id }}
<tr>
<td>{host}</td>
<td>
@@ -58,8 +58,8 @@
</td>
<td class="text-right">
<div class="btn-group">
<button class="btn btn-sm btn-primary" on:click={() => edit(_id)}>Edit</button>
<button class="btn btn-sm btn-error tooltip" data-tooltip="Hold to delete" use:longPress={() => del(_id)}>
<button class="btn btn-sm btn-primary" on:click={() => edit(id)}>Edit</button>
<button class="btn btn-sm btn-error tooltip" data-tooltip="Hold to delete" use:longPress={() => del(id)}>
Delete
</button>
</div>

View File

@@ -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)
}

View File

@@ -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
}