diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d9dc3b4 Binary files /dev/null and b/.DS_Store differ diff --git a/_client.js b/_client.js index 0511642..e9bd022 100644 --- a/_client.js +++ b/_client.js @@ -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({ diff --git a/_randomEvent.js b/_randomEvent.js index 77cd4c7..8919ec7 100644 --- a/_randomEvent.js +++ b/_randomEvent.js @@ -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(); @@ -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; @@ -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) { @@ -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(); diff --git a/argv/_parseDays.js b/argv/_parseDays.js deleted file mode 100644 index 6c620f0..0000000 --- a/argv/_parseDays.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var moment = require('moment'); - -var customDayBoundsRE = /^\-?\d+\/\-?\d+$/; -var oldDayExpressionRE = /[\-\+\,]/; - -module.exports = function parseDays(argv) { - var startBase = moment().utc().startOf('day'); - var endBase = moment().utc().endOf('day'); - - if (typeof argv.days === 'number') { - return [ - startBase.subtract(argv.days, 'days'), - endBase.add(argv.days, 'days') - ]; - } - - if (typeof argv.days === 'string') { - if (customDayBoundsRE.test(argv.days)) { - var ends = argv.days.split('/').map(parseFloat); - return [ - startBase.subtract(ends[0], 'days'), - endBase.add(ends[1], 'days') - ]; - } - else if (oldDayExpressionRE.test(argv.days)) { - throw new TypeError('the format of the --days flag has changed. run `makelogs --help` for more info.') - } - } - - throw new TypeError('Unable to determine the starting and end dates.'); -}; diff --git a/argv/_parseTime.js b/argv/_parseTime.js new file mode 100644 index 0000000..71da399 --- /dev/null +++ b/argv/_parseTime.js @@ -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 + ]; +}; diff --git a/argv/index.js b/argv/index.js index ef5a96f..aa65239 100644 --- a/argv/index.js +++ b/argv/index.js @@ -8,7 +8,9 @@ program .name('makelogs') .description('A utility to generate sample log data.') .option('-c, --count ', 'Total event that will be created, accepts expressions like "1m" for 1 million (b,m,t,h)', parseNumber, 14000) - .requiredOption('-d, --days ', '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 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 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 ', 'Elasticsearch url, overrides host and auth, can include any url part.') .option('-h, --host ', 'The host name and port', 'localhost:9200') .option('--auth ', 'user:password when you want to connect to a secured elasticsearch cluster over basic auth', null) @@ -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); @@ -60,6 +63,7 @@ function parseNumberStrict (str) { if (isNaN(num)) { throw new TypeError(`${str} is not a number`); } + console.log("test", num); return num } @@ -74,3 +78,7 @@ function parseIndexInterval (str) { return parseNumberStrict(str); } } + +function parseString (str) { + return str +} \ No newline at end of file