From c5be16963db58178ced40409031cc10cad7a2873 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sun, 16 Aug 2020 11:48:06 +0200 Subject: [PATCH] split per app --- lib/api/get-app.js | 10 ++++++++++ lib/api/get-log.js | 13 +++++++++++-- lib/api/index.js | 1 + lib/db/app.js | 9 +++++++++ lib/db/index.js | 10 ++++++---- lib/db/log.js | 15 +++++++++++++++ migrations/00000-init.js | 18 ++++++++++++++++-- seeds/fake.js | 23 ++++++++++++++++++++--- src/router/index.js | 6 ++++++ src/store/index.js | 4 ++-- src/views/AppView.vue | 2 +- src/views/Home.vue | 29 +++++++++++++++++++++++++++++ 12 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 lib/api/get-app.js create mode 100644 lib/db/app.js create mode 100644 lib/db/log.js create mode 100644 src/views/Home.vue diff --git a/lib/api/get-app.js b/lib/api/get-app.js new file mode 100644 index 0000000..ecb9065 --- /dev/null +++ b/lib/api/get-app.js @@ -0,0 +1,10 @@ +'use strict'; + +const db = require ('../db'); +const { http } = require ('@sapphirecode/consts'); + +module.exports = async (req, res) => { + const apps = await db.app.get_all (); + res.status (http.status_ok) + .json (apps); +}; diff --git a/lib/api/get-log.js b/lib/api/get-log.js index 3550299..3f9efd0 100644 --- a/lib/api/get-log.js +++ b/lib/api/get-log.js @@ -4,6 +4,15 @@ const db = require ('../db'); const { http } = require ('@sapphirecode/consts'); module.exports = async (req, res) => { - res.status (http.status_ok) - .json (await db.get_all ()); + if ( + typeof req.headers.app_id === 'undefined' + || isNaN (parseInt (req.headers.app_id)) + ) { + res.status (http.status_bad_request) + .end (); + } + else { + res.status (http.status_ok) + .json (await db.log.get_all (parseInt (req.headers.app_id))); + } }; diff --git a/lib/api/index.js b/lib/api/index.js index ff39ee9..65a934b 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -4,5 +4,6 @@ const router = require ('express') .Router (); router.get ('/log', require ('./get-log')); +router.get ('/app', require ('./get-app')); module.exports = router; diff --git a/lib/db/app.js b/lib/db/app.js new file mode 100644 index 0000000..762c92c --- /dev/null +++ b/lib/db/app.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = (get_db) => ({ + get_all () { + const knex = get_db (); + return knex ('app') + .select (); + } +}); diff --git a/lib/db/index.js b/lib/db/index.js index 1b0c191..8b284b6 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -18,9 +18,11 @@ async function init (use_fake_seed) { await db.seed.run ({ specific: 'fake.js' }); } -function get_all () { - return db ('log') - .select (); +function get_db () { + return db; } -module.exports = { init, get_all }; +const log = require ('./log') (get_db); +const app = require ('./app') (get_db); + +module.exports = { init, log, app }; diff --git a/lib/db/log.js b/lib/db/log.js new file mode 100644 index 0000000..20db904 --- /dev/null +++ b/lib/db/log.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = (get_db) => ({ + get_all (app_id) { + const knex = get_db (); + return knex.select ( + 'id', + 'message', + 'data', + 'timestamp' + ) + .from ('log') + .where ({ app_id }); + } +}); diff --git a/migrations/00000-init.js b/migrations/00000-init.js index 65249b0..710e79b 100644 --- a/migrations/00000-init.js +++ b/migrations/00000-init.js @@ -1,15 +1,29 @@ 'use strict'; -async function up (knex) { +async function create_app (knex) { + await knex.schema.createTable ('app', (table) => { + table.increments ('id'); + table.string ('name'); + }); +} + +async function create_log (knex) { await knex.schema.createTable ('log', (table) => { table.increments ('id'); - table.string ('app'); + table.integer ('app_id') + .references ('id') + .inTable ('app'); table.string ('message'); table.json ('data'); table.timestamp ('timestamp'); }); } +async function up (knex) { + await create_app (knex); + await create_log (knex); +} + function down () { // noop } diff --git a/seeds/fake.js b/seeds/fake.js index e9284e3..4023946 100644 --- a/seeds/fake.js +++ b/seeds/fake.js @@ -3,6 +3,16 @@ const faker = require ('faker'); const sn = require ('simplex-noise'); +const apps = []; + +async function create_app (knex) { + const [ id ] = await knex ('app') + .insert ( + { name: faker.random.word () } + ); + apps.push (id); +} + function create_log (index, simplex) { const data = { num1: faker.random.number (), @@ -10,7 +20,7 @@ function create_log (index, simplex) { num3: simplex.noise2D (index * 0.1, 1000) }; return { - app: faker.random.word (), + app_id: faker.random.arrayElement (apps), message: faker.random.words (), data: JSON.stringify (data), timestamp: faker.date.recent () @@ -18,14 +28,21 @@ function create_log (index, simplex) { } async function seed (knex) { + await knex ('log') + .del (); + await knex ('app') + .del (); + // eslint-disable-next-line no-console console.log ('creating seeds'); + for (let i = 0; i < 5; i++) + // eslint-disable-next-line no-await-in-loop + await create_app (knex); + const simplex = (new sn); const log = (Array (20)) .fill (() => null) .map ((a, index) => create_log (index, simplex)); - await knex ('log') - .del (); await knex.batchInsert ('log', log, 10); } diff --git a/src/router/index.js b/src/router/index.js index 22a40d4..54d65bb 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -11,10 +11,16 @@ import Vue from 'vue'; import VueRouter from 'vue-router'; import AppView from '../views/AppView.vue'; import NotFound from '../views/NotFound.vue'; +import Home from '../views/Home.vue'; Vue.use (VueRouter); const routes = [ + { + path: '/', + name: 'Home', + component: Home + }, { path: '/app/:id', name: 'AppView', diff --git a/src/store/index.js b/src/store/index.js index 62af398..554f552 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -18,8 +18,8 @@ export default new Vuex.Store ({ } }, actions: { - async get_log ({ commit }) { - const log = await fetch ('/log') + async get_log ({ commit }, { app_id }) { + const log = await fetch ('/log', { headers: { app_id } }) .then ((res) => res.json ()); commit ('set_log', log); } diff --git a/src/views/AppView.vue b/src/views/AppView.vue index 383a0a1..9198e24 100644 --- a/src/views/AppView.vue +++ b/src/views/AppView.vue @@ -42,7 +42,7 @@ export default { ...Vuex.mapState ({ log: (state) => state.log }) }, mounted () { - this.get_log (); + this.get_log ({ app_id: this.$route.params.id }); document.body.addEventListener ('keydown', (ev) => { if (ev.key === 's' && ev.ctrlKey) { this.save_config (); diff --git a/src/views/Home.vue b/src/views/Home.vue new file mode 100644 index 0000000..eefa485 --- /dev/null +++ b/src/views/Home.vue @@ -0,0 +1,29 @@ + + + + +