使用Javascript和Mongodb重新采样时间序列数据
Resample Time Series Data using Javascript and Mongodb
一个时间序列数据集需要从一个不规则的时间间隔变成一个规则的时间序列,可能使用插值和重采样。
Python的pandas.Dataframe.resample
是一个执行此操作的函数。Javascript可以做同样的事情吗?时间序列数据集存储在Mongodb中
这是有可能的。请记住,Pandas是为这些任务明确构建的库,并且是一个野兽,而MongoDB是一个数据库。但是,如果忽略了使用插值的可能需求,那么以下内容很有可能满足您的需求:
假设您有以下数据存储在名为devices
的MongoDB集合中
/* 0 */
{
"_id" : ObjectId("543fc08ccf1e8c06c0288802"),
"t" : ISODate("2014-10-20T14:56:44.097+02:00"),
"a" : "192.168.0.16",
"i" : 0,
"o" : 32
}
/* 1 */
{
"_id" : ObjectId("543fc08ccf1e8c06c0288803"),
"t" : ISODate("2014-10-20T14:56:59.107+02:00"),
"a" : "192.168.0.16",
"i" : 14243,
"o" : 8430
}
and so on...
,在这种情况下,大约每15秒采样一次,但它也可能是不规则的。如果您想在某一天重新采样到5分钟的边界,那么您应该执行以下操作:
var low = ISODate("2014-10-23T00:00:00.000+02:00")
var high = ISODate("2014-10-24T00:00:00.000+02:00")
var interval = 5*60*1000;
db.devices.aggregate([
{$match: {t:{$gte: low, $lt: high}, a:"192.168.0.16"}},
{$group: {
_id:{
$subtract: ["$t", {
$mod: [{
$subtract: ["$t", low]
}, interval]
}]
},
total: {$sum: 1},
incoming: {$sum: "$i"},
outgoing: {$sum: "$o"},
}
},
{
$project: {
total: true,
incoming: true,
outgoing: true,
incoming_avg: {$divide: ["$incoming", "$total"]},
outgoing_avg: {$divide: ["$outgoing", "$total"]},
},
},
{$sort: {_id : 1}}
])
这将导致如下内容
{
"result" : [
{
"_id" : ISODate("2014-10-23T07:25:00.000+02:00"),
"total" : 8,
"incoming" : 11039108,
"outgoing" : 404983,
"incoming_avg" : 1379888.5,
"outgoing_avg" : 50622.875
},
{
"_id" : ISODate("2014-10-23T07:30:00.000+02:00"),
"total" : 19,
"incoming" : 187241,
"outgoing" : 239912,
"incoming_avg" : 9854.78947368421,
"outgoing_avg" : 12626.94736842105
},
{
"_id" : ISODate("2014-10-23T07:35:00.000+02:00"),
"total" : 17,
"incoming" : 22420099,
"outgoing" : 1018766,
"incoming_avg" : 1318829.352941176,
"outgoing_avg" : 59927.41176470588
},
...
如果您想放弃总收入,那么只需在$project阶段中保留该行。incoming_average只是如何计算平均值的一个示例,以防您存储的数据类似于rrdtool命名的测量(温度、cpu、传感器数据)。如果您只在该时间逆值(即传入和传出字段)中汇总的总和之后,那么您可以将整个$project阶段排除在外。它只是用来计算时间间隔的平均值。
参见Mongo将ISODate聚合为45分钟块
相关文章:
- 时间序列数据的线性回归
- 动态时间序列C3js图表
- 用Javascript编辑范围数据(时间序列)
- 更新 FLOT 时间序列图
- 时间序列统计(如相关性、傅里叶变换)
- 如何用javascript更简洁地填充时间序列数据中缺失的值
- 如何生成时间序列的所有排列
- Javascript 添加了缺失天数的时间序列结果
- d3.js 时间序列图表上的标签
- JQPLOT 中的垂直折线图(时间序列)
- 使用套接字io和angularjs处理频繁变化的时间序列数据的策略
- C3 时间序列图表给出错误 <路径>属性的值无效
- 在 D3.js 中绘制实时时间序列时奇怪的 x 值
- 动画时间序列流程图
- 如何在 c3.js 中将零值添加到时间序列中
- 您能否告诉 JSON.Net 即使未指定也将日期时间序列化为 UTC
- 绘制 D3 折线图后如何添加更多时间序列数据
- C3.js 中的时间序列图无法访问时间戳
- 使用Dygraph很难指向近距离的时间序列点
- 使用Javascript和Mongodb重新采样时间序列数据