/**
  * Test that PUT {host}/db/compress compresses the database tables.
  *
  * @throws Exception If problems occur.
  */
 @Test
 public void testCompress() throws Exception { // NOPMD
   SensorBaseClient client = new SensorBaseClient(getHostName(), adminEmail, adminPassword);
   client.authenticate();
   client.setTimeout(200000);
   client.compressTables();
 }
  /**
   * Contacts Hackystat services, gets data, and returns a summary as a string.
   *
   * @return The summary of Hackystat activity.
   */
  public String getInfo() {
    // First, validate that we are authorized for these Hackystat services.
    SensorBaseClient sensorbaseClient;
    DailyProjectDataClient dpdClient;
    try {
      sensorbaseClient = new SensorBaseClient(sensorbaseHost, user, password);
      dpdClient = new DailyProjectDataClient(dpdHost, user, password);
      sensorbaseClient.authenticate();
      dpdClient.authenticate();
    } catch (Exception e) {
      return "Error getting Hackystat info. Message is: " + e.getMessage();
    }
    // Second, validate that we can get info about the specified project.
    try {
      sensorbaseClient.inProject(projectName, projectOwner);
    } catch (Exception e) {
      return String.format("%s is not in project %s", user, projectName);
    }

    // We're OK, so figure out the time interval of interest.
    XMLGregorianCalendar endTime = Tstamp.makeTimestamp();
    XMLGregorianCalendar startTime = Tstamp.incrementMinutes(endTime, (-1 * interval));

    // Now get information.
    StringBuffer info = new StringBuffer();
    info.append(getDevEventInfo(sensorbaseClient, startTime, endTime)).append(' ');
    info.append(getCommitInfo(sensorbaseClient, startTime, endTime)).append(' ');
    info.append(getUnitTestInfo(sensorbaseClient, startTime, endTime)).append(' ');
    info.append(getBuildInfo(sensorbaseClient, startTime, endTime)).append(' ');
    info.append(getCoverageInfo(sensorbaseClient, dpdClient, startTime, endTime)).append(' ');
    info.append(getSizeInfo(sensorbaseClient, dpdClient, startTime, endTime)).append(' ');

    // Return the info we've found.
    return info.toString().trim().replace("  ", " ");
  }
  /**
   * Returns an UnitTestDailyProjectData instance.
   *
   * @param variant The representational variant requested.
   * @return The representation.
   */
  @Override
  public Representation represent(Variant variant) {
    Logger logger = this.server.getLogger();
    logger.fine("UnitTest DPD: Starting");
    if (variant.getMediaType().equals(MediaType.TEXT_XML)) {
      try {
        // [1] get the SensorBaseClient for the user making this request.
        SensorBaseClient client = super.getSensorBaseClient();
        // [2] Check the front side cache and return if the DPD is found and is OK to access.
        String cachedDpd = this.server.getFrontSideCache().get(uriUser, project, uriString);
        if ((cachedDpd != null) && client.inProject(uriUser, project)) {
          return super.getStringRepresentation(cachedDpd);
        }
        // [2] get a SensorDataIndex of UnitTest sensor data for this Project on the requested day.
        XMLGregorianCalendar startTime = Tstamp.makeTimestamp(this.timestamp);
        XMLGregorianCalendar endTime = Tstamp.incrementDays(startTime, 1);
        logger.fine("UnitTest DPD: Requesting index: " + uriUser + " " + project);
        SensorDataIndex index =
            client.getProjectSensorData(uriUser, project, startTime, endTime, "UnitTest");
        // [3] Update the counter with this data.
        logger.fine("UnitTest DPD: Got index.  " + index.getSensorDataRef().size() + " instances");
        UnitTestCounter counter = new UnitTestCounter();
        for (SensorDataRef ref : index.getSensorDataRef()) {
          counter.add(client.getSensorData(ref));
        }
        logger.fine("UnitTest DPD: Finished retrieving instances. ");

        // return resulting data
        UnitTestDailyProjectData unitTestDPD = new UnitTestDailyProjectData();
        // create the individual MemberData elements.
        String sensorBaseHost = this.server.getServerProperties().get(SENSORBASE_FULLHOST_KEY);
        for (String member : counter.getMembers()) {
          MemberData memberData = new MemberData();
          memberData.setMemberUri(sensorBaseHost + "users/" + member);
          memberData.setSuccess(counter.getPassCount(member));
          memberData.setFailure(counter.getFailCount(member));
          unitTestDPD.getMemberData().add(memberData);
        }

        unitTestDPD.setOwner(uriUser);
        unitTestDPD.setProject(project);
        unitTestDPD.setStartTime(startTime);
        unitTestDPD.setUriPattern("**"); // we don't support UriPatterns yet.

        String xmlData = makeUnitTestDPD(unitTestDPD);
        if (!Tstamp.isTodayOrLater(startTime)) {
          this.server.getFrontSideCache().put(uriUser, project, uriString, xmlData);
        }
        logRequest("UnitTest");
        return super.getStringRepresentation(xmlData);

      } catch (Exception e) {
        setStatusError("Error creating UnitTest DPD.", e);
        return null;
      }
    }
    return null;
  }
 /**
  * Returns a summary string regarding Size info, or the empty string if no info available.
  *
  * @param client The SensorBaseClient.
  * @param dpdClient The DailyProjectData client.
  * @param startTime The start time.
  * @param endTime The end time.
  * @return The summary string, or an empty string.
  */
 private String getSizeInfo(
     SensorBaseClient client,
     DailyProjectDataClient dpdClient,
     XMLGregorianCalendar startTime,
     XMLGregorianCalendar endTime) {
   String message = "";
   try {
     SensorDataIndex index =
         client.getProjectSensorData(projectOwner, projectName, startTime, endTime, "FileMetric");
     if (!index.getSensorDataRef().isEmpty()) {
       FileMetricDailyProjectData dpd =
           dpdClient.getFileMetric(projectOwner, projectName, startTime, "TotalLines");
       int totalLines = 0;
       for (FileData data : dpd.getFileData()) {
         totalLines += data.getSizeMetricValue();
       }
       message = String.format("The system has %s lines of code.", totalLines);
     }
   } catch (SensorBaseClientException e) {
     message = "Error retrieving FileMetric sensor data. ";
   } catch (DailyProjectDataClientException e) {
     message = "Error retrieving FileMetric DPD data. ";
   }
   return message;
 }
 /**
  * Returns a summary string regarding Build info, or the empty string if no info available.
  *
  * @param client The SensorBaseClient.
  * @param dpdClient The DailyProjectData client.
  * @param startTime The start time.
  * @param endTime The end time.
  * @return The summary string, or an empty string.
  */
 private String getCoverageInfo(
     SensorBaseClient client,
     DailyProjectDataClient dpdClient,
     XMLGregorianCalendar startTime,
     XMLGregorianCalendar endTime) {
   String message = "";
   try {
     SensorDataIndex index =
         client.getProjectSensorData(projectOwner, projectName, startTime, endTime, "Coverage");
     if (!index.getSensorDataRef().isEmpty()) {
       CoverageDailyProjectData dpd =
           dpdClient.getCoverage(projectOwner, projectName, startTime, "Line");
       double covered = 0.0;
       double total = 0.0;
       for (ConstructData data : dpd.getConstructData()) {
         covered += data.getNumCovered();
         total += data.getNumCovered() + data.getNumUncovered();
       }
       int percentCovered = (total == 0) ? 0 : (int) ((covered / total) * 100);
       message = String.format("Line coverage is %s percent.", percentCovered);
     }
   } catch (SensorBaseClientException e) {
     message = "Error retrieving Coverage sensor data. ";
   } catch (DailyProjectDataClientException e) {
     message = "Error retrieving Coverage DPD data. ";
   }
   return message;
 }
 /**
  * Used by the SdtSummary link to indicate how the model should be updated.
  *
  * @param sdtName The sdtName.
  * @param tool The tool.
  * @param start The start time.
  */
 public void setSensorDataDetailsProvider(String sdtName, String tool, long start) {
   detailsList.clear();
   SensorBaseClient client = ProjectBrowserSession.get().getSensorBaseClient();
   SensorDataSession session = ProjectBrowserSession.get().getSensorDataSession();
   String projectName = session.getProject().getName();
   String owner = session.getProject().getOwner();
   XMLGregorianCalendar startTime = Tstamp.makeTimestamp(start);
   XMLGregorianCalendar endTime = Tstamp.incrementDays(startTime, 1);
   try {
     SensorDataIndex index =
         client.getProjectSensorData(owner, projectName, startTime, endTime, sdtName, tool);
     for (SensorDataRef ref : index.getSensorDataRef()) {
       detailsList.add(new SensorDataDetails(ref));
     }
   } catch (Exception e) {
     System.out.println("Error getting sensor data: " + StackTrace.toString(e));
   }
 }
 /**
  * Returns a summary string regarding Build info, or the empty string if no info available.
  *
  * @param client The SensorBaseClient.
  * @param startTime The start time.
  * @param endTime The end time.
  * @return The summary string, or an empty string.
  */
 private String getBuildInfo(
     SensorBaseClient client, XMLGregorianCalendar startTime, XMLGregorianCalendar endTime) {
   String message = "";
   try {
     SensorDataIndex index =
         client.getProjectSensorData(projectOwner, projectName, startTime, endTime, "Build");
     message = getWorkers(index, "built the system.");
   } catch (Exception e) {
     message = "Error retrieving Build data. ";
   }
   return message;
 }
 /**
  * Returns a summary string regarding Commit info, or the empty string if no info available.
  *
  * @param client The SensorBaseClient.
  * @param startTime The start time.
  * @param endTime The end time.
  * @return The summary string, or an empty string.
  */
 private String getUnitTestInfo(
     SensorBaseClient client, XMLGregorianCalendar startTime, XMLGregorianCalendar endTime) {
   String message = "";
   try {
     SensorDataIndex index =
         client.getProjectSensorData(projectOwner, projectName, startTime, endTime, "UnitTest");
     message = getWorkers(index, "run tests.");
   } catch (Exception e) {
     message = "Error retrieving UnitTest data. ";
   }
   return message;
 }
 /**
  * Returns a summary string regarding DevEvent info, or the empty string if no info available.
  *
  * @param client The SensorBaseClient.
  * @param startTime The start time.
  * @param endTime The end time.
  * @return The summary string, or an empty string.
  */
 private String getDevEventInfo(
     SensorBaseClient client, XMLGregorianCalendar startTime, XMLGregorianCalendar endTime) {
   String message = "";
   try {
     SensorDataIndex index =
         client.getProjectSensorData(projectOwner, projectName, startTime, endTime, "DevEvent");
     message = getWorkers(index, "been editing files.");
   } catch (Exception e) {
     message = "Error retrieving DevEvent data. ";
   }
   return message;
 }