From 83f16421c0e9a9791205f8ecda46a8c98542df2c Mon Sep 17 00:00:00 2001 From: Michael Salihi Date: Tue, 8 Dec 2020 15:54:10 +0100 Subject: [PATCH] Add DigitalOcean util example [skip ci] (#673) --- examples/README.md | 12 ++++++++++++ examples/digitalocean-ping.clj | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/digitalocean-ping.clj diff --git a/examples/README.md b/examples/README.md index 9e247831..bd5c614f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -381,3 +381,15 @@ Example usage: ``` shell $ cat src/babashka/main.clj | bb examples/rofi.clj ``` + +### [digitalocean-ping.clj](digitalocean-ping.clj) + +The script allows to define which DigitalOcean cloud datacenter (region) has best network performance (ping latency). + +See [digitalocean-ping.clj](digitalocean-ping.clj) + +Example usage: + +``` shell +$ bb digitalocean-ping.clj +``` diff --git a/examples/digitalocean-ping.clj b/examples/digitalocean-ping.clj new file mode 100644 index 00000000..206ecd2c --- /dev/null +++ b/examples/digitalocean-ping.clj @@ -0,0 +1,30 @@ +#!/usr/bin/env bb + +(require '[babashka.curl :as curl] + '[clojure.java.shell :as shell] + '[clojure.string :as str]) + +(def url "http://speedtest-ams2.digitalocean.com/") + +(def get-endpoints + (let [{:keys [body]} (curl/get url)] + (re-seq #"speedtest\-.+.digitalocean.com" body))) + +(defn get-average [result] + (-> result + str/split-lines + last + (str/split #"/") + (get 4))) + +(def mac? (str/starts-with? (System/getProperty "os.name") "Mac"));; TODO: test on Windows + +(def timeout-arg (if mac? "-t3" "-w3")) + +(defn ping-result [endpoint] + (let [{:keys [out]} (shell/sh "ping" "-c5" timeout-arg endpoint) + msg (str endpoint " => " (get-average out) "ms")] + (println msg))) + +(doseq [endpoint get-endpoints] + (ping-result endpoint))