我有以下查询
engagement_metrics = EngagementMetric.where(engagement_id: engagement_ids).order('metrics_date desc').limit(7).group_by { |p| p.metrics_date }
会产生类似这样的内容
{
"2016-05-13": [
{
"id": 4,
"provider": "facebook",
"likes": -2,
"comments": 0,
"shares": 0,
"views": 0,
"reach": 0,
"reactions": {
"sad_count": "0",
"wow_count": "-1",
"haha_count": "0",
"like_count": "-1",
"love_count": "0",
"angry_count": "0"
}
},
{
"id": 5,
"provider": "facebook",
"likes": 2,
"comments": 2,
"shares": 2,
"views": 2,
"reach": 0,
"reactions": {
"sad_count": "0",
"wow_count": "0",
"haha_count": "0",
"like_count": "0",
"love_count": "0",
"angry_count": "0"
}
}
],
"2016-05-12": [
{
"id": 3,
"provider": "facebook",
"likes": 1,
"comments": 3,
"shares": 0,
"views": 0,
"reach": 0,
"reactions": {
"sad_count": "1",
"wow_count": "0",
"haha_count": "0",
"like_count": "0",
"love_count": "0",
"angry_count": "0"
},
"engagement_id": 1,
"participation_id": 1,
"campaign_id": 1,
"influencer_authorization_id": 1,
"influencer_id": 1,
"social_account_id": 1,
"metrics_date": "2016-05-12",
"status": "processed",
"deleted_at": null,
"created_at": "2016-05-14T11:36:55.995Z",
"updated_at": "2016-05-14T11:36:55.995Z"
}
],
"2016-05-11": [
{
"id": 2,
"provider": "facebook",
"likes": 0,
"comments": 16,
"shares": 0,
"views": 0,
"reach": 0,
"reactions": {
"sad_count": "0",
"wow_count": "0",
"haha_count": "0",
"like_count": "0",
"love_count": "0",
"angry_count": "0"
}
}
],
"2016-05-10": [
{
"id": 1,
"provider": "facebook",
"likes": 3,
"comments": 4,
"shares": 0,
"views": 0,
"reach": 0,
"reactions": {
"sad_count": "0",
"wow_count": "1",
"haha_count": "0",
"like_count": "1",
"love_count": "1",
"angry_count": "0"
}
}
]
}
这是迭代获取如下数据的最佳方式
[
{
"date": "24/03/16",
"metrics": {
"likes_count": "29",
"comments_count": "456",
"shares_count": "234",
"views_count": "65",
"clicks_count": "123"
}
},
{
"date": "25/03/16",
"metrics": {
"likes_count": "345",
"comments_count": "234",
"shares_count": "876",
"views_count": "345",
"clicks_count": "45"
}
},
{
"date": "26/03/16",
"metrics": {
"likes_count": "345",
"comments_count": "265",
"shares_count": "243",
"views_count": "165",
"clicks_count": "87"
}
},
{
"date": "27/03/16",
"metrics": {
"likes_count": "376",
"comments_count": "87",
"shares_count": "54",
"views_count": "754",
"clicks_count": "34"
}
},
{
"date": "28/03/16",
"metrics": {
"likes_count": "103",
"comments_count": "324",
"shares_count": "405",
"views_count": "87",
"clicks_count": "354"
}
},
{
"date": "29/03/16",
"metrics": {
"likes_count": "23",
"comments_count": "65",
"shares_count": "234",
"views_count": "87",
"clicks_count": "34"
}
},
{
"date": "30/03/16",
"metrics": {
"likes_count": "98",
"comments_count": "576",
"shares_count": "34",
"views_count": "365",
"clicks_count": "212"
}
}
]
答案 0 :(得分:2)
出于性能原因,您应该始终尽力在数据库中进行分组(使用return (int) players.stream()
.filter(p -> p.level > average)
.count();
)而不是ruby代码(java -cp /path/to/my/jar/dependencies -jar file.jar com.package.of.my.main.class.MyMainClass "any arguments if any"
)。我认为您可以使用自定义选择和分组检索您请求的内容,即给定列的每日总和:
java -jar file.jar
即。这将汇总每日组中的所有数据并返回结果。如果您希望在“metrics”子项下使用.appear(可以使用custom as_json
method完成),您只需要构建一个不同的JSON。