Implementing User Stats for SUSI.AI Admin Panel

Implementing User Stats for SUSI.AI Admin Panel

SUSI.AI has an admin panel where users with roles operator or above get an overview of various stats and manage other users and skills. The admin panel allows the system admins to get a list of all users registered on susi and also provide an option to change their user roles as well. The admin tab provides us with the statistics of its users. In this post we will discuss how this is implemented on susi server.

SUSI.AI server has an API called getUsers.java which can be accessed at endpoint

/aaa/getUsers.json

 

This API has a total of 5 parameters out of which minimum two are needed to make a successful call to the API

  • access_token (required)
  • Atleast one from the following parameters
    • getUserStats
    • getPageCount
    • getUserCount
    • page

To fetch user statistics from the server we need to pass the access token and the boolean parameter getUserStats. The following call is requested from the server

https: //api.susi.ai/aaa/getUsers.json?
access_token=Djrt0w2VzJowSRfHdN8tlmEI57LZ&callback&getUserStats=true

 

When this call is made the API first verifies the user role of the user from the access token to check whether the role is operator or above. It the user fails this verification they recieve an Error : 401 stating that this user role is not sufficient. On the other hand if they are positively verified then they receive users stats in form of a json object.

{
  "accepted": true,
  "userStats": {
    "anonymous": 3,
    "users": 1165,
    "reviewers": 100,
    "operators": 200,
    "admins": 3,
    "superAdmins": 18,
    "activeUsers": 879,
    "inactiveUsers": 411,
    "totalUsers": 1490
  },
  "message": "Success: Fetched all users stats!",
  "session": {
    "identity": {
      "type": "email",
      "name": "[email protected]",
      "anonymous": false
    }
  }
}

 

working of getUsers.java

The data about user registered to susi with their roles is stored in the authorisation.json file on the server so the first step is to fetch data from authorization.json and translate it into an array of string with each value is the id of the user which is done by the following codeblock

Collection < ClientIdentity > authorized = DAO.getAuthorizedClients();

List < String > keysList = new ArrayList < String > ();

authorized.forEach(client - > keysList.add(client.toString()));

String[] keysArray = keysList.toArray(new String[keysList.size()]);

 

Once we have the array of IDs of users we can get the total user count by the size of keysArray. To calculate users with different roles we need to iterate the array and find their respective roles and then keep a count for each role. To find user role for a particular id, we use the DAO.getAuthorization(identity) method. This method returns an object with the authorization data of the identity which is passed to the function. To get its corresponding user role we define a variable userRole of data type UserRole and its value is fetched from authorization.getUserRole(). For their activation status we need their credentials using

ClientCredential(ClientCredential.Type.passwd_login, identity.getName());

 

Then we use these credentials to access their authentication object which is stored in the authentication.json file on the server. This authentication object has a boolean type key named activated which stores the activation status of the user. This is used to count users based on activation status. This along with roles status is send as the response as shown above.

Resources

Close Menu