More unitization.

This commit is contained in:
John Wiseman 2020-05-04 14:13:48 -07:00
parent 2e0ad025a9
commit b9c2e02ecb
4 changed files with 105 additions and 65 deletions

View File

@ -5,9 +5,9 @@ dir=$(dirname "$0")
instance="$1" instance="$1"
config_dir=/etc/advisorycircular config_dir=/etc/advisorycircular
run_dir=/var/run/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 config_path="$config_dir"/"$instance"-config.yaml
secrets_path="$config_dir"/"$instance"-secrets.yaml secrets_path="$config_dir"/"$instance"-secrets.yaml
history_path="$run_dir"/"$instance"-history.edn 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")

View File

@ -19,6 +19,9 @@
(logging/deflog "advisorycircular" logger) (logging/deflog "advisorycircular" logger)
(def log-prefix (atom ""))
(defn get-basestation-sqb-record [icao db-path] (defn get-basestation-sqb-record [icao db-path]
(log-info "%s: Looking up in %s" icao db-path) (log-info "%s: Looking up in %s" icao db-path)
(p/let [record (p/let [record
@ -168,18 +171,19 @@
(defn ac-desc [ac] (defn ac-desc [ac]
(str (:icao ac) " " "http://tar1090.adsbexchange.com/?icao=" (:icao ac) (str (:icao ac) " " "http://tar1090.adsbexchange.com/?icao=" (:icao ac)
" " (:lat ac) " " (:lon ac) " " (:lat ac) " " (:lon ac)
" #" (:registration ac) " " (:alt ac) " " (:curviness ac) " " " " (:registration ac) " " (:alt ac) " " (:curviness ac) " "
(:normalized-curviness ac))) (:normalized-curviness ac)))
(defn screenshot [icao lat lon] (defn screenshot [icao lat lon now]
(p/let [image-path (p/let [image-path (str icao "-" (util/format-utc-ts now) ".png")
(adsbx/screenshot-aircraft icao lat lon _ (adsbx/screenshot-aircraft icao lat lon
{:timeout 30000 {:timeout 30000
;;:headless? false :output-path image-path
;; :viewport {:width 1600 :height 800} ;;:headless? false
;; :clip {:width 1600 :height 800 :x 0 :y 0} ;; :viewport {:width 1600 :height 800}
})] ;; :clip {:width 1600 :height 800 :x 0 :y 0}
})]
(log-verbose "%s: Got screenshot" icao) (log-verbose "%s: Got screenshot" icao)
image-path)) image-path))
@ -191,8 +195,8 @@
(defn circling2? [ac config] (defn circling2? [ac config]
(let [relevant-history (filter #(and (:alt %) (> (:alt %) 300)) (:history ac))] (let [relevant-history (filter #(and (:alt %) (> (:alt %) 300)) (:history ac))]
(and (> (geo/flight-curviness relevant-history) (:curviness-threshold-degrees config)) (and (> (geo/flight-curviness relevant-history) (:curviness-threshold-degrees config))
(> (:alt ac) 300)))) (> (:alt ac) 300))))
;; Returns a vector of two elements, ;; Returns a vector of two elements,
@ -436,7 +440,7 @@
(defn track->geojson [older-positions recent-positions icao centroid] (defn track->geojson [older-positions recent-positions icao centroid]
{:type "FeatureCollection" {:type "FeatureCollection"
:features :features
[(geojson-linestring older-positions [(geojson-linestring older-positions
{:stroke "#c0070b" {:stroke "#c0070b"
:stroke-width 2 :stroke-width 2
@ -477,48 +481,48 @@
(:distance airport-properties) (:distance airport-properties)
(:minimum-airport-distance-km config) (:minimum-airport-distance-km config)
(:label airport-properties) (:label airport-properties)
())) ())
(let [alts (map :alt recent-positions) ;; (let [alts (map :alt recent-positions)
min-alt (apply min alts) ;; min-alt (apply min alts)
max-alt (apply max alts) ;; max-alt (apply max alts)
ratio (/ max-alt min-alt)] ;; 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)) ;; (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 (do
(p/let [coarse (pelias/reverse (:pelias config) lat lon {:layers "coarse"})] (p/let [coarse (pelias/reverse (:pelias config) lat lon {:layers "coarse"})]
(let [coarse (first (:features coarse))] (let [coarse (first (:features coarse))]
(log-info "%s: Reverse geocode: %s" icao (:properties coarse)) (log-info "%s: Reverse geocode: %s" icao (:properties coarse))
;; Note that if we're over the ocean we get null :( ;; Note that if we're over the ocean we get null :(
(p/then (p/all [(screenshot (:icao ac) lat lon) (p/then (p/all [(screenshot (:icao ac) lat lon now)
(p/let [nearby (landmark config lat lon) (p/let [nearby (landmark config lat lon)
_ (log-info "WOOO %s" nearby) _ (log-info "WOOO %s" nearby)
sqb (if-let [sqb-path (:basestation-sqb config)] sqb (if-let [sqb-path (:basestation-sqb config)]
(get-basestation-sqb-record icao sqb-path))] (get-basestation-sqb-record icao sqb-path))]
(let [description (generate-description ac sqb coarse nearby)] (let [description (generate-description ac sqb coarse nearby)]
(log-info "Description: %s" description) (log-info "Description: %s" description)
description))]) description))])
(fn [[image-path description]] (fn [[image-path description]]
(if (or (nil? coarse) (if (or (nil? coarse)
;; TODO: Filter using the layer hierarchy; we want ;; TODO: Filter using the layer hierarchy; we want
;; anything smaller than "region" (state). ;; anything smaller than "region" (state).
(= (get-in coarse [:properties :name]) "California")) (= (get-in coarse [:properties :name]) "California"))
(log-info "%s: Filtering out because it is outside Los Angeles County" (:icao ac)) (log-info "%s: Filtering out because it is outside Los Angeles County" (:icao ac))
(if (and image-path description) (if (and image-path description)
(do (do
(if (get-in config [:twitter :enabled?]) (if (get-in config [:twitter :enabled?])
(twitter/tweet (twitter/twit (:twitter config)) (twitter/tweet (twitter/twit (:twitter config))
description description
[image-path] [image-path]
lat lat
lon) lon)
(log-warn "Skipping tweeting")) (log-warn "Skipping tweeting"))
(let [path (str (.toFixed (/ now 1000) 0) "-" icao "-recent" ".geojson")] (let [path (str icao "-" (util/format-utc-ts now) ".geojson")]
(util/write-file (util/write-file
path path
(.stringify (.stringify
js/JSON js/JSON
(clj->js (track->geojson older-positions recent-positions icao centroid))) (clj->js (track->geojson older-positions recent-positions icao centroid)))
{}))) {})))
(log-warn "Skipping tweet %s %s" image-path description)))))))))) (log-warn "Skipping tweet %s %s" image-path description)))))))))))
(defn process-potential-circles [acs config now] (defn process-potential-circles [acs config now]
@ -536,7 +540,7 @@
;; This is how many degrees of turning we need to see over ;; This is how many degrees of turning we need to see over
;; max-history-age-ms ms to consider it a potential circling ;; max-history-age-ms ms to consider it a potential circling
;; aircraft. ;; aircraft.
:curviness-threshold-degrees (* 2 360) :curviness-threshold-degrees (* 4 360)
;; If the centroid of the aircraft's positions is less than this ;; If the centroid of the aircraft's positions is less than this
;; close to an airport, then it's probably just doing flight ;; close to an airport, then it's probably just doing flight
;; training. ;; training.
@ -610,7 +614,12 @@
(.option "--config <path>" "Path to the configuration yaml file") (.option "--config <path>" "Path to the configuration yaml file")
(.option "--secrets <path>" "Path to the secrets yaml file" "secrets.yaml") (.option "--secrets <path>" "Path to the secrets yaml file" "secrets.yaml")
(.option "--history <path>" "Path to history/state file" "advisorycircular.edn") (.option "--history <path>" "Path to history/state file" "advisorycircular.edn")
(.option "--log-prefix <prefix>" "Log prefix to use")
(.parse (.-argv js/process))) (.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)] (let [start-time (current-time)]
;; If --config-path is specified, definitely try to read that ;; If --config-path is specified, definitely try to read that
;; file. Otherwise, only read config.yaml if it exists. ;; file. Otherwise, only read config.yaml if it exists.

View File

@ -6,15 +6,34 @@
(:require-macros (:require-macros
[lemondronor.advisorycircular.logging])) [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) (let [createLogger (.-createLogger winston)
format (.-format winston) format (.-format winston)
transports (.-transports winston) transports (.-transports winston)]
printf-fmt #(gstring/format "%s%-7s %-9s/%-18s| %s"
(.-timestamp %)
(.-ms %)
(.-service %)
(.-level %)
(.-message %))]
(def logger (createLogger (def logger (createLogger
#js {:level "info" #js {:level "info"
:format (.combine :format (.combine
@ -30,7 +49,7 @@
:defaultMeta #js {}})) :defaultMeta #js {}}))
(.add logger (new (.-Console transports) (.add logger (new (.-Console transports)
#js {:format (.combine format #js {:format (.combine format
(.printf format printf-fmt))}))) (.printf format format-msg))})))
(defn get-logger [service] (defn get-logger [service]

View File

@ -85,3 +85,15 @@
[[k]]))) [[k]])))
m)) 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))))