All files / kanbn/src/controller burndown.js

97.12% Statements 169/174
88.09% Branches 37/42
100% Functions 4/4
97.12% Lines 169/174

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 1751x 1x 1x 1x 1x 1x 1x 1x 5x 9x 9x 9x 9x 29x 29x 29x 29x 29x 29x 29x 9x 9x 29x 4x 4x 29x 29x 29x 4x 4x 4x 25x 25x 25x 25x 9x 9x 5x 5x 9x     1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 21x 21x 210x 210x 21x 5x 5x 1x 1x 1x 9x 9x 9x 1x 1x 1x 8x 8x 8x 8x 9x 1x 1x 1x 1x 1x 8x 8x 8x 9x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 7x 7x 7x 9x 1x 1x 7x 7x 7x 9x 1x 1x 7x 7x 7x 9x 1x 1x     1x 7x 7x 7x 7x 7x 7x 2x 2x 2x 7x 5x 5x 5x 5x 5x 5x 5x 6x 6x 6x 6x 384x 384x 384x 384x 384x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x   7x 1x  
const kanbn = require('../main');
const utility = require('../utility');
const asciichart = require('asciichart');
const term = require('terminal-kit').terminal;
const chrono = require('chrono-node');
const formatDate = require('dateformat');
 
const getLabelPlacements = (from, to, width, dateFormat, maxLabels) => {
  for (let count = maxLabels; count >= 1; count--) {
    const placements = [];
    let previousEnd = -1;
 
    for (let i = 0; i < count; i++) {
      const ratio = count === 1 ? 0 : i / (count - 1);
      const position = Math.round(ratio * (width - 1));
      const value = new Date(from.getTime() + Math.round((to.getTime() - from.getTime()) * ratio));
      const label = formatDate(value, dateFormat);
 
      let start = position - Math.floor(label.length / 2);
      if (start < 0) {
        start = 0;
      }
      if (start + label.length > width) {
        start = Math.max(0, width - label.length);
      }
 
      const end = start + label.length - 1;
      if (i > 0 && start <= previousEnd + 1) {
        placements.length = 0;
        break;
      }
 
      placements.push({ position, start, label, end });
      previousEnd = end;
    }
 
    if (placements.length) {
      return placements;
    }
  }

  return [];
};
 
const renderXAxisLabels = (from, to, width, dateFormat, leftPadding) => {
  const sampleLabel = formatDate(from, dateFormat);
  const maxLabels = Math.max(1, Math.floor((width + 2) / (sampleLabel.length + 2)));
  const placements = getLabelPlacements(from, to, width, dateFormat, maxLabels);
 
  const ticks = Array(width).fill(' ');
  const labels = Array(width).fill(' ');
 
  for (const placement of placements) {
    ticks[placement.position] = '|';
    for (let i = 0; i < placement.label.length; i++) {
      labels[placement.start + i] = placement.label[i];
    }
  }
 
  return `${leftPadding}${ticks.join('')}\n${leftPadding}${labels.join('')}`;
};
 
module.exports = async args => {
 
  // Make sure kanbn has been initialised
  if (!await kanbn.initialised()) {
    utility.error('Kanbn has not been initialised in this folder\nTry running: {b}kanbn init{b}');
    return;
  }
  const index = await kanbn.getIndex();
 
  // Get sprint numbers or names
  let sprints = null;
  if (args.sprint) {
    sprints = utility.arrayArg(args.sprint).map(s => {
      const sprintNumber = parseInt(s);
      return isNaN(sprintNumber) ? s : sprintNumber;
    });
  }
 
  // Get dates
  let dates = null;
  if (args.date) {
    dates = utility.arrayArg(args.date);
    if (dates.length) {
      for (let i = 0; i < dates.length; i++) {
        const dateValue = chrono.parseDate(dates[i]);
        if (dateValue === null) {
          utility.error('Unable to parse date');
          return;
        }
        dates[i] = dateValue;
      }
    }
  }
 
  // Get assigned
  let assigned = null;
  if (args.assigned) {
    assigned = utility.strArg(args.assigned);
  }
 
  // Get columns
  let columns = null;
  if (args.column) {
    columns = utility.arrayArg(args.column);
  }
 
  // Get normalisation mode
  let normalise = null;
  if (args.normalise) {
    normalise = args.normalise.toLowerCase();
    if (!['days', 'hours', 'minutes', 'seconds'].includes(normalise)) {
      normalise = 'auto';
    }
  }
 
  // Show burndown chart
  kanbn
  .burndown(sprints, dates, assigned, columns, normalise)
  .then(data => {
    if (args.json) {
 
      // Output raw data
      console.log(JSON.stringify(data, null, 2));
    } else {
 
      // Render chart
      const PADDING = '     ';
      const width = Math.max(1, term.width - (PADDING.length + 1));
 
      const plots = [];
      for (const s of data.series) {
        const plot = [];
        const span = Math.max(1, s.to.getTime() - s.from.getTime());
        const delta = width > 1 ? span / (width - 1) : 0;
        for (let i = 0; i < width; i++) {
          const x = new Date(s.from.getTime() + Math.round(i * delta));
          plot.push((s.dataPoints.find(d => d.x >= x) || s.dataPoints[0]).y);
        }
        plots.push(plot);
      }
 
      const referenceSeries = data.series[0];
      const dateFormat = kanbn.getDateFormat(index);
      console.log(`${formatDate(referenceSeries.from, dateFormat)} to ${formatDate(referenceSeries.to, dateFormat)}:`);
      console.log(asciichart.plot(
        plots,
        {
          offset: 2,
          height: 10,
          padding: PADDING,
          format: x => (PADDING + x.toFixed(0)).slice(-PADDING.length),
          colors: [
            asciichart.default,
            asciichart.green,
            asciichart.blue,
            asciichart.red
          ]
        }
      ));
      console.log(renderXAxisLabels(
        referenceSeries.from,
        referenceSeries.to,
        width,
        dateFormat,
        ' '.repeat(PADDING.length + 1)
      ));
    }
  })
  .catch(error => {
    utility.error(error);
  });
};