Validate config.

This commit is contained in:
John Wiseman 2020-01-21 20:29:18 -08:00
parent ea0bb782dc
commit fe17c489d9
3 changed files with 74 additions and 30 deletions

View File

@ -484,6 +484,27 @@
(util/deep-merge default-config config cli-config secrets))
(defn validate-config [config]
(let [required [[:lat]
[:lon]
[:radius-km]
[:basestation-sqb]
[:adsbx :url]
[:adsbx :api-key]
[:pelias :url]]
present (util/nested-keys config)
missing1 (set/difference (set required) (set present))
missing2 (when (get-in config [:twitter :enabled?])
(let [required (map (fn [key] [:twitter key])
[:consumer-key :consumer-secret
:access-token :access-token-secret])]
(set/difference (set required) (set present))))
missing (concat missing1 missing2)]
(when missing
(throw (js/Error. (str "Missing configuration values: "
(string/join ", " (sort-by str missing))))))))
(def default-config-path "config.yaml")
(defn main [& args]
@ -504,36 +525,37 @@
;; file. Otherwise, only read config.yaml if it exists.
(p/try
(p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
secrets (util/read-config (.-secrets commander))
config (build-config base-config cli-config secrets)
db (read-history-db (:history-db-path config))
data (get-adsbexchange-live-data
{:url (get-in config [:adsbx :url])
:api-key (get-in config [:adsbx :api-key])
:lat (:lat config)
:lon (:lon config)
:radius-nm (* (:radius-km config) 0.539957)})
now (current-time)
[new-db potential-circles] (-> db
(update-history-db (:aircraft data) now config)
(detect-circles now config))]
(p/do
(when potential-circles
(doseq [ac potential-circles]
(log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac)))
(process-potential-circles potential-circles config now))
(write-history-db new-db (:history-db-path config))
(let [end-time (current-time)]
(log-info
"Completed processing in %f seconds: tracking %s aircraft; %s potential circles"
(/ (- end-time start-time) 1000)
(count new-db)
(count potential-circles)))))
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
secrets (util/read-config (.-secrets commander))
config (build-config base-config cli-config secrets)
_ (validate-config config)
db (read-history-db (:history-db-path config))
data (get-adsbexchange-live-data
{:url (get-in config [:adsbx :url])
:api-key (get-in config [:adsbx :api-key])
:lat (:lat config)
:lon (:lon config)
:radius-nm (* (:radius-km config) 0.539957)})
now (current-time)
[new-db potential-circles] (-> db
(update-history-db (:aircraft data) now config)
(detect-circles now config))]
(p/do
(when potential-circles
(doseq [ac potential-circles]
(log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac)))
(process-potential-circles potential-circles config now))
(write-history-db new-db (:history-db-path config))
(let [end-time (current-time)]
(log-info
"Completed processing in %f seconds: tracking %s aircraft; %s potential circles"
(/ (- end-time start-time) 1000)
(count new-db)
(count potential-circles)))))
(p/catch :default e
(log-error "%s" (.-stack e))
(.exit js/process 1)))))

View File

@ -70,3 +70,18 @@
(if (every? map? vs)
(apply merge-with deep-merge vs)
(last vs)))
;; From https://stackoverflow.com/a/21769626/122762
(defn nested-keys [m]
(if (map? m)
(vec
(mapcat (fn [[k v]]
(let [sub (nested-keys v)
nested (map #(into [k] %) (filter (comp not empty?) sub))]
(if (seq nested)
nested
[[k]])))
m))
[]))

View File

@ -13,3 +13,10 @@
{}
{:adsbx {:secret "123"}})
{:adsbx {:url "http://bar", :secret "123"}})))
(deftest nested-keys
(is (= (util/nested-keys {:a 1 :b 2})
[[:a] [:b]]))
(is (= (util/nested-keys {:a {:b {:c 1} :c {:d 2}} :b {:c 3} :c 4})
[[:a :b :c] [:a :c :d] [:b :c] [:c]])))