RethinkDB - How to return a sliding window on grouped data -


i have objects:

[   { 'time': 1, 'data': { '1': 10, '2': 100} },   { 'time': 2, 'data': { '1': 20, '2': 100} },   { 'time': 3, 'data': { '1': 30, '2': 200} },   { 'time': 4, 'data': { '1': 40, '2': 100} },   { 'time': 5, 'data': { '1': 50, '2': 300} },   { 'time': 6, 'data': { '1': 60, '2': 200} } ] 

and 2 variables width , overlap:

  • width - max length of 'values' list
  • overlap - number of mutual values

assume width = 3 , overlap = 2. there way obtain following?

[ { 'key': '1',     'rows': [ { 'time': 1, 'values': [10,20,30] },               { 'time': 2, 'values': [20,30,40] },               { 'time': 3, 'values': [30,40,50] },               { 'time': 4, 'values': [40,50,60] }             ]   },   { 'key': '2',     'rows': [ { 'time': 1, 'values': [100,100,200] },               { 'time': 2, 'values': [100,200,100] },               { 'time': 3, 'values': [200,100,300] },               { 'time': 4, 'values': [100,300,200] }             ]   } ] 

so far i've managed this:

[ { 'key': '1',     'row': { 'time': 1, 'values': [10,20,30,40,50,60] }   },   { 'key': '2',     'row': { 'time': 1, 'values': [100,100,200,100,300,200] }   } ] 

using this:

.concatmap(function(item) {   return item('data').keys().map(function(key) {     return {       'key': key,       'row': {          'time': item('time'),         'values': [item('data')(key)]       }     }   }) }) .group('key') .ungroup() .map(function(list) {    return list('reduction').reduce(function(left, right) {     return {       'key': left('key'),       'row': {         'time': left('row')('time'),         'values': left('row')('values').union(right('row')('values'))       }     }   }) }) 

maybe need add or change everything?

thanks.

this pretty similar mlucy's solution, doesn't assume time fields consecutive integers. data sorted time before concatmap below - large datasets, should done index.

r.expr(data)  .orderby('time')  .concatmap(function (row) {    return row('data').coerceto('array').map(function (pair) {      return { key: pair(0), value: pair(1), time: row('time') };    });  })  .group('key')  .ungroup()  .map(function (g) {    let rows = g('reduction').count().do(function (c) {      return r.range(c.sub(2)).map(function (i) {        let values = r.range(3).map(function (j) {          return g('reduction')(i.add(j))('value');        }).coerceto('array');        return { 'time': g('reduction')(i)('time'), 'values': values };      }).coerceto('array');    });    return { key: g('group'), rows: rows };  }) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -