@ -167,9 +167,9 @@ const getProjectionByFullDate = (): { day: string; month: string; year: string }
} ) ;
export const aggregates = {
dailySessionsOfYesterday (
dailySessions (
collection : Collection < ISession > ,
{ year , month , day } : DestructuredDat e,
{ start , end } : DestructuredRang e,
) : AggregationCursor <
Pick < ISession , ' mostImportantRole ' | ' userId ' | ' day ' | ' year ' | ' month ' | ' type ' > & {
time : number ;
@ -178,115 +178,101 @@ export const aggregates = {
_computedAt : string ;
}
> {
return collection . aggregate <
Pick < ISession , ' mostImportantRole ' | ' userId ' | ' day ' | ' year ' | ' month ' | ' type ' > & {
time : number ;
sessions : number ;
devices : ISession [ 'device' ] [ ] ;
_computedAt : string ;
}
> (
[
{
$match : {
userId : { $exists : true } ,
lastActivityAt : { $exists : true } ,
device : { $exists : true } ,
type : 'session' ,
$or : [
{
year : { $lt : year } ,
} ,
{
year ,
month : { $lt : month } ,
} ,
{
year ,
month ,
day : { $lte : day } ,
} ,
] ,
} ,
} ,
{
$project : {
userId : 1 ,
device : 1 ,
day : 1 ,
month : 1 ,
year : 1 ,
mostImportantRole : 1 ,
time : { $trunc : { $divide : [ { $subtract : [ '$lastActivityAt' , '$loginAt' ] } , 1000 ] } } ,
} ,
} ,
{
$match : {
time : { $gt : 0 } ,
} ,
const pipeline = [
{
$match : {
userId : { $exists : true } ,
lastActivityAt : { $exists : true } ,
device : { $exists : true } ,
type : 'session' ,
. . . matchBasedOnDate ( start , end ) ,
} ,
{
$group : {
_id : {
userId : '$userId' ,
device : '$device' ,
day : '$day' ,
month : '$month' ,
year : '$year' ,
} ,
mostImportantRole : { $first : '$mostImportantRole' } ,
time : { $sum : '$time' } ,
sessions : { $sum : 1 } ,
} ,
} ,
{
$project : {
userId : 1 ,
device : 1 ,
day : 1 ,
month : 1 ,
year : 1 ,
mostImportantRole : 1 ,
time : { $trunc : { $divide : [ { $subtract : [ '$lastActivityAt' , '$loginAt' ] } , 1000 ] } } ,
} ,
{
$sort : {
time : - 1 ,
} ,
} ,
{
$match : {
time : { $gt : 0 } ,
} ,
{
$group : {
_id : {
userId : '$_id.userId' ,
day : '$_id.day' ,
month : '$_id.month' ,
year : '$_id.year' ,
} ,
mostImportantRole : { $first : '$mostImportantRole' } ,
time : { $sum : '$time' } ,
sessions : { $sum : '$sessions' } ,
devices : {
$push : {
sessions : '$sessions' ,
time : '$time' ,
device : '$_id.device' ,
} ,
} ,
} ,
{
$group : {
_id : {
userId : '$userId' ,
device : '$device' ,
day : '$day' ,
month : '$month' ,
year : '$year' ,
} ,
mostImportantRole : { $first : '$mostImportantRole' } ,
time : { $sum : '$time' } ,
sessions : { $sum : 1 } ,
} ,
{
$sort : {
_id : 1 ,
} ,
} ,
{
$sort : {
time : - 1 ,
} ,
{
$project : {
_id : 0 ,
type : { $literal : 'user_daily' } ,
_computedAt : { $literal : new Date ( ) } ,
} ,
{
$group : {
_id : {
userId : '$_id.userId' ,
day : '$_id.day' ,
month : '$_id.month' ,
year : '$_id.year' ,
userId : '$_id.userId' ,
mostImportantRole : 1 ,
time : 1 ,
sessions : 1 ,
devices : 1 ,
} ,
mostImportantRole : { $first : '$mostImportantRole' } ,
time : { $sum : '$time' } ,
sessions : { $sum : '$sessions' } ,
devices : {
$push : {
sessions : '$sessions' ,
time : '$time' ,
device : '$_id.device' ,
} ,
} ,
} ,
] ,
{ allowDiskUse : true } ,
) ;
} ,
{
$sort : {
_id : 1 ,
} ,
} ,
{
$project : {
_id : 0 ,
type : { $literal : 'user_daily' } ,
_computedAt : { $literal : new Date ( ) } ,
day : '$_id.day' ,
month : '$_id.month' ,
year : '$_id.year' ,
userId : '$_id.userId' ,
mostImportantRole : 1 ,
time : 1 ,
sessions : 1 ,
devices : 1 ,
} ,
} ,
] ;
return collection . aggregate <
Pick < ISession , ' mostImportantRole ' | ' userId ' | ' day ' | ' year ' | ' month ' | ' type ' > & {
time : number ;
sessions : number ;
devices : ISession [ 'device' ] [ ] ;
_computedAt : string ;
}
> ( pipeline , { allowDiskUse : true } ) ;
} ,
async getUniqueUsersOfYesterday (
@ -1616,4 +1602,23 @@ export class SessionsRaw extends BaseRaw<ISession> implements ISessionsModel {
return this . col . bulkWrite ( ops , { ordered : false } ) ;
}
async updateDailySessionById ( _id : ISession [ '_id' ] , record : Partial < ISession > ) : Promise < UpdateResult > {
return this . updateOne ( { _id } , { $set : record } , { upsert : true } ) ;
}
async updateAllSessionsByDateToComputed ( { start , end } : DestructuredRange ) : Promise < UpdateResult | Document > {
return this . updateMany (
{
type : 'session' ,
. . . matchBasedOnDate ( start , end ) ,
} ,
{
$set : {
type : 'computed-session' ,
_computedAt : new Date ( ) ,
} ,
} ,
) ;
}
}