04-21-2021 04:21 PM
Hello everyone,
I am working on an Audit Report requested by my client. I am getting entries of users Login and Logout time but I also want how much time he spent when the user was logged in. So I simply subtracted my login time and logout and stored it in a variable in my javascript controller of my webscript.
The problem is the subtracted date and time is not appearing on Alfresco. Instead there is symbol appearing on my report.
Have a look at my js code, you may get an idea of what im trying to do.
var auditJSON = {} var connector= remote.connect("alfresco-api"); var user = page.url.args["user"]; //var result = connector.get("/-default-/public/alfresco/versions/1/audit-applications/alfresco-access/audit-entries?skipCount=0&maxItems=1000&where=(createdByUser=" + user +"%20and%20valuesKey='/alfresco-access/login/user')&include=values'"); var result = connector.get("/-default-/public/alfresco/versions/1/audit-applications/alfresco-access/audit-entries?skipCount=0&maxItems=1000000&where=(createdByUser="+ user +"%20and%20createdAt%20BETWEEN%20('2020-12-01T12%3A13%3A51.593%2B01%3A00'%20%2C%20'2021-04-25T10%3A05%3A16.536%2B01%3A00'))&include=values"); var final_results = []; if (result.status.code == status.STATUS_OK) { var auditJSON = jsonUtils.toObject(result); } var entries = auditJSON["list"]["entries"]; model.user= user; var map = {} var activities = ""; var login_date; var logout_date; var map_key = ""; var results = []; var result = {}; for(var i = 0; i < entries.length; i++){ var values = entries[i].entry.values; if(values["/alfresco-access/login/user"] != null){ activities = ""; login_date = entries[i].entry.createdAt; } else if (values["/alfresco-access/logout/user"] != null){ logout_date = entries[i].entry.createdAt; map_key = login_date + "-" + logout_date + "-" + user; map[map_key] = activities; //CALCULATING LOGIN DURATION var time_elapsed = (new Date(logout_date).getTime() - new Date(login_date).getTime())/1000/60/60; time_elapsed = Math.round(time_elapsed * 100); time_elapsed = time_elapsed / 100; result = {login_date: login_date, logout_date: logout_date, user: user, activities: activities, time_elapsed: time_elapsed} results.push(result); activities = ""; } else { activities += values["/alfresco-access/transaction/action"] + "\n" + "\n" + values["/alfresco-access/transaction/path"] + "\n" + "\n"; activities = activities.replace("/app:company_home","").replace("st:sites","Sites").replace("/cm:","/").replace("/sys:system/sys:people","User"); } } model.results = results;
Now as you can see the difference between login and logout is stored in "time_elapsed" variable.
But when I view my report a symbol is appearing instead. Take a look at the report.
As you can see in the end of every row there is this symbol appearing.
I calculated the difference of the same datetime format in an onlne js editor and the result is printing fine their. But the output is not printing on Alfresco.
Any idea how this problem can be solved?
05-27-2021 12:28 AM
I have resolved my issue with the difference between login_date and logout_date not showing.
The issue was fixed by formatting the date through splitting every element of the date like
new Date( (year),(month),(date),(minutes),(seconds),(miliseconds))
Since I was getting login and logout date from Audit Rest API, what i did is
var res = login_date.match(/\d{1,4}/g) var login_time = new Date(res[0], res[1], res[2], res[3], res[4], res[5], res[6]).getTime(); res = logout_date.match(/\d{1,4}/g) var logout_time = new Date(res[0], res[1], res[2], res[3], res[4], res[5], res[6]).getTime(); var time_elapsed = (logout_time - login_time)/1000/60/60;
Anyway the issue is resolved, however thanks for actively replying to my queries and making me more clear about the problem I was facing.
04-27-2021 03:50 AM
Hi @Syedjunaid
Can you:
1) show us the actual value that you expect to be displayed
2) print the value to the log output - how does it appear there?
Thanks,
05-19-2021 01:19 AM
Hi @EddieMay
I wanted my output to display number of hours user spent when he was logged in.
My output is printing fine in Javascript Console, I installed in alfresco.
The login and logout date is set as same as it is appearing in my report and as you can see in Javascript console it printed the output fine.
The problem is same output is not printing on my report page.
Secondly I tried to log the time_elapsed value in my catalina and alfresco logs by enabling debug mode in share/WEB-INF/classes/log4j.properties like this..
log4j.logger.org.springframework.extensions.webscripts.ScriptLogger=debug
And then I added line, logger.system.out(time_elapsed); in my js code but nothing logged in catalina.out or alfresco.log. Also tried logger.log(time_elapsed) but it didn't logged the value
Can you please tell me how can I log "time_elapsed" in my alfresco logs?
Thank you.
05-19-2021 02:16 AM
1) logger.system.out has nothing to do with the log4j logger configuration - it simply prints to the standard output stream, which is not handled by log4j, and may - depending on Tomcat configuration - be either included in catalina.out or dropped completely
2) for logging via log4j you should use logger.warn('your message') (alternatively, the operations debug(), log(), info(), error() should be available) - you should also set the logger for org.alfresco.repo.jscript.ScriptLogger to the same level in the log4j configuration, since Alfresco unfortunately has two similarily named classes used in different layers of the application (though for Repository scripts, the class I mentioned should be the one used for 99% of cases)
3) I expect your issue with the weird character to be caused by your custom FTL / rendering code, which turns the calculated value into an undisplayable or even invalid unicode character due to implicit value type conversion
05-27-2021 12:28 AM
I have resolved my issue with the difference between login_date and logout_date not showing.
The issue was fixed by formatting the date through splitting every element of the date like
new Date( (year),(month),(date),(minutes),(seconds),(miliseconds))
Since I was getting login and logout date from Audit Rest API, what i did is
var res = login_date.match(/\d{1,4}/g) var login_time = new Date(res[0], res[1], res[2], res[3], res[4], res[5], res[6]).getTime(); res = logout_date.match(/\d{1,4}/g) var logout_time = new Date(res[0], res[1], res[2], res[3], res[4], res[5], res[6]).getTime(); var time_elapsed = (logout_time - login_time)/1000/60/60;
Anyway the issue is resolved, however thanks for actively replying to my queries and making me more clear about the problem I was facing.
Explore our Alfresco products with the links below. Use labels to filter content by product module.