001    package org.hackystat.telemetry.analyzer.reducer.impl;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
007    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
008    import org.hackystat.sensorbase.resource.projects.jaxb.Property;
009    import org.hackystat.telemetry.analyzer.model.TelemetryStreamCollection;
010    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducer;
011    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducerException;
012    import org.hackystat.telemetry.analyzer.reducer.impl.UnitTestReducer.Mode;
013    import org.hackystat.telemetry.service.server.ServerProperties;
014    import org.hackystat.utilities.time.interval.Interval;
015    
016    /**
017     * Returns a set of streams providing UnitTest invocation data for each member of the project. 
018     * <p>
019     * Options:
020     * <ol>
021     * <li> mode: One of 'TotalCount', 'SuccessCount', or 'FailureCount'. Default is 'TotalCount'. 
022     * <li> isCumulative: True or false. Default is false.
023     * </ol>
024     * 
025     * @author Hongbing Kou, Philip Johnson
026     */
027    public class MemberUnitTestReducer implements TelemetryReducer { 
028      
029      private static UnitTestReducer genericUnitTestReducer = new UnitTestReducer();
030      
031      /**
032       * Computes and returns the required telemetry streams object.
033       *
034       * @param project The project.
035       * @param dpdClient The DPD Client.
036       * @param interval The interval.
037       * @param options The optional parameters.
038       *
039       * @return Telemetry stream collection.
040       * @throws TelemetryReducerException If there is any error.
041       */
042      public TelemetryStreamCollection compute(Project project, DailyProjectDataClient dpdClient, 
043          Interval interval, String[] options) throws TelemetryReducerException {
044        Mode mode = Mode.TOTALCOUNT;
045        boolean isCumulative = false;
046        //process options
047        if (options.length > 2) {
048          throw new TelemetryReducerException("MemberUnitTest reducer takes 2 optional parameters.");
049        }
050        if (options.length >= 1) {
051          try {
052            mode = Mode.valueOf(options[0].toUpperCase());
053          }
054          catch (Exception e) {
055            throw new TelemetryReducerException("Illegal mode value.", e);
056          }
057        }
058    
059        if (options.length >= 2) {
060          try {
061            isCumulative = Boolean.valueOf(options[1]);
062          }
063          catch (Exception e) {
064            throw new TelemetryReducerException("Illegal cumulative value.", e);
065          }
066        }
067        
068        // Find out the DailyProjectData host, throw error if not found.
069        String dpdHost = System.getProperty(ServerProperties.DAILYPROJECTDATA_FULLHOST_KEY);
070        if (dpdHost == null) {
071          throw new TelemetryReducerException("Null DPD host in MemberUnitTestReducer");
072        }
073    
074        // now get the telemetry stream. 
075        try {
076          TelemetryStreamCollection streams = new TelemetryStreamCollection(null, project, interval);
077          // Make a list of emails containing all members plus the owner. 
078          List<String> emails = new ArrayList<String>(); 
079          emails.addAll(project.getMembers().getMember());
080          emails.add(project.getOwner());
081          // Now remove any email that has been specified as an agent.
082          for (Property property : project.getProperties().getProperty()) {
083            if (property.getKey().equals("agent")) {
084              emails.remove(property.getValue());
085            }
086          }
087          for (String email : emails) {
088            streams.add(genericUnitTestReducer.getStream(dpdClient, project, interval, mode, email, 
089                isCumulative, email));
090          }
091          return streams;
092        } 
093        catch (Exception e) {
094          throw new TelemetryReducerException(e);
095        }
096      }
097    }