Skip to main content
summaryrefslogtreecommitdiffstats
blob: e31a99e2283559d3b61b6d6695b73a6e6fd43f2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (c) 2015 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ats.rest.internal.agile;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.impl.IAtsServer;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResourceToken;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.template.engine.PageCreator;

/**
 * David.W.Miller
 */
public class SprintPageBuilder {
   private final IAtsServer atsServer;
   private final ArtifactReadable sprint;
   private Date startDate;
   private Date endDate;
   private final SimpleDateFormat df = new SimpleDateFormat("EEE, MMM d, yyyy");
   private int numActionsCompleted = 0;
   private double workCompleted = 0;
   private double walkupCompleted = 0;
   private int numActionsStarted = 0;
   private int numActionsBacklog = 0;
   TreeMap<String, FeatureGroupSum> featureSums = new TreeMap<String, FeatureGroupSum>();

   public SprintPageBuilder(IAtsServer atsServer, ArtifactReadable sprint) {
      this.atsServer = atsServer;
      this.sprint = sprint;
   }

   public String generatePage(PageCreator page, ResourceToken token) {
      calculateData();
      page.addKeyValuePairs("sprintName", sprint.getName());
      page.addKeyValuePairs("beginDate", getStartDate());
      page.addKeyValuePairs("endDate", getEndDate());
      page.addKeyValuePairs("workCompleted", getWorkCompleted());
      page.addKeyValuePairs("walkupCompleted", getWalkupCompleted());
      page.addKeyValuePairs("numActionsCompleted", getNumActionsCompleted());
      page.addKeyValuePairs("numActionsStarted", getNumActionsStarted());
      page.addKeyValuePairs("numActionsBacklog", getNumActionsBacklog());
      page.addKeyValuePairs("tableContent", getTable());
      return page.realizePage(token);
   }

   private void calculateData() {
      initCalculations();
      calculateBacklogCount();
      calculateActionsCreated();
      featureSums.clear();
      ResultSet<ArtifactReadable> actions = sprint.getRelated(AtsRelationTypes.AgileSprintToItem_AtsItem);

      for (ArtifactReadable item : actions) {
         if (includeInCount(item)) {
            ++numActionsCompleted;
            double points = getPointsFromAction(item);
            if (item.getSoleAttributeValue(AtsAttributeTypes.UnplannedWork, false)) {
               walkupCompleted += points;
            } else {
               workCompleted += points;
            }
            updateFeatureGroupSum(item, featureSums, points);
         }
      }
   }

   private double getPointsFromAction(ArtifactReadable item) {
      double points = 0;
      ArtifactReadable agileTeam = sprint.getRelated(AtsRelationTypes.AgileTeamToSprint_AgileTeam).getOneOrNull();
      if (agileTeam != null) {
         String pointsAttrType = agileTeam.getSoleAttributeAsString(AtsAttributeTypes.PointsAttributeType, "");
         if (Strings.isValid(pointsAttrType) && pointsAttrType.equals(AtsAttributeTypes.PointsNumeric.getName())) {
            points = item.getSoleAttributeValue(AtsAttributeTypes.PointsNumeric, 0.0);
         } else {
            String value = item.getSoleAttributeAsString(AtsAttributeTypes.Points, "0");
            if (Strings.isNumeric(value)) {
               points = Double.parseDouble(value);
            }
         }
      }
      return points;
   }

   private ArtifactReadable calculateActionsCreated() {
      ArtifactReadable agileTeam = sprint.getRelated(AtsRelationTypes.AgileTeamToSprint_AgileTeam).getOneOrNull();
      if (agileTeam != null) {
         // get items created and closed during sprint
         ResultSet<ArtifactReadable> agileItems = sprint.getRelated(AtsRelationTypes.AgileSprintToItem_AtsItem);
         numActionsStarted += countIfInTimeFrame(agileItems);
         // get items created and still open (in backlog) during the sprint time frame
         ArtifactReadable backlog = agileTeam.getRelated(AtsRelationTypes.AgileTeamToBacklog_Backlog).getOneOrNull();
         if (backlog != null) {
            ResultSet<ArtifactReadable> backlogItems = backlog.getRelated(AtsRelationTypes.Goal_Member);
            numActionsStarted += countIfInTimeFrame(backlogItems);
         }
      }
      return agileTeam;
   }

