Skip to content

Commit

Permalink
#128 fixed codesmells raised by sonarcloud.
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwinderg committed Dec 20, 2023
1 parent 7d8ae33 commit d462679
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 361 deletions.
6 changes: 3 additions & 3 deletions app/app/public/js/esriMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define([
// hide location search error message
document.getElementById('location-error').style.display = 'none';

var markerSymbol = {
const markerSymbol = {
type: 'simple-marker', // autocasts as new SimpleMarkerSymbol()
color: [226, 119, 40],
outline: {
Expand All @@ -40,7 +40,7 @@ define([
};

// Create a graphic and add the geometry and symbol to it
var pointGraphic = new Graphic({
const pointGraphic = new Graphic({
geometry: evt.mapPoint,
symbol: markerSymbol,
});
Expand All @@ -62,7 +62,7 @@ define([
const point = new Point({ x: longitude, y: latitude });

// Create a symbol for drawing the point
var markerSymbol = {
const markerSymbol = {
type: 'simple-marker', // autocasts as new SimpleMarkerSymbol()
color: [226, 119, 40],
outline: {
Expand Down
10 changes: 6 additions & 4 deletions app/app/public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ define(['app/esriMap'], function (esriMap) {

// if browser is ie11, fix the responsiveness of the datepicker inputs
if (!!window.MSInputMethodContext && !!document.documentMode) {
var element = document.getElementById('responsivebr');
const element = document.getElementById('responsivebr');
element.classList.remove('responsivebr');
document.getElementById('end-div').style.textAlign = 'left';
}
Expand Down Expand Up @@ -44,8 +44,10 @@ define(['app/esriMap'], function (esriMap) {
function eventListenerGetCoords(event) {
event.preventDefault();
// get the coordinates and add a point to the map based on search box value
getCoords(function () { /* This is intentional */ });
}
getCoords(function () {
/* This is intentional */
});
}

// view on map button listener
document
Expand Down Expand Up @@ -169,7 +171,7 @@ define(['app/esriMap'], function (esriMap) {
let endday = endDate.slice(8);
let newendDate = endmonth + '/' + endday + '/' + endyear;

var api = null;
let api = null;
if (window.location.host.toLowerCase().startsWith('localhost')) {
api = 'http://localhost:' + window.location.port + '/v1/rfactor';
} else if (
Expand Down
18 changes: 9 additions & 9 deletions app/app/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const express = require('express');
const helmet = require('helmet');
const noCache = require('nocache');
var cors = require('cors');
var favicon = require('serve-favicon');
const cors = require('cors');
const favicon = require('serve-favicon');
const basicAuth = require('express-basic-auth');
var path = require('path');
const path = require('path');
const logger = require('./server/utilities/logger.js');

const app = express();
Expand All @@ -21,16 +21,16 @@ app.use(
}),
);
app.use(cors());
var log = logger.logger;
var port = process.env.PORT || 9090;
const log = logger.logger;
let port = process.env.PORT || 9090;
const browserSync_port = 9091;

/****************************************************************
Which environment
****************************************************************/
var isLocal = false;
var isDevelopment = false;
var isStaging = false;
let isLocal = false;
let isDevelopment = false;
let isStaging = false;

if (process.env.NODE_ENV) {
isLocal = 'local' === process.env.NODE_ENV.toLowerCase();
Expand All @@ -57,7 +57,7 @@ if (isDevelopment || isStaging) {
);
}

var user_json =
const user_json =
'{"' +
process.env.LEW_BASIC_AUTH_USER_NAME +
'" : "' +
Expand Down
64 changes: 31 additions & 33 deletions app/app/server/controllers/rfactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ function buildLeapYearData(data) {
}

function getDayOfYear(date) {
var start = new Date(date.getFullYear(), 0, 0);
var diff =
const start = new Date(date.getFullYear(), 0, 0);
const diff =
date -
start +
(start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
var oneDay = 1000 * 60 * 60 * 24;
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}

function getCountyUrl(metadataObj, lat, lon) {
return new Promise((resolve, reject) => {
var options = {
const options = {
method: 'POST',
url,
// url: 'https://httpstat.us/400',
Expand Down Expand Up @@ -86,7 +86,7 @@ function getCountyUrl(metadataObj, lat, lon) {
reject(err_json);
return;
} else {
var results = null;
let results = null;
try {
results = response.data.result;
} catch (err) {
Expand Down Expand Up @@ -121,7 +121,7 @@ function getCountyUrl(metadataObj, lat, lon) {
return;
}

for (var i = 0, len = results.length; i < len; i++) {
for (let i = 0, len = results.length; i < len; i++) {
if (results[i].name === 'climate') {
const resultUrl = results[i].value;
log.debug(
Expand Down Expand Up @@ -152,7 +152,7 @@ function getCountyUrl(metadataObj, lat, lon) {

function getClimateData(metadataObj, countyUrl) {
return new Promise((resolve, reject) => {
var options = {
const options = {
method: 'GET',
url: countyUrl,
// url: 'https://httpstat.us/402',
Expand All @@ -165,7 +165,7 @@ function getClimateData(metadataObj, countyUrl) {
log.debug('climate data status = ' + response.status);

if (response.status != 200) {
var err_json = {
const err_json = {
error_id: 70,
error_msg: 'Error retrieving county level data.',
};
Expand All @@ -177,11 +177,11 @@ function getClimateData(metadataObj, countyUrl) {
reject(err_json);
return;
} else {
var xmlData = response.data;
const xmlData = response.data;
if (XMLValidator.validate(xmlData) === true) {
var jsonObj = parser.parse(xmlData);
const jsonObj = parser.parse(xmlData);
// find EI_DAILY_AMOUNT
for (var i = 0, len = jsonObj.Obj.Flt.length; i < len; i++) {
for (let i = 0, len = jsonObj.Obj.Flt.length; i < len; i++) {
if (jsonObj.Obj.Flt[i].Name === 'EI_DAILY_AMOUNT') {
resolve(jsonObj.Obj.Flt[i].Calc);
return;
Expand All @@ -192,7 +192,7 @@ function getClimateData(metadataObj, countyUrl) {
})
.catch(function (err) {
if (err) {
var err_json = {
const err_json = {
error_id: 71,
error_msg: 'Climate attribute not found.',
};
Expand All @@ -210,7 +210,7 @@ function getClimateData(metadataObj, countyUrl) {
function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {
return new Promise((resolve, reject) => {
if (!EI_DAILY_AMOUNT) {
var err_json = {
const err_json = {
error_id: 80,
error_msg: '15: Internal Web Service Error. [EI_DAILY_AMOUNT is empty]',
};
Expand All @@ -234,7 +234,7 @@ function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {
// find number of days the project spans
// add one day to end date because timestamp is set to midnight for both dates,
// but want to include end date in count
var numProjectDays =
const numProjectDays =
(end_date.getTime() + 1000 * 3600 * 24 - start_date.getTime()) /
(1000 * 3600 * 24);
log.debug('numProjectDays = ' + numProjectDays);
Expand All @@ -247,15 +247,15 @@ function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {
rfactor = rfactor + Number(dailyEIdataNormal[p]);
}
} else {
var startDayOfYear = getDayOfYear(start_date);
var endDayOfYear = getDayOfYear(end_date);
const startDayOfYear = getDayOfYear(start_date);
const endDayOfYear = getDayOfYear(end_date);

if (endDayOfYear > startDayOfYear) {
log.debug('Project is contained within a year');

// determine whether or not the leap year data is needed for the start year
let dailyEIdata = dailyEIdataNormal;
if(isStartLeapYear || isEndLeapYear) dailyEIdata = dailyEIdataLeapYear;
if (isStartLeapYear || isEndLeapYear) dailyEIdata = dailyEIdataLeapYear;

// subtract 1 from startDayOfYear because index on dailyEIdata starts with 0
for (let p = startDayOfYear - 1; p < endDayOfYear; p++) {
Expand All @@ -266,10 +266,10 @@ function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {

// determine whether or not the leap year data is needed for the start year
let dailyEIdata = dailyEIdataNormal;
if(isStartLeapYear) dailyEIdata = dailyEIdataLeapYear;
if (isStartLeapYear) dailyEIdata = dailyEIdataLeapYear;

// dayCounter variable ensures all days of project span are counted, even if a leap day is included
var dayCounter = 0;
let dayCounter = 0;
// first calculate from start date to 12/31
// subtract 1 from startDayOfYear because index on dailyEIdata starts with 0
const numDays = isStartLeapYear ? 366 : 365;
Expand All @@ -280,10 +280,10 @@ function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {
log.debug('dayCounter = ' + dayCounter);

// determine whether or not the leap year data is needed for the end year
if(isEndLeapYear) dailyEIdata = dailyEIdataLeapYear;
if (isEndLeapYear) dailyEIdata = dailyEIdataLeapYear;
else dailyEIdata = dailyEIdataNormal;

var daysRemaining = numProjectDays - dayCounter;
const daysRemaining = numProjectDays - dayCounter;
// then start at 1/1 and go for number of days remaining in project
log.debug('daysRemaining = ' + daysRemaining);
for (let p = 0; p < daysRemaining; p++) {
Expand All @@ -310,9 +310,9 @@ function calculateRFactor(metadataObj, EI_DAILY_AMOUNT, start_date, end_date) {
});
}

var sendResponse = function sendResponse(metadataObj, rfactor, res) {
const sendResponse = function sendResponse(metadataObj, rfactor, res) {
return new Promise((resolve, reject) => {
var out = { rfactor: Number(rfactor) };
const out = { rfactor: Number(rfactor) };
res.json(out);
log.info(logger.formatLogMsg(metadataObj, out));
resolve();
Expand Down Expand Up @@ -359,16 +359,16 @@ function populateMetdataObj(request) {
* Default route
***********************************************************************/
module.exports.calculateRFactor = async (req, res) => {
var start_date = null;
var end_date = null;
var location = null;
let start_date = null;
let end_date = null;
let location = null;

const metadataObj = populateMetdataObj(req);

/*********************************************************
Check the existence and then validate api_key or X-Api-User-Id
*********************************************************/
var err_json = null;
let err_json = null;
if (
(req.hostname !== 'localhost' &&
req.header('X-Api-User-Id') === undefined) ||
Expand All @@ -383,7 +383,7 @@ module.exports.calculateRFactor = async (req, res) => {
};
log.warn(logger.formatLogMsg(metadataObj, err_json));
} else {
var api_user_id = req.header('X-Api-User-Id');
const api_user_id = req.header('X-Api-User-Id');
log.debug(logger.formatLogMsg(metadataObj, 'API User ID = ' + api_user_id));
}

Expand Down Expand Up @@ -501,9 +501,7 @@ module.exports.calculateRFactor = async (req, res) => {

if (
!err_json &&
(!location ||
!location.geometry ||
!location.geometry.coordinates ||
(!location?.geometry?.coordinates ||
location.geometry.coordinates.length !== 2)
) {
err_json = {
Expand All @@ -525,8 +523,8 @@ module.exports.calculateRFactor = async (req, res) => {
/***********************************************************************
*
***********************************************************************/
var lon = location.geometry.coordinates[0];
var lat = location.geometry.coordinates[1];
const lon = location.geometry.coordinates[0];
const lat = location.geometry.coordinates[1];

try {
const countyUrl = await getCountyUrl(metadataObj, lat, lon);
Expand Down
12 changes: 6 additions & 6 deletions app/app/server/routes/health.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var express = require("express");
const express = require('express');

module.exports = function(app) {
var router = express.Router();
module.exports = function (app) {
const router = express.Router();

router.get("/", function(req, res, next) {
res.json({ status: "UP" });
router.get('/', function (req, res, next) {
res.json({ status: 'UP' });
});

app.use("/v1/health", router);
app.use('/v1/health', router);
};
4 changes: 2 additions & 2 deletions app/app/server/routes/rfactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const express = require('express');
const logger = require('../utilities/logger.js');
const rfactorContoller = require('../controllers/rfactor.js');

module.exports = function(app) {
var router = express.Router();
module.exports = function (app) {
const router = express.Router();

router.get('/', rfactorContoller.calculateRFactor);

Expand Down
Loading

0 comments on commit d462679

Please sign in to comment.