Display Active Sessions

Extensibility
Extensibility covers the mechanisms by which you, as the user or developer, can extend the functionality of the Teradata Database, for example with the use of User Defined Functions, or UDFs.
Teradata Employee

Display Active Sessions

The following article describes how to fetch session data from the cache database and display it in a table.

Here is what we are going to do:

  1. Fetch our preferences to find the name of the system
  2. Fetch the active sessions that are currently running on that system
  3. Populate a table model with the active sessions
  4. Use the TableWidget to display the table model in the view

The following diagram depicts the flow for the summary screen.

Let's start at the bottom by adding a method to our service to fetch the active sessions from the cache database. Since we anticipate that we will be fetching only skewed sessions when we are finished, we will create a method in our SkewedSessionsManagerImpl class that returns a list of sessions. For now, all non-idle sessions will be returned. In the next part of this tutorial we will add code to filter the session list to only those sessions that are skewed.

SkewedSession\src\java\com\teradata\portlets\skewedsessions\service\impl\SkewedSessionsManagerImpl.java


import com.teradata.dcs.data.model.Session;
import com.teradata.portlets.skewedsessions.model.SkewedSessionsPreferences;

/**
* Gets a list of active skewed sessions running on the specified system. Each list
* item is a session model.
*
* @param preferences The user's portlet preferences for this portlet instance.
*/
public List<Session> getCurrentSkewedSessions(SkewedSessionsPreferences preferences)
{
List<Session> result = new ArrayList<Session>();
Integer id = getSystemId(preferences.getSystem());
if (id != null)
{
List<Integer> systemIds = new ArrayList<Integer>();
systemIds.add(id);

// fetch the data
result = sessionDAO.getCurrentActiveSessions(systemIds);
}
return result;
}

/**
* Get the system ID for the given system name.
*
* @param systemName The name of the system.
* @return The ID for the specified system.
*/
private Integer getSystemId(String systemName)
{
Integer result = null;

List<System> systems = sessionDAO.getAvailableSystems();

// find the system that matches the specified system name
for (System system : systems)
{
if (system.getName().equals(systemName))
{
result = system.getId();
break;
}
}
return result;
}

Because our service is accessed from the controller via an interface, we need to add this new public method to the SkewedSessionManager.java interface:



import com.teradata.dcs.data.model.Session;
import com.teradata.portlets.skewedsessions.model.SkewedSessionsPreferences;

/**
* Gets a list of active skewed sessions running on the specified system. Each list
* item is a session model.
*
* @param preferences The user's portlet preferences for this portlet instance.
*/
public List<Session> getCurrentSkewedSessions(SkewedSessionsPreferences preferences);

Now we can call our new method from our controller. Since the argument to this method is a preferences object, we first need to fetch the preferences from the database. Here are the changes to the SkewedSessionsViewController.java class.

import com.teradata.portlets.skewedsessions.model.SkewedSessionsPreferences;
import java.util.List;
import com.teradata.dcs.data.model.Session;

public void summary(ControllerContext ctx)
{
// set 'summary.jsp' as the view for this page.
ctx.setViewName("summary");

// get the user's preferences for this portlet instance
SkewedSessionsPreferences preferences = new SkewedSessionsPreferences();
preferences = (SkewedSessionsPreferences)ctx.loadPreferences("SkewedSessions", preferences);

// get the active sessions for the user's prefered system
List<Session> sessions =
skewedSessionsManager.getCurrentSkewedSessions(preferences);

Ordinarily, we would add this session list as a view object and let the view figure out how to display it, but since we are planning to use the TableWidget tag to display the data, we need to construct a TableWidget model object that the TableWidget tag will use to render an HTML table.


We could write code in the controller that picks fields out of the session model and populates a TableWidget model, but then our controller class would contain code that is very much aware of the implementation of the session model and TableWidget model. A better approach is to isolate this code in a new subclass of the TableWidget model like this new SkewedSessionsTableWidget.java class.


package com.teradata.portlets.skewedsessions.model;

import java.util.List;

import com.teradata.dcs.data.model.Session;
import com.teradata.tags.widgets.model.TableColumn;
import com.teradata.tags.widgets.model.TableWidget;

public class SkewedSessionsTableWidget extends TableWidget
{
private List<Session> sessions;

// constructor
public SkewedSessionsTableWidget()
{
// set default columns
// note: table column names must match session model field names
TableColumn[] columns = new TableColumn[] {
new TableColumn("Session ID", "sessionNo", null, ColumnType.INTEGER),
new TableColumn("Username", "userName", null, ColumnType.STRING),
new TableColumn("CPU", "requestAmpCPU", null, ColumnType.FLOAT),
new TableColumn("CPU Skew", "cpuSkew", null, ColumnType.FLOAT),
new TableColumn("Hot AMP", "hotAmp1CPUId", null, ColumnType.INTEGER),
};
setColumns(columns);
}

/**
* Sets the list of Sessions that will be used to populate the table.
*
* @param sessions The session list
*/
public void setSessions(List<Session> sessions)
{
this.sessions = sessions;
}

/**
* Builds table data from the session list
*/
@Override
public Object[][] getData()
{
Object[][] result = null;
int row = 0;
TableColumn[] columns = getColumns();

if (((sessions != null) && (sessions.size() > 0)) &&
((columns != null) && (columns.length > 0)))
{
result = new Object[sessions.size()][columns.length];
for (Session session : sessions)
{
int col = 0;
for (TableColumn column : columns)
{
result[row][col++] = session.getValue(column.getName());
}
row += 1;
}
}
else
{
result = new Object[0][0];
}
return result;
}
}


With this new class, all we need to do in the controller is create a SkewedSessionTableWidget object and add it as a view object. Here is our completed view controller:

import com.teradata.portlets.skewedsessions.model.SkewedSessionsTableWidget;

public void summary(ControllerContext ctx)
{
// set 'summary.jsp' as the view for this page.
ctx.setViewName("summary");
// get the user's preferences for this portlet instance
SkewedSessionsPreferences preferences = new SkewedSessionsPreferences();
preferences = ctx.loadPreferences(SKEWED_SESSIONS, preferences);

// get the active sessions for the user's prefered system
List<Session> sessions =
skewedSessionsManager.getCurrentSkewedSessions(preferences);

// build a TableWidget model
SkewedSessionsTableWidget tableWidget = new SkewedSessionsTableWidget();
tableWidget.setSessions(sessions);

// pass TableWidget model to the view
ctx.addViewObject("skewedSessions", tableWidget);

// pass the preferences to the view
ctx.addViewObject("preferences", preferences);
}


The last step is to add the TableWidget tag to the summary view.  Replace your summary view contents with the code below:

<%@ page contentType="text/html" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="tera" uri="http://teradata.com/tdportal"%>
<%@ taglib prefix="vp" uri="http://teradata.com/viewpoint/core"%>
<jsp:include page="include.jsp"/>
<c:choose>
<c:when test="${preferences.system == null}">
Please use preferences to choose a system.
</c:when>
<c:otherwise>
<vp:tableWidget context="${context}" model="${skewedSessions}" />
</c:otherwise>
</c:choose>

 Your summary page should now look something like this:


Tags (2)