split per app
This commit is contained in:
parent
d3a19c953a
commit
c5be16963d
10
lib/api/get-app.js
Normal file
10
lib/api/get-app.js
Normal file
@ -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);
|
||||
};
|
@ -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)));
|
||||
}
|
||||
};
|
||||
|
@ -4,5 +4,6 @@ const router = require ('express')
|
||||
.Router ();
|
||||
|
||||
router.get ('/log', require ('./get-log'));
|
||||
router.get ('/app', require ('./get-app'));
|
||||
|
||||
module.exports = router;
|
||||
|
9
lib/db/app.js
Normal file
9
lib/db/app.js
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (get_db) => ({
|
||||
get_all () {
|
||||
const knex = get_db ();
|
||||
return knex ('app')
|
||||
.select ();
|
||||
}
|
||||
});
|
@ -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 };
|
||||
|
15
lib/db/log.js
Normal file
15
lib/db/log.js
Normal file
@ -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 });
|
||||
}
|
||||
});
|
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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',
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 ();
|
||||
|
29
src/views/Home.vue
Normal file
29
src/views/Home.vue
Normal file
@ -0,0 +1,29 @@
|
||||
<template>
|
||||
<ul>
|
||||
<li
|
||||
v-for="(app,key) in apps"
|
||||
:key="key"
|
||||
>
|
||||
<a
|
||||
:href="'/app/' + app.id"
|
||||
v-text="app.name"
|
||||
/>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data () {
|
||||
return { apps: [] };
|
||||
},
|
||||
async mounted () {
|
||||
this.apps = await fetch ('/app')
|
||||
.then ((res) => res.json ());
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
</style>
|
Loading…
x
Reference in New Issue
Block a user