Skip to content

Commit

Permalink
fix: prevent NaN dates
Browse files Browse the repository at this point in the history
* fix: filter NaN dates in getRouteAnalysisImportantDates

* feat: move getAirlineCode and getServerName to helper.js

* chore: change arrow function to method

* feat: move formatDate to helpers.js as formatDateString

fix: check if date is provided, else return

* feat: move formatWeekDate to helpers.js as formatDateStringWeek

* feat: move getDateDiff to helpers.js
  • Loading branch information
ZoeBijl authored Jun 5, 2024
1 parent 3033b72 commit f565c2c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 53 deletions.
80 changes: 28 additions & 52 deletions extension/content_dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
var settings,airline,server,todayDate;
$(function(){
todayDate = (getDate('today',0));
airline = getAirlineCode();
server = getServerName();
airline = AES.getAirlineCode();
server = AES.getServerName();
chrome.storage.local.get(['settings'], function(result) {
settings = result.settings;

Expand Down Expand Up @@ -798,12 +798,12 @@ function updateRouteAnalysisCollumns(data,dates,routeIndex){
if(dates.analysis){
//Analysis date

$(rowId+' .aes-analysisDate').text(formatDate(dates.analysis));
$(rowId+' .aes-analysisDate').text(AES.formatDateString(dates.analysis));


//Pricing date
if(dates.pricing){
$(rowId+' .aes-pricingDate').text(formatDate(dates.pricing));
$(rowId+' .aes-pricingDate').text(AES.formatDateString(dates.pricing));
}

//Pax Load
Expand All @@ -822,7 +822,7 @@ function updateRouteAnalysisCollumns(data,dates,routeIndex){

if(dates.analysisOneBefore){
//Previous analysis date
$(rowId+' .aes-analysisPreDate').text(formatDate(dates.analysisOneBefore));
$(rowId+' .aes-analysisPreDate').text(AES.formatDateString(dates.analysisOneBefore));

//Pax Load Delta
$(rowId+' .aes-paxLoadDelta').html(displayRouteAnalysisLoadDelta(data.date[dates.analysis].data,data.date[dates.analysisOneBefore].data,'pax'));
Expand Down Expand Up @@ -877,7 +877,7 @@ function updateRouteAnalysisCollumns(data,dates,routeIndex){

outDates = getInvPricingAnalaysisPricingDate(dataOut.date);
if(outDates.analysis){
$('#aes-row-invPricing-'+origin+dest+'-analysis',tbody).text(formatDate(outDates.analysis));
$('#aes-row-invPricing-'+origin+dest+'-analysis',tbody).text(AES.formatDateString(outDates.analysis));
outIndex = dataOut.date[outDates.analysis].routeIndex;
let td = $('#aes-row-invPricing-'+origin+dest+'-OWindex',tbody);
td.html(displayIndex(outIndex));
Expand All @@ -887,7 +887,7 @@ function updateRouteAnalysisCollumns(data,dates,routeIndex){
}
}
if(outDates.pricing){
$('#aes-row-invPricing-'+origin+dest+'-pricing',tbody).text(formatDate(outDates.pricing));
$('#aes-row-invPricing-'+origin+dest+'-pricing',tbody).text(AES.formatDateString(outDates.pricing));
}

}
Expand Down Expand Up @@ -1011,12 +1011,14 @@ function getRouteAnalysisImportantDates(dates){
pricingOneBefore:0
}
let analysisDates = [];
let pricingDates = []
for(let date in dates) {
if(dates[date].pricingUpdated){
pricingDates.push(date);
}
analysisDates.push(date);
let pricingDates = []
for(let date in dates) {
if (Number.isInteger(parseInt(date))) {
if(dates[date].pricingUpdated){
pricingDates.push(date);
}
analysisDates.push(date);
}
}
analysisDates.reverse();
pricingDates.reverse();
Expand Down Expand Up @@ -1169,7 +1171,7 @@ function displayCompetitorMonitoringAirlinesTable(div){
if(dates.length){
data.airlineCode = value.tab0[dates[0]].code;
data.airlineName = value.tab0[dates[0]].displayName;
data.overviewDate = formatDate(dates[0]);
data.overviewDate = AES.formatDateString(dates[0]);
data.overviewRating = value.tab0[dates[0]].rating;
data.overviewTotalPax = value.tab0[dates[0]].pax;
data.overviewTotalCargo = value.tab0[dates[0]].cargo;
Expand All @@ -1178,7 +1180,7 @@ function displayCompetitorMonitoringAirlinesTable(div){
data.overviewStaff = value.tab0[dates[0]].employees;
//If previous date exists
if(dates[1]){
data.overviewPreDate = formatDate(dates[1]);
data.overviewPreDate = AES.formatDateString(dates[1]);
data.overviewRatingDelta = getDelta(getRatingNr(data.overviewRating),getRatingNr(value.tab0[dates[1]].rating));
data.overviewTotalPaxDelta = getDelta(data.overviewTotalPax,value.tab0[dates[1]].pax);
data.overviewTotalCargoDelta = getDelta(data.overviewTotalCargo,value.tab0[dates[1]].cargo);
Expand All @@ -1194,7 +1196,7 @@ function displayCompetitorMonitoringAirlinesTable(div){
}
dates.sort(function(a, b){return b-a});
if(dates.length){
data.fafWeek = formatWeekDate(value.tab2[dates[0]].week);
data.fafWeek = AES.formatDateStringWeek(value.tab2[dates[0]].week);
data.fafAirportsServed = value.tab2[dates[0]].airportsServed;
data.fafOperatedFlights = value.tab2[dates[0]].operatedFlights;
data.fafSeatsOffered = value.tab2[dates[0]].seatsOffered;
Expand All @@ -1203,7 +1205,7 @@ function displayCompetitorMonitoringAirlinesTable(div){
data.faffko = value.tab2[dates[0]].fko;
//If previous date exists
if(dates[1]){
data.fafWeekPre = formatWeekDate(value.tab2[dates[1]].week);
data.fafWeekPre = AES.formatDateStringWeek(value.tab2[dates[1]].week);
data.fafAirportsServedDelta = getDelta(data.fafAirportsServed,value.tab2[dates[1]].airportsServed);
data.fafOperatedFlightsDelta = getDelta(data.fafOperatedFlights,value.tab2[dates[1]].operatedFlights);
data.fafSeatsOfferedDelta = getDelta(data.fafSeatsOffered,value.tab2[dates[1]].seatsOffered);
Expand All @@ -1222,7 +1224,7 @@ function displayCompetitorMonitoringAirlinesTable(div){
if(dates.length){
let hubs = {};
//For display
data.scheduleDate = formatDate(dates[0]);
data.scheduleDate = AES.formatDateString(dates[0]);
//For table
data.scheduleDateUse = dates[0];
data.scheduleCargoFreq = 0;
Expand Down Expand Up @@ -1265,7 +1267,7 @@ function displayCompetitorMonitoringAirlinesTable(div){

//Previous schedule data
if(dates[1]){
data.scheduleDatePre = formatDate(dates[1]);
data.scheduleDatePre = AES.formatDateString(dates[1]);
data.scheduleCargoFreqPre = 0;
data.schedulePAXFreqPre = 0;
data.scheduleFltNrPre = 0;
Expand Down Expand Up @@ -2185,7 +2187,7 @@ function displayAircraftProfitability(){
profit.finishedFlights=value.profit.finishedFlights;
profit.profitFlights=value.profit.profitFlights;
profit.profit=value.profit.profit;
profit.dateProfit=formatDate(value.profit.date)+' '+value.profit.time;
profit.dateProfit=AES.formatDateString(value.profit.date)+' '+value.profit.time;
}
data.push({
aircraftId:value.aircraftId,
Expand All @@ -2196,7 +2198,7 @@ function displayAircraftProfitability(){
note:value.note,
age:value.age,
maintenance:value.maintanance,
dateAircraft:formatDate(value.date)+' '+value.time,
dateAircraft:AES.formatDateString(value.date)+' '+value.time,
totalFlights:profit.totalFlights,
finishedFlights:profit.finishedFlights,
profitFlights:profit.profitFlights,
Expand Down Expand Up @@ -2691,8 +2693,8 @@ function generalAddScheduleRow(tbody){
let scheduleData = result[scheduleKey];
if(scheduleData){
let lastUpdate = getDate('schedule',scheduleData.date);
let diff = getDateDiff(todayDate.date,lastUpdate);
let span = $('<span></span>').text('Last schedule extract '+formatDate(lastUpdate)+' ('+diff+' days ago). Extract new schedule if there are new routes.');
let diff = AES.getDateDiff([todayDate.date, lastUpdate]);
let span = $('<span></span>').text('Last schedule extract '+AES.formatDateString(lastUpdate)+' ('+diff+' days ago). Extract new schedule if there are new routes.');
if(diff >= 0 && diff < 7){
span.addClass('good');
} else {
Expand Down Expand Up @@ -2738,8 +2740,8 @@ function generalAddPersonelManagementRow(tbody){
let personelManagementData = result[key];
if(personelManagementData){
let lastUpdate = personelManagementData.date;
let diff = getDateDiff(todayDate.date,lastUpdate);
let span = $('<span></span>').text('Last personnel salary update: '+formatDate(lastUpdate)+' ('+diff+' days ago).');
let diff = AES.getDateDiff([todayDate.date, lastUpdate]);
let span = $('<span></span>').text('Last personnel salary update: '+AES.formatDateString(lastUpdate)+' ('+diff+' days ago).');
if(diff >= 0 && diff < 7){
span.addClass('good');
} else {
Expand Down Expand Up @@ -2876,30 +2878,4 @@ function getDate(type, scheduleData){
default:
return 0;
}
}
function getDateDiff(date1,date2){
//Returns day differnece between date1 - date2
let d1 = new Date(formatDate(date1)+'T12:00:00Z');
let d2 = new Date(formatDate(date2)+'T12:00:00Z');
let diff = Math.round((d1 - d2)/(1000 * 60 * 60 * 24));
return diff;
}
function getAirlineCode(){
//code
let airline = $("#enterprise-dashboard table tr:eq(1) td").text();
//name
let name = $("#as-navbar-main-collapse ul li:eq(0) a:eq(0)").text().trim().replace(/[^A-Za-z0-9]/g, '');
return {code:airline,name:name};
}
function getServerName(){
let server = window.location.hostname
server = server.split('.');
return server[0];
}
function formatDate(date){
return date.substring(0, 4)+'-'+date.substring(4, 6)+'-'+date.substring(6, 8);
}
function formatWeekDate(date){
let a = date.toString();
return a.substring(0, 2)+'/'+a.substring(2, 6);
}
}
85 changes: 84 additions & 1 deletion extension/helpers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
/** Shared logic */
class AES {
/**
* Returns the airline name and code from the dashboard
* @returns {object} {name: string, code: string}
*/
static getAirlineCode() {
const factsTable = document.querySelector(".facts table")
const nameElement = factsTable.querySelector("tr:nth-child(1) td:last-child")
const codeElement = factsTable.querySelector("tr:nth-child(2) td:last-child")

return {
name: nameElement.innerText,
code: codeElement.innerText
}
}

/**
* Returns the server name
* @returns {string} server name
*/
static getServerName() {
const hostname = window.location.hostname
const servername = hostname.split(".")[0]

return servername
}

/**
* Formats a currency value local standards
* @param {integer} currency value
* @param {string} alignment: "right" | "left"
* @returns {HTMLElement} span with formatted value
*/
static formatCurrency = (value, alignment) => {
static formatCurrency(value, alignment) {
let container = document.createElement("span")
let formattedValue = Intl.NumberFormat().format(value)
let indicatorEl = document.createElement("span")
Expand Down Expand Up @@ -39,4 +65,61 @@ class AES {

return container
}

/**
* Formats a date string to human readable format
* @param {string} "20240524"
* @returns {string} "2024-05-24" | "error: invalid format for AES.formatDateString"
*/
static formatDateString(date) {
if (!date) {
return
}

const correctLength = date.length === 8
const isInteger = Number.isInteger(parseInt(date))
let result = "error: invalid format for AES.formatDateString"

if (correctLength && isInteger) {
const year = date.substring(0, 4)
const month = date.substring(4, 6)
const day = date.substring(6, 8)
result = `${year}-${month}-${day}`
}

return result
}
/**
* Returns a formatted date (week) string
* @param {string} "212024"
* @returns {string} "21/2014 | "error: invalid format for AES.formatDateStringWeek"
*/
static formatDateStringWeek(date) {
const correctLength = date.toString().length === 6
const isInteger = Number.isInteger(parseInt(date))
let result = "error: invalid format for AES.formatDateStringWeek"

if (correctLength && isInteger) {
const DateAsString = date.toString()
const week = DateAsString.substring(0, 2)
const year = DateAsString.substring(2, 6)

result = `${week}/${year}`
}

return result
}

/**
* Returns the difference between dates in days
* @param {array} ["20240520", "20240524"]
* @returns {integer} 4
*/
static getDateDiff(dates) {
let dateA = new Date(`${this.formatDateString(dates[0])}T12:00:00Z`)
let dateB = new Date(`${this.formatDateString(dates[1])}T12:00:00Z`)
let result = Math.round((dateA - dateB)/(1000 * 60 * 60 * 24))

return result
}
}

0 comments on commit f565c2c

Please sign in to comment.