From 70292ca208ac8e6774e07d1022b75b4d1d1a5a89 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 22 Dec 2015 09:29:00 +0000 Subject: [PATCH 1/2] Adds 'error' to forecast Also fixes bug with `latest?` --- resources/db-schema.cql | 1 + src/witan/app/forecast.clj | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/resources/db-schema.cql b/resources/db-schema.cql index c661065..d0d4bb6 100644 --- a/resources/db-schema.cql +++ b/resources/db-schema.cql @@ -16,6 +16,7 @@ CREATE TABLE forecast_headers ( version int, description text, in_progress boolean, + error text, public boolean, name text, owner uuid, diff --git a/src/witan/app/forecast.clj b/src/witan/app/forecast.clj index 9d57b26..4f336e3 100644 --- a/src/witan/app/forecast.clj +++ b/src/witan/app/forecast.clj @@ -176,6 +176,13 @@ :in_progress false}) (hayt/where {:forecast_id forecast-id :version version}))) +(defn update-forecast-header-error + [{:keys [forecast-id error]}] + (hayt/update :forecast_headers + (hayt/set-columns {:error error + :in_progress false}) + (hayt/where {:forecast_id forecast-id}))) + (defn get-most-recent-version [id] (first (c/exec (find-most-recent-version id)))) @@ -312,8 +319,13 @@ (hayt/user-type output-as-data)) outputs))) (defn process-error! - [{:keys [forecast-id version]} error] - (c/exec (update-forecast-error {:forecast-id forecast-id :version version :error (or error "No error message was provided.")}))) + [{:keys [forecast-id version version-id]} error] + (let [latest (get-most-recent-version forecast-id) + error (or error "No error message was provided.")] + (c/exec (update-forecast-error {:forecast-id forecast-id :version version :error error})) + (when (= (:version-id latest) version-id) + (c/exec (update-forecast-header-error {:forecast-id forecast-id :version version :error error}))))) + (defn run-model! ([forecast] @@ -370,11 +382,12 @@ (hash-map (name category) (data/get-data-by-data-id data-id))) (defn create-new-forecast-version! - [{:keys [forecast-id version] :as forecast}] + [{:keys [forecast-id version] :as forecast} old-version] (c/exec (create-forecast-version forecast)) (c/exec (update-forecast-current-version-id forecast)) - (if (== version 1) - (c/exec (delete-forecast-by-version forecast-id 0)))) + (if (zero? old-version) + (c/exec (delete-forecast-by-version forecast-id 0)) + (c/exec (update-forecast-latest forecast-id old-version false)))) (defn update-forecast! [{:keys [forecast-id owner inputs]}] @@ -396,7 +409,7 @@ :model-id (:model_id latest-forecast) :model-property-values (into {} (for [[k v] (:model_property_values latest-forecast)] [k (hayt/user-type v)])) :inputs (into {} (for [[k v] inputs] [(name k) (hayt/user-type v)])))] - (create-new-forecast-version! new-forecast) + (create-new-forecast-version! new-forecast old-version) (run-model! (assoc new-forecast :inputs inputs) model) ;; assoc to use the original inputs (not UDT'd) (get-forecast-version forecast-id new-version)) (do (log/error "The incorrect number of inputs was supplied") From c08f5367f455b448e2cdfb24ce70e12bb79706ef Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 22 Dec 2015 09:37:41 +0000 Subject: [PATCH 2/2] Fix tests --- test/witan/app/handler_test.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/witan/app/handler_test.clj b/test/witan/app/handler_test.clj index 53f6180..03ba63a 100644 --- a/test/witan/app/handler_test.clj +++ b/test/witan/app/handler_test.clj @@ -251,7 +251,7 @@ user/retrieve-user (fn [_] {:name "user1"}) model/get-model-by-model-id (fn [_] (first (get-dummy-models))) forecast/get-most-recent-version (fn [_] (first (get-dummy-forecasts))) - forecast/create-new-forecast-version! (fn [_] {}) + forecast/create-new-forecast-version! (fn [_ _] {}) mex/execute-model (fn [_ _] {"housing-linked-population" [{:name "housing linked population figures - single year" :data 123} @@ -268,7 +268,7 @@ user/retrieve-user (fn [_] {:name "user2"}) model/get-model-by-model-id (fn [_] (first (get-dummy-models))) forecast/get-most-recent-version (fn [_] (first (get-dummy-forecasts))) - forecast/create-new-forecast-version! (fn [_] {}) + forecast/create-new-forecast-version! (fn [_ _] {}) forecast/process-error! get-error-forecast mex/execute-model (fn [_ _] {:error "this is an error"})