Difference between two dates extracted by Audit API is not viewing.

cancel
Showing results for 
Search instead for 
Did you mean: 
Syedjunaid
Active Member II

Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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.Screenshot from 2021-04-22 04-18-25.png

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?

1 Solution

Accepted Solutions
Syedjunaid
Active Member II

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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.

thanks alott @afaust @EddieMay 

View solution in original post

5 Replies
EddieMay
Alfresco Employee

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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,

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
Syedjunaid
Active Member II

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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.

Output.png

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.

afaust
Master

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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

Syedjunaid
Active Member II

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

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.

thanks alott @afaust @EddieMay 

EddieMay
Alfresco Employee

Re: Difference between two dates extracted by Audit API is not viewing.

Jump to solution

Hi @Syedjunaid 

Well done for resolving your problem & thanks for updating your thread on how you did it - I'm sure that will be very helpful to other users.

Onwards and upwards!

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!