AppReports/seeds/fake.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-08-19 12:33:09 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of appreports which is released under GPL-3.0-or-later.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, August 2020
*/
2020-07-29 20:56:03 +02:00
'use strict';
const faker = require ('faker');
2020-08-16 11:48:06 +02:00
const apps = [];
async function create_app (knex) {
const [ id ] = await knex ('app')
.insert (
{ name: faker.random.word () }
);
apps.push (id);
}
let last_t = 0;
let last_h = 0;
function create_log (timestamp) {
last_t += faker.random.number (3) - 1;
last_h += faker.random.number (2) - 1;
2020-07-30 18:39:37 +02:00
const data = {
light: faker.random.number (10),
temperature: last_t,
humidity: last_h
2020-07-30 18:39:37 +02:00
};
2020-07-29 20:56:03 +02:00
return {
2020-08-16 11:48:06 +02:00
app_id: faker.random.arrayElement (apps),
2020-07-29 20:56:03 +02:00
message: faker.random.words (),
2020-07-30 18:39:37 +02:00
data: JSON.stringify (data),
timestamp
2020-07-29 20:56:03 +02:00
};
}
async function seed (knex) {
2020-08-15 15:19:52 +02:00
// eslint-disable-next-line no-console
2020-07-29 20:56:03 +02:00
console.log ('creating seeds');
2020-08-16 11:48:06 +02:00
for (let i = 0; i < 5; i++)
// eslint-disable-next-line no-await-in-loop
await create_app (knex);
const log = (Array (1000))
2020-07-30 18:39:37 +02:00
.fill (() => null)
.map (() => faker.date.recent (30))
.sort ()
.map ((t) => create_log (t));
2020-07-30 18:39:37 +02:00
await knex.batchInsert ('log', log, 10);
2020-07-29 20:56:03 +02:00
}
module.exports = { seed };