All files / ee/app/assets/javascripts/burndown_chart/components burn_charts.vue

87.5% Statements 63/72
50% Branches 19/38
71.43% Functions 20/28
87.32% Lines 62/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373  6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                                                                                                                                                                   18x                                   18x     18x     1x                       1x           20x     20x     19x 1x   18x     19x 1x   18x       1x 1x   1x     1x 1x           1x 1x                   36x 2x       1x 1x   1x     2x   4x     4x 10x   4x 1x                   4x             14x 4x 2x 2x           4x     12x                                     12x 8x   8x 8x   8x   8x 3x             12x   12x           84x                                                                                                                                                                                    
<script>
import { GlAlert, GlButton, GlButtonGroup } from '@gitlab/ui';
import dateFormat from 'dateformat';
import { cloneDeep } from 'lodash';
import BurnupQuery from 'shared_queries/burndown_chart/burnup.query.graphql';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { getDayDifference, nDaysAfter, newDateAsLocaleTime } from '~/lib/utils/datetime_utility';
import { __ } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import BurndownChartData from '../burn_chart_data';
import { Namespace } from '../constants';
import BurndownChart from './burndown_chart.vue';
import BurnupChart from './burnup_chart.vue';
import OpenTimeboxSummary from './open_timebox_summary.vue';
import TimeboxSummaryCards from './timebox_summary_cards.vue';
 
