timeseries.js refactor

This commit is contained in:
Alexander Zobnin
2017-06-25 19:03:43 +03:00
parent b0da0ffb3e
commit 8562dae998
8 changed files with 42 additions and 24 deletions

View File

@@ -5,8 +5,8 @@ import ts from './timeseries';
let downsampleSeries = ts.downsample;
let groupBy = ts.groupBy;
let sumSeries = ts.sumSeries;
let scale = ts.scale;
let delta = ts.delta;
let scale = (factor, datapoints) => ts.scale(datapoints, factor);
let SUM = ts.SUM;
let COUNT = ts.COUNT;
@@ -63,19 +63,19 @@ function extractText(str, pattern) {
function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints);
return groupBy(datapoints, interval, groupByCallback);
}
function aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy()
var flattenedPoints = _.flatten(datapoints, true);
var groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(interval, groupByCallback, flattenedPoints);
return groupBy(flattenedPoints, interval, groupByCallback);
}
function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
return groupBy(interval, groupByCallback, flattenedPoints);
return groupBy(flattenedPoints, interval, groupByCallback);
}
function timeShift(interval, range) {

View File

@@ -2,6 +2,10 @@
* timeseries.js
*
* This module contains functions for working with time series.
*
* datapoints - array of points where point is [value, timestamp]. In almost all cases (if other wasn't
* explicitly said) we assume datapoints are sorted by timestamp.
*
*/
import _ from 'lodash';
@@ -62,7 +66,7 @@ function downsample(datapoints, time_to, ms_interval, func) {
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
function groupBy(datapoints, interval, groupByCallback) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
@@ -88,7 +92,7 @@ function groupBy(interval, groupByCallback, datapoints) {
/**
* Summarize set of time series into one.
* @param {object[]} timeseries
* @param {datapoints[]} timeseries array of time series
*/
function sumSeries(timeseries) {
@@ -124,7 +128,7 @@ function sumSeries(timeseries) {
return sortByTime(new_timeseries);
}
function scale(factor, datapoints) {
function scale(datapoints, factor) {
return _.map(datapoints, point => {
return [
point[0] * factor,