CherrelleTucker / snwg-automation

SNWG Coding
0 stars 0 forks source link

{{Adjusted PI}} - calculation is not rolling over to new fiscal year; #23

Closed CherrelleTucker closed 5 months ago

CherrelleTucker commented 8 months ago
// immediately invoked function expression - adjustedPIcalculator to calculate current PI  ****DO NOT TOUCH****
var adjustedPIcalculator = (function(){
  // Adjustment Weeks to account for holidays, vacations, etc. Add the first Sunday of the Adjustment Week at the bottom of the list in 'YYYY-MM-DD' format. 
  var ADJUSTMENT_WEEKS = []; // Add adjustment week dates here.
  ADJUSTMENT_WEEKS.push(new Date('2023-07-02')); // Innovation Week #2
  ADJUSTMENT_WEEKS.push(new Date('2023-07-16')); // IGARSS 2023
  // Add dates as 'yyyy-mm-dd'

  ///////////////////////////////////////

  // Global variables
  var BASE_DATE = new Date('2023-04-16');  // The date for '23.3.1 Week 1', the beginning sprint of record for the purposes of this script. 
  var BASE_FY = 23;
  var BASE_PI = 3;
  var BASE_SPRINT = 1;
  var BASE_WEEK = 1;

  // Helper function to convert a date to the number of milliseconds since Jan 1, 1970
  function toMillis(date) {
    return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
  }

  // Helper function to calculate Adjustment Weeks
  function adjustmentWeeks(date) {
    var adjustmentWeeks = 0;
    var currentTime = toMillis(date);
    for(var i = 0; i < ADJUSTMENT_WEEKS.length; i++) {
      if(toMillis(ADJUSTMENT_WEEKS[i]) <= currentTime) {
        adjustmentWeeks++;
      }
    }
    return adjustmentWeeks;
  }

  // Helper function to calculate the current fiscal year
  function getFiscalYear(date) {
    var year = date.getFullYear();
    var month = date.getMonth();

    // The fiscal year starts in October (month index 9)
    return month >= 9 ? year + 1 : year;
  }

  // Helper function to get current PI (Q) (1-4)
  function getQuarter(date) {
    // In JavaScript, months are 0-indexed, so October is 9 and September is 8
    var month = date.getMonth();

    // The first quarter of the fiscal year starts in October
    if (month >= 9) {
      return ((month - 9) / 3 | 0) + 1;
    } else {
      return ((month + 3) / 3 | 0) + 1;
    }
  }

  // Helper function to get current Sprint (1-6)
  function getSprint(date) {
    var fiscalYearStart = new Date(getFiscalYear(date), 9, 1);
    var diffWeeks = Math.ceil(((date - fiscalYearStart + 1) / (24 * 60 * 60 * 1000)) / 7);

    // Subtract adjustment weeks
    diffWeeks -= adjustmentWeeks(date);

    // Subtract the base week of the quarter
    var baseWeek = (getQuarter(date) - 1) * 13;
    diffWeeks -= baseWeek;

    // Calculate the sprint based on the week of the fiscal year, after accounting for adjustment weeks
    var sprint = Math.ceil(diffWeeks / 2);

    return sprint;
  }

  // Helper function to get current week (1-2)
  function getWeek(date) {
    return Math.ceil(((date - new Date(getFiscalYear(date), 9, 1) + 1) / (24 * 60 * 60 * 1000)) / 7) % 2 === 0 ? 2 : 1;
  }

  // Helper function to get week of the year
  function getWeekOfYear(date) {
    var start = new Date(date.getFullYear(), 0, 1);
    var diff = date - start + (start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);
    return Math.ceil(day / 7);
  }

  // Helper function to rename FY.Q.6.1 to "Innovation Week"
  function renameInnovation(pi) {
    return pi.endsWith(".6.1") ? "Innovation Week" : pi;
  }

  // Helper function to rename FY.Q.6.2 to "Next PI Planning"
  function renameNextPiPlanning(pi) {
    return pi.endsWith(".6.2") ? "Next PI Planning" : pi;
  }

  // Helper function to calculate the Adjusted PI
  function getPI(inputDate) {
    if (!(inputDate instanceof Date)) {
      // Attempt to parse the inputDate as a string and convert it to a Date object
      inputDate = new Date(inputDate);

      // Check if the parsed inputDate is valid
      if (isNaN(inputDate.getTime())) {
        throw new Error("Invalid date provided to getPI function.");
      }
    }

    var daysPassed = (toMillis(inputDate) - toMillis(BASE_DATE)) / (24 * 60 * 60 * 1000) - adjustmentWeeks(inputDate) * 7;
    var weeksPassed = Math.floor(daysPassed / 7);

    // calculate the total two week periods passed since the BASE_DATE
    var totalTwoWeekPeriods = Math.floor(weeksPassed / 2);

    var week = weeksPassed % 2 + 1;
    var sprint = totalTwoWeekPeriods % 6 + 1;
    var pi = Math.floor(totalTwoWeekPeriods / 6) % 4 + BASE_PI;
    var fy = BASE_FY + Math.floor(totalTwoWeekPeriods / (6 * 4));
    var piStr = "FY" + fy + "." + pi + "." + sprint + " Week " + week;  // Format result as FY.Q.S Week W
    piStr = renameInnovation(piStr);
    piStr = renameNextPiPlanning(piStr);

    return piStr;
  }

  // Primary function to replace placeholder text {{Adjusted PI}} with the calculated Adjusted PI
  function replacePlaceholderWithPI(document, adjustedPI) {
    var body = document.getBody();
    var text = body.editAsText();
    text.replaceText("{{Adjusted PI}}", adjustedPI);
  }

  // Make the getPI function accessible outside the IIFE
  return {
    getPI: getPI,
    replacePlaceholderWithPI: replacePlaceholderWithPI
  };
})();
CherrelleTucker commented 7 months ago

createSprintReviewPresentationIMPACT

placeholder {{currentSprintNumber}} appears to generate correct sprint

Template: IMPACT Sprint Review

CherrelleTucker commented 6 months ago

createSprintReviewPresentationIMPACT

placeholder {{currentSprintNumber}} appears to generate correct sprint

Template: IMPACT Sprint Review

{{currentSprintNumber}} is created by trimming file title. File title is created by searching PI calendar for relevant Sprint Increment, then naming to match. .

CherrelleTucker commented 5 months ago

Instead of calculating a PI, have scripts read the PI info from the PI calendar https://calendar.google.com/calendar/u/0?cid=Y19lNmU1MzJjZWZjNWRkZmRkN2YzYzcxNWU3YTA3MzI2NjA3Y2QyNDBkOTUxOTkxZjZhNGUzYjg3NjUzZTY3ZWYzQGdyb3VwLmNhbGVuZGFyLmdvb2dsZS5jb20

38

CherrelleTucker commented 5 months ago

`// function to get FY.PI.Sprint from IMPACT PI Calendar function getPiFromImpactPiCalendar() { var calendar = CalendarApp.getCalendarById(impactPiCalendar); var currentWeekDates = getCurrentWeekDates(); var events = calendar.getEvents(currentWeekDates.start, currentWeekDates.end); var piRegex = /PI \d{2}.\d Sprint \d/; // Regex to match "PI YY.Q Sprint S" format

for (var i = 0; i < events.length; i++) {
    var event = events[i];
    if (piRegex.test(event.getTitle())) {
        return event.getTitle();
    }
}
return "No PI Event Found"; // Return this if no matching event is found

}

'{{Adjusted PI}}': currentPI, // Update PI Placeholder

`

CherrelleTucker commented 5 months ago

Not working on this anymore. Just pull from the calendar.