Sunday, July 20, 2014

GSoC Week 09 - Starting with Application Indicators

This week i started with application indicators and as discussed previously with Pascal and Jan my first indicator to work on is Logged-in users count. Therefore i tried to record data on Logged In users.


However currently i have blocked a bit in that in by this week.


I first created the relevant table structure, DAO objects etc. for LoginValue DAO where we can store [timestamp, metric_id, logged in user count] records. Then in order to retrieve data on user log-ins i created a collector thread and implemented it with HTTPSessionListener class. So when the system starts the login data collector thread would run in each 30 seconds and create LoginValue entry and add it into data table. Then we can retrieve data from this table and draw the graph.


My intention was that when a user logs in, the sessionCreated() event will be triggered in this LoggedInUsersCountCollectorThread collector class which is given below.


public class LoggedInUsersCountCollectorThread implements Runnable, HttpSessionListener {


private boolean start;
LoginValue loginValue;
List<String> currentUsers = new ArrayList<String>();


PerformanceMonitoringService performanceMonitoringService;

public LoggedInUsersCountCollectorThread() {
           startLoggedInUsersCountCollectorThread();
}

@Override
public void run() {
Context.openSession();
performanceMonitoringService = Context.getService(PerformanceMonitoringService.class);
while (start){
int userCount = currentUsers.size();
loginValue = new LoginValue(System.currentTimeMillis(),3,userCount);
performanceMonitoringService.addLoginValue(loginValue);
try {
System.out.println("Users count " + userCount + " - sleeping 30s now");
Thread.sleep(30000);
} catch (InterruptedException e) 
{ /* ignore*/  }
    }
}

private boolean isStarted(){
return start;
}

public void startLoggedInUsersCountCollectorThread(){
start = true;
}

public void stopLoggedInUsersCountCollectorThread(){
start = false;
}

@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
currentUsers = CurrentUsers.getCurrentUsernames(httpSessionEvent.getSession());
System.out.println("User logged in " + currentUsers.size());
}


@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("User logged out " + currentUsers.size());
}



However this listener doesn't trigger when i started the module. My other functions works as expected and it updates the table etc. however since this thread always returns currentUsers list size as zero ( as sessionCreated event never triggered), the count is always recorded as 0 so i am unable to proceed to draw tables. I also tried with adding a listener entry into override-web.xml file in OpenMRS/test/resource directory as this but no luck.


<listener>
<listener-class>org.openmrs.module.systemmetrics.api.collectors.LoggedInUsersCountCollectorThread</listener-class>
</listener>



I will be discussing this with Pascal on the weekly update call this Monday and will get his advice. Based on that i hope i can correct if there is any issue or else i will be moving to try an alternative way.

No comments:

Post a Comment