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.sensorbase.uripattern.UriPattern;
010    import org.hackystat.telemetry.analyzer.model.TelemetryStreamCollection;
011    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducer;
012    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducerException;
013    import org.hackystat.telemetry.analyzer.reducer.util.ReducerOptionUtility;
014    import org.hackystat.telemetry.service.server.ServerProperties;
015    import org.hackystat.utilities.time.interval.Interval;
016    
017    /**
018     * Returns a set of streams providing DevTime data in hours for each member of the project. 
019     * <p>
020     * Accepts the following options in the following order, although only isCumulative
021     * is supported at the current time.
022     * <ol>
023     * <li> EventType: Supply an Event Type to restrict the DevTime to just the time 
024     *      associated with that Event Type. 
025     *      Default is "*" which indicates all file types are used in computing the 
026     *      DevTime.  
027     *  <li> ResourceFilterPattern: Restricts the files over which the DevTime 
028     *       is computed. Default is "**".
029     *  <li> isCumulative: True or false. Default is false.
030     * </ol>
031     * 
032     * @author Hongbing Kou, Philip Johnson
033     */
034    public class MemberDevTimeReducer implements TelemetryReducer {
035      
036      private static DevTimeReducer genericDevTimeReducer = new DevTimeReducer();
037     
038    
039      /**
040       * Computes and returns the required telemetry streams object.
041       *
042       * @param project The project.
043       * @param dpdClient The DPD Client.
044       * @param interval The interval.
045       * @param options The optional parameters.
046       *
047       * @return Telemetry stream collection.
048       * @throws TelemetryReducerException If there is any error.
049       */
050      public TelemetryStreamCollection compute(Project project, DailyProjectDataClient dpdClient, 
051          Interval interval, String[] options) throws TelemetryReducerException {
052        // weird. for some reason we want 'null' as default rather than '*' etc.
053        String eventType = null;
054        UriPattern resourcePattern = null;
055        boolean isCumulative = false;
056        //process options
057        if (options.length > 3) {
058          throw new TelemetryReducerException("MemberDevTime reducer takes 3 optional parameters.");
059        }
060    
061        if (options.length >= 1 && ! "*".equals(options[0])) {
062          eventType = options[0];
063          eventType = eventType.toLowerCase();
064        }
065    
066        if (options.length >= 2 && ! "*".equals(options[1])) {
067          resourcePattern = new UriPattern(options[1]);
068        }
069        
070        if (options.length >= 3) {
071          isCumulative = ReducerOptionUtility.parseBooleanOption(4, options[2]);
072        }
073        
074        // Find out the DailyProjectData host, throw error if not found.
075        String dpdHost = System.getProperty(ServerProperties.DAILYPROJECTDATA_FULLHOST_KEY);
076        if (dpdHost == null) {
077          throw new TelemetryReducerException("Null DPD host in MemberDevTimeReducer");
078        }
079    
080        // now compute the set of telemetry streams. Remember, we only process the Cumulative
081        // optional parameter.
082        try {
083          TelemetryStreamCollection streams = new TelemetryStreamCollection(null, project, interval);
084          // Make a list of emails containing all members plus the owner. 
085          List<String> emails = new ArrayList<String>(); 
086          emails.addAll(project.getMembers().getMember());
087          emails.add(project.getOwner());
088          // Now remove any email that has been specified as an agent.
089          for (Property property : project.getProperties().getProperty()) {
090            if (property.getKey().equals("agent")) {
091              emails.remove(property.getValue());
092            }
093          }
094          for (String email : emails) {
095            streams.add(genericDevTimeReducer.getStream(dpdClient, project, interval, eventType, 
096                email, resourcePattern, isCumulative, email));
097          }
098          return streams;
099        } 
100        catch (Exception e) {
101          throw new TelemetryReducerException(e);
102        }
103      }
104    }