From 72da06a78f3819b02f8e676f6df51d5452f0e844 Mon Sep 17 00:00:00 2001 From: Moe Date: Wed, 27 Oct 2021 10:14:24 -0700 Subject: [PATCH] Show Number of Events for last 24 hours --- languages/en_CA.json | 1 + libs/webServerPaths.js | 71 +++++++++++++++++++++---------- web/assets/js/bs5.recentVideos.js | 18 ++++++++ web/assets/js/bs5.videos.js | 24 +++++++++++ 4 files changed, 91 insertions(+), 23 deletions(-) diff --git a/languages/en_CA.json b/languages/en_CA.json index 361807f1..515081eb 100644 --- a/languages/en_CA.json +++ b/languages/en_CA.json @@ -1245,5 +1245,6 @@ "Playback": "Playback", "Backup": "Backup", "Close All Monitors": "Close All Monitors", + "Daily Events": "Daily Events", "Cloud": "Cloud" } diff --git a/libs/webServerPaths.js b/libs/webServerPaths.js index 78d69219..8335fc6a 100644 --- a/libs/webServerPaths.js +++ b/libs/webServerPaths.js @@ -974,29 +974,54 @@ module.exports = function(s,config,lang,app,io){ const monitorId = req.params.id const groupKey = req.params.ke const hasRestrictions = userDetails.sub && userDetails.allmonitors !== '1'; - s.sqlQueryBetweenTimesWithPermissions({ - table: 'Events', - user: user, - groupKey: req.params.ke, - monitorId: req.params.id, - startTime: req.query.start, - endTime: req.query.end, - startTimeOperator: req.query.startOperator, - endTimeOperator: req.query.endOperator, - limit: req.query.limit, - endIsStartTo: true, - parseRowDetails: true, - noFormat: true, - noCount: true, - rowName: 'events', - preliminaryValidationFailed: ( - user.permissions.watch_videos === "0" || - hasRestrictions && - (!userDetails.video_view || userDetails.video_view.indexOf(monitorId)===-1) - ) - },(response) => { - res.end(s.prettyPrint(response)) - }) + const monitorRestrictions = s.getMonitorRestrictions(user.details,monitorId) + const preliminaryValidationFailed = ( + user.permissions.watch_videos === "0" || + hasRestrictions && + (!userDetails.video_view || userDetails.video_view.indexOf(monitorId)===-1) + ); + if(req.query.onlyCount === '1' && !preliminaryValidationFailed){ + const response = {ok: true} + s.knexQuery({ + action: "count", + columns: "mid", + table: "Events", + where: [ + ['ke','=',groupKey], + ['time','>=',req.query.start], + ['time','<=',req.query.end], + monitorRestrictions + ] + },(err,r) => { + if(err){ + s.debugLog(err) + response.ok = false + }else{ + response.count = r[0]['count(`mid`)'] + } + s.closeJsonResponse(res,response) + }) + }else{ + s.sqlQueryBetweenTimesWithPermissions({ + table: 'Events', + user: user, + groupKey: req.params.ke, + monitorId: req.params.id, + startTime: req.query.start, + endTime: req.query.end, + startTimeOperator: req.query.startOperator, + endTimeOperator: req.query.endOperator, + limit: req.query.limit, + endIsStartTo: true, + parseRowDetails: true, + noFormat: true, + noCount: true, + rowName: 'events', + preliminaryValidationFailed: preliminaryValidationFailed + },(response) => { + res.end(s.prettyPrint(response)) + }) + } }) }) /** diff --git a/web/assets/js/bs5.recentVideos.js b/web/assets/js/bs5.recentVideos.js index ea04443c..e213a8c5 100644 --- a/web/assets/js/bs5.recentVideos.js +++ b/web/assets/js/bs5.recentVideos.js @@ -13,9 +13,27 @@ $(document).ready(function(){ $.each(videos,function(n,row){ drawRowToList(row) }) + getCountOfEvents({ + monitorId: options.monitorId, + }) callback(data) }) } + function getCountOfEvents(options){ + var monitorId = options.monitorId + var loadedMonitor = loadedMonitors[monitorId] + options.onlyCount = '1'; + if(!options.startDate)options.startDate = moment().subtract(24, 'hour').utc()._d + if(!options.endDate)options.endDate = moment().add(1, 'hour').utc()._d + getEvents(options,function(data){ + var eventDesignationText = `${lang['All Monitors']}` + if(monitorId){ + eventDesignationText = `${loadedMonitor ? loadedMonitor.name : monitorId}` + } + $('.events_from_last_24_which_monitor').text(eventDesignationText) + $('.events_from_last_24').text(data.count) + }) + } monitorList.change(function(){ var theSelected = `${monitorList.val()}` loadVideos({ diff --git a/web/assets/js/bs5.videos.js b/web/assets/js/bs5.videos.js index cf43904f..18595ad0 100644 --- a/web/assets/js/bs5.videos.js +++ b/web/assets/js/bs5.videos.js @@ -155,6 +155,30 @@ function getVideos(options,callback){ }) }) } +function getEvents(options,callback){ + options = options ? options : {} + var requestQueries = [] + var monitorId = options.monitorId + var limit = options.limit || 5000 + var eventStartTime + var eventEndTime + // var startDate = options.startDate + // var endDate = options.endDate + if(options.startDate){ + eventStartTime = formattedTimeForFilename(options.startDate,false) + requestQueries.push(`start=${eventStartTime}`) + } + if(options.endDate){ + eventEndTime = formattedTimeForFilename(options.endDate,false) + requestQueries.push(`end=${eventEndTime}`) + } + if(options.onlyCount){ + requestQueries.push(`onlyCount=1`) + } + $.get(`${getApiPrefix(`events`)}${monitorId ? `/${monitorId}` : ''}?${requestQueries.join('&')}`,function(eventData){ + callback(eventData) + }) +} $(document).ready(function(){ $('body') .on('click','.open-video',function(){