私は Riemann で Clojure を発見/学習しており、ホストごとに CPU メトリックを集計する次のコードを作成しました。
(streams
(by [:host]
smap (aggregate-cpu-metrics "user" folds/mean)
smap (aggregate-cpu-metrics "nice" folds/mean)
smap (aggregate-cpu-metrics "system" folds/mean)
smap (aggregate-cpu-metrics "idle" folds/mean)
smap (aggregate-cpu-metrics "wait" folds/mean)
smap (aggregate-cpu-metrics "interrupt" folds/mean)
smap (aggregate-cpu-metrics "softirq" folds/mean)
smap (aggregate-cpu-metrics "steal" folds/mean)))
(defn aggregate-cpu-metrics
[name, aggr]
(where (service (re-pattern (str "cpu-[0-9]+ " name)))
(coalesce 10
(smap aggr
(with :service (str "cpu-average " name) reinject)))))
コードを少し説明すると、次のようなイベントを受け取ります。
- :サービス「cpu-0 アイドル」:メトリック 58.23
- :service "cpu-1 idle" :metric 98.11
- :service "cpu-2 idle" :metric 12.23
そして、私の目標は、平均を計算し、このイベントを riemann に再注入することです。
- :service "cpu-average" : メトリック 56.19
それは機能しています、それは問題ではありません。しかし、3 行目から 10 行目を見るとわかるように、ここには多くの重複コードがあります。このコードをリファクタリングする方法を探していますが、行き詰まっています。
メトリック名でベクトルを定義したいと思います:
(def cpu-metrics ["user", "nice", "system", "idle", "interrupt", "softirq", "steal"])
...そしてそれを使用してsmap(aggregate-cpu-metrics ...を呼び出す
しかし、私はそれを行う方法がわかりません。mapまたはdoseqを試しましたが、成功しませんでした。
どのようにしますか?
(更新/解決策) :
アーサーの答えを読んだ後、これが私のリファクタリングされたバージョンです。
(streams
(where
(service #"^cpu-[0-9]+ ")
(adjust
[:service #(clojure.string/replace % #"^cpu-[0-9]+" "cpu-average")]
(by [:host :service]
(fixed-time-window 10 (smap folds/mean reinject))))))