Added support for planet-queries that are then locally filtered.

This is just for *me* right now.

If you don't specify lat, lon and radius-km in the config, then we
assume we should do a planet-scale query to adsbx-api-proxy and filter
the returned aircraft to include only the ICAOs specified in the icaos
collection in config.
This commit is contained in:
John Wiseman 2020-10-12 15:08:29 -07:00
parent 02ee1c5050
commit 6a55807542
2 changed files with 64 additions and 9 deletions

View File

@ -110,14 +110,22 @@
(defn adsbx-url [{:keys [url lat lon radius-nm rapid-api?]}]
(let [url-pieces [url
"lat" lat
"lon" lon
"dist" (.toFixed radius-nm (if rapid-api? 0 1))]
url-str (->> url-pieces
(map str)
(string/join "/"))]
url-str))
(if (or lat lon radius-nm)
(do
(when-not (and lat lon radius-nm)
(throw (js/Error. "Need all of :lat, :lon, and :radius-nm")))
(let [url-pieces [url
"lat" lat
"lon" lon
"dist" (.toFixed radius-nm (if rapid-api? 0 1))]
url-str (->> url-pieces
(map str)
(string/join "/"))]
url-str))
(do
(when rapid-api?
(throw (js/Error. "Must specify :lat, :lon, and :radius-nm with RapidAPI")))
(str url "/all"))))
(defn adsbx-headers [{:keys [url api-key api-whitelist rapid-api?]}]
@ -153,6 +161,20 @@
updated))
;; Filters out aircraft that aren't in the keep list--unless the keep
;; list is empty, in which case we don't do any filtering.
(defn keep-specified-icaos [live-data keep-list]
(if (seq keep-list)
(let [keep-list (set (map string/upper-case keep-list))]
(update
live-data
:aircraft
(fn [aircraft]
(filter #(keep-list (string/upper-case (:icao %)))
aircraft))))
live-data))
;; Given a vector of position history, removes old entries.
(defn prune-history [history now config]
@ -872,6 +894,7 @@
:radius-nm (* (:radius-km config) 0.539957)
:rapid-api? (:rapid-api? config)}))
filtered-api-data (remove-blocked-icaos api-data (get config :icao-blocklist '()))
filtered-api-data (keep-specified-icaos filtered-api-data (get config :icaos '()))
now (current-time)
[new-db potential-circles] (-> db
(update-history-db (:aircraft filtered-api-data) now config)

View File

@ -473,7 +473,21 @@
:lon 1
:radius-nm 2
:rapid-api? true})
"http://adsbx/lat/0/lon/1/dist/2")))
"http://adsbx/lat/0/lon/1/dist/2"))
(is (thrown-with-msg?
js/Error #"Need all of :lat, :lon, and :radius-nm"
(advisorycircular/adsbx-url
{:url "http://adsbx"
:lat 0
:rapid-api? true})))
(is (= (advisorycircular/adsbx-url
{:url "http://adsbx"})
"http://adsbx/all"))
(is (thrown-with-msg?
js/Error #"Must specify.*lat.*RapidAPI"
(advisorycircular/adsbx-url
{:url "http://adsbx"
:rapid-api? true}))))
(deftest adsbx-headers
@ -494,3 +508,21 @@
{:x-rapidapi-key "key"
:x-rapidapi-host "adsbx"
:useQueryString true})))
(deftest remove-blocked-icaos
(let [aircraft {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}]
(is (= (advisorycircular/remove-blocked-icaos aircraft '())
{:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}))
(is (= (advisorycircular/remove-blocked-icaos aircraft '("a.*"))
{:aircraft [{:icao "123"} {:icao "124"}]}))
(is (= (advisorycircular/remove-blocked-icaos aircraft '("a.*" "123"))
{:aircraft [{:icao "124"}]}))))
(deftest keep-specified-icaos
(let [aircraft {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}]
(is (= (advisorycircular/keep-specified-icaos aircraft '())
{:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}))
(is (= (advisorycircular/keep-specified-icaos aircraft '("123" "adef"))
{:aircraft [{:icao "ADEF"} {:icao "123"}]}))))