From 3f77bcf1e04c0f7eb2daedb83a218d67eeb8da04 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 8 Oct 2020 20:03:09 +0200 Subject: [PATCH] move to dexie es nedb does not support multiple isntances --- package.json | 3 +- src/background/index.js | 3 +- src/client/index.js | 1 - src/dashboard/components/Dev.svelte | 5 +- src/dashboard/components/RulesEditor.svelte | 8 +-- src/dashboard/pages/Limits.svelte | 14 ++--- src/shared/db.js | 30 +++------- src/shared/lib.js | 13 ++--- yarn.lock | 64 ++++----------------- 9 files changed, 40 insertions(+), 101 deletions(-) diff --git a/package.json b/package.json index ed4dc2e..34deed7 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,9 @@ "dependencies": { "d3": "^6.1.1", "dayjs": "^1.8.36", + "dexie": "^3.0.2", "faker": "^5.1.0", "lodash": "^4.17.20", - "nedb": "^1.8.0", - "nedb-promises": "^4.0.4", "spectre.css": "^0.5.9", "svelte-spa-router": "^2.2.0", "tailwindcss": "^1.8.10", diff --git a/src/background/index.js b/src/background/index.js index e094312..e647450 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -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)) }) diff --git a/src/client/index.js b/src/client/index.js index 0b0b91f..46a8a69 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -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' } diff --git a/src/dashboard/components/Dev.svelte b/src/dashboard/components/Dev.svelte index 4456462..5ac236e 100644 --- a/src/dashboard/components/Dev.svelte +++ b/src/dashboard/components/Dev.svelte @@ -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 } diff --git a/src/dashboard/components/RulesEditor.svelte b/src/dashboard/components/RulesEditor.svelte index e3f471d..0bfa50b 100644 --- a/src/dashboard/components/RulesEditor.svelte +++ b/src/dashboard/components/RulesEditor.svelte @@ -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 @@
{#if limit}
Rules
diff --git a/src/dashboard/pages/Limits.svelte b/src/dashboard/pages/Limits.svelte index 909d5df..cd8158a 100644 --- a/src/dashboard/pages/Limits.svelte +++ b/src/dashboard/pages/Limits.svelte @@ -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 @@ Rules Actions - {#each limits as { host, rules, _id }} + {#each limits as { host, rules, id }} {host} @@ -58,8 +58,8 @@
- - +
diff --git a/src/shared/db.js b/src/shared/db.js index c88bcad..46010b1 100644 --- a/src/shared/db.js +++ b/src/shared/db.js @@ -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) } diff --git a/src/shared/lib.js b/src/shared/lib.js index 195ea17..774afeb 100644 --- a/src/shared/lib.js +++ b/src/shared/lib.js @@ -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 } diff --git a/yarn.lock b/yarn.lock index e59ce34..a092e83 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1392,11 +1392,6 @@ async-limiter@~1.0.0: resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd" integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ== -async@0.2.10, async@~0.2.9: - version "0.2.10" - resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1" - integrity sha1-trvgsGdLnXGXCMo43owjfLUmw9E= - async@^1.5.2: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" @@ -1409,6 +1404,11 @@ async@^2.0.0: dependencies: lodash "^4.17.14" +async@~0.2.9: + version "0.2.10" + resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1" + integrity sha1-trvgsGdLnXGXCMo43owjfLUmw9E= + async@~2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/async/-/async-2.5.0.tgz#843190fd6b7357a0b9e1c956edddd5ec8462b54d" @@ -1538,13 +1538,6 @@ binary-extensions@^2.0.0: resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.1.0.tgz#30fa40c9e7fe07dbc895678cd287024dea241dd9" integrity sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ== -binary-search-tree@0.2.5: - version "0.2.5" - resolved "https://registry.yarnpkg.com/binary-search-tree/-/binary-search-tree-0.2.5.tgz#7dbb3b210fdca082450dad2334c304af39bdc784" - integrity sha1-fbs7IQ/coIJFDa0jNMMErzm9x4Q= - dependencies: - underscore "~1.4.4" - bindings@^1.5.0: version "1.5.0" resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df" @@ -2950,6 +2943,11 @@ detective@^5.2.0: defined "^1.0.0" minimist "^1.1.1" +dexie@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/dexie/-/dexie-3.0.2.tgz#4b979904d739e0530b68352005f175a82633a075" + integrity sha512-go4FnIoAhcUiCdxutfIZRxnSaSyDgfEq+GH7N0I8nTCJbC2FmeBj+0FrETa3ln5ix+VQMOPsFeYHlgE/8SZWwQ== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875" @@ -4137,11 +4135,6 @@ ignore@^4.0.6: resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== -immediate@~3.0.5: - version "3.0.6" - resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" - integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps= - import-fresh@3.2.1, import-fresh@^3.0.0: version "3.2.1" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.2.1.tgz#633ff618506e793af5ac91bf48b72677e15cbe66" @@ -4839,13 +4832,6 @@ levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" -lie@3.1.1: - version "3.1.1" - resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e" - integrity sha1-mkNrLMd0bKWd56QfpGmz77dr2H4= - dependencies: - immediate "~3.0.5" - lighthouse-logger@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/lighthouse-logger/-/lighthouse-logger-1.2.0.tgz#b76d56935e9c137e86a04741f6bb9b2776e886ca" @@ -4859,13 +4845,6 @@ lines-and-columns@^1.1.6: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA= -localforage@^1.3.0: - version "1.9.0" - resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1" - integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g== - dependencies: - lie "3.1.1" - locate-path@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0" @@ -5239,24 +5218,6 @@ ncp@~2.0.0: resolved "https://registry.yarnpkg.com/ncp/-/ncp-2.0.0.tgz#195a21d6c46e361d2fb1281ba38b91e9df7bdbb3" integrity sha1-GVoh1sRuNh0vsSgbo4uR6d9727M= -nedb-promises@^4.0.4: - version "4.0.4" - resolved "https://registry.yarnpkg.com/nedb-promises/-/nedb-promises-4.0.4.tgz#51aa3f43ae98269c9c9d512288ff67de890b7f7f" - integrity sha512-+z5kzrNOW0rDA1FiCfAtPGR7ZW3mnaOrYZytGAT0TbL6EqoEJUCxAuu9VME4+B1yZNG1gBx61F0c0jcacq1EsQ== - dependencies: - nedb "^1.8.0" - -nedb@^1.8.0: - version "1.8.0" - resolved "https://registry.yarnpkg.com/nedb/-/nedb-1.8.0.tgz#0e3502cd82c004d5355a43c9e55577bd7bd91d88" - integrity sha1-DjUCzYLABNU1WkPJ5VV3vXvZHYg= - dependencies: - async "0.2.10" - binary-search-tree "0.2.5" - localforage "^1.3.0" - mkdirp "~0.5.1" - underscore "~1.4.4" - neo-async@^2.5.0: version "2.6.2" resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f" @@ -7663,11 +7624,6 @@ uncss@^0.17.3: postcss-selector-parser "6.0.2" request "^2.88.0" -underscore@~1.4.4: - version "1.4.4" - resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.4.4.tgz#61a6a32010622afa07963bf325203cf12239d604" - integrity sha1-YaajIBBiKvoHljvzJSA88SI51gQ= - unicode-canonical-property-names-ecmascript@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"