<%
include("util/constants.jag");
var log = new Log();
var multitenantUtils = Packages.org.wso2.carbon.utils.multitenancy.MultitenantUtils;

var cookie = request.getParameter("cookie");

var user = request.getParameter("user");
var userName = request.getParameter("username");

fetchData();
function fetchData(){

  if(cookie != null) {
    session.put('auth-cookie', cookie);
  }

  if(user != null) {
    session.put('user', user);
  } else {
    user = session.get('user');
  }

  var tenantDomain = multitenantUtils.getTenantDomain(user);

  var Base64 = Packages.org.apache.commons.codec.binary.Base64;

  var str = new Packages.java.lang.String(USERNAME + ':' + PASSWORD);
  var base64 = Base64.encodeBase64(str.getBytes());

  var base64Str = new java.lang.String(base64);

  var headers = {"Authorization":"Basic " + base64Str, "Content-Type":"application/json"};

  var data = {"tableName":"ORG_WSO2_IS_ANALYTICS_STREAM_ACTIVESESSIONS", "query":"tenantDomain:" + tenantDomain, "start":0, "count":10};

  if(userName != null ) {
     data = {"tableName":"ORG_WSO2_IS_ANALYTICS_STREAM_ACTIVESESSIONS", "query":"tenantDomain:" + tenantDomain + " AND userName:" + userName ,"start":0, "count":3};
  }

  var jsonResult = post(DAS_SERVER_URL, stringify(data), headers,"json");
  var dataResult = jsonResult.data;

  var tableRows = new Array ();

   if(dataResult != null) {
      for (i = 0; i < dataResult.length; i++) {

        var obj = {};
        obj.userName = dataResult[i].values.username;
        obj.IP = dataResult[i].values.remoteIp;
        obj.userAgent = dataResult[i].values.userAgent;
        obj.startTimestamp = new Date(dataResult[i].values.startTimestamp).toString("MMM dd");
        obj.sessionId = dataResult[i].values.sessionId;

        tableRows [i] = obj;
      }
   }
  print(tableRows);
}

%>
