From 030395e919725223173ff2e5ff91cbb08539000d Mon Sep 17 00:00:00 2001
From: tuckersiegel <siegel.tucker@gmail.com>
Date: Thu, 11 Mar 2021 12:50:00 -0500
Subject: [PATCH] update gdd logic

---
 controllers/gddAccumulatedController.js |  5 ++---
 controllers/gddController.js            |  6 ++----
 controllers/gddNormalController.js      |  6 +++---
 lib/utils.js                            | 12 +++++++++++-
 4 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/controllers/gddAccumulatedController.js b/controllers/gddAccumulatedController.js
index e00ef0c..5be4c73 100644
--- a/controllers/gddAccumulatedController.js
+++ b/controllers/gddAccumulatedController.js
@@ -27,9 +27,8 @@ function find(collection, query, projection, temps, res) {
         var gdd_sum = 0;
         
         for (var i = 0; i < min_temps.length; i++) {
-            min_temp = min_temps[i] >= t_min ? min_temps[i] : t_min;
-            max_temp = max_temps[i] <= t_max ? max_temps[i] : t_max;
-            gdd_sum += ((max_temp + min_temp) / 2) - t_base
+            gdd = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base);
+            gdd_sum += gdd
             gdds.push(gdd_sum)
         }
         send_response("Accumulated GDDs", gdds, data, res);
diff --git a/controllers/gddController.js b/controllers/gddController.js
index f0d9bd0..095c684 100644
--- a/controllers/gddController.js
+++ b/controllers/gddController.js
@@ -26,10 +26,8 @@ function find(collection, query, projection, temps, res) {
         var max_temp = 0
         
         for (var i = 0; i < min_temps.length; i++) {
-            min_temp = min_temps[i] >= t_min ? min_temps[i] : t_min;
-            max_temp = max_temps[i] <= t_max ? max_temps[i] : t_max;
-
-            gdds.push(((max_temp + min_temp) / 2) - t_base)
+            gdd = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base);
+            gdds.push(gdd)
         }
         send_response("GDDs", gdds, data, res);
     }, function(err) {
diff --git a/controllers/gddNormalController.js b/controllers/gddNormalController.js
index 349dba5..638e871 100644
--- a/controllers/gddNormalController.js
+++ b/controllers/gddNormalController.js
@@ -13,9 +13,9 @@ function find(collection, query, projection, temps, res) {
         var max_temp = 0
 
         for (var i = 0; i < min_temps.length; i++) {
-            min_temp = min_temps[i] >= t_min ? min_temps[i] : t_min;
-            max_temp = max_temps[i] <= t_max ? max_temps[i] : t_max;
-            gdds.push(((max_temp + min_temp) / 2) - t_base)
+            gdd = utils.calculate_gdd(min_temps[i], t_min, max_temps[i], t_max, t_base);
+
+            gdds.push(gdd)
         }
 
         res.json({
diff --git a/lib/utils.js b/lib/utils.js
index 45ec426..b450a8f 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -41,4 +41,14 @@ function product_base_switch(product, errors, t_base, t_min) {
     }
 }
 
-module.exports.product_base_switch = product_base_switch;
\ No newline at end of file
+
+function calculate_gdd(min_temp, t_min, max_temp, t_max, t_base) {
+    min_temp = min_temp >= t_min ? min_temp : t_min;
+    max_temp = max_temp >= t_min ? max_temp : t_min;
+    max_temp = max_temp <= t_max ? max_temp : t_max;
+    gdd = ((max_temp + min_temp) / 2) - t_base;
+    return gdd
+}
+
+module.exports.product_base_switch = product_base_switch;
+module.exports.calculate_gdd = calculate_gdd;
\ No newline at end of file
-- 
GitLab