Last active
October 15, 2017 17:39
-
-
Save jonyrock/fbf5618ffb9ebe8aa8d1f48e5feff4d6 to your computer and use it in GitHub Desktop.
Generation test data for https://github.com/CorpGlory/grafana-progress-list/issues/13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var date = new Date('2017-06-10 16:49:29'); | |
var names = [ 'Samsung Galaxy S8', 'HTC U11', 'LG G6', 'Sony Xperia XZ', 'Honor 9', 'iPhone 7', 'OnePlus 5' ]; | |
var charges = []; | |
var chargeDir = []; | |
const N = names.length; | |
const SWAP_TIME = Math.round(24 * 60 * 60 * 1000 / N); // 24 hours divided | |
function writeHeader() { | |
console.log('date,phoneName,charge'); | |
} | |
writeHeader(); | |
function generateCharges() { | |
charges = []; | |
chargeDir = []; | |
for(var i = 0; i < N; i++) { | |
charges.push(100 * Math.random()); | |
chargeDir.push((Math.random() > 0.5) ? 1 : -1); | |
} | |
} | |
generateCharges(); | |
var lastDirSwap = date.getTime(); | |
function logStep() { | |
date.setTime(date.getTime() + 1000 + Math.round(Math.random() * 1000)); | |
var pi = Math.round(Math.random() * N) % N; | |
var name = names[pi]; | |
var charge = charges[pi]; | |
if(date.getTime() - lastDirSwap > SWAP_TIME) { | |
lastDirSwap = date.getTime(); | |
chargeDir[pi] = -chargeDir[pi]; | |
} | |
if(charges[pi] === 0 || charges[pi] === 100) { | |
if(Math.random() > 0.9) { | |
chargeDir[pi] = -chargeDir[pi]; | |
} | |
} | |
charges[pi] += chargeDir[pi] * Math.random() * 0.1; | |
charges[pi] = Math.min(Math.max(charges[pi], 0), 100); | |
console.log(date.toISOString() + ',' + name + ',' + charge); | |
} | |
function generateLog() { | |
for(var i = 0; i < 100000; i++) { | |
logStep(); | |
} | |
} | |
generateLog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment