From b9c2e02ecbf9cd7ad9962a461e5eb1a4e4fb94ea Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Mon, 4 May 2020 14:13:48 -0700 Subject: [PATCH] More unitization. --- advisorycircular.sh | 4 +- src/main/lemondronor/advisorycircular.cljs | 119 ++++++++++-------- .../lemondronor/advisorycircular/logging.cljs | 35 ++++-- .../lemondronor/advisorycircular/util.cljs | 12 ++ 4 files changed, 105 insertions(+), 65 deletions(-) diff --git a/advisorycircular.sh b/advisorycircular.sh index 22ee7d3..a2ca56e 100644 --- a/advisorycircular.sh +++ b/advisorycircular.sh @@ -5,9 +5,9 @@ dir=$(dirname "$0") instance="$1" config_dir=/etc/advisorycircular run_dir=/var/run/advisorycircular -sqb_path=/usr/local/share/run/advisorycircular/clean-basestation.sqb +sqb_path=/usr/local/share/advisorycircular/clean-basestation.sqb config_path="$config_dir"/"$instance"-config.yaml secrets_path="$config_dir"/"$instance"-secrets.yaml history_path="$run_dir"/"$instance"-history.edn -(cd "$run_dir" && "$dir"/intervalexec 10 node $dir/out/advisorycircular.js --config "$config_path" --secrets "$secrets_path" --basestation-sqb "$sqb_path" --history "$history_path") +(cd "$run_dir" && "$dir"/intervalexec 10 node $dir/out/advisorycircular.js --config "$config_path" --secrets "$secrets_path" --basestation-sqb "$sqb_path" --history "$history_path" --log-prefix "$instance") diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 403f306..0f0e38a 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -19,6 +19,9 @@ (logging/deflog "advisorycircular" logger) +(def log-prefix (atom "")) + + (defn get-basestation-sqb-record [icao db-path] (log-info "%s: Looking up in %s" icao db-path) (p/let [record @@ -168,18 +171,19 @@ (defn ac-desc [ac] (str (:icao ac) " " "http://tar1090.adsbexchange.com/?icao=" (:icao ac) " " (:lat ac) " " (:lon ac) - " #" (:registration ac) " " (:alt ac) " " (:curviness ac) " " + " " (:registration ac) " " (:alt ac) " " (:curviness ac) " " (:normalized-curviness ac))) -(defn screenshot [icao lat lon] - (p/let [image-path - (adsbx/screenshot-aircraft icao lat lon - {:timeout 30000 - ;;:headless? false - ;; :viewport {:width 1600 :height 800} - ;; :clip {:width 1600 :height 800 :x 0 :y 0} - })] +(defn screenshot [icao lat lon now] + (p/let [image-path (str icao "-" (util/format-utc-ts now) ".png") + _ (adsbx/screenshot-aircraft icao lat lon + {:timeout 30000 + :output-path image-path + ;;:headless? false + ;; :viewport {:width 1600 :height 800} + ;; :clip {:width 1600 :height 800 :x 0 :y 0} + })] (log-verbose "%s: Got screenshot" icao) image-path)) @@ -191,8 +195,8 @@ (defn circling2? [ac config] (let [relevant-history (filter #(and (:alt %) (> (:alt %) 300)) (:history ac))] - (and (> (geo/flight-curviness relevant-history) (:curviness-threshold-degrees config)) - (> (:alt ac) 300)))) + (and (> (geo/flight-curviness relevant-history) (:curviness-threshold-degrees config)) + (> (:alt ac) 300)))) ;; Returns a vector of two elements, @@ -436,7 +440,7 @@ (defn track->geojson [older-positions recent-positions icao centroid] {:type "FeatureCollection" - :features + :features [(geojson-linestring older-positions {:stroke "#c0070b" :stroke-width 2 @@ -477,48 +481,48 @@ (:distance airport-properties) (:minimum-airport-distance-km config) (:label airport-properties) - ())) - (let [alts (map :alt recent-positions) - min-alt (apply min alts) - max-alt (apply max alts) - ratio (/ max-alt min-alt)] - (log-info "%s: ratio of min alt to max alt: %s [%s - %s]" icao (.toFixed (/ max-alt min-alt) 1) min-alt max-alt)) - (do - (p/let [coarse (pelias/reverse (:pelias config) lat lon {:layers "coarse"})] - (let [coarse (first (:features coarse))] - (log-info "%s: Reverse geocode: %s" icao (:properties coarse)) - ;; Note that if we're over the ocean we get null :( - (p/then (p/all [(screenshot (:icao ac) lat lon) - (p/let [nearby (landmark config lat lon) - _ (log-info "WOOO %s" nearby) - sqb (if-let [sqb-path (:basestation-sqb config)] - (get-basestation-sqb-record icao sqb-path))] - (let [description (generate-description ac sqb coarse nearby)] - (log-info "Description: %s" description) - description))]) - (fn [[image-path description]] - (if (or (nil? coarse) - ;; TODO: Filter using the layer hierarchy; we want - ;; anything smaller than "region" (state). - (= (get-in coarse [:properties :name]) "California")) - (log-info "%s: Filtering out because it is outside Los Angeles County" (:icao ac)) - (if (and image-path description) - (do - (if (get-in config [:twitter :enabled?]) - (twitter/tweet (twitter/twit (:twitter config)) - description - [image-path] - lat - lon) - (log-warn "Skipping tweeting")) - (let [path (str (.toFixed (/ now 1000) 0) "-" icao "-recent" ".geojson")] - (util/write-file - path - (.stringify - js/JSON - (clj->js (track->geojson older-positions recent-positions icao centroid))) - {}))) - (log-warn "Skipping tweet %s %s" image-path description)))))))))) + ()) + ;; (let [alts (map :alt recent-positions) + ;; min-alt (apply min alts) + ;; max-alt (apply max alts) + ;; ratio (/ max-alt min-alt)] + ;; (log-info "%s: ratio of min alt to max alt: %s [%s - %s]" icao (.toFixed (/ max-alt min-alt) 1) min-alt max-alt)) + (do + (p/let [coarse (pelias/reverse (:pelias config) lat lon {:layers "coarse"})] + (let [coarse (first (:features coarse))] + (log-info "%s: Reverse geocode: %s" icao (:properties coarse)) + ;; Note that if we're over the ocean we get null :( + (p/then (p/all [(screenshot (:icao ac) lat lon now) + (p/let [nearby (landmark config lat lon) + _ (log-info "WOOO %s" nearby) + sqb (if-let [sqb-path (:basestation-sqb config)] + (get-basestation-sqb-record icao sqb-path))] + (let [description (generate-description ac sqb coarse nearby)] + (log-info "Description: %s" description) + description))]) + (fn [[image-path description]] + (if (or (nil? coarse) + ;; TODO: Filter using the layer hierarchy; we want + ;; anything smaller than "region" (state). + (= (get-in coarse [:properties :name]) "California")) + (log-info "%s: Filtering out because it is outside Los Angeles County" (:icao ac)) + (if (and image-path description) + (do + (if (get-in config [:twitter :enabled?]) + (twitter/tweet (twitter/twit (:twitter config)) + description + [image-path] + lat + lon) + (log-warn "Skipping tweeting")) + (let [path (str icao "-" (util/format-utc-ts now) ".geojson")] + (util/write-file + path + (.stringify + js/JSON + (clj->js (track->geojson older-positions recent-positions icao centroid))) + {}))) + (log-warn "Skipping tweet %s %s" image-path description))))))))))) (defn process-potential-circles [acs config now] @@ -536,7 +540,7 @@ ;; This is how many degrees of turning we need to see over ;; max-history-age-ms ms to consider it a potential circling ;; aircraft. - :curviness-threshold-degrees (* 2 360) + :curviness-threshold-degrees (* 4 360) ;; If the centroid of the aircraft's positions is less than this ;; close to an airport, then it's probably just doing flight ;; training. @@ -610,7 +614,12 @@ (.option "--config " "Path to the configuration yaml file") (.option "--secrets " "Path to the secrets yaml file" "secrets.yaml") (.option "--history " "Path to history/state file" "advisorycircular.edn") + (.option "--log-prefix " "Log prefix to use") (.parse (.-argv js/process))) + (logging/set-log-prefix! (or (.-logPrefix commander) "")) + (reset! log-prefix (if (.-logPrefix commander) + (str (.-logPrefix commander) "-") + "")) (let [start-time (current-time)] ;; If --config-path is specified, definitely try to read that ;; file. Otherwise, only read config.yaml if it exists. diff --git a/src/main/lemondronor/advisorycircular/logging.cljs b/src/main/lemondronor/advisorycircular/logging.cljs index 600c47f..6b18b81 100644 --- a/src/main/lemondronor/advisorycircular/logging.cljs +++ b/src/main/lemondronor/advisorycircular/logging.cljs @@ -6,15 +6,34 @@ (:require-macros [lemondronor.advisorycircular.logging])) + +;; (def format-msg [msg] +;; (gstring/format "%s%-7s %-9s/%-18s| %s" +;; (.-timestamp %) +;; (.-ms %) +;; (.-service %) +;; (.-level %) +;; (.-message %))) + +(def log-prefix (atom "")) + +(defn set-log-prefix! [prefix] + (swap! log-prefix (constantly prefix))) + + +(defn format-msg [msg] + (let [prefix (if (and @log-prefix (> (count @log-prefix) 0)) + (str @log-prefix " ") + "")] + (gstring/format "%s%s %s" + prefix + (.-level msg) + (.-message msg)))) + + (let [createLogger (.-createLogger winston) format (.-format winston) - transports (.-transports winston) - printf-fmt #(gstring/format "%s%-7s %-9s/%-18s| %s" - (.-timestamp %) - (.-ms %) - (.-service %) - (.-level %) - (.-message %))] + transports (.-transports winston)] (def logger (createLogger #js {:level "info" :format (.combine @@ -30,7 +49,7 @@ :defaultMeta #js {}})) (.add logger (new (.-Console transports) #js {:format (.combine format - (.printf format printf-fmt))}))) + (.printf format format-msg))}))) (defn get-logger [service] diff --git a/src/main/lemondronor/advisorycircular/util.cljs b/src/main/lemondronor/advisorycircular/util.cljs index b396140..d2d0b5e 100644 --- a/src/main/lemondronor/advisorycircular/util.cljs +++ b/src/main/lemondronor/advisorycircular/util.cljs @@ -85,3 +85,15 @@ [[k]]))) m)) [])) + + +(defn format-utc-ts [millis] + (let [date (js/Date. millis)] + (str (.getUTCFullYear date) + (.getUTCMonth date) + (.getUTCDate date) + "-" + (.getUTCHours date) + (.getUTCMinutes date) + (.getUTCSeconds date) + (.getUTCMilliseconds date))))