export default {
  components: {
    GlAlert,
    GlButton,
    GlButtonGroup,
    BurndownChart,
    BurnupChart,
    OpenTimeboxSummary,
    TimeboxSummaryCards,
  },
  mixins: [glFeatureFlagsMixin()],
  props: {
    startDate: {
      type: String,
      required: true,
    },
    dueDate: {
      type: String,
      required: true,
    },
    milestoneId: {
      type: String,
      required: false,
      default: '',
    },
    iterationId: {
      type: String,
      required: false,
      default: '',
    },
    iterationState: {
      type: String,
      required: false,
      default: '',
    },
    fullPath: {
      type: String,
      required: false,
      default: '',
    },
    namespaceType: {
      type: String,
      required: false,
      default: Namespace.Group,
    },
    burndownEventsPath: {
      type: String,
      required: false,
      default: '',
    },
    showNewOldBurndownToggle: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  apollo: {
    report: {
      skip() {
        return !this.milestoneId && !this.iterationId;
      },
      query: BurnupQuery,
      variables() {
        return {
          id: this.iterationId || this.milestoneId,
          isIteration: Boolean(this.iterationId),
          weight: !this.issuesSelected,
        };
      },
      update(data) {
        const sparseBurnupData = data[this.parent]?.report.burnupTimeSeries || [];
        const stats = data[this.parent]?.report?.stats || {};
 
        return {
          burnupData: this.padSparseBurnupData(sparseBurnupData),
          stats: {
            complete: stats.complete?.[this.displayValue] || 0,
            incomplete: stats.incomplete?.[this.displayValue] || 0,
            total: stats.total?.[this.displayValue] || 0,
          },
        };
      },
      error() {
        this.error = __('Error fetching burnup chart data');
      },
    },
  },
  data() {
    return {
      openIssuesCount: [],
      openIssuesWeight: [],
      issuesSelected: true,
      report: {
        burnupData: [],
        stats: {
          complete: 0,
          incomplete: 0,
          total: 0,
        },
      },
      useLegacyBurndown: false,
      error: '',
    };
  },
  computed: {
    loading() {
      return this.$apollo.queries.report.loading;
    },
    burnupData() {
      return this.report.burnupData;
    },
    columns() {
      return [
        {
          title: __('Completed'),
          value: this.report.stats.complete,
        },
        {
          title: __('Incomplete'),
          value: this.report.stats.incomplete,
        },
      ];
    },
    displayValue() {
      return this.issuesSelected ? 'count' : 'weight';
    },
    parent() {
      return this.iterationId ? 'iteration' : 'milestone';
    },
    issueButtonCategory() {
      return this.issuesSelected ? 'primary' : 'secondary';
    },
    weightButtonCategory() {
      return this.issuesSelected ? 'secondary' : 'primary';
    },
    issuesCount() {
      if (this.useLegacyBurndown) {
        return this.openIssuesCount;
      }
      return this.pluckBurnupDataProperties('scopeCount', 'completedCount');
    },
    issuesWeight() {
      if (this.useLegacyBurndown) {
        return this.openIssuesWeight;
      }
      return this.pluckBurnupDataProperties('scopeWeight', 'completedWeight');
    },
  },
  methods: {
    fetchLegacyBurndownEvents() {
      this.fetchedLegacyData = true;
 
      axios
        .get(this.burndownEventsPath)
        .then((burndownResponse) => {
          const burndownEvents = burndownResponse.data;
          const burndownChartData = new BurndownChartData(
            burndownEvents,
            this.startDate,
            this.dueDate,
          ).generateBurndownTimeseries();
 
          this.openIssuesCount = burndownChartData.map((d) => [d[0], d[1]]);
          this.openIssuesWeight = burndownChartData.map((d) => [d[0], d[2]]);
        })
        .catch(() => {
          this.fetchedLegacyData = false;
          createFlash({
            message: __('Error loading burndown chart data'),
          });
        });
    },
    pluckBurnupDataProperties(total, completed) {
      return this.burnupData.map((data) => {
        return [data.date, data[total] - data[completed]];
      });
    },
    toggleLegacyBurndown(enabled) {
E      if (!this.fetchedLegacyData) {
        this.fetchLegacyBurndownEvents();
      }
      this.useLegacyBurndown = enabled;
    },
    setIssueSelected(selected) {
      this.issuesSelected = selected;
    },
    padSparseBurnupData(data) {
      // if we don't have data for the startDate, we still want to draw a point at 0
      // on the chart, so add an item to the start of the array
      const sparseBurnupData = cloneDeep(data);
      const hasDataForStartDate = sparseBurnupData.find((d) => d.date === this.startDate);
 
      if (!hasDataForStartDate) {
        sparseBurnupData.unshift({
          date: this.startDate,
          completedCount: 0,
          completedWeight: 0,
          scopeCount: 0,
          scopeWeight: 0,
        });
      }
 
      // chart runs to dueDate or the current date, whichever is earlier
      const lastDate = dateFormat(
        Math.min(Date.parse(this.dueDate), Date.parse(new Date())),
        'yyyy-mm-dd',
      );
      // similar to the startDate padding, if we don't have a value for the
      // last item in the array, we should add one. If no events occur on
      // a day then we don't get any data for that day in the response
      const hasDataForLastDate = sparseBurnupData.find((d) => d.date === lastDate);
      if (!hasDataForLastDate) {
        const lastItem = sparseBurnupData[sparseBurnupData.length - 1];
        sparseBurnupData.push({
          ...lastItem,
          date: lastDate,
        });
      }
 
      return sparseBurnupData.reduce(this.addMissingDates, []);
    },
    addMissingDates(acc, current) {
      const { date } = current;
 
      // we might not have data for every day in the timebox, as graphql
      // endpoint only returns days when events have happened
      // if the previous array item is >1 day, then fill in the gap
      // using the data from the previous entry.
      // example: [
      //   { date: '2020-08-01', count: 10 }
      //   { date: '2020-08-04', count: 12 }
      // ]
      // should be transformed to
      // example: [
      //   { date: '2020-08-01', count: 10 }
      //   { date: '2020-08-02', count: 10 }
      //   { date: '2020-08-03', count: 10 }
      //   { date: '2020-08-04', count: 12 }
      // ]
 
      // skip the start date since we have no previous values
      if (date !== this.startDate) {
        const { date: prevDate, ...previousValues } = acc[acc.length - 1] || {};
 
        const currentDateUTC = newDateAsLocaleTime(date);
        const prevDateUTC = newDateAsLocaleTime(prevDate);
 
        const gap = getDayDifference(prevDateUTC, currentDateUTC);
 
        for (let i = 1; i < gap; i += 1) {
          acc.push({
            date: dateFormat(nDaysAfter(prevDateUTC, i), 'yyyy-mm-dd'),
            ...previousValues,
          });
        }
      }
 
      acc.push(current);
 
      return acc;
    },
  },
};
</script>
 
<template>
  <div>
    <div class="burndown-header gl-display-flex gl-align-items-center gl-flex-wrap">
      <strong ref="filterLabel">{{ __('Filter by') }}</strong>
      <gl-button-group>
        <gl-button
          ref="totalIssuesButton"
          :category="issueButtonCategory"
          variant="info"
          size="small"
          @click="setIssueSelected(true)"
        >
          {{ __('Issues') }}
        </gl-button>
        <gl-button
          ref="totalWeightButton"
          :category="weightButtonCategory"
          variant="info"
          size="small"
          data-qa-selector="weight_button"
          @click="setIssueSelected(false)"
        >
          {{ __('Issue weight') }}
        </gl-button>
      </gl-button-group>
 
      <gl-button-group v-if="showNewOldBurndownToggle">
        <gl-button
          ref="oldBurndown"
          :category="useLegacyBurndown ? 'primary' : 'secondary'"
          variant="info"
          size="small"
          @click="toggleLegacyBurndown(true)"
        >
          {{ __('Legacy burndown chart') }}
        </gl-button>
        <gl-button
          ref="newBurndown"
          :category="useLegacyBurndown ? 'secondary' : 'primary'"
          variant="info"
          size="small"
          @click="toggleLegacyBurndown(false)"
        >
          {{ __('Fixed burndown chart') }}
        </gl-button>
      </gl-button-group>
    </div>
    <template v-if="iterationId">
      <timebox-summary-cards
        v-if="iterationState === 'closed'"
        :columns="columns"
        :loading="loading"
        :total="report.stats.total"
      />
      <open-timebox-summary
        v-else
        :full-path="fullPath"
        :iteration-id="iterationId"
        :namespace-type="namespaceType"
        :display-value="displayValue"
      >
        <template #default="{ columns: openColumns, loading: summaryLoading, total }">
          <timebox-summary-cards :columns="openColumns" :loading="summaryLoading" :total="total" />
        </template>
      </open-timebox-summary>
    </template>
    <div class="row">
      <gl-alert v-if="error" variant="danger" class="col-12" @dismiss="error = null">
        {{ error }}
      </gl-alert>
      <burndown-chart
        :start-date="startDate"
        :due-date="dueDate"
        :open-issues-count="issuesCount"
        :open-issues-weight="issuesWeight"
        :issues-selected="issuesSelected"
        :loading="loading"
        class="col-md-6"
      />
      <burnup-chart
        :start-date="startDate"
        :due-date="dueDate"
        :burnup-data="burnupData"
        :issues-selected="issuesSelected"
        :loading="loading"
        class="col-md-6"
      />
    </div>
  </div>
</template>