有状态传感器故障排除

时间:2018-05-28 23:26:20

标签: clojure core.async transducer

我正在尝试制作有状态的传感器,join-averagesgist here)。

运行let块告诉我,我正确地加入了值。但结果输出仍然没有连接值。

...
c' {:tick-list {:uuid 1, :last-trade-price 11.1}, :ema-list {:uuid 1, :last-trade-price-exponential 10}, :sma-list {:uuid 1, :last-trade-price-average 10.1}}
record {:uuid 1, :last-trade-price 11.1}
c' {:tick-list {:uuid 2, :last-trade-price 11.2}, :ema-list {:uuid 2, :last-trade-price-exponential 11}, :sma-list {:uuid 2, :last-trade-price-average 10.2}}
record {:uuid 2, :last-trade-price 11.2}
...
c' {:tick-list {:uuid 3, :last-trade-price 11.3}, :ema-list {:uuid 3, :last-trade-price-exponential 12}, :sma-list {:uuid 3, :last-trade-price-average 10.3}}
record {:uuid 1, :last-trade-price-exponential 10}
record {:uuid 2, :last-trade-price-exponential 11}
record {:uuid 3, :last-trade-price 11.3}
record {:uuid 3, :last-trade-price-exponential 12}

关于join-averages有状态传感器为什么没有返回正确结果的任何想法?

CODE

(:require [clojure.core.async :refer [chan sliding-buffer <! go-loop pipeline onto-chan] :as async]
          [clojure.set :refer [subset?]]
          [clojure.tools.logging :as log])

(defn has-all-lists? [averages-map]
  (subset? #{:tick-list :sma-list :ema-list} (->> averages-map keys (into #{}))))

(defn join-averages []
  (let [state (atom {})]
    (fn [rf]
      (fn
        ([] (rf))
        ([accumulator] (rf accumulator))
        ([accumulator input]
         (let [uuid (:uuid input)
               entry (cond
                       (:last-trade-price-exponential input) {:ema-list input}
                       (:last-trade-price-average input) {:sma-list input}
                       (:last-trade-price input) {:tick-list input})]

           (if-let [current (get @state uuid)]

             (let [_ (swap! state update-in [uuid] merge entry)
                   c' (get @state uuid)]

               (log/info "c'" c')
               (log/info "accumulator" accumulator)
               (log/info "state" (with-out-str (clojure.pprint/pprint @state)))

               (if (has-all-lists? c')
                 c'
                 (rf accumulator input)))

             (do (swap! state merge {uuid entry})
                 (rf accumulator input)))))))))

(comment

  (let [ema-list [{:uuid "1" :last-trade-price-exponential 10}
                  {:uuid "2" :last-trade-price-exponential 11}
                  {:uuid "3" :last-trade-price-exponential 12}]
        sma-list [{:uuid "1" :last-trade-price-average 10.1}
                  {:uuid "2" :last-trade-price-average 10.2}
                  {:uuid "3" :last-trade-price-average 10.3}]
        tick-list [{:uuid "1" :last-trade-price 11.1}
                   {:uuid "2" :last-trade-price 11.2}
                   {:uuid "3" :last-trade-price 11.3}]

        ec (chan (sliding-buffer 100))
        sc (chan (sliding-buffer 100))
        tc (chan (sliding-buffer 100))

        _ (onto-chan ec ema-list)
        _ (onto-chan sc sma-list)
        _ (onto-chan tc tick-list)

        merged-ch (async/merge [tc sc ec])
        output-ch (chan (sliding-buffer 100) (join-averages))]

    (async/pipeline 1 output-ch (join-averages) merged-ch)
    (go-loop [r (<! output-ch)]
      (when-not (nil? r)
        (log/info "record" r)
        (recur (<! output-ch))))))

1 个答案:

答案 0 :(得分:0)

你没有告诉我们你的结果应该是什么样的,所以我不得不猜测。

另外,要了解如何实现状态转换器,我通常只看distinct。在传感器初始化后,您应该初始化状态。这应该有效:

(defn has-all-lists? [averages-map]
  (set/subset? #{:tick-list :sma-list :ema-list} (->> averages-map keys (into #{}))))

(defn join-averages []
  (fn [rf]
    (let [state (atom {})]
      (fn
        ([] (rf))
        ([accumulator] (rf accumulator))
        ([accumulator input]
         (let [uuid (:uuid input)
               entry (condp #(%1 %2) input
                       :last-trade-price-exponential {:ema-list input}
                       :last-trade-price-average {:sma-list input}
                       :last-trade-price {:tick-list input})]
           (let [nv (swap! state update-in [uuid] merge entry)
                 c' (get nv uuid)]
             (if (has-all-lists? c')
               (rf accumulator c')
               accumulator))))))))

(let [ema-list [{:uuid "1" :last-trade-price-exponential 10}
                {:uuid "2" :last-trade-price-exponential 11}
                {:uuid "3" :last-trade-price-exponential 12}]
      sma-list [{:uuid "1" :last-trade-price-average 10.1}
                {:uuid "2" :last-trade-price-average 10.2}
                {:uuid "3" :last-trade-price-average 10.3}]
      tick-list [{:uuid "1" :last-trade-price 11.1}
                 {:uuid "2" :last-trade-price 11.2}
                 {:uuid "3" :last-trade-price 11.3}]]
  (into []
        (join-averages)
        (concat ema-list sma-list tick-list)))