Wednesday, February 18, 2009

Hackystat Programming 101

Purpose:
In this blog entry I'll describe my experiences working with Hackystat services and building my first Hackystat program.

Installation:
The installation of the Hackystat services and sensor binaries were fairly straightforward, given the aid of the Hackystat wiki pages and Professor Johnson's very helpful screencasts. All links and provided show notes for the screencasts can be found here.

Building my very first Hackystat App:
The following are two code snippets taken from my first Hackystat driven application which returns back the number of instances for each day of the given month and year, and my test-driven application for determining the max number of instances in a given month:

public class HackyProg1 {

/* Constants */
public static final String SENSORBASE_URL = "http://dasha.ics.hawaii.edu:9876/sensorbase";
public static final String PROJECT_NAME = "Default";

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
boolean isHost = SensorBaseClient.isHost(SENSORBASE_URL);
System.out.println("Try to contact host is: " + isHost);
SensorShellProperties properties = new SensorShellProperties();
String user = properties.getSensorBaseUser();
String password = properties.getSensorBasePassword();
System.out.println("U/P: " + user + "/****");

/* Check to see if the user is registered with provided host */
boolean isValidUser = SensorBaseClient.isRegistered(SENSORBASE_URL, user, password);
System.out.println("Check to see if this user is valid: " + isValidUser);

/* SensorBaseClient simulates a client instance */
SensorBaseClient client = new SensorBaseClient(SENSORBASE_URL, user, password);
/* Authenticate sensor base client instance */
client.authenticate(); // Is valid if returns client instance back.

/* Display the number of instances per day of the given month and year. Finally give the total # of instances for that month. */
int totalInstances = 0;
for (ProjectSummary projectSummary : client.getMonthProjectSummary(user, PROJECT_NAME, 2008, 11).getProjectSummary()) {
for (SensorDataSummary dataSummary : projectSummary.getSensorDataSummaries().getSensorDataSummary()) {
System.out.println(String.format("%s - %s : %s instances", projectSummary.getStartTime(), projectSummary.getEndTime(), dataSummary.getNumInstances()));
totalInstances += dataSummary.getNumInstances().intValue();
}
}
System.out.println("Total instances for the month: " + totalInstances);
System.out.println("--END--");
}

}

This was my attempt at determining the max day of instances for a given year and month. Unfortunately I ran into an issue specifying the project for a user given raw sets of data. Because of this, the test application is incomplete.

public class MaxMonth {
private String host, user, password, project;

public MaxMonth(String host, String user, String password, String project) {
this.host = host;
this.user = user;
this.password = password;
this.project = project;
}

public SensorBaseClient loginUser() throws Exception {
/* Check to see if the user is registered with provided host */
boolean isValidUser = SensorBaseClient.isRegistered(host, user, password);
System.out.println("Check to see if this user is valid: " + isValidUser);

/* SensorBaseClient simulates a client instance */
SensorBaseClient client = new SensorBaseClient(host, user, password);
/* Authenticate sensor base client instance */
return client.authenticate(); // Is valid if returns client instance back.
}

/**
* Returns the day containing the the maximum number of sensor data instance give the month and year.
* @param m - month
* @param y - year
* @return
* @throws Exception
*/
public int getMaxMonth(int month, int year) throws Exception {
int maxDay = 1; // 1-31
SensorBaseClient client = loginUser();
int totalInstances = 0;
for (ProjectSummary projectSummary : client.getMonthProjectSummary(user, project, year, month).getProjectSummary()) {
for (SensorDataSummary dataSummary : projectSummary.getSensorDataSummaries().getSensorDataSummary()) {
System.out.println(String.format("%s - %s : %s instances", projectSummary.getStartTime(), projectSummary.getEndTime(), dataSummary.getNumInstances()));
totalInstances += dataSummary.getNumInstances().intValue();
}
}
return totalInstances;
}
}

public class TestMaxMonth {
/* SensorBase test instance 'overwrites' SensorBase properties */

/* Test server host name */
private String host = "";
private String user = "anthony@hackystat.org";
private SensorBaseClient client = null;

@Before
public void setUp() throws Exception {
Server server = Server.newTestInstance();
host = server.getHostName();
/* Registration test will do the following:
* Create the user and not send an email
* Set the password equal to the user name.
* */
SensorBaseClient.registerUser(host, user);
assertTrue("Testing registration", SensorBaseClient.isRegistered(host, user, user));
/* Test will fail if JAVA MAIL is NOT installed properly */

/* Authenticate newly created user client with test sensorbase server */
client = new SensorBaseClient(host, user, user);
client.authenticate();
/* Send data from client to host with new user */
/* Setting raw sensor data fields */
SensorData data = new SensorData();
data.setOwner(user);
XMLGregorianCalendar tstamp = Tstamp.makeTimestamp();
tstamp.setDay(3);
tstamp.setYear(2008);
tstamp.setMonth(2);
data.setTimestamp(tstamp);
data.setResource("/Users/hackystattest/TestHackystat.java");
data.setSensorDataType("Build");
data.addProperty("TypeOfBuild", "Development");
data.setTool("Eclipse");
client.putSensorData(data);
tstamp = Tstamp.incrementHours(tstamp, 1);
data.setTimestamp(tstamp);
client.putSensorData(data);

tstamp.setDay(15);
data.setTimestamp(tstamp);
client.putSensorData(data);
tstamp = Tstamp.incrementHours(tstamp, 1);
data.setTimestamp(tstamp);
client.putSensorData(data);
tstamp = Tstamp.incrementHours(tstamp, 1);
data.setTimestamp(tstamp);
client.putSensorData(data);

tstamp.setDay(20);
data.setTimestamp(tstamp);
data.setSensorDataType(null);
client.putSensorData(data);

/* Return data given user and timestamp */
SensorData returnedData = client.getSensorData(user, tstamp);
assertEquals("Testing returned data", tstamp, returnedData.getTimestamp());
}
@After
public void tearDown() throws Exception {
/* Delete the user */
client.deleteSensorData(user);
client.deleteUser(user);
assertFalse("Testing deletion", SensorBaseClient.isRegistered(host, user, user));
}

@Test
public void testGetMaxMonth() throws Exception {
int numInstances = client.getSensorDataIndex(user).getSensorDataRef().size();
assertEquals("Testing number of sensor data instances", 6, numInstances);

String project = "Default";
MaxMonth maxMonth = new MaxMonth(host, user, user, project);
// assertEquals("Testing getMaxMonth", 6, maxMonth.getMaxMonth(3,2008));
}
}

Overall, I still have very much to learn about using the provided Hackystat API's. Hopefully in the course of a few more days, I'll gain a better grasp on resolving my earlier issues.

No comments: