Skip to content

Commit

Permalink
Linted the middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mollybsmith-noaa committed Oct 18, 2024
1 parent 5725d56 commit 4c19166
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 220 deletions.
13 changes: 3 additions & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@
// our first pass, we'll only consider the checks that ESLint can auto-fix as errors.
// https://eslint.org/docs/latest/use/configure/rules#rule-severities
"meteor/no-session": "off",
"import/no-unresolved": ["warn", { "ignore": ["^meteor/", "^/"] }] // This may be tricky to resolve due to: https://github.com/clayne11/eslint-import-resolver-meteor/issues/5
},
"ignorePatterns": [
"meteor_packages/mats-common/imports/startup/server/matsMiddle_common.js",
"meteor_packages/mats-common/imports/startup/server/matsMiddle_dailyModelCycle.js",
"meteor_packages/mats-common/imports/startup/server/matsMiddle_dieoff.js",
"meteor_packages/mats-common/imports/startup/server/matsMiddle_map.js",
"meteor_packages/mats-common/imports/startup/server/matsMiddle_timeSeries.js",
"meteor_packages/mats-common/imports/startup/server/matsMiddle_validTime.js"
]
"import/no-unresolved": ["warn", { "ignore": ["^meteor/", "^/"] }], // This may be tricky to resolve due to: https://github.com/clayne11/eslint-import-resolver-meteor/issues/5
"no-await-in-loop": "warn"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { matsTypes, matsDataQueryUtils } from "meteor/randyp:mats-common";
import { Meteor } from "meteor/meteor";
/* global Assets */

class MatsMiddleCommon {
cbPool = null;
Expand All @@ -10,69 +9,51 @@ class MatsMiddleCommon {
this.cbPool = cbPool;
}

/* eslint-disable global-require */
/* eslint-disable no-console */
/* eslint-disable class-methods-use-this */

writeToLocalFile(filePath, contentStr) {
const fs = require("fs");
const homedir = require("os").homedir();
fs.writeFileSync(homedir + filePath, contentStr);
}

get_fcstValidEpoch_Array = async (fromSecs, toSecs) => {
console.log(`get_fcstValidEpoch_Array(${fromSecs},${toSecs})`);

const fs = require("fs");
this.conn = await cbPool.getConnection();

const startTime = new Date().valueOf();
getFcstValidEpochArray = async (fromSecs, toSecs) => {
this.conn = await this.cbPool.getConnection();

let queryTemplate = Assets.getText(
"imports/startup/server/matsMiddle/sqlTemplates/tmpl_distinct_fcstValidEpoch_obs.sql"
);
queryTemplate = queryTemplate.replace(/{{vxFROM_SECS}}/g, fromSecs);
queryTemplate = queryTemplate.replace(/{{vxTO_SECS}}/g, toSecs);
console.log(`fromSecs:${fromSecs},toSecs:${toSecs}`);

const qr_fcstValidEpoch = await this.conn.cluster.query(queryTemplate);
const qrFcstValidEpoch = await this.conn.cluster.query(queryTemplate);

const fcstValidEpoch_Array = [];
for (let imfve = 0; imfve < qr_fcstValidEpoch.rows.length; imfve++) {
fcstValidEpoch_Array.push(qr_fcstValidEpoch.rows[imfve].fcstValidEpoch);
const fcstValidEpochArray = [];
for (let imfve = 0; imfve < qrFcstValidEpoch.rows.length; imfve += 1) {
fcstValidEpochArray.push(qrFcstValidEpoch.rows[imfve].fcstValidEpoch);
}
const endTime = new Date().valueOf();
console.log(
`\tget_fcstValidEpoch_Array():${fcstValidEpoch_Array.length} in ${
endTime - startTime
} ms.`
);

return fcstValidEpoch_Array;
return fcstValidEpochArray;
};

get_fcstLen_Array = async (model, fromSecs, toSecs) => {
console.log(`get_fcstLen_Array(${model},${fromSecs},${toSecs})`);

const fs = require("fs");
this.conn = await cbPool.getConnection();

const startTime = new Date().valueOf();
this.conn = await this.cbPool.getConnection();

let queryTemplate = Assets.getText(
"imports/startup/server/matsMiddle/sqlTemplates/tmpl_get_distinct_fcstLen.sql"
);
queryTemplate = queryTemplate.replace(/{{vxMODEL}}/g, `"${model}"`);
queryTemplate = queryTemplate.replace(/{{vxFROM_SECS}}/g, fromSecs);
queryTemplate = queryTemplate.replace(/{{vxTO_SECS}}/g, toSecs);
console.log(`model:${model},fromSecs:${fromSecs},toSecs:${toSecs}`);

const qr_distinct_fcstLen = await this.conn.cluster.query(queryTemplate);
const qrDistinctFcstLen = await this.conn.cluster.query(queryTemplate);

const fcstLenArray = [];
for (let ifcstLen = 0; ifcstLen < qr_distinct_fcstLen.rows.length; ifcstLen++) {
fcstLenArray.push(qr_distinct_fcstLen.rows[ifcstLen].fcstLen);
for (let ifcstLen = 0; ifcstLen < qrDistinctFcstLen.rows.length; ifcstLen += 1) {
fcstLenArray.push(qrDistinctFcstLen.rows[ifcstLen].fcstLen);
}
endTime = new Date().valueOf();
console.log(
`fcstLenArray:${qr_distinct_fcstLen.rows.length} in ${endTime - startTime} ms.`
);

return fcstLenArray;
};
Expand Down Expand Up @@ -163,47 +144,50 @@ class MatsMiddleCommon {
obsSingleFve,
modelSingleFve
) {
const thisCtc = ctc;
for (let i = 0; i < stationNames.length; i += 1) {
const station = stationNames[i];
const varValO = obsSingleFve.stations[station];
const varValM = modelSingleFve.stations[station];

if (varValO && varValM) {
ctc.n0 += 1;
thisCtc.n0 += 1;
let sub = `${fve};`;
if (varValO < threshold && varValM < threshold) {
ctc.hit += 1;
thisCtc.hit += 1;
sub += "1;";
} else {
sub += "0;";
}

if (varValO >= threshold && varValM < threshold) {
ctc.fa += 1;
thisCtc.fa += 1;
sub += "1;";
} else {
sub += "0;";
}

if (varValO < threshold && varValM >= threshold) {
ctc.miss += 1;
thisCtc.miss += 1;
sub += "1;";
} else {
sub += "0;";
}

if (varValO >= threshold && varValM >= threshold) {
ctc.cn += 1;
thisCtc.cn += 1;
sub += "1";
} else {
sub += "0";
}
ctc.sub_data.push(sub);
thisCtc.sub_data.push(sub);
}
}
return thisCtc;
}

computeSumsForStations(fve, sums, stationNames, obsSingleFve, modelSingleFve) {
const thisSums = sums;
for (let i = 0; i < stationNames.length; i += 1) {
const station = stationNames[i];
const varValO = obsSingleFve.stations[station];
Expand All @@ -217,21 +201,23 @@ class MatsMiddleCommon {
const obsSum = varValO;
const absSum = Math.abs(varValO - varValM);

sums.n0 += 1;
sums.square_diff_sum += squareDiffSum;
sums.N_sum += nSum;
sums.obs_model_diff_sum += obsModelDiffSum;
sums.model_sum += modelSum;
sums.obs_sum += obsSum;
sums.abs_sum += absSum;
thisSums.n0 += 1;
thisSums.square_diff_sum += squareDiffSum;
thisSums.N_sum += nSum;
thisSums.obs_model_diff_sum += obsModelDiffSum;
thisSums.model_sum += modelSum;
thisSums.obs_sum += obsSum;
thisSums.abs_sum += absSum;

const sub = `${fve};${squareDiffSum};${nSum};${obsModelDiffSum};${modelSum};${obsSum};${absSum};`;
sums.sub_data.push(sub);
thisSums.sub_data.push(sub);
}
}
return thisSums;
}
}

// eslint-disable-next-line no-undef
export default matsMiddleCommon = {
MatsMiddleCommon,
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* Copyright (c) 2021 Colorado State University and Regents of the University of Colorado. All rights reserved.
*/

/* global cbPool, Assets */
/* global Assets */

import { matsTypes, matsMiddleCommon } from "meteor/randyp:mats-common";
import { matsMiddleCommon } from "meteor/randyp:mats-common";
import { _ } from "meteor/underscore";

class MatsMiddleDailyModelCycle {
logToFile = false;

logMemUsage = false;

fcstValidEpoch_Array = [];
fcstValidEpochArray = [];

indVar_Array = [];
indVarArray = [];

cbPool = null;

Expand Down Expand Up @@ -53,6 +53,10 @@ class MatsMiddleDailyModelCycle {
this.mmCommon = new matsMiddleCommon.MatsMiddleCommon(cbPool);
}

/* eslint-disable global-require */
/* eslint-disable no-console */
/* eslint-disable class-methods-use-this */

processStationQuery = (
statType,
varName,
Expand All @@ -69,7 +73,7 @@ class MatsMiddleDailyModelCycle {
let rv = [];
const dFuture = new Future();
(async () => {
rv = await this.processStationQuery_int(
rv = await this.processStationQueryInt(
statType,
varName,
stationNames,
Expand All @@ -86,7 +90,7 @@ class MatsMiddleDailyModelCycle {
return rv;
};

processStationQuery_int = async (
processStationQueryInt = async (
statType,
varName,
stationNames,
Expand All @@ -97,8 +101,6 @@ class MatsMiddleDailyModelCycle {
utcCycleStart,
filterInfo
) => {
const fs = require("fs");

this.statType = statType;
this.varName = varName;
this.stationNames = stationNames;
Expand All @@ -109,21 +111,21 @@ class MatsMiddleDailyModelCycle {
this.utcCycleStart = utcCycleStart;
this.filterInfo = filterInfo;

this.conn = await cbPool.getConnection();
this.conn = await this.cbPool.getConnection();

this.fcstValidEpoch_Array = await this.mmCommon.get_fcstValidEpoch_Array(
this.fcstValidEpochArray = await this.mmCommon.getFcstValidEpochArray(
fromSecs,
toSecs
);

// create distinct indVar array
for (let iofve = 0; iofve < this.fcstValidEpoch_Array.length; iofve += 1) {
const indVar = this.fcstValidEpoch_Array[iofve];
if (!this.indVar_Array.includes(indVar)) {
this.indVar_Array.push(indVar);
for (let iofve = 0; iofve < this.fcstValidEpochArray.length; iofve += 1) {
const indVar = this.fcstValidEpochArray[iofve];
if (!this.indVarArray.includes(indVar)) {
this.indVarArray.push(indVar);
}
}
this.indVar_Array.sort((a, b) => Number(a) - Number(b));
this.indVarArray.sort((a, b) => Number(a) - Number(b));

await this.createObsData();
await this.createModelData();
Expand All @@ -147,8 +149,6 @@ class MatsMiddleDailyModelCycle {
};

createObsData = async () => {
const fs = require("fs");

const tmplGetNStationsMfveObs = Assets.getText(
"imports/startup/server/matsMiddle/sqlTemplates/tmpl_get_N_stations_mfve_IN_obs.sql"
);
Expand Down Expand Up @@ -177,8 +177,8 @@ class MatsMiddleDailyModelCycle {
);

const promises = [];
for (let iofve = 0; iofve < this.fcstValidEpoch_Array.length; iofve += 100) {
const fveArraySlice = this.fcstValidEpoch_Array.slice(iofve, iofve + 100);
for (let iofve = 0; iofve < this.fcstValidEpochArray.length; iofve += 100) {
const fveArraySlice = this.fcstValidEpochArray.slice(iofve, iofve + 100);
const sql = tmplWithStationNamesObs.replace(
/{{fcstValidEpoch}}/g,
JSON.stringify(fveArraySlice)
Expand Down Expand Up @@ -216,8 +216,6 @@ class MatsMiddleDailyModelCycle {
};

createModelData = async () => {
const fs = require("fs");

let tmplGetNStationsMfveModel = Assets.getText(
"imports/startup/server/matsMiddle/sqlTemplates/tmpl_get_N_stations_mfve_IN_model.sql"
);
Expand Down Expand Up @@ -259,8 +257,8 @@ class MatsMiddleDailyModelCycle {
);

const promises = [];
for (let imfve = 0; imfve < this.fcstValidEpoch_Array.length; imfve += 100) {
const fveArraySlice = this.fcstValidEpoch_Array.slice(imfve, imfve + 100);
for (let imfve = 0; imfve < this.fcstValidEpochArray.length; imfve += 100) {
const fveArraySlice = this.fcstValidEpochArray.slice(imfve, imfve + 100);
const sql = tmplWithStationNamesModels.replace(
/{{fcstValidEpoch}}/g,
JSON.stringify(fveArraySlice)
Expand Down Expand Up @@ -313,7 +311,7 @@ class MatsMiddleDailyModelCycle {
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const ctcStats = {};
let ctcStats = {};

const indVar = indVarsWithData[idx];
ctcStats.avtime = Number(indVar);
Expand All @@ -338,7 +336,7 @@ class MatsMiddleDailyModelCycle {
const modelSingleFve = indVarSingle[fve];

if (obsSingleFve && modelSingleFve) {
this.mmCommon.computeCtcForStations(
ctcStats = this.mmCommon.computeCtcForStations(
fve,
threshold,
ctcStats,
Expand Down Expand Up @@ -368,7 +366,7 @@ class MatsMiddleDailyModelCycle {
});

for (let idx = 0; idx < indVarsWithData.length; idx += 1) {
const sumsStats = {};
let sumsStats = {};

const indVar = indVarsWithData[idx];
sumsStats.avtime = Number(indVar);
Expand All @@ -395,7 +393,7 @@ class MatsMiddleDailyModelCycle {
const modelSingleFve = indVarSingle[fve];

if (obsSingleFve && modelSingleFve) {
this.mmCommon.computeSumsForStations(
sumsStats = this.mmCommon.computeSumsForStations(
fve,
sumsStats,
this.stationNames,
Expand All @@ -415,6 +413,7 @@ class MatsMiddleDailyModelCycle {
};
}

// eslint-disable-next-line no-undef
export default matsMiddleDailyModelCycle = {
MatsMiddleDailyModelCycle,
};
Loading

0 comments on commit 4c19166

Please sign in to comment.