001    package org.hackystat.telemetry.analyzer.reducer.impl;
002    
003    import java.util.List;
004    import org.hackystat.telemetry.analyzer.model.TelemetryDataPoint;
005    import org.hackystat.telemetry.analyzer.model.TelemetryStream;
006    import org.hackystat.telemetry.analyzer.model.TelemetryStreamCollection;
007    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducer;
008    import org.hackystat.telemetry.analyzer.reducer.TelemetryReducerException;
009    import org.hackystat.telemetry.analyzer.reducer.util.IntervalUtility;
010    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
011    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
012    import org.hackystat.utilities.time.interval.Interval;
013    
014    /**
015     * Draws a straight line over the chart.
016     * 
017     * @author Pavel Senin.
018     */
019    public class StraightLine implements TelemetryReducer {
020    
021      /**
022       * Performs the computation.
023       * 
024       * @param project The project.
025       * @param dpdClient The DPD Client.
026       * @param interval The interval over which the computation should be performed.
027       * @param parameters parameters, first one is initial line value, second one is delta.
028       * @return Telemetry stream collection.
029       * @throws TelemetryReducerException If there is any error.
030       */
031      public TelemetryStreamCollection compute(Project project, DailyProjectDataClient dpdClient, 
032          Interval interval, String[] parameters)
033          throws TelemetryReducerException {
034        try {
035    
036          Double initialValue = 1D;
037          Double deltaValue = 0D;
038          if (parameters.length > 0) {
039            initialValue = Double.valueOf(parameters[0]);
040          }
041          if (parameters.length > 1) {
042            deltaValue = Double.valueOf(parameters[1]);
043          }
044          Double currentValue = initialValue;
045    
046          TelemetryStream telemetryStream = new TelemetryStream(null);
047          // Use a utility class 'IntervalUtility' to break interval into periods.
048          // One period corresponds to one data point on the telemetry stream.
049          List<IntervalUtility.Period> periods = IntervalUtility.getPeriods(interval);
050          for (IntervalUtility.Period period : periods) {
051            // Compute total elapsed time for the current period.
052            // We make use of the DailyProjectSimpleSdt abstraction layer, instead of the raw data.
053            // We simply add elapsed time for each data to get the total elapsed time for this period.
054            currentValue += deltaValue;
055            TelemetryDataPoint dp = new TelemetryDataPoint(period.getTimePeriod(), new Double(
056                currentValue));
057    
058            // Add the data point to the telemetry stream.
059            telemetryStream.addDataPoint(dp);
060          }
061    
062          // Wrap the telemetry stream in a telemetry stream collection, and return it.
063          TelemetryStreamCollection streams = new TelemetryStreamCollection(null, project, interval);
064          streams.add(telemetryStream);
065          return streams;
066    
067        }
068        catch (Exception e) {
069          throw new TelemetryReducerException(e.getMessage(), e);
070        }
071      }
072    
073    }