Skip to content
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
187dde5
added in the tests for area and meter type. These include a new funct…
SageMar Nov 27, 2024
02712f0
fixed issue within the added validation for area and meter type, also…
SageMar Nov 27, 2024
55db711
Added validation for all boolean value types.
cmatthews444 Nov 28, 2024
9722c00
added in area unit type checking, still need to adjust to allow for d…
SageMar Nov 28, 2024
67b8078
Removed old initial testing code.
cmatthews444 Nov 28, 2024
62288b6
Merge pull request #1 from cmatthews444/Issue1217-csv-validation
SageMar Nov 28, 2024
034e7b4
Merge branch 'OpenEnergyDashboard:development' into Issue1217-csv-val…
SageMar Dec 2, 2024
4f12d03
added in a time sort check and fixed some linter errors
SageMar Dec 2, 2024
f4b60f3
Add validation for minmax values
cmatthews444 Dec 3, 2024
5f30318
tweaked timedout
cmatthews444 Dec 3, 2024
143a96d
Merge pull request #2 from cmatthews444/Issue1217-csv-validation
SageMar Dec 3, 2024
6c612b8
Timezone validation completed
SageMar Dec 3, 2024
30d213e
added time zone validation
SageMar Dec 3, 2024
f6b4c1c
clarified error messages and removed trailing spaces. Working on debu…
SageMar Jan 3, 2025
06751f8
Merge branch 'OpenEnergyDashboard:development' into Issue1217-csv-val…
SageMar Jan 3, 2025
45ae817
Updating to match current OED vers
SageMar Jan 3, 2025
7c317f5
allow empty min/max values and booleanfields
SageMar Jan 8, 2025
4d85514
Fixed errors in tests! It was an issue with the way area was being ch…
SageMar Jan 24, 2025
9b9e51d
fixed spacing + spelling, removed unused import, max value now being …
SageMar Feb 14, 2025
ee6c066
Enums added where requested. Working on fixing area check now
SageMar Apr 7, 2025
f11ba17
Fixed issues with areaUnit validity check. Zero now returns false and…
SageMar Apr 7, 2025
57f219d
Timezone checks done using moment now
SageMar Jun 4, 2025
454397d
Merge branch 'OpenEnergyDashboard:development' into Issue1217-csv-val…
SageMar Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 209 additions & 7 deletions src/server/services/csvPipeline/uploadMeters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ const { CSVPipelineError } = require('./CustomErrors');
const Meter = require('../../models/Meter');
const readCsv = require('../pipeline-in-progress/readCsv');
const Unit = require('../../models/Unit');
const { normalizeBoolean } = require('./validateCsvUploadParams');
const { normalizeBoolean, MeterTimeSortTypesJS } = require('./validateCsvUploadParams');
const moment = require('moment-timezone');

