Skip to content

Commit

Permalink
fix(join): Make join robust against time series with different times
Browse files Browse the repository at this point in the history
  • Loading branch information
vinsonchuong committed Dec 12, 2019
1 parent b9d3bdb commit a02c011
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
23 changes: 20 additions & 3 deletions time-series/join/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
import type { TimeSeries, TimeSeriesPoint } from 'ikeru/time-series'
import { initialize, next } from '../tournament-tree'

export default function<Data>(
timeSeriesList: Array<TimeSeries<Data>>,
Expand All @@ -9,7 +10,23 @@ export default function<Data>(
return []
}

return timeSeriesList[0].map(({ time }, i) =>
combine(time.valueOf(), timeSeriesList.map(timeSeries => timeSeries[i]))
)
const getTime = item => item.time

let tournamentTree = initialize(timeSeriesList, getTime)
const groups = []
let currentGroup = []

while (tournamentTree.value) {
if (currentGroup[0] && tournamentTree.value.time !== currentGroup[0].time) {
groups.push(currentGroup)
currentGroup = []
}

currentGroup.push(tournamentTree.value)
tournamentTree = next(tournamentTree, getTime)
}

groups.push(currentGroup)

return groups.map(group => combine(group[0].time.valueOf(), group))
}
20 changes: 11 additions & 9 deletions time-series/join/index.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
/* @flow */
import test from 'ava'
import { sumBy } from 'lodash'
import { join } from 'ikeru/time-series'

test('combining multiple time series by time', t => {
const series1 = [
{ time: Date.parse('2019-01-01'), value: 1 },
{ time: Date.parse('2019-01-02'), value: 2 },
{ time: Date.parse('2019-01-03'), value: 3 }
{ time: Date.parse('2019-01-03'), value: 3 },
{ time: Date.parse('2019-01-04'), value: 4 }
]

const series2 = [
{ time: Date.parse('2019-01-01'), value: 4 },
{ time: Date.parse('2019-01-02'), value: 5 },
{ time: Date.parse('2019-01-03'), value: 6 }
{ time: Date.parse('2019-01-02'), value: 2 },
{ time: Date.parse('2019-01-03'), value: 3 }
]

t.deepEqual(
join([series1, series2], (time, [p1, p2]) => ({
join([series1, series2], (time, points) => ({
time,
value: p1.value + p2.value
value: sumBy(points, 'value')
})),
[
{ time: Date.parse('2019-01-01'), value: 5 },
{ time: Date.parse('2019-01-02'), value: 7 },
{ time: Date.parse('2019-01-03'), value: 9 }
{ time: Date.parse('2019-01-01'), value: 1 },
{ time: Date.parse('2019-01-02'), value: 4 },
{ time: Date.parse('2019-01-03'), value: 6 },
{ time: Date.parse('2019-01-04'), value: 4 }
]
)
})
Expand Down
2 changes: 1 addition & 1 deletion time-series/merge/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */
import type { TimeSeries } from 'ikeru/time-series'
import { initialize, next } from './tournament-tree'
import { initialize, next } from '../tournament-tree'

export default function<Data>(
...timeSeriesList: Array<TimeSeries<Data>>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a02c011

Please sign in to comment.