AppReports/seeds/fake.js
Timo Hocker 36d12a784a
Some checks failed
continuous-integration/drone/push Build is failing
polishing chart, build info in top left
2020-08-24 21:20:58 +02:00

59 lines
1.3 KiB
JavaScript

/*
* 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
*/
'use strict';
const faker = require ('faker');
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;
const data = {
light: faker.random.number (10),
temperature: last_t,
humidity: last_h
};
return {
app_id: faker.random.arrayElement (apps),
message: faker.random.words (),
data: JSON.stringify (data),
timestamp
};
}
async function seed (knex) {
// 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 log = (Array (1000))
.fill (() => null)
.map (() => faker.date.recent (30))
.sort ()
.map ((t) => create_log (t));
await knex.batchInsert ('log', log, 10);
}
module.exports = { seed };