Perspective Log events

Hi

I would like to record the user's login and logout events in separate table in db rather than using audit events.
I have a python lib 'auditing' as:

def Login():
	import datetime
	
	user = system.tag.readBlocking("[System]Client/User/Username")[0].value
	timestamp = system.date.now()
	

	vLocation =system.tag.readBlocking("[System]Client/Network/Hostname")[0].value
	query ="Insert into tvs_mes.tbl_logevents (user,Location,LoginTime,_createdDate,_createdUser)	values(?,?,?,?,?)"
	args= [user,vLocation,timestamp,timestamp,"ign"]
	system.db.runPrepUpdate(query,args)
	
	
def Logout():
	from time import  strftime 
	user = system.tag.readBlocking("[System]Client/User/Username")[0].value
	timestamp = system.date.now()
	

	vLocation =system.tag.readBlocking("[System]Client/Network/Hostname")[0].value
	params={"iLogout":timestamp,"iMUser":"update","iUser":user,"iLocation":vLocation}
	system.db.runNamedQuery("Logging/UpdateLogout",params)

I tried calling out this function in session startup and shutdown. Unfortunately this did not work but the script worked as expected in the script console.
I even tried inserting data to this table from trigger of audit event and the written trigger in mysql is:

CREATE TRIGGER audit_events_trigger
AFTER INSERT ON audit_events
FOR EACH ROW
BEGIN
    IF NEW.ACTION = 'Login Request' THEN
        INSERT INTO tbl_logevents (user, Location, LoginTime, _createdDate, _createdUser)
        VALUES (NEW.ACTOR, NEW.ACTOR_HOST, NEW.EVENT_TIMESTAMP, NOW(), 'ign');
    ELSEIF NEW.ACTION = 'Logout Response' THEN
        UPDATE tbl_logevents
        SET LogoutTime = NEW.EVENT_TIMESTAMP,
            ActiveTime = TIMEDIFF(NEW.EVENT_TIMESTAMP, LoginTime),
            _modifiedDate = NOW(),
            _modifiedUser = 'UPDATE'
        WHERE user = NEW.ACTOR AND Location = NEW.ACTOR_HOST AND LogoutTime IS NULL;
    END IF;
END;

Both the method did not worked as expected. Any ideas to achieve this?

Have you made anymore progress on this problem?

No Justin, I have tried writing the script on change script on auth.user.timestamp. Still not achieved the result.

Perspective does not use Tags like you're thinking it does. The [System]Client tags are for Vision. You should instead use a change script on session.props.auth.user.username.

Note that I specified a change script and not the onStartup Event of the session. This is because Session startup actually occurs before any login occurs because not every project requires login, and this event must still be the first thing that happens. If you watch the Perspective Session page of the Gateway, you'll notice that new sessions get started the moment a user opens a browser tab to the project - before they've logged in.

So the code to invoke your custom library functions would look something like this:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue:  # null check
        if currentValue.value:  # new value is not null, meaning login has occurred
            auditing.Login(username=currentValue.value, userHost=self.session.props.host)
        else:
            auditing.Logout(username=previousValue.value, userHost=self.session.props.host)

Two more things to note here:

  1. As you're not reading tags, you'll need to actually pass the values you're looking for into the functions you're using.
  2. This form of auditing will miss events where a user simply closes their browser tab instead of logging out. When the session eventually times out and is closed out on the backend I don't believe the change script will execute, so you'll have no associated logout entry for the user in question.
1 Like