2019-10-31 16:52:10 +01:00
|
|
|
/*
|
2021-01-20 02:54:11 +01:00
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
2021-02-04 03:39:13 +01:00
|
|
|
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
|
|
|
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
|
|
|
* Side Public License, v 1.
|
2019-10-31 16:52:10 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
import Chance from 'chance';
|
2021-05-25 22:31:02 +02:00
|
|
|
import { ToolingLog } from '@kbn/dev-utils';
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
import { FtrService } from '../ftr_provider_context';
|
|
|
|
|
|
|
|
let __CACHED_SEED__: number | undefined;
|
|
|
|
function getSeed(log: ToolingLog) {
|
|
|
|
if (__CACHED_SEED__ !== undefined) {
|
|
|
|
return __CACHED_SEED__;
|
|
|
|
}
|
|
|
|
|
|
|
|
__CACHED_SEED__ = Date.now();
|
|
|
|
log.debug('randomness seed: %j', __CACHED_SEED__);
|
|
|
|
return __CACHED_SEED__;
|
|
|
|
}
|
2019-10-31 16:52:10 +01:00
|
|
|
|
|
|
|
interface CharOptions {
|
|
|
|
pool?: string;
|
|
|
|
alpha?: boolean;
|
|
|
|
numeric?: boolean;
|
|
|
|
symbols?: boolean;
|
|
|
|
casing?: 'lower' | 'upper';
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StringOptions extends CharOptions {
|
|
|
|
length?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NumberOptions {
|
|
|
|
min?: number;
|
|
|
|
max?: number;
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
export class RandomnessService extends FtrService {
|
|
|
|
private readonly chance = new Chance(getSeed(this.ctx.getService('log')));
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
/**
|
|
|
|
* Generate a random natural number
|
|
|
|
*
|
|
|
|
* range: 0 to 9007199254740991
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
naturalNumber(options?: NumberOptions) {
|
|
|
|
return this.chance.natural(options);
|
|
|
|
}
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
/**
|
|
|
|
* Generate a random integer
|
|
|
|
*/
|
|
|
|
integer(options?: NumberOptions) {
|
|
|
|
return this.chance.integer(options);
|
|
|
|
}
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
/**
|
|
|
|
* Generate a random number, defaults to at least 4 and no more than 8 syllables
|
|
|
|
*/
|
|
|
|
word(options: { syllables?: number } = {}) {
|
|
|
|
const { syllables = this.naturalNumber({ min: 4, max: 8 }) } = options;
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
return this.chance.word({
|
|
|
|
syllables,
|
|
|
|
});
|
|
|
|
}
|
2019-10-31 16:52:10 +01:00
|
|
|
|
2021-05-25 22:31:02 +02:00
|
|
|
/**
|
|
|
|
* Generate a random string, defaults to at least 8 and no more than 15 alpha-numeric characters
|
|
|
|
*/
|
|
|
|
string(options: StringOptions = {}) {
|
|
|
|
return this.chance.string({
|
|
|
|
length: this.naturalNumber({ min: 8, max: 15 }),
|
|
|
|
...(options.pool === 'undefined' ? { alpha: true, numeric: true, symbols: false } : {}),
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
2019-10-31 16:52:10 +01:00
|
|
|
}
|