Source: series.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
 * Series module.
 * @module lib/series
 */

const Ajv = require("ajv");
const { Range } = require("immutable");
const is = require("is_js");
const { jStat } = require("jStat");
const math = require("mathjs");
const moment = require("moment");
const R = require("ramda");

const { randomInt } = require("./random");
const Ratio = require("./ratio");

/* Enum of Types */
const TYPE_MONOSPACED = "monospaced";
const TYPE_RANDOM = "random";

/**
 * Class generating time series data.
 * @example
 * const from     = '2016-01-01T00:00:00Z';
 * const until    = '2016-01-01T01:00:00Z';
 * const interval = 5 * 60; // seconds
 * const keyName  = 'favorite name';
 * new Series({                    from, until, interval, keyName}); // => Series' instance
 * new Series({type: 'monospaced', from, until, interval, keyName}); // => same as above
 * @example
 * const from      = '2016-01-01T00:00:00Z';
 * const until     = '2016-01-01T01:00:00Z';
 * const numOfData = 5 * 60; // seconds
 * const keyName   = 'favorite name';
 * new Series({type: 'random', from, until, numOfData, keyName}); // => Series' instance
 */
class Series {
  /**
   * Create a series.
   * @param {Object}           [options={}]
   * @param {(integer|string)} [options.type='monospaced']   - 'monospaced' or 'random'.
   * @param {(integer|string)} [options.from=<now - 1 hour>] - Lower bound of date range.
   * @param {string}           [options.until=<now>]         - Upper bound of date range.
   * @param {integer}          [options.interval=5 * 60]     - ('monospaced' only) Intervel of seconds between two data. [1 <= interval]
   * @param {integer}          [options.numOfData=10]        - ('random' only) Number of data. [0 <= numOfData]
   * @param {string}           [options.keyName='value']     - Value's key name of result.
   */
  constructor(options = {}) {
    const now = moment().startOf("second");
    const defaults = {
      type: TYPE_MONOSPACED,
      from: moment(now)
        .subtract(1, "hour")
        .toISOString(),
      until: now.toISOString(),
      interval: 5 * 60, // seconds
      numOfData: 10,
      keyName: "value"
    };
    const schema = {
      $schema: "http://json-schema.org/schema#",
      type: "object",
      properties: {
        type: {
          type: "string",
          enum: [TYPE_MONOSPACED, TYPE_RANDOM],
          default: defaults.type
        },
        from: {
          type: ["integer", "string"],
          format: "date-time",
          default: defaults.from
        },
        until: {
          type: ["integer", "string"],
          format: "date-time",
          default: defaults.until
        },
        interval: { type: "integer", minimum: 1, default: defaults.interval },
        numOfData: { type: "integer", minimum: 0, default: defaults.numOfData },
        keyName: { type: "string", default: defaults.keyName }
      },
      additionalProperties: false
    };
    const ajv = new Ajv({ useDefaults: true });
    const isValid = ajv.validate(schema, options);
    if (!isValid) {
      const error = ajv.errors[0];
      throw Error(`options${error.dataPath} ${error.message}`);
    }

    this.type = options.type;
    this.from = is.integer(options.from)
      ? moment.unix(options.from)
      : moment(options.from);
    this.until = is.integer(options.until)
      ? moment.unix(options.until)
      : moment(options.until);
    this.interval = moment.duration(options.interval, "seconds");
    this.numOfData = options.numOfData;
    this.keyName = options.keyName;
  }

  /**
   * Clone self instance.
   * @param {Object}           [options={}]
   * @param {string}           [options.type=this.type]           - 'monospaced' or 'random'.
   * @param {(integer|string)} [options.from=this.from]           - Lower bound of date range.
   * @param {(integer|string)} [options.until=this.until]         - Upper bound of date range.
   * @param {integer}          [options.interval=this.interval]   - ('monospaced' only) Intervel of seconds between two data. [1 <= interval]
   * @param {integer}          [options.numOfData=this.numOfData] - ('random' only) Number of data. [0 <= numOfData]
   * @param {string}           [options.keyName=this.keyName]     - Value's key name of result.
   * @return {Series}
   * @example
   * new Series().clone({keyName: 'changed name'}); // => Series' instance with keyName 'changed name'
   */
  clone(options = {}) {
    const defaults = {
      type: this.type,
      from: this.from.unix(),
      until: this.until.unix(),
      interval: this.interval.asSeconds(),
      numOfData: this.numOfData,
      keyName: this.keyName
    };
    return new Series(is.json(options) ? R.merge(defaults, options) : options);
  }

  /**
   * (Private) Create UNIX timestamps.
   */
  _timestamps() {
    switch (this.type) {
      case TYPE_MONOSPACED: {
        return Range(
          this.from.unix(),
          this.until.unix() + 1,
          this.interval.asSeconds()
        );
      }
      case TYPE_RANDOM: {
        const min = this.from.unix();
        const max = this.until.unix();
        return Range(0, this.numOfData)
          .map(() => randomInt(min, max))
          .sort();
      }
      default: {
        throw Error("Illegal use of Series");
      }
    }
  }

  /**
   * Return time series data by any functions using UNIX timestamp.
   * @param {function} func - (unixTimestamp) => any
   * @return {Array.<{timestamp: string, (string): any}>}
   * @example
   * new Series().generate((unix) => unix); // => [{timestamp: '2017-05-31T02:43:57.000Z', value: 1496198637}, ...]
   */
  generate(func) {
    if (is.not.function(func)) {
      throw new Error("1st argument(func) must be function");
    }
    return this._timestamps()
      .map(unix =>
        R.assoc(this.keyName, func(unix), {
          timestamp: moment.unix(unix).toISOString()
        })
      )
      .toJSON();
  }

