Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New time parameter #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
3 changes: 1 addition & 2 deletions _client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ var client = module.exports = new Client({
});
})
},
host: url,
host: url,
ssl: argv.insecure ? { rejectUnauthorized: false, pfx: [] } : {}
});

client.usable = usable;

client.ping({
Expand Down
96 changes: 69 additions & 27 deletions _randomEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var stringGenerator = require('./samples/string_generator');

var eventCounter = -1;
var count = argv.total;
var moment = require('moment');

var countOfDays = (function () {
var cursor = argv.start.clone();
Expand All @@ -14,15 +15,15 @@ var countOfDays = (function () {

if (cursor.valueOf() <= end) {
do {
cursor.add(1, 'day');
cursor.add(1, argv.timeType.valueOf());
count += 1;
} while (cursor.valueOf() <= end);
}

return count;
}());
var countPerDay = Math.ceil(count / countOfDays);

var countPerDay = Math.ceil(count / countOfDays);
var indexInterval = argv.indexInterval;
var dayMoment = argv.start.clone();
var day;
Expand All @@ -32,36 +33,78 @@ module.exports = function RandomEvent(indexPrefix) {

var i = ++eventCounter;
var iInDay = i % countPerDay;
var ms = samples.lessRandomMsInDay();

if (day && iInDay === 0) {
dayMoment.add(1, 'day');
dayMoment.add(1, argv.timeType.valueOf());
day = null;
}

if (day == null) {
day = {
year: dayMoment.year(),
month: dayMoment.month(),
date: dayMoment.date(),
};
if (argv.timeType.valueOf() === "day") {
if (day == null) {
day = {
year: dayMoment.year(),
month: dayMoment.month(),
date: dayMoment.date(),
};
}
// extract number of hours from the milliseconds
var hours = Math.floor(ms / 3600000);
ms = ms - hours * 3600000;
// extract number of minutes from the milliseconds
var minutes = Math.floor(ms / 60000);
ms = ms - minutes * 60000;

// extract number of seconds from the milliseconds
var seconds = Math.floor(ms / 1000);
ms = ms - seconds * 1000;
// apply the values found to the date
var date = new Date(day.year, day.month, day.date, hours, minutes, seconds, ms);
}

var ms = samples.lessRandomMsInDay();

// extract number of hours from the milliseconds
var hours = Math.floor(ms / 3600000);
ms = ms - hours * 3600000;

// extract number of minutes from the milliseconds
var minutes = Math.floor(ms / 60000);
ms = ms - minutes * 60000;

// extract number of seconds from the milliseconds
var seconds = Math.floor(ms / 1000);
ms = ms - seconds * 1000;

// apply the values found to the date
var date = new Date(day.year, day.month, day.date, hours, minutes, seconds, ms);
else if (argv.timeType.valueOf() === "hour") {
if (day == null) {
day = {
year: dayMoment.year(),
month: dayMoment.month(),
date: dayMoment.date(),
hour: dayMoment.hour(),
};
}
// extract number of hours from the milliseconds
var hours = Math.floor(ms / 3600000);
ms = ms - hours * 3600000;
// extract number of minutes from the milliseconds
var minutes = Math.floor(ms / 60000);
ms = ms - minutes * 60000;
// extract number of seconds from the milliseconds
var seconds = Math.floor(ms / 1000);
ms = ms - seconds * 1000;
// apply the values found to the date
var date = new Date(day.year, day.month, day.date, day.hour, minutes, seconds, ms);
}
else {
if (day == null) {
day = {
year: dayMoment.year(),
month: dayMoment.month(),
date: dayMoment.date(),
hour: dayMoment.hour(),
minute: dayMoment.minute(),
};
}
// extract number of hours from the milliseconds
var hours = Math.floor(ms / 3600000);
ms = ms - hours * 3600000;
// extract number of minutes from the milliseconds
var minutes = Math.floor(ms / 60000);
ms = ms - minutes * 60000;
// extract number of seconds from the milliseconds
var seconds = Math.floor(ms / 1000);
ms = ms - seconds * 1000;
// apply the values found to the date
var date = new Date(day.year, day.month, day.date, day.hour, day.minute, seconds, ms);
}

var dateAsIso = date.toISOString();

switch (indexInterval) {
Expand All @@ -80,7 +123,6 @@ module.exports = function RandomEvent(indexPrefix) {
event.index = indexPrefix + Math.floor(i / indexInterval);
break;
}

event['@timestamp'] = dateAsIso;
event.ip = samples.ips();
event.extension = samples.extensions();
Expand Down
33 changes: 0 additions & 33 deletions argv/_parseDays.js

This file was deleted.

79 changes: 79 additions & 0 deletions argv/_parseTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

var moment = require('moment');

var customDayBoundsRE = /^\-?\d+\/\-?\d+$/;
var oldDayExpressionRE = /[\-\+\,]/;

module.exports = function parseDays(argv) {
if (argv.days === null && argv.time === null) {
var arg_time = '1d/1d';
}
else {
if ( argv.days !== null ) {
var arg_time = argv.days;
}
else {
var arg_time = argv.time;
}
}
var splitTime = arg_time.split("/");
var startBaseTmp = splitTime[0];
var startBaseTypeTmp = startBaseTmp.at(-1);

switch (startBaseTypeTmp) {
case 'd':
var timeType = "day";
var startBaseValueTmp = startBaseTmp.slice(0, -1);
break;
case 'h':
var timeType = "hour";
var startBaseValueTmp = startBaseTmp.slice(0, -1);
break;
case 'm':
var timeType = "minute";
var startBaseValueTmp = startBaseTmp.slice(0, -1);
break;
default:
var timeType = "day";
var startBaseValueTmp = startBaseTmp;
break;
}

if (typeof splitTime[1] === "undefined"){
endBaseTypeTmp = "d";
endBaseValueTmp = 0;
endTimeType = "d";
}
else {
var endBaseTmp = splitTime[1];
var endBaseTypeTmp = endBaseTmp.at(-1);
switch (endBaseTypeTmp) {
case 'd':
var endTimeType = "day";
var endBaseValueTmp = endBaseTmp.slice(0, -1);
break;
case 'h':
var endTimeType = "hour";
var endBaseValueTmp = endBaseTmp.slice(0, -1);
break;
case 'm':
var endTimeType = "minute";
var endBaseValueTmp = endBaseTmp.slice(0, -1);
break;
default:
var endTimeType = "day";
var endBaseValueTmp = endBaseTmp;
break;
}
}

var startBase = moment().utc().startOf(timeType);
var endBase = moment().utc().endOf(endTimeType);
// console.log(timeType, endTimeType, startBaseValueTmp, endBaseValueTmp,startBase.subtract(startBaseValueTmp, timeType), endBase.add(endBaseValueTmp, endTimeType));
return [
startBase.subtract(startBaseValueTmp, timeType),
endBase.add(endBaseValueTmp, endTimeType),
timeType
];
};
14 changes: 11 additions & 3 deletions argv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ program
.name('makelogs')
.description('A utility to generate sample log data.')
.option('-c, --count <number>', 'Total event that will be created, accepts expressions like "1m" for 1 million (b,m,t,h)', parseNumber, 14000)
.requiredOption('-d, --days <number>', 'Number of days ± today to generate data for. Use one number or two separated by a slash, e.g. "1/10" to go back one day, and forward 10', parseNumber, 1)
// .requiredOption('-d, --days <number>', 'Number of days ± today to generate data for. Use one number or two separated by a slash, e.g. "1/10" to go back one day, and forward 10', parseNumber, 1)
.option('-d, --days <number>', 'Number of days ± today to generate data for. Use one number or two separated by a slash, e.g. "1/10" to go back one day, and forward 10', parseString, null)
.option('-t, --time <...>', 'Number of days/hours/minutes ± today/now to generate data for. Use one number&first_letter or two separated by a slash, e.g. "1d/10h" to go back one day, and forward 10 hours', parseString, null)
.option('--url <url>', 'Elasticsearch url, overrides host and auth, can include any url part.')
.option('-h, --host <host>', 'The host name and port', 'localhost:9200')
.option('--auth <auth>', 'user:password when you want to connect to a secured elasticsearch cluster over basic auth', null)
Expand All @@ -34,10 +36,11 @@ program

program.parse(process.argv);

// get the start and end moments
var moments = require('./_parseDays')(program);
// // get the start and end moments
var moments = require('./_parseTime')(program);
program.start = moments[0];
program.end = moments[1];
program.timeType = moments[2];

// parsing allows short notation like "10m" or "1b"
program.total = require('./_parseCount')(program);
Expand All @@ -60,6 +63,7 @@ function parseNumberStrict (str) {
if (isNaN(num)) {
throw new TypeError(`${str} is not a number`);
}
console.log("test", num);
return num
}

Expand All @@ -74,3 +78,7 @@ function parseIndexInterval (str) {
return parseNumberStrict(str);
}
}

function parseString (str) {
return str
}