/**
* Middleware that uploads meters via the pipeline. This should be the final stage of the CSV Pipeline.
* @param {express.Request} req
* @param {express.Response} res
* @param {express.Request} req
* @param {express.Response} res
* @param {filepath} filepath Path to meters csv file.
* @param conn Connection to the database.
*/
async function uploadMeters(req, res, filepath, conn) {
const temp = (await readCsv(filepath)).map(row => {
// The Canonical structure of each row in the Meters CSV file is the order of the fields
// The Canonical structure of each row in the Meters CSV file is the order of the fields
// declared in the Meter constructor. If no headerRow is provided (i.e. headerRow === false),
// then we assume that the uploaded CSV file follows this Canonical structure.

Expand All @@ -41,21 +42,71 @@ async function uploadMeters(req, res, filepath, conn) {
try {
for (let i = 0; i < meters.length; i++) {
let meter = meters[i];
//validation for boolean values
validateBooleanFields(meter, i);

// Validate min and max values
validateMinMaxValues(meter, i);

// First verify GPS is okay
// This assumes that the sixth column is the GPS as order is assumed for now in a GPS file.
const gpsInput = meter[6];
// Skip if undefined.
if (gpsInput) {
// Verify GPS is okay values
if (!isValidGPSInput(gpsInput)) {
let msg = `For meter ${meter[0]} the gps coordinates of ${gpsInput} are invalid`;
let msg = `For meter ${meter[0]} the gps coordinates of ${gpsInput} are invalid.`;
throw new CSVPipelineError(msg, undefined, 500);
}
// Need to reverse latitude & longitude because standard GPS gives in that order but a GPSPoint for the
// DB is longitude, latitude.
meter[6] = switchGPS(gpsInput);
}

// verify the area input
const areaInput = meter[9];
if (areaInput) {
if (!isValidArea(areaInput)) {
let msg = `For meter ${meter[0]} the area entry of ${areaInput} is invalid. Area must be a number greater than 0.`;
throw new CSVPipelineError(msg, undefined, 500);
}
}

const timeSortValue = meter[17];
if (timeSortValue) {
if (!isValidTimeSort(timeSortValue)) {
let msg = `For meter ${meter[0]} the time sort ${timeSortValue} is invalid. Valid options are increasing or decreasing.`;
throw new CSVPipelineError(msg, undefined, 500);
}
}

const timezone = meter[5];
if (timezone) {
if (!isValidTimeZone(timezone)) {
let msg = `For meter ${meter[0]}, ${timezone} is not a valid time zone.`;
throw new CSVPipelineError(msg, undefined, 500);
}
}

// Verify area unit provided
const areaUnitString = meter[25];
if (areaUnitString) {
if (!isValidAreaUnit(areaUnitString)) {
let msg = `For meter ${meter[0]} the area unit of ${areaUnitString} is invalid. Unit must be feet, meters, or none.`;
throw new CSVPipelineError(msg, undefined, 500);
}
}

// Verify meter type
const meterTypeString = meter[4];
if (meterTypeString) {
if (!isValidMeterType(meterTypeString)) {
let msg = `For meter ${meter[0]} the meter type of ${meterTypeString} is invalid. Valid types include:
egauge, mamac, metasys, obvius, and other. `;
throw new CSVPipelineError(msg, undefined, 500);
}
}

// Process unit.
const unitName = meter[23];
const unitId = await getUnitId(unitName, Unit.unitType.METER, conn);
Expand Down Expand Up @@ -109,7 +160,7 @@ async function uploadMeters(req, res, filepath, conn) {
throw new CSVPipelineError(
`Meter name of \"${meter[0]}\" got database error of: ${error.message}`, undefined, 500);
}
);
);
}
}
} catch (error) {
Expand Down Expand Up @@ -152,6 +203,83 @@ function switchGPS(gpsString) {
return (array[1] + ',' + array[0]);
}

/**
* Checks if the area provided is a number and if it is larger than zero.
* @param areaInput the provided area for the meter
* @returns true or false
*/
function isValidArea(areaInput) {
// check for non-number input, which is not allowed
if (Number.isNaN(areaInput)){
return false;
}

// must be a number and must be non-negative
if (areaInput > 0) {
return true;
} else {
return false;
}
}

/**
* Checks if the area unit provided is an option
* @param areaUnit the provided area for the meter
* @returns true or false
*/
function isValidAreaUnit(areaUnit) {
const validTypes = Object.values(Unit.areaUnitType);
// must be one of the three values
if (validTypes.includes(areaUnit)) {
return true;
} else {
return false;
}
}

/**
* Checks if the time sort value provided is accurate (should be increasing or decreasing)
* @param timeSortValue the provided time sort
* @returns true or false
*/
function isValidTimeSort(timeSortValue) {
const validTimes = Object.values(MeterTimeSortTypesJS);
// must be one of the three values
if (validTimes.includes(timeSortValue)) {
return true;
} else {
return false;
}
}

/**
* Checks if the meter type provided is one of the 5 options allowed when creating a meter.
* @param meterTypeString the string for the meter type
* @returns true or false
*/
function isValidMeterType(meterTypeString) {
const validTypes = Object.values(Meter.type);
if (validTypes.includes(meterTypeString)) {
return true;
} else {
return false;
}
}

/**
* Checks the provided time zone and if it is a real time zone.
* @param zone the provided time zone from the csv
* @returns true or false
*/
function isValidTimeZone(zone) {
const validZones = moment.tz.names();
if (validZones.includes(zone)) {
return true;
} else {
return false;
}
}

/**
* Return the id associated with the given unit's name.
* If the unit's name is invalid or its type is different from expected type, return null.
Expand All @@ -170,4 +298,78 @@ async function getUnitId(unitName, expectedUnitType, conn) {
return unit.id;
}

module.exports = uploadMeters;

/**
* Validates all boolean-like fields for a given meter row.
* @param {Array} meter - A single row from the CSV file.
* @param {number} rowIndex - The current row index for error reporting.
*/
function validateBooleanFields(meter, rowIndex) {
// all inputs that involve a true or false all being validated together.
const booleanFields = {
2: 'enabled',
3: 'displayable',
10: 'cumulative',
11: 'reset',
18: 'end only',
32: 'disableChecks'
};

// this array has values which may be left empty
const booleanUndefinedAcceptable = [
'cumulative', 'reset', 'end only', 'disableChecks'
];

for (const [index, name] of Object.entries(booleanFields)) {
let value = meter[index];

// allows upper/lower case.
if ((value === '' || value === undefined) && booleanUndefinedAcceptable.includes(name)) {
// skip if the value is undefined
continue;
} else {
if (typeof value === 'string') {
value = value.toLowerCase();
}
}

// Validates read values to either false or true
if (value !== 'true' && value !== 'false' && value !== true && value !== false
&& value !== 'yes' && value !== 'no') {
throw new CSVPipelineError(
`Invalid input for '${name}' in row ${rowIndex + 1}: "${meter[index]}". Expected 'true' or 'false'.`,
undefined,
500
);
}
}
}

function validateMinMaxValues(meter, rowIndex) {
const minValue = Number(meter[27]);
const maxValue = Number(meter[28]);

if (isNaN(minValue) && isNaN(maxValue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the logic is tricky. For example, assume only maxValue is a NaN. The first else if will compare minValue > maxValue in the first else if but maxValue is a NaN. Also, the second if below stops the else if so I don't see how the exception can occur in this case. Maybe this logic is better and simpler to understand (open to ideas):

// Check minValue is okay. If maxValue is a NaN then skip last check but it will throw an exception below.
if (isNaN(minValue) || minValue < -9007199254740991 || (!isNaN(maxValue) && minValue > maxValue)) {
    exception
}
// Check maxValue is okay.
// A check for minValue > maxValue is not needed since it would have caused an exception in the
// if above unless minValue or maxValue is a NaN but those cause the exception anyway.
if (isNaN(maxValue) || maxValue > 9007199254740991) {
    exception
}

// do nothing, pass it through
} else if (isNaN(minValue) || minValue < -9007199254740991 || minValue > maxValue) {
throw new CSVPipelineError(
`Invalid min/max values in row ${rowIndex + 1}: min="${meter[27]}", max="${meter[28]}". ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below you directly use the meter value. Since you assigned it a variable above I think it would be best to use that to be consistent.

`Min or/and max must be a number larger than -9007199254740991, and less then 9007199254740991, and min must be less than max.`,
undefined,
500
);
}

if (isNaN(maxValue)) {
// do nothing, pass it through
} else if (isNaN(maxValue) || maxValue > 9007199254740991 || minValue > maxValue) {
throw new CSVPipelineError(
`Invalid min/max values in row ${rowIndex + 1}: min="${meter[27]}", max="${meter[28]}". ` +
`Min or/and max must be a number larger than -9007199254740991, and less then 9007199254740991, and min must be less than max.`,
undefined,
500
);
}
}

module.exports = uploadMeters;
Loading