Skip to content

Commit

Permalink
Merge pull request #16 from daniel-fernandez-edosoft/master
Browse files Browse the repository at this point in the history
Convert all date fields to year/month/day and add granularity column
  • Loading branch information
sdelquin authored May 10, 2022
2 parents e858f99 + 9fef98d commit e35405f
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/CacheHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cacheVersion = "20200413_1137";
const cacheVersion = "20220510_1058";

/**
* Builds the Cache Helper object.
Expand Down
28 changes: 14 additions & 14 deletions src/Connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ function Connector(services) {
string: types.TEXT,
float: types.NUMBER,
date: types.YEAR_MONTH_DAY,
date_YEARLY: types.YEAR,
date_BIYEARLY: types.YEAR_MONTH,
date_FOUR_MONTHLY: types.YEAR_MONTH,
date_MONTHLY: types.YEAR_MONTH,
date_QUARTERLY: types.YEAR_QUARTER,
date_DAILY: types.YEAR_MONTH_DAY,
date_WEEKLY: types.YEAR_WEEK
// date_YEARLY: types.YEAR,
// date_BIYEARLY: types.YEAR_MONTH,
// date_FOUR_MONTHLY: types.YEAR_MONTH,
// date_MONTHLY: types.YEAR_MONTH,
// date_QUARTERLY: types.YEAR_QUARTER,
// date_DAILY: types.YEAR_MONTH_DAY,
// date_WEEKLY: types.YEAR_WEEK
};

// sufijo añadido al id de columna para que al actualizar una URL
// editando la conexion se actualice el tipo de fecha (si no, no se actualiza)
const idsTranslator = {
date: "_date_YEAR_MONTH_DAY",
date_YEARLY: "_date_YEAR",
date_BIYEARLY: "_date_YEAR_MONTH",
date_FOUR_MONTHLY: "_date_YEAR_MONTH",
date_MONTHLY: "_date_YEAR_MONTH",
date_QUARTERLY: "_date_YEAR_QUARTER",
date_DAILY: "_date_YEAR_MONTH_DAY",
date_WEEKLY: "_date_YEAR_WEEK"
// date_YEARLY: "_date_YEAR",
// date_BIYEARLY: "_date_YEAR_MONTH",
// date_FOUR_MONTHLY: "_date_YEAR_MONTH",
// date_MONTHLY: "_date_YEAR_MONTH",
// date_QUARTERLY: "_date_YEAR_QUARTER",
// date_DAILY: "_date_YEAR_MONTH_DAY",
// date_WEEKLY: "_date_YEAR_WEEK"
};

for(let column of columns) {
Expand Down
66 changes: 33 additions & 33 deletions src/DataHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,38 +183,38 @@ function DataHelper(services) {

const col = utils.getColNameAndId(indicatorsResponse, configParams);

const granularityOrder = {
'YEARLY': 1,
'BIYEARLY': 2,
'QUARTERLY': 3,
'MONTHLY': 4,
'WEEKLY': 5,
'DAILY': 6
};
// const granularityOrder = {
// 'YEARLY': 1,
// 'BIYEARLY': 2,
// 'QUARTERLY': 3,
// 'MONTHLY': 4,
// 'WEEKLY': 5,
// 'DAILY': 6
// };

let desiredGranularity = null;
// let desiredGranularity = null;

if(configParams.recodeDates) {
const granularitySet = new Set();
// if(configParams.recodeDates) {
// const granularitySet = new Set();

if(indicatorsResponse.dimension.TIME && indicatorsResponse.dimension.TIME.representation) {
for(let timeRepresentation of indicatorsResponse.dimension.TIME.representation) {
if(timeRepresentation.granularityCode) {
granularitySet.add(timeRepresentation.granularityCode);
}
}
}
// if(indicatorsResponse.dimension.TIME && indicatorsResponse.dimension.TIME.representation) {
// for(let timeRepresentation of indicatorsResponse.dimension.TIME.representation) {
// if(timeRepresentation.granularityCode) {
// granularitySet.add(timeRepresentation.granularityCode);
// }
// }
// }

let minimumGranularityIndex = 0;
// let minimumGranularityIndex = 0;

// TODO: comprobar valores posibles
for(let temporalGranularity of granularitySet) {
if(granularityOrder[temporalGranularity] && granularityOrder[temporalGranularity] > minimumGranularityIndex) {
minimumGranularityIndex = granularityOrder[temporalGranularity];
desiredGranularity = temporalGranularity;
}
}
}
// // TODO: comprobar valores posibles
// for(let temporalGranularity of granularitySet) {
// if(granularityOrder[temporalGranularity] && granularityOrder[temporalGranularity] > minimumGranularityIndex) {
// minimumGranularityIndex = granularityOrder[temporalGranularity];
// desiredGranularity = temporalGranularity;
// }
// }
// }

const lengthGeographical = indicatorsDataResponse.dimension.GEOGRAPHICAL.representation.size;
for (var i = 0; i < lengthGeographical; i++) {
Expand All @@ -231,9 +231,9 @@ function DataHelper(services) {

if(configParams.recodeDates) {
const granularity = indexedTimeRepresentations[codigoTemporal].granularityCode;
if(desiredGranularity && granularity != desiredGranularity) {
continue;
}
// if(desiredGranularity && granularity != desiredGranularity) {
// continue;
// }
const date = codigoTemporal;
row.Fecha = recodeDatesHelper.converDate(date, granularity);
}
Expand Down Expand Up @@ -281,9 +281,9 @@ function DataHelper(services) {

if(configParams.recodeDates) {
const granularity = indexedTimeRepresentations[codigoTemporal].granularityCode;
if(desiredGranularity && granularity != desiredGranularity) {
continue;
}
// if(desiredGranularity && granularity != desiredGranularity) {
// continue;
// }
const date = codigoTemporal;
row.Fecha = recodeDatesHelper.converDate(date, granularity);
}
Expand Down
116 changes: 58 additions & 58 deletions src/RecodeDatesHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function RecodeDatesHelper() {
date = date.split("T")[0];
}
date = date.split("-");
return date[0] + "-" + date[1] + "-" + date[2];
return strPad(date[0], 4, "0") + strPad(date[1], 2, "0") + strPad(date[2], 2, "0");
}

// REPORTING TIME PERIOD
Expand All @@ -138,39 +138,39 @@ function RecodeDatesHelper() {
// 2000-D353
const _calculateReportingPeriod = function (time) {
if (time.match(new RegExp(PATTERN_REPORTING_YEAR_TYPE))) {
//return time.match(new RegExp(PATTERN_REPORTING_YEAR_TYPE))[1] + "0101";
return time.match(new RegExp(PATTERN_REPORTING_YEAR_TYPE))[1];
return time.match(new RegExp(PATTERN_REPORTING_YEAR_TYPE))[1] + "0101";
//return time.match(new RegExp(PATTERN_REPORTING_YEAR_TYPE))[1];
}
if (time.match(new RegExp(PATTERN_REPORTING_SEMESTER_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_SEMESTER_TYPE))[1];
const month = (time.match(new RegExp(PATTERN_REPORTING_SEMESTER_TYPE))[3] - 1) * 6;
return year + (month + 1);
return year + strPad("" + (month + 1), 2, "0") + "01";
}
if (time.match(new RegExp(PATTERN_REPORTING_TRIMESTER_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_TRIMESTER_TYPE))[1];
const month = (time.match(new RegExp(PATTERN_REPORTING_TRIMESTER_TYPE))[3] - 1) * 4;
return year + (month + 1);
return year + strPad("" + (month + 1), 2, "0") + "01";
}
if (time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))[1];
//const month = (time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))[3] - 1) * 3;
//return year + (month + 1) + "01";
const quarter = time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))[3];
return year + quarter;
const month = (time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))[3] - 1) * 3;
return year + strPad("" + (month + 1), 2, "0") + "01";
// const quarter = time.match(new RegExp(PATTERN_REPORTING_QUARTER_TYPE))[3];
// return year + quarter;
}
if (time.match(new RegExp(PATTERN_REPORTING_MONTH_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_MONTH_TYPE))[1];
const month = parseInt(time.match(new RegExp(PATTERN_REPORTING_MONTH_TYPE))[3]);
return year + strPad("" + month, 2, "0");
return year + strPad("" + month, 2, "0") + "01";
//return year + month + "01";
}
if (time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))[1];
//const day = (time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))[3] - 1) * 7;
//const date = new Date(year, "", day + 1);
//return year + (date.getMonth() + 1) + date.getDate();
const week = parseInt(time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))[3]);
return year + strPad("" + week, 2, "0");
const day = (time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))[3] - 1) * 7;
const date = new Date(year, "", day + 1);
return year + strPad("" + (date.getMonth() + 1), 2, "0") + strPad("" + date.getDate(), 2, "0");
// const week = parseInt(time.match(new RegExp(PATTERN_REPORTING_WEEK_TYPE))[3]);
// return year + strPad("" + week, 2, "0");
}
if (time.match(new RegExp(PATTERN_REPORTING_DAY_TYPE))) {
const year = time.match(new RegExp(PATTERN_REPORTING_DAY_TYPE))[1];
Expand Down Expand Up @@ -209,42 +209,42 @@ function RecodeDatesHelper() {
const _calculateGpeReportingPeriod = function (time) {
if (time.match(new RegExp(START + GPE_YEARLY_PATTERN + END))) {
//return date + "0101";
return time;
return time + "0101";
}
if (time.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))) {
const year = time.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[1];
const month = (time.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[2] - 1) * 6;
//return year + strPad("" + (month + 1), 2, "0") + "01";
return year + strPad("" + (month + 1), 2, "0");
return year + strPad("" + (month + 1), 2, "0") + "01";
}
if (time.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))) {
const year = time.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[1];
// const month = (date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2] - 1) * 3;
// return year + strPad("" + (month + 1), 2, "0") + "01";
const quarter = time.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2];
return year + quarter;
const month = (time.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2] - 1) * 3;
return year + strPad("" + (month + 1), 2, "0") + "01";
// const quarter = time.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2];
// return year + quarter;
}
if (time.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))) {
const year = time.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))[1];
const month = parseInt(time.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))[2]);
//return year + strPad("" + (month), 2, "0") + "01";
return year + strPad("" + month, 2, "0");
return year + strPad("" + (month), 2, "0") + "01";
//return year + strPad("" + month, 2, "0");
}
if (time.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))) {
const year = time.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))[1];
const month = parseInt(time.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))[2]);
//return year + strPad("" + (month), 2, "0") + "01";
return year + strPad("" + month, 2, "0");
return year + strPad("" + (month), 2, "0") + "01";
//return year + strPad("" + month, 2, "0");
}
if (time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))) {
// const year = date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
// const day =
// (date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2] - 1) * 7;
// date = new Date(year, "", day + 1);
// return year + strPad("" + (date.getMonth()), 2, "0") + date.getDate();
const year = time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
const week = parseInt(time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2]);
return year + strPad("" + week, 2, "0");
const day =
(time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2] - 1) * 7;
const date = new Date(year, "", day + 1);
return year + strPad("" + (date.getMonth()), 2, "0") + strPad("" + date.getDate(), 2, "0");
// const year = time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
// const week = parseInt(time.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2]);
// return year + strPad("" + week, 2, "0");
}
if (time.match(new RegExp(START + GPE_DAILY_PATTERN + END))) {
const year = time.match(new RegExp(START + GPE_DAILY_PATTERN + END))[1];
Expand Down Expand Up @@ -288,22 +288,22 @@ function RecodeDatesHelper() {
time = time.split("T")[0];
}
const date = time.split("-");
return date[0] + date[1] + date[2];
return strPad(date[0], 4, "0") + strPad(date[1], 2, "0") + strPad(date[2], 2, "0");
}

// GREGORIAN TIME
// 2000
// 2000-01
// 2000-01-31
// 200001
// 200001-31
const _calculateGregorianTimePeriod = function (time) {
time = time.split("-");
switch (time.length) {
case 1:
return time[0] + "0101";
return strPad(time[0], 4, "0") + "0101";
case 2:
return time[0] + time[1] + "01";
return strPad(time[0], 4, "0") + strPad(time[1], 2, "0") + "01";
case 3:
return time[0] + time[1] + time[2];
return strPad(time[0], 4, "0") + strPad(time[1], 2, "0") + strPad(time[2], 2, "0");
}
}

Expand Down Expand Up @@ -345,7 +345,7 @@ function RecodeDatesHelper() {
case "YEARLY": {
if (date.match(new RegExp(START + GPE_YEARLY_PATTERN + END))) {
//return date + "0101";
return date;
return date + "0101";
}
break;
}
Expand All @@ -355,19 +355,19 @@ function RecodeDatesHelper() {
if (date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))) {
const year = date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[1];
const month = (date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[2] - 1) * 6;
//return year + strPad("" + (month + 1), 2, "0") + "01";
return year + strPad("" + (month + 1), 2, "0");
return year + strPad("" + (month + 1), 2, "0") + "01";
//return year + strPad("" + (month + 1), 2, "0");
}
break;
}
// trimestral
case "QUARTERLY": {
if (date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))) {
const year = date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[1];
// const month = (date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2] - 1) * 3;
// return year + strPad("" + (month + 1), 2, "0") + "01";
const quarter = date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2];
return year + quarter;
const month = (date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2] - 1) * 3;
return year + strPad("" + (month + 1), 2, "0") + "01";
// const quarter = date.match(new RegExp(START + GPE_QUARTERLY_PATTERN + END))[2];
// return year + quarter;
}
break;
}
Expand All @@ -377,8 +377,8 @@ function RecodeDatesHelper() {
if (date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))) {
const year = date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[1];
const month = (date.match(new RegExp(START + GPE_BIYEARLY_PATTERN + END))[2] - 1) * 3;
//return year + strPad("" + (month + 1), 2, "0") + "01";
return year + strPad("" + (month + 1), 2, "0");
return year + strPad("" + (month + 1), 2, "0") + "01";
//return year + strPad("" + (month + 1), 2, "0");
}
break;
}
Expand All @@ -387,28 +387,28 @@ function RecodeDatesHelper() {
if (date.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))) {
const year = date.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))[1];
const month = parseInt(date.match(new RegExp(START + GPE_MONTHLY_PATTERN + END))[2]);
//return year + strPad("" + (month), 2, "0") + "01";
return year + strPad("" + month, 2, "0");
return year + strPad("" + (month), 2, "0") + "01";
//return year + strPad("" + month, 2, "0");
} else if(date.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))) {
const year = date.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))[1];
const month = parseInt(date.match(new RegExp(START + GPE_MONTHLY_PATTERN_WITHOUT_CHARACTER + END))[2]);
//return year + strPad("" + (month), 2, "0") + "01";
return year + strPad("" + month, 2, "0");
return year + strPad("" + (month), 2, "0") + "01";
//return year + strPad("" + month, 2, "0");
}

break;
}
// semanal
case "WEEKLY": {
if (date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))) {
// const year = date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
// const day =
// (date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2] - 1) * 7;
// date = new Date(year, "", day + 1);
// return year + strPad("" + (date.getMonth()), 2, "0") + date.getDate();
const year = date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
const week = parseInt(date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2]);
return year + strPad("" + week, 2, "0");
const day =
(date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2] - 1) * 7;
date = new Date(year, "", day + 1);
return year + strPad("" + (date.getMonth()), 2, "0") + strPad(""+date.getDate(), 2, "0");
// const year = date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[1];
// const week = parseInt(date.match(new RegExp(START + GPE_WEEKLY_PATTERN + END))[2]);
// return year + strPad("" + week, 2, "0");
}
break;
}
Expand Down
Loading

0 comments on commit e35405f

Please sign in to comment.