  /**
   * (Private) Return time series data by trigonometric functions.
   */
  _trigonometric(func, options = {}) {
    const defaults = {
      coefficient: 1.0,
      constant: 0.0,
      decimalDigits: 2,
      period: 1 * 60 * 60 // seconds
    };
    const schema = {
      $schema: "http://json-schema.org/schema#",
      type: "object",
      properties: {
        coefficient: { type: "number", default: defaults.coefficient },
        constant: { type: "number", default: defaults.constant },
        decimalDigits: {
          type: "integer",
          minimum: 0,
          maximum: 10,
          default: defaults.decimalDigits
        },
        period: { type: "integer", minimum: 1, default: defaults.period }
      },
      additionalProperties: false
    };
    const ajv = new Ajv({ useDefaults: true });
    const isValid = ajv.validate(schema, options);
    if (!isValid) {
      const error = ajv.errors[0];
      throw Error(`options${error.dataPath} ${error.message}`);
    }

    const scale = (2 * Math.PI) / options.period;
    return this.generate(unix => {
      const value = options.coefficient * func(unix * scale) + options.constant;
      return math.round(value, options.decimalDigits);
    });
  }

  /**
   * Return time series data describing sine curve.
   * @param {Object}  [options={}]
   * @param {number}  [options.coefficient=1.0]    - Coefficient of sine curve.
   * @param {number}  [options.constant=0.0]       - Constant of sine curve.
   * @param {integer} [options.decimalDigits=2]    - Number of decimal places. [0 <= decimalDigits <= 10]
   * @param {integer} [options.period=1 * 60 * 60] - Period of sine curve. [1 <= period]
   * @return {Array.<{timestamp: string, (string): number}>}
   * @example
   * const coefficient   = 1;
   * const constant      = 1;
   * const decimalDigits = 3;
   * const period        = 1 * 60 * 60; // seconds
   * new Series().sin({coefficient, constant, decimalDigits, period})); // => [{timestamp: '2017-05-31T02:17:23.000Z', value: 1.969}, ...]
   */
  sin(options) {
    return this._trigonometric(Math.sin, options);
  }

  /**
   * Return time series data describing cosine curve.
   * @param {Object}  [options={}]
   * @param {number}  [options.coefficient=1.0]    - Coefficient of cosine curve.
   * @param {number}  [options.constant=0.0]       - Constant of cosine curve.
   * @param {integer} [options.decimalDigits=2]    - Number of decimal places. [0 <= decimalDigits <= 10]
   * @param {integer} [options.period=1 * 60 * 60] - Period of cosine curve. [1 <= period]
   * @return {Array.<{timestamp: string, (string): number}>}
   * @example
   * const coefficient   = 1;
   * const constant      = 1;
   * const decimalDigits = 3;
   * const period        = 1 * 60 * 60; // seconds
   * new Series().cos({coefficient, constant, decimalDigits, period})); // => [{timestamp: '2017-05-31T02:20:48.000Z', value: 0.429}, ...]
   */
  cos(options) {
    return this._trigonometric(Math.cos, options);
  }

  /**
   * Return time series data by normal distribution.
   * @param {Object}  [options={}]
   * @param {number}  [options.mean=10]         - Mean of normal distribution.
   * @param {number}  [options.variance=1]      - Variance of normal distribution.
   * @param {integer} [options.decimalDigits=2] - Number of decimal places. [0 <= decimalDigits <= 10]
   * @return {Array.<{timestamp: string, (string): number}>}
   * @example
   * const mean          = 5;
   * const variance      = 1.5;
   * const decimalDigits = 3;
   * new Series().gaussian({mean, variance, decimalDigits}); // => [{timestamp: '2017-05-31T02:25:38.000Z', value: 2.56}, ...]
   */
  gaussian(options = {}) {
    const defaults = {
      mean: 10.0,
      variance: 1.0,
      decimalDigits: 2
    };
    const schema = {
      $schema: "http://json-schema.org/schema#",
      type: "object",
      properties: {
        mean: { type: "number", default: defaults.mean },
        variance: { type: "number", default: defaults.variance },
        decimalDigits: {
          type: "integer",
          minimum: 0,
          maximum: 10,
          default: defaults.decimalDigits
        }
      },
      additionalProperties: false
    };
    const ajv = new Ajv({ useDefaults: true });
    const isValid = ajv.validate(schema, options);
    if (!isValid) {
      const error = ajv.errors[0];
      throw Error(`options${error.dataPath} ${error.message}`);
    }

    return this.generate(() => {
      const value = jStat.normal.sample(options.mean, options.variance);
      return math.round(value, options.decimalDigits);
    });
  }

  /**
   * Return time series data by ratio.
   * @param {Object.<string, integer>} weights - Map representing pairs of key and weight.
   * @return {Array.<{timestamp: string, (string): string}>}
   * @example
   * const weights = {
   *   rock    : 1,
   *   scissors: 2,
   *   paper   : 1,
   * };
   * new Series().ratio(weights); // => [{timestamp: '2017-05-31T02:30:25.000Z', value: 'rock'}, ...]
   */
  ratio(weights) {
    const ratio = new Ratio(weights);
    return this.generate(() => ratio.sample());
  }
}

module.exports = Series;