I’m trying to figure out a query to get all user status totals of each user that have logged into an area of our application within a certain time grouped by the application area.
We keep a record of each session that a user has and the duration of their “active” and “inactive”
states within that session.
So if my data looks like this:
{"userId":"user:1",
"location":"beach",
"start":0,
"end":22,
"statuses":[
{ "start":1,
"end":4,
"duration":3,
"status":"active"
},
{ "start":5,
"end":10,
"duration":5,
"status":"inactive"
},
{ "start":11,
"end":16,
"duration":5,
"status":"active"
},
{ "start":17,
"end":22,
"duration":5,
"status":"inactive"
},
]
},
{"userId":"user:1",
"location":"beach",
"start":30,
"end":45,
"statuses":[
{ "start":30,
"end":39,
"duration":9,
"status":"active"
},
{ "start":40,
"end":45,
"duration":5,
"status":"inactive"
]
},
{"userId":"user:2",
"location":"beach",
"start":3,
"end":10,
"statuses":[
{ "start":1,
"end":4,
"duration":3,
"status":"active"
},
{ "start":5,
"end":10,
"duration":5,
"status":"inactive"
}
]
},
{"userId":"user:2",
"location":"mountains",
"start":11,
"end":20,
"statuses":[
{ "start":11,
"end":16,
"duration":5,
"status":"active"
},
{ "start":17,
"end":20,
"duration":3,
"status":"inactive"
}
]
}
I’d like to find out the status totals for each user in each area starting at 0 and ending at 39
and get results like:
{
"results": [{
"area": "beach",
"users": [{
"userId": "user:1",
"active_total": 17,
"inactive_total": 10
},
{
"userId": "user: 2",
"active_total": 3,
"inactive_total": 5
}
]
},
{
"area": "mountains",
"users": [{
"userId": "user: 2",
"active_total": 5,
"inactive_total": 3
}]
}
]
}
How should i go about doing this?
I’ve been trying to UNNEST all session.statuses between 0 and 39
and GROUPING them by area but then I get a little stuck totaling the durations per user.
Are there any N1QL experts that could help me out?