                     "log-body")}
     (str " " (:line item))]])
 
+(rum/defc form-history < rum/reactive [history id taskId]
+  (let [anchorEl (state/react (conj state/form-state-cursor :historyAnchorEl))]
+    (html
+      [:div
+       [:div.Swarmpit-log-history-fab
+        (comp/button
+          {:variant "extendedFab"
+           :onClick #(state/update-value [:historyAnchorEl] (.-currentTarget %) state/form-state-cursor)}
+          (icon/access-time {:className "Swarmpit-log-history-fab-ico"})
+          (get history-options history))]
+       (comp/menu
+         {:id              "list-filter-menu"
+          :anchorEl        anchorEl
+          :anchorOrigin    {:vertical   "top"
+                            :horizontal "right"}
+          :transformOrigin {:vertical   "top"
+                            :horizontal "right"}
+          :open            (some? anchorEl)
+          :onClose         #(state/update-value [:historyAnchorEl] nil state/form-state-cursor)}
+         (comp/menu-item
+           {:className "Swarmpit-menu-info"
+            :disabled  true}
+           (html [:span (str "Period of last")]))
+         (map
+           (fn [[k v]]
+             (comp/menu-item
+               {:key      (str "item-" v)
+                :selected (= k history)
+                :onClick  (fn [e]
+                            (state/update-value [:initialized] false state/form-state-cursor)
+                            (log-handler id taskId k)
+                            (state/update-value [:history] k state/form-state-cursor)
+                            (state/update-value [:historyAnchorEl] nil state/form-state-cursor))}
+               v)) history-options))])))
+
+(rum/defc form-scroll < rum/static [autoscroll]
+  (html
+    [:div.Swarmpit-log-fab
+     (comp/button
+       {:variant "fab"
+        :color   (if autoscroll "primary")
+        :mini    true
+        :onClick (fn []
+                   (reset! last-scroll 0)
+                   (state/update-value [:autoscroll] (not autoscroll) state/form-state-cursor))}
+       icon/scroll-down)]))
+
+(rum/defc form-logs < rum/static [{:keys [initialized error service]} logs filtered-logs]
+  (html
+    [:div#autoscroll.Swarmpit-log
+     (cond
+       error [:span "Logs for this service couldn't be fetched."]
+       (and (empty? logs) initialized) [:span "No logs for given period."]
+       (not initialized) [:span "Loading..."]
+       :else (->> filtered-logs
+                  (take-last 500)
+                  (map #(rum/with-key (line service % (:key %)) (:key %)))))]))
+
 (rum/defc form < rum/reactive
                  mixin-init-form
                  mixin-refresh-form
                  {:did-mount  (fn [state] (auto-scroll!) state)
-                  :did-update (fn [state] (auto-scroll!) state)} [{{:keys [id]} :params}]
-  (let [{:keys [filter autoscroll initialized error service]} (state/react state/form-state-cursor)
+                  :did-update (fn [state] (auto-scroll!) state)} [{{:keys [id taskId]} :params}]
+  (let [{:keys [filter autoscroll history] :as log-state} (state/react state/form-state-cursor)
         logs (state/react state/form-value-cursor)
         filtered-logs (filter-items logs (:predicate filter))]
     (comp/mui
       (html
         [:div
-         [:div.Swarmpit-log-fab
-          (comp/button
-            {:variant "fab"
-             :color   (if autoscroll "primary")
-             :mini    true
-             :onClick (fn []
-                        (reset! last-scroll 0)
-                        (state/update-value [:autoscroll] (not autoscroll) state/form-state-cursor))}
-            icon/scroll-down)]
-         [:div#autoscroll.Swarmpit-log
-          (cond
-            error [:span "Logs for this service couldn't be fetched."]
-            (and (empty? logs) initialized) [:span "Log is empty in this service."]
-            (not initialized) [:span "Loading..."]
-            :else (->> filtered-logs
-                       (take-last 500)
-                       (map #(rum/with-key (line service % (:key %)) (:key %)))))]]))))
+         (form-history history id taskId)
+         (form-scroll autoscroll)
+         (form-logs log-state logs filtered-logs)]))))
 
 (rum/defc form-task < form [params]
   (form params))

       (map #(assoc-in % [:key] (swap! line-num inc)))))

(defn- log-handler
  [service-id task-id]
  (ajax/get
    (routes/path-for-backend :service-logs {:id service-id})
    {:state      [:fetching]
     :on-success (fn [{:keys [response]}]
                   (let [logs (filter-logs task-id response)]
                     (reset! last-glow (count logs))
                     (state/update-value [:initialized] true state/form-state-cursor)
                     (state/set-value logs state/form-value-cursor)))
     :on-error   #(state/update-value [:error] true state/form-state-cursor)}))

(defn- log-append-handler
  [service-id task-id from-timestamp]
  (ajax/get
    (routes/path-for-backend :service-logs {:id service-id})
    {:state      [:fetching]
     :params     (if from-timestamp {:from from-timestamp})
     :on-success (fn [{:keys [response]}]
                   (state/set-value (-> (state/get-value state/form-value-cursor)
                                        (concat (filter-logs task-id response))) state/form-value-cursor))}))

(defn- init-form-state
  []

  (reset! last-scroll 0)
  (reset! line-num 0)
  (state/set-value {:filter      {:predicate ""}
                    :history     "15m"
                    :initialized false
                    :fetching    false
                    :autoscroll  false
