Skip to content

Commit

Permalink
Merge pull request #117 from MastodonC/feature/add-error-to-forecast-…
Browse files Browse the repository at this point in the history
…header

Adds 'error' to forecast
  • Loading branch information
acron0 committed Dec 22, 2015
2 parents 62a3b1a + c08f536 commit dad63ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions resources/db-schema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE forecast_headers (
version int,
description text,
in_progress boolean,
error text,
public boolean,
name text,
owner uuid,
Expand Down
25 changes: 19 additions & 6 deletions src/witan/app/forecast.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]}]
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions test/witan/app/handler_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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"})
Expand Down

0 comments on commit dad63ea

Please sign in to comment.