 
 (defn end-edit [ text state ]
   (when (parse-query-window text)
+    (put! time-event-channel {:msg-type :query-window :window-spec text})
     (reset! query-window text)))
 
 (defn input-field [ initial-text text-valid? on-enter ]

      (fn [ this old-argv ]
        (let [canvas (.-firstChild @dom-node)
              ctx (.getContext canvas "2d")
              range (range-ending-at (time/now))]
          (tsplot/draw ctx (.-clientWidth canvas) (.-clientHeight canvas)
                       (:series-data @series-state)
                       (:begin-t range)

             false)))))

(defonce query-window (reagent/atom "1d"))

(defn periodic-event-channel [ interval-ms ]
  (let [channel (chan)
        put-now #(put! channel {:msg-type :timestamp :t (time/now)})]
    (put-now)
    (js/setInterval put-now interval-ms)
    channel))

(defonce time-event-channel (periodic-event-channel 15000))

(defn range-ending-at [ end-t window-seconds ]
  (let [begin-t (time/minus end-t (time/seconds window-seconds))]
    {:msg-type :range
     :begin-t (time-coerce/to-long begin-t)
     :end-t (time-coerce/to-long end-t)}))

(defn query-range-channel-2 [ time-event-channel ]
  (let [ output (chan)
        output-pub (pub output :msg-type)]
    (go-loop [ time (time/now) qws @query-window ]
      (>! output (range-ending-at time (parse-query-window qws)))
      (when-let [ msg (<! time-event-channel)]
        (case (:msg-type msg)
          :timestamp (recur (:t msg) qws)
          :query-window (recur time (:window-spec msg)))))
    output-pub))

(defonce update-pub (query-range-channel-2 time-event-channel))

(defonce dashboard-state (reagent/atom {:series [] :text ""}))