   private int countIfInTimeFrame(ResultSet<ArtifactReadable> agileItems) {
      int numFound = 0;
      for (ArtifactReadable item : agileItems) {
         Date artCreated = item.getSoleAttributeValue(AtsAttributeTypes.CreatedDate, null);
         if (artCreated != null && artCreated.after(startDate) && artCreated.before(endDate)) {
            numFound++;
         }
      }
      return numFound;
   }

   private void updateFeatureGroupSum(ArtifactReadable item, TreeMap<String, FeatureGroupSum> featureSums, double points) {
      ArtifactReadable featureGroup = item.getRelated(AtsRelationTypes.AgileFeatureToItem_FeatureGroup).getExactlyOne();
      FeatureGroupSum feature = featureSums.get(featureGroup.getName());
      if (feature == null) {
         feature = new FeatureGroupSum(featureGroup.getName(),
            featureGroup.getSoleAttributeAsString(AtsAttributeTypes.Description, ""));
      }
      feature.addToSum(points);
      featureSums.put(featureGroup.getName(), feature);
   }

   private boolean includeInCount(ArtifactReadable item) {
      if ("Completed".equals(item.getSoleAttributeAsString(AtsAttributeTypes.CurrentStateType)) || "Cancelled".equals(
         item.getSoleAttributeAsString(AtsAttributeTypes.CurrentStateType))) {
         return true;
      }
      return false;
   }

   private void calculateBacklogCount() {
      ArtifactReadable team = sprint.getRelated(AtsRelationTypes.AgileTeamToSprint_AgileTeam).getOneOrNull();
      if (team != null) {
         ArtifactReadable goal = team.getRelated(AtsRelationTypes.AgileTeamToBacklog_Backlog).getOneOrNull();
         if (goal != null) {
            ResultSet<ArtifactReadable> members = goal.getRelated(AtsRelationTypes.Goal_Member);
            numActionsBacklog = members.size();
         }
      }
   }

   private void initCalculations() {
      try {
         // since the sprint start is set to whatever time the sprint date was chosen,
         // and the sprint end is similar, there is a possiblity of a gap between sprint dates
         // to close the gap, the start date is set to hour, min, sec = 0, and the end date is 23:59:59
         startDate = adjustDate((Date) sprint.getSoleAttributeValue(AtsAttributeTypes.StartDate), false);
         endDate = adjustDate((Date) sprint.getSoleAttributeValue(AtsAttributeTypes.EndDate), true);
      } catch (Exception e) {
         throw new OseeCoreException("Date not available in sprint %s", sprint.getName());
      }
      numActionsCompleted = 0;
      workCompleted = 0;
      walkupCompleted = 0;
      numActionsCompleted = 0;
      numActionsStarted = 0;
      numActionsBacklog = 0;
   }

   private Date adjustDate(Date date, boolean isEnd) {
      int hour = 0;
      int min = 0;
      int sec = 0;
      if (isEnd) {
         hour = 23;
         min = 59;
         sec = 59;
      }
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      cal.set(Calendar.HOUR_OF_DAY, hour);
      cal.set(Calendar.MINUTE, min);
      cal.set(Calendar.SECOND, sec);
      cal.set(Calendar.MILLISECOND, 0);
      return cal.getTime();
   }

   private String getStartDate() {
      return df.format(startDate);
   }

   private String getEndDate() {
      return df.format(endDate);
   }

   private String getWorkCompleted() {
      return Integer.valueOf(Double.valueOf(workCompleted).intValue()).toString();
   }

   private String getWalkupCompleted() {
      return Integer.valueOf(Double.valueOf(walkupCompleted).intValue()).toString();
   }

   private String getNumActionsCompleted() {
      return Integer.toString(numActionsCompleted);
   }

   private String getNumActionsStarted() {
      return Integer.toString(numActionsStarted);
   }

   private String getNumActionsBacklog() {
      return Integer.toString(numActionsBacklog);
   }

   private String getTable() {
      StringBuilder sb = new StringBuilder();
      for (Entry<String, FeatureGroupSum> feature : featureSums.entrySet()) {
         sb.append(feature.getValue().getHTML());
      }
      return sb.toString();
   }
}

Back to the top