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.util.ReducerOptionUtility;
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 Build data for each user. 
018     * <p>
019     * Accepts the following options in the following order.
020     * <ol>
021     *  <li> Result: One of Success, Failure, or *, indicating whether the count is
022     *       just of successful builds, failed builds, or all builds. Default is "*". 
023     *  <li> Type: A string to restrict the counts to those builds with a "Type" property
024     *       matching this string, or "*" to indicate all builds regardless of Type. 
025     *       Default is "*".
026     *  <li> isCumulative: True or false. Default is false.
027     * </ol>
028     * 
029     * @author Philip Johnson
030     */
031    public class MemberBuildReducer implements TelemetryReducer {
032      private static BuildReducer genericBuildReducer = new BuildReducer();
033    
034      /**
035       * Computes and returns the required telemetry streams object.
036       *
037       * @param project The project.
038       * @param dpdClient The DPD Client.
039       * @param interval The interval.
040       * @param options The optional parameters.
041       *
042       * @return Telemetry stream collection.
043       * @throws TelemetryReducerException If there is any error.
044       */
045      public TelemetryStreamCollection compute(Project project, DailyProjectDataClient dpdClient, 
046          Interval interval, String[] options) throws TelemetryReducerException {
047        // weird. for some reason we want 'null' as default rather than '*' etc.
048        String result = null;
049        String type = null;
050        boolean isCumulative = false;
051        //process options
052        if (options.length > 3) {
053          throw new TelemetryReducerException("Member Build reducer takes 3 optional parameters.");
054        }
055    
056        if (options.length >= 1 && !"*".equals(options[0])) {
057          result = options[0];
058        }
059    
060        if (options.length >= 2 && !"*".equals(options[1])) {
061          type = options[1];
062        }
063    
064        if (options.length >= 3) {
065          isCumulative = ReducerOptionUtility.parseBooleanOption(4, options[2]);
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 MemberBuildReducer");
072        }
073    
074        // now compute the set of telemetry streams. 
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          // Now build streams for the remaining emails. 
088          for (String email : emails) {
089            streams.add(genericBuildReducer.getStream(dpdClient, project, interval, email, result, 
090                type, isCumulative, email));
091          }
092          return streams;
093        } 
094        catch (Exception e) {
095          throw new TelemetryReducerException(e);
096        }
097      }
098    
099    }