@ -7,7 +7,7 @@
const mongoose = require ( 'mongoose' ) ;
const mongoose = require ( 'mongoose' ) ;
const Link = mongoose . model ( 'Link' ) ;
const Link = mongoose . model ( 'Link' ) ;
const Link Visit = mongoose . model ( 'Link Visit' ) ;
const Resource Visit = mongoose . model ( 'Resource Visit' ) ;
const moment = require ( 'moment' ) ;
const moment = require ( 'moment' ) ;
@ -21,16 +21,18 @@ class DashboardService extends SiteService {
super ( dtp , module . exports ) ;
super ( dtp , module . exports ) ;
}
}
/ *
async getResourceVisitStats ( resourceType , resourceId ) {
*
if ( ! resourceId ) {
* USER VISIT STATS
throw new Error ( 'Invalid resource' ) ;
*
}
* /
async getUserVisitStats ( user ) {
// this will throw if not a valid ObjectId (or able to become one)
resourceId = mongoose . Types . ObjectId ( resourceId ) ;
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:us er: ${ user . _ id } :visit ` ;
const cacheKey = ` stats: ${ reso urceType . toLow erCase ( ) } : ${ resourceId . toString ( ) } :visit ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -38,18 +40,15 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getUserVisitStats' , 'generating user visit stats report' , { userId : user . _ i d } ) ;
this . log . info ( 'generating resource visit stats report' , { resourceI d } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
stats = await ResourceVisit . aggregate ( [
const linkIds = links . map ( ( link ) => link . _ id ) ;
stats = await LinkVisit . aggregate ( [
{
{
$match : {
$match : {
link : { $in : linkIds } ,
resource : resourceId ,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,
@ -86,25 +85,25 @@ class DashboardService extends SiteService {
} ,
} ,
] ) ;
] ) ;
const response = {
const response = { start : START_DATE , end : END_DATE , stats } ;
start : START_DATE ,
end : END_DATE ,
stats ,
} ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
return response ;
return response ;
}
}
/ *
/ *
*
*
* LINK VISIT STATS
* RESOURCE COUNTRY STATS
*
*
* /
* /
async getLinkVisitStats ( link ) {
async getResourceCountryStats ( resourceType , resourceId ) {
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:link: ${ link . _ id } :visit ` ;
// this will throw if not a valid ObjectId (or able to become one)
resourceId = mongoose . Types . ObjectId ( resourceId ) ;
const cacheKey = ` stats: ${ resourceType . toLowerCase ( ) } : ${ resourceId . toString ( ) } :country ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -112,15 +111,15 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getLinkVisitStats' , 'generating link visit stats report' , { linkId : link . _ i d } ) ;
this . log . info ( 'generating resource country stats report' , { resourceI d } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
stats = await Link Visit. aggregate ( [
stats = await Resource Visit. aggregate ( [
{
{
$match : {
$match : {
link : link . _ i d,
resource : resourceI d,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,
@ -130,10 +129,7 @@ class DashboardService extends SiteService {
{
{
$group : {
$group : {
_ id : {
_ id : {
year : { $year : '$created' } ,
country : '$geoip.country' ,
month : { $month : '$created' } ,
day : { $dayOfMonth : '$created' } ,
hour : { $hour : '$created' } ,
} ,
} ,
count : { $sum : 1 } ,
count : { $sum : 1 } ,
} ,
} ,
@ -141,41 +137,37 @@ class DashboardService extends SiteService {
{
{
$project : {
$project : {
_ id : false ,
_ id : false ,
date : {
country : '$_id.country' ,
$dateFromParts : {
year : '$_id.year' ,
month : '$_id.month' ,
day : '$_id.day' ,
hour : '$_id.hour' ,
} ,
} ,
count : '$count' ,
count : '$count' ,
} ,
} ,
} ,
} ,
{
{
$sort : { date : 1 } ,
$sort : { count : - 1 , country : 1 } ,
} ,
{
$limit : 10 ,
} ,
} ,
] ) ;
] ) ;
const response = {
const response = { start : START_DATE , end : END_DATE , stats } ;
start : START_DATE ,
end : END_DATE ,
stats ,
} ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
return response ;
return response ;
}
}
/ *
/ *
*
*
* USER COUNTR Y STATS
* RESOURCE CIT Y STATS
*
*
* /
* /
async getUserCountr yStats ( us er ) {
async getResourceCit yStats ( reso urceType , r esou rceId ) {
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:user: ${ user . _ id } :country ` ;
// this will throw if not a valid ObjectId (or able to become one)
resourceId = mongoose . Types . ObjectId ( resourceId ) ;
const cacheKey = ` stats: ${ resourceType . toLowerCase ( ) } : ${ resourceId . toString ( ) } :city ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -183,18 +175,15 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getUserCountryStats' , 'generating user country stats report' , { userId : user . _ i d } ) ;
this . log . info ( 'generating resource city stats report' , { resourceI d } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
stats = await ResourceVisit . aggregate ( [
const linkIds = links . map ( ( link ) => link . _ id ) ;
stats = await LinkVisit . aggregate ( [
{
{
$match : {
$match : {
link : { $in : linkIds } ,
resource : resourceId ,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,
@ -205,6 +194,8 @@ class DashboardService extends SiteService {
$group : {
$group : {
_ id : {
_ id : {
country : '$geoip.country' ,
country : '$geoip.country' ,
region : '$geoip.region' ,
city : '$geoip.city' ,
} ,
} ,
count : { $sum : 1 } ,
count : { $sum : 1 } ,
} ,
} ,
@ -213,36 +204,35 @@ class DashboardService extends SiteService {
$project : {
$project : {
_ id : false ,
_ id : false ,
country : '$_id.country' ,
country : '$_id.country' ,
region : '$_id.region' ,
city : '$_id.city' ,
count : '$count' ,
count : '$count' ,
} ,
} ,
} ,
} ,
{
{
$sort : { count : - 1 , country : 1 } ,
$sort : { count : - 1 , city : 1 , region : 1 , c ountry : 1 } ,
} ,
} ,
{
{
$limit : 10 ,
$limit : 10 ,
} ,
} ,
] ) ;
] ) ;
const response = {
const response = { start : START_DATE , end : END_DATE , stats } ;
start : START_DATE ,
end : END_DATE ,
stats ,
} ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
await cacheService . setObjectEx ( cacheKey , 60 * 5 , response ) ;
return response ;
return response ;
}
}
/ *
/ *
*
*
* LINK COUNTRY STATS
* USER VISIT STATS
*
*
* /
* /
async getLinkCountryStats ( link ) {
async getUserVisitStats ( user ) {
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:link: ${ link . _ id } :country ` ;
const cacheKey = ` stats:user- links : ${ user . _ id } :visit ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -250,15 +240,18 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getLinkCountryStats' , 'generating link country stats report' , { linkId : link . _ id } ) ;
this . log . info ( 'generating user visit stats report' , { userId : user . _ id } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
stats = await LinkVisit . aggregate ( [
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
const linkIds = links . map ( ( link ) => link . _ id ) ;
stats = await ResourceVisit . aggregate ( [
{
{
$match : {
$match : {
link : link . _ id ,
resource : { $in : linkIds } ,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,
@ -268,7 +261,10 @@ class DashboardService extends SiteService {
{
{
$group : {
$group : {
_ id : {
_ id : {
country : '$geoip.country' ,
year : { $year : '$created' } ,
month : { $month : '$created' } ,
day : { $dayOfMonth : '$created' } ,
hour : { $hour : '$created' } ,
} ,
} ,
count : { $sum : 1 } ,
count : { $sum : 1 } ,
} ,
} ,
@ -276,15 +272,19 @@ class DashboardService extends SiteService {
{
{
$project : {
$project : {
_ id : false ,
_ id : false ,
country : '$_id.country' ,
date : {
count : '$count' ,
$dateFromParts : {
year : '$_id.year' ,
month : '$_id.month' ,
day : '$_id.day' ,
hour : '$_id.hour' ,
} ,
} ,
} ,
} ,
{
count : '$count' ,
$sort : { count : - 1 , country : 1 } ,
} ,
} ,
} ,
{
{
$limit : 10 ,
$sort : { date : 1 } ,
} ,
} ,
] ) ;
] ) ;
@ -299,14 +299,14 @@ class DashboardService extends SiteService {
/ *
/ *
*
*
* USER CIT Y STATS
* USER COUNTR Y STATS
*
*
* /
* /
async getUserCit yStats ( user ) {
async getUserCountr yStats ( user ) {
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:user: ${ user . _ id } :cit y ` ;
const cacheKey = ` stats:user-links : ${ user . _ id } :countr y ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -314,7 +314,7 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getUserCityStats' , 'generating user cit y stats report' , { userId : user . _ id } ) ;
this . log . info ( 'generating user countr y stats report' , { userId : user . _ id } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
@ -322,10 +322,10 @@ class DashboardService extends SiteService {
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
const linkIds = links . map ( ( link ) => link . _ id ) ;
const linkIds = links . map ( ( link ) => link . _ id ) ;
stats = await Link Visit. aggregate ( [
stats = await Resource Visit. aggregate ( [
{
{
$match : {
$match : {
link : { $in : linkIds } ,
resource : { $in : linkIds } ,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,
@ -336,8 +336,6 @@ class DashboardService extends SiteService {
$group : {
$group : {
_ id : {
_ id : {
country : '$geoip.country' ,
country : '$geoip.country' ,
region : '$geoip.region' ,
city : '$geoip.city' ,
} ,
} ,
count : { $sum : 1 } ,
count : { $sum : 1 } ,
} ,
} ,
@ -346,13 +344,11 @@ class DashboardService extends SiteService {
$project : {
$project : {
_ id : false ,
_ id : false ,
country : '$_id.country' ,
country : '$_id.country' ,
region : '$_id.region' ,
city : '$_id.city' ,
count : '$count' ,
count : '$count' ,
} ,
} ,
} ,
} ,
{
{
$sort : { count : - 1 , city : 1 , region : 1 , c ountry : 1 } ,
$sort : { count : - 1 , country : 1 } ,
} ,
} ,
{
{
$limit : 10 ,
$limit : 10 ,
@ -370,14 +366,14 @@ class DashboardService extends SiteService {
/ *
/ *
*
*
* LINK CITY STATS
* USER CITY STATS
*
*
* /
* /
async getLinkCityStats ( link ) {
async getUserCityStats ( user ) {
const { cache : cacheService } = this . dtp . services ;
const { cache : cacheService } = this . dtp . services ;
let stats ;
let stats ;
const cacheKey = ` stats:link: ${ link . _ id } :city ` ;
const cacheKey = ` stats:user- links : ${ user . _ id } :city ` ;
if ( DashboardService . CACHE_ENABLED ) {
if ( DashboardService . CACHE_ENABLED ) {
stats = await cacheService . getObject ( cacheKey ) ;
stats = await cacheService . getObject ( cacheKey ) ;
if ( stats ) {
if ( stats ) {
@ -385,15 +381,18 @@ class DashboardService extends SiteService {
}
}
}
}
this . log . info ( 'getLinkCityStats' , 'generating link city stats report' , { linkId : link . _ id } ) ;
this . log . info ( 'generating user city stats report' , { userId : user . _ id } ) ;
const END_DATE = new Date ( ) ;
const END_DATE = new Date ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
const START_DATE = moment ( END_DATE ) . subtract ( 7 , 'day' ) . toDate ( ) ;
stats = await LinkVisit . aggregate ( [
const links = await Link . find ( { user : user . _ id } ) . lean ( ) ;
const linkIds = links . map ( ( link ) => link . _ id ) ;
stats = await ResourceVisit . aggregate ( [
{
{
$match : {
$match : {
link : link . _ id ,
resource : { $in : linkIds } ,
$and : [
$and : [
{ created : { $gt : START_DATE } } ,
{ created : { $gt : START_DATE } } ,
{ created : { $lt : END_DATE } } ,
{ created : { $lt : END_DATE } } ,