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)) (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") (def default-config-path "config.yaml")
(defn main [& args] (defn main [& args]
@ -504,36 +525,37 @@
;; file. Otherwise, only read config.yaml if it exists. ;; file. Otherwise, only read config.yaml if it exists.
(p/try (p/try
(p/let [base-config (if-let [config-path (.-config commander)] (p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path) (util/read-config config-path)
(if (fs/existsSync default-config-path) (if (fs/existsSync default-config-path)
(util/read-config default-config-path) (util/read-config default-config-path)
{})) {}))
cli-config (build-config-from-commander commander) cli-config (build-config-from-commander commander)
secrets (util/read-config (.-secrets commander)) secrets (util/read-config (.-secrets commander))
config (build-config base-config cli-config secrets) config (build-config base-config cli-config secrets)
db (read-history-db (:history-db-path config)) _ (validate-config config)
data (get-adsbexchange-live-data db (read-history-db (:history-db-path config))
{:url (get-in config [:adsbx :url]) data (get-adsbexchange-live-data
:api-key (get-in config [:adsbx :api-key]) {:url (get-in config [:adsbx :url])
:lat (:lat config) :api-key (get-in config [:adsbx :api-key])
:lon (:lon config) :lat (:lat config)
:radius-nm (* (:radius-km config) 0.539957)}) :lon (:lon config)
now (current-time) :radius-nm (* (:radius-km config) 0.539957)})
[new-db potential-circles] (-> db now (current-time)
(update-history-db (:aircraft data) now config) [new-db potential-circles] (-> db
(detect-circles now config))] (update-history-db (:aircraft data) now config)
(p/do (detect-circles now config))]
(when potential-circles (p/do
(doseq [ac potential-circles] (when potential-circles
(log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac))) (doseq [ac potential-circles]
(process-potential-circles potential-circles config now)) (log-warn "%s: New circle detected: %s" (:icao ac) (ac-desc ac)))
(write-history-db new-db (:history-db-path config)) (process-potential-circles potential-circles config now))
(let [end-time (current-time)] (write-history-db new-db (:history-db-path config))
(log-info (let [end-time (current-time)]
"Completed processing in %f seconds: tracking %s aircraft; %s potential circles" (log-info
(/ (- end-time start-time) 1000) "Completed processing in %f seconds: tracking %s aircraft; %s potential circles"
(count new-db) (/ (- end-time start-time) 1000)
(count potential-circles))))) (count new-db)
(count potential-circles)))))
(p/catch :default e (p/catch :default e
(log-error "%s" (.-stack e)) (log-error "%s" (.-stack e))
(.exit js/process 1))))) (.exit js/process 1)))))

View File

@ -70,3 +70,18 @@
(if (every? map? vs) (if (every? map? vs)
(apply merge-with deep-merge vs) (apply merge-with deep-merge vs)
(last 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 {:secret "123"}})
{:adsbx {:url "http://bar", :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]])))