From 6950c81e6e5a50e65f6241eb7a78459da0b3f40d Mon Sep 17 00:00:00 2001 From: Daniel Compton Date: Fri, 3 Nov 2017 13:31:46 +1300 Subject: [PATCH] Move files around --- src/day8/re_frame/trace.cljs | 179 +----------------- .../trace/{ => components}/components.cljs | 2 +- .../re_frame/trace/{ => components}/d3.cljs | 2 +- src/day8/re_frame/trace/log.cljs | 1 - .../re_frame/trace/{ => panels}/app_db.cljs | 2 +- .../re_frame/trace/{ => panels}/subvis.cljs | 6 +- src/day8/re_frame/trace/panels/traces.cljs | 173 +++++++++++++++++ src/day8/re_frame/trace/styles.cljs | 2 +- .../re_frame/trace/{ => utils}/graph.cljc | 2 +- .../trace/{ => utils}/localstorage.cljs | 2 +- src/day8/re_frame/trace/utils/log.cljs | 1 + .../re_frame/trace/{ => utils}/macros.clj | 2 +- 12 files changed, 192 insertions(+), 182 deletions(-) rename src/day8/re_frame/trace/{ => components}/components.cljs (98%) rename src/day8/re_frame/trace/{ => components}/d3.cljs (97%) delete mode 100644 src/day8/re_frame/trace/log.cljs rename src/day8/re_frame/trace/{ => panels}/app_db.cljs (99%) rename src/day8/re_frame/trace/{ => panels}/subvis.cljs (98%) create mode 100644 src/day8/re_frame/trace/panels/traces.cljs rename src/day8/re_frame/trace/{ => utils}/graph.cljc (98%) rename src/day8/re_frame/trace/{ => utils}/localstorage.cljs (94%) create mode 100644 src/day8/re_frame/trace/utils/log.cljs rename src/day8/re_frame/trace/{ => utils}/macros.clj (94%) diff --git a/src/day8/re_frame/trace.cljs b/src/day8/re_frame/trace.cljs index 14bbea6..4b7d7ac 100644 --- a/src/day8/re_frame/trace.cljs +++ b/src/day8/re_frame/trace.cljs @@ -1,9 +1,10 @@ (ns day8.re-frame.trace - (:require [day8.re-frame.trace.subvis :as subvis] - [day8.re-frame.trace.app-db :as app-state] + (:require [day8.re-frame.trace.panels.subvis :as subvis] + [day8.re-frame.trace.panels.app-db :as app-db] [day8.re-frame.trace.styles :as styles] - [day8.re-frame.trace.components :as components] - [day8.re-frame.trace.localstorage :as localstorage] + [day8.re-frame.trace.components.components :as components] + [day8.re-frame.trace.utils.localstorage :as localstorage] + [day8.re-frame.trace.panels.traces :as traces] [re-frame.trace :as trace :include-macros true] [re-frame.db :as db] [cljs.pprint :as pprint] @@ -148,170 +149,6 @@ [] (monkey-patch-reagent)) -(defn query->fn [query] - (if (= :contains (:filter-type query)) - (fn [trace] - (str/includes? (str/lower-case (str (:operation trace) " " (:op-type trace))) - (:query query))) - (fn [trace] - (< (:query query) (:duration trace))))) - -(defn add-filter [filter-items filter-input filter-type] - ;; prevent duplicate filter strings - (when-not (some #(= filter-input (:query %)) @filter-items) - ;; if existing, remove prior filter for :slower-than - (when (and (= :slower-than filter-type) - (some #(= filter-type (:filter-type %)) @filter-items)) - (swap! filter-items (fn [item] - (remove #(= :slower-than (:filter-type %)) item)))) - ;; add new filter - (swap! filter-items conj {:id (random-uuid) - :query (if (= filter-type :contains) - (str/lower-case filter-input) - (js/parseFloat filter-input)) - :filter-type filter-type}))) - -(defn render-traces [visible-traces filter-items filter-input trace-detail-expansions] - (doall - (->> - visible-traces - (map-indexed (fn [index {:keys [op-type id operation tags duration] :as trace}] - (let [show-row? (get-in @trace-detail-expansions [:overrides id] - (:show-all? @trace-detail-expansions)) - op-name (if (vector? operation) - (second operation) - operation) - #_#__ (js/console.log (devtools/header-api-call tags))] - (list [:tr {:key id - :on-click (fn [ev] - (swap! trace-detail-expansions update-in [:overrides id] - #(if show-row? false (not %)))) - :class (str/join " " ["trace--trace" - (case op-type - :sub/create "trace--sub-create" - :sub/run "trace--sub-run" - :event "trace--event" - :render "trace--render" - :re-frame.router/fsm-trigger "trace--fsm-trigger" - nil)])} - - [:td.trace--toggle - [:button.expansion-button (if show-row? "▼" "▶")]] - [:td.trace--op - [:span.op-string {:on-click (fn [ev] - (add-filter filter-items (name op-type) :contains) - (.stopPropagation ev))} - (str op-type)]] - [:td.trace--op-string - [:span.op-string {:on-click (fn [ev] - (add-filter filter-items (name op-name) :contains) - (.stopPropagation ev))} - (str op-name)]] - [:td.trace--meta - (.toFixed duration 1) " ms"]] - (when show-row? - [:tr.trace--details {:key (str id "-details") - :tab-index 0} - [:td] - [:td.trace--details-tags {:col-span 2 - :on-click #(.log js/console tags)} - [:div.trace--details-tags-text - (let [tag-str (with-out-str (pprint/pprint tags)) - string-size-limit 400] - (if (< string-size-limit (count tag-str)) - (str (subs tag-str 0 string-size-limit) " ...") - tag-str))]] - [:td.trace--meta.trace--details-icon - {:on-click #(.log js/console tags)}]])))))))) - -(defn render-trace-panel [] - (let [filter-input (r/atom "") - filter-items (r/atom (localstorage/get "filter-items" [])) - filter-type (r/atom :contains) - input-error (r/atom false) - categories (r/atom #{:event :sub/run :sub/create}) - trace-detail-expansions (r/atom {:show-all? false :overrides {}})] - (add-watch filter-items - :update-localstorage - (fn [_ _ _ new-state] - (localstorage/save! "filter-items" new-state))) - (fn [] - (let [toggle-category-fn (fn [category-keys] - (swap! categories #(if (set/superset? % category-keys) - (set/difference % category-keys) - (set/union % category-keys)))) - - visible-traces (cond->> @traces - (seq @categories) (filter (fn [trace] (when (contains? @categories (:op-type trace)) trace))) - (seq @filter-items) (filter (apply every-pred (map query->fn @filter-items)))) - save-query (fn [_] - (if (and (= @filter-type :slower-than) - (js/isNaN (js/parseFloat @filter-input))) - (reset! input-error true) - (do - (reset! input-error false) - (add-filter filter-items @filter-input @filter-type))))] - [:div.tab-contents - [:div.filter - [:div.filter-control - [:ul.filter-categories "show: " - [:li.filter-category {:class (when (contains? @categories :event) "active") - :on-click #(toggle-category-fn #{:event})} - "events"] - [:li.filter-category {:class (when (contains? @categories :sub/run) "active") - :on-click #(toggle-category-fn #{:sub/run :sub/create})} - "subscriptions"] - [:li.filter-category {:class (when (contains? @categories :render) "active") - :on-click #(toggle-category-fn #{:render})} - "reagent"] - [:li.filter-category {:class (when (contains? @categories :re-frame.router/fsm-trigger) "active") - :on-click #(toggle-category-fn #{:re-frame.router/fsm-trigger :componentWillUnmount})} - "internals"]] - [:div.filter-fields - [:select {:value @filter-type - :on-change #(reset! filter-type (keyword (.. % -target -value)))} - [:option {:value "contains"} "contains"] - [:option {:value "slower-than"} "slower than"]] - [:div.filter-control-input {:style {:margin-left 10}} - [components/search-input {:on-save save-query - :on-change #(reset! filter-input (.. % -target -value))}] - (if @input-error - [:div.input-error {:style {:color "red" :margin-top 5}} - "Please enter a valid number."])]]] - [:ul.filter-items - (map (fn [item] - ^{:key (:id item)} - [:li.filter-item - [:button.button - {:style {:margin 0} - :on-click (fn [event] (swap! filter-items #(remove (comp (partial = (:query item)) :query) %)))} - (:filter-type item) ": " [:span.filter-item-string (:query item)]]]) - @filter-items)]] - [components/autoscroll-list {:class "panel-content-scrollable" :scroll? true} - [:table - [:thead>tr - [:th {:style {:padding 0}} - [:button.text-button - {:style {:cursor "pointer"} - :on-click (fn [ev] - ;; Always reset expansions - (swap! trace-detail-expansions assoc :overrides {}) - ;; Then toggle :show-all? - (swap! trace-detail-expansions update :show-all? not))} - (if (:show-all? @trace-detail-expansions) "-" "+")]] - [:th "operations"] - [:th - [:button {:class (str/join " " ["filter-items-count" - (when (pos? (count @filter-items)) "active")]) - :on-click #(reset! filter-items [])} - (when (pos? (count @filter-items)) - (str (count visible-traces) " of ")) - (str (count @traces))] - " traces " - (when (pos? (count @traces)) - [:span "(" [:button.text-button {:on-click #(do (trace/reset-tracing!) (reset! traces []))} "clear"] ")"])] - [:th {:style {:text-align "right"}} "meta"]] - [:tbody (render-traces visible-traces filter-items filter-input trace-detail-expansions)]]]])))) (defn resizer-style [draggable-area] {:position "absolute" :z-index 2 :opacity 0 @@ -409,11 +246,11 @@ #_[:button {:class (str "tab button " (when (= @selected-tab :subvis) "active")) :on-click #(reset! selected-tab :subvis)} "SubVis"]]] (case @selected-tab - :traces [render-trace-panel] - :app-db [app-state/render-state db/app-db] + :traces [traces/render-trace-panel traces] + :app-db [app-db/render-state db/app-db] :subvis [subvis/render-subvis traces [:div.panel-content-scrollable]] - [app-state/render-state db/app-db])]]]))}))) + [app-db/render-state db/app-db])]]]))}))) (defn panel-div [] (let [id "--re-frame-trace--" diff --git a/src/day8/re_frame/trace/components.cljs b/src/day8/re_frame/trace/components/components.cljs similarity index 98% rename from src/day8/re_frame/trace/components.cljs rename to src/day8/re_frame/trace/components/components.cljs index e7875d8..2e8cb0b 100644 --- a/src/day8/re_frame/trace/components.cljs +++ b/src/day8/re_frame/trace/components/components.cljs @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.components +(ns day8.re-frame.trace.components.components (:require [reagent.core :as r] [clojure.string :as str] [goog.fx.dom :as fx])) diff --git a/src/day8/re_frame/trace/d3.cljs b/src/day8/re_frame/trace/components/d3.cljs similarity index 97% rename from src/day8/re_frame/trace/d3.cljs rename to src/day8/re_frame/trace/components/d3.cljs index 95b7061..65a6ed8 100644 --- a/src/day8/re_frame/trace/d3.cljs +++ b/src/day8/re_frame/trace/components/d3.cljs @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.d3 +(ns day8.re-frame.trace.components.d3 (:require [reagent.core :as r])) (defn no-op [desc] diff --git a/src/day8/re_frame/trace/log.cljs b/src/day8/re_frame/trace/log.cljs deleted file mode 100644 index 156a874..0000000 --- a/src/day8/re_frame/trace/log.cljs +++ /dev/null @@ -1 +0,0 @@ -(ns day8.re-frame.trace.log) diff --git a/src/day8/re_frame/trace/app_db.cljs b/src/day8/re_frame/trace/panels/app_db.cljs similarity index 99% rename from src/day8/re_frame/trace/app_db.cljs rename to src/day8/re_frame/trace/panels/app_db.cljs index 4f2e3b1..25d9b53 100644 --- a/src/day8/re_frame/trace/app_db.cljs +++ b/src/day8/re_frame/trace/panels/app_db.cljs @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.app-db +(ns day8.re-frame.trace.panels.app-db (:require-macros [day8.re-frame.trace.macros :refer [with-cljs-devtools-prefs]]) (:require [reagent.core :as r] [clojure.string :as str] diff --git a/src/day8/re_frame/trace/subvis.cljs b/src/day8/re_frame/trace/panels/subvis.cljs similarity index 98% rename from src/day8/re_frame/trace/subvis.cljs rename to src/day8/re_frame/trace/panels/subvis.cljs index e929834..74eef71 100644 --- a/src/day8/re_frame/trace/subvis.cljs +++ b/src/day8/re_frame/trace/panels/subvis.cljs @@ -1,7 +1,7 @@ -(ns day8.re-frame.trace.subvis +(ns day8.re-frame.trace.panels.subvis (:require cljsjs.d3 - [day8.re-frame.trace.d3 :as d3t] - [day8.re-frame.trace.graph :as graph] + [day8.re-frame.trace.components.d3 :as d3t] + [day8.re-frame.trace.utils.graph :as graph] [reagent.core :as r] [re-frame.interop :as interop] [re-frame.db :as db] diff --git a/src/day8/re_frame/trace/panels/traces.cljs b/src/day8/re_frame/trace/panels/traces.cljs new file mode 100644 index 0000000..8ca4cc8 --- /dev/null +++ b/src/day8/re_frame/trace/panels/traces.cljs @@ -0,0 +1,173 @@ +(ns day8.re-frame.trace.panels.traces + (:require [day8.re-frame.trace.components.components :as components] + [re-frame.trace :as trace] + [clojure.string :as str] + [reagent.core :as r] + [day8.re-frame.trace.utils.localstorage :as localstorage] + [cljs.pprint :as pprint] + [clojure.set :as set])) + +(defn query->fn [query] + (if (= :contains (:filter-type query)) + (fn [trace] + (str/includes? (str/lower-case (str (:operation trace) " " (:op-type trace))) + (:query query))) + (fn [trace] + (< (:query query) (:duration trace))))) + +(defn add-filter [filter-items filter-input filter-type] + ;; prevent duplicate filter strings + (when-not (some #(= filter-input (:query %)) @filter-items) + ;; if existing, remove prior filter for :slower-than + (when (and (= :slower-than filter-type) + (some #(= filter-type (:filter-type %)) @filter-items)) + (swap! filter-items (fn [item] + (remove #(= :slower-than (:filter-type %)) item)))) + ;; add new filter + (swap! filter-items conj {:id (random-uuid) + :query (if (= filter-type :contains) + (str/lower-case filter-input) + (js/parseFloat filter-input)) + :filter-type filter-type}))) + +(defn render-traces [visible-traces filter-items filter-input trace-detail-expansions] + (doall + (->> + visible-traces + (map-indexed (fn [index {:keys [op-type id operation tags duration] :as trace}] + (let [show-row? (get-in @trace-detail-expansions [:overrides id] + (:show-all? @trace-detail-expansions)) + op-name (if (vector? operation) + (second operation) + operation) + #_#__ (js/console.log (devtools/header-api-call tags))] + (list [:tr {:key id + :on-click (fn [ev] + (swap! trace-detail-expansions update-in [:overrides id] + #(if show-row? false (not %)))) + :class (str/join " " ["trace--trace" + (case op-type + :sub/create "trace--sub-create" + :sub/run "trace--sub-run" + :event "trace--event" + :render "trace--render" + :re-frame.router/fsm-trigger "trace--fsm-trigger" + nil)])} + + [:td.trace--toggle + [:button.expansion-button (if show-row? "▼" "▶")]] + [:td.trace--op + [:span.op-string {:on-click (fn [ev] + (add-filter filter-items (name op-type) :contains) + (.stopPropagation ev))} + (str op-type)]] + [:td.trace--op-string + [:span.op-string {:on-click (fn [ev] + (add-filter filter-items (name op-name) :contains) + (.stopPropagation ev))} + (str op-name)]] + [:td.trace--meta + (.toFixed duration 1) " ms"]] + (when show-row? + [:tr.trace--details {:key (str id "-details") + :tab-index 0} + [:td] + [:td.trace--details-tags {:col-span 2 + :on-click #(.log js/console tags)} + [:div.trace--details-tags-text + (let [tag-str (with-out-str (pprint/pprint tags)) + string-size-limit 400] + (if (< string-size-limit (count tag-str)) + (str (subs tag-str 0 string-size-limit) " ...") + tag-str))]] + [:td.trace--meta.trace--details-icon + {:on-click #(.log js/console tags)}]])))))))) + +(defn render-trace-panel [traces] + (let [filter-input (r/atom "") + filter-items (r/atom (localstorage/get "filter-items" [])) + filter-type (r/atom :contains) + input-error (r/atom false) + categories (r/atom #{:event :sub/run :sub/create}) + trace-detail-expansions (r/atom {:show-all? false :overrides {}})] + (add-watch filter-items + :update-localstorage + (fn [_ _ _ new-state] + (localstorage/save! "filter-items" new-state))) + (fn [] + (let [toggle-category-fn (fn [category-keys] + (swap! categories #(if (set/superset? % category-keys) + (set/difference % category-keys) + (set/union % category-keys)))) + + visible-traces (cond->> @traces + (seq @categories) (filter (fn [trace] (when (contains? @categories (:op-type trace)) trace))) + (seq @filter-items) (filter (apply every-pred (map query->fn @filter-items)))) + save-query (fn [_] + (if (and (= @filter-type :slower-than) + (js/isNaN (js/parseFloat @filter-input))) + (reset! input-error true) + (do + (reset! input-error false) + (add-filter filter-items @filter-input @filter-type))))] + [:div.tab-contents + [:div.filter + [:div.filter-control + [:ul.filter-categories "show: " + [:li.filter-category {:class (when (contains? @categories :event) "active") + :on-click #(toggle-category-fn #{:event})} + "events"] + [:li.filter-category {:class (when (contains? @categories :sub/run) "active") + :on-click #(toggle-category-fn #{:sub/run :sub/create})} + "subscriptions"] + [:li.filter-category {:class (when (contains? @categories :render) "active") + :on-click #(toggle-category-fn #{:render})} + "reagent"] + [:li.filter-category {:class (when (contains? @categories :re-frame.router/fsm-trigger) "active") + :on-click #(toggle-category-fn #{:re-frame.router/fsm-trigger :componentWillUnmount})} + "internals"]] + [:div.filter-fields + [:select {:value @filter-type + :on-change #(reset! filter-type (keyword (.. % -target -value)))} + [:option {:value "contains"} "contains"] + [:option {:value "slower-than"} "slower than"]] + [:div.filter-control-input {:style {:margin-left 10}} + [components/search-input {:on-save save-query + :on-change #(reset! filter-input (.. % -target -value))}] + (if @input-error + [:div.input-error {:style {:color "red" :margin-top 5}} + "Please enter a valid number."])]]] + [:ul.filter-items + (map (fn [item] + ^{:key (:id item)} + [:li.filter-item + [:button.button + {:style {:margin 0} + :on-click (fn [event] (swap! filter-items #(remove (comp (partial = (:query item)) :query) %)))} + (:filter-type item) ": " [:span.filter-item-string (:query item)]]]) + @filter-items)]] + [components/autoscroll-list {:class "panel-content-scrollable" :scroll? true} + [:table + [:thead>tr + [:th {:style {:padding 0}} + [:button.text-button + {:style {:cursor "pointer"} + :on-click (fn [ev] + ;; Always reset expansions + (swap! trace-detail-expansions assoc :overrides {}) + ;; Then toggle :show-all? + (swap! trace-detail-expansions update :show-all? not))} + (if (:show-all? @trace-detail-expansions) "-" "+")]] + [:th "operations"] + [:th + [:button {:class (str/join " " ["filter-items-count" + (when (pos? (count @filter-items)) "active")]) + :on-click #(reset! filter-items [])} + (when (pos? (count @filter-items)) + (str (count visible-traces) " of ")) + (str (count @traces))] + " traces " + (when (pos? (count @traces)) + [:span "(" [:button.text-button {:on-click #(do (trace/reset-tracing!) (reset! traces []))} "clear"] ")"])] + [:th {:style {:text-align "right"}} "meta"]] + [:tbody (render-traces visible-traces filter-items filter-input trace-detail-expansions)]]]])))) diff --git a/src/day8/re_frame/trace/styles.cljs b/src/day8/re_frame/trace/styles.cljs index f0f01f6..ed91055 100644 --- a/src/day8/re_frame/trace/styles.cljs +++ b/src/day8/re_frame/trace/styles.cljs @@ -1,4 +1,4 @@ (ns day8.re-frame.trace.styles - (:require-macros [day8.re-frame.trace.macros :as macros])) + (:require-macros [day8.re-frame.trace.utils.macros :as macros])) (def panel-styles (macros/slurp-macro "day8/re_frame/trace/main.css")) diff --git a/src/day8/re_frame/trace/graph.cljc b/src/day8/re_frame/trace/utils/graph.cljc similarity index 98% rename from src/day8/re_frame/trace/graph.cljc rename to src/day8/re_frame/trace/utils/graph.cljc index 8c26021..8c5788b 100644 --- a/src/day8/re_frame/trace/graph.cljc +++ b/src/day8/re_frame/trace/utils/graph.cljc @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.graph +(ns day8.re-frame.trace.utils.graph (:require [clojure.set :as set])) (defn select-type [type traces] diff --git a/src/day8/re_frame/trace/localstorage.cljs b/src/day8/re_frame/trace/utils/localstorage.cljs similarity index 94% rename from src/day8/re_frame/trace/localstorage.cljs rename to src/day8/re_frame/trace/utils/localstorage.cljs index a600b33..7de4ef4 100644 --- a/src/day8/re_frame/trace/localstorage.cljs +++ b/src/day8/re_frame/trace/utils/localstorage.cljs @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.localstorage +(ns day8.re-frame.trace.utils.localstorage (:require [goog.storage.Storage :as Storage] [goog.storage.mechanism.HTML5LocalStorage :as html5localstore] [cljs.reader :as reader]) diff --git a/src/day8/re_frame/trace/utils/log.cljs b/src/day8/re_frame/trace/utils/log.cljs new file mode 100644 index 0000000..3b6ff5c --- /dev/null +++ b/src/day8/re_frame/trace/utils/log.cljs @@ -0,0 +1 @@ +(ns day8.re-frame.trace.utils.log) diff --git a/src/day8/re_frame/trace/macros.clj b/src/day8/re_frame/trace/utils/macros.clj similarity index 94% rename from src/day8/re_frame/trace/macros.clj rename to src/day8/re_frame/trace/utils/macros.clj index c4a980a..03ee813 100644 --- a/src/day8/re_frame/trace/macros.clj +++ b/src/day8/re_frame/trace/utils/macros.clj @@ -1,4 +1,4 @@ -(ns day8.re-frame.trace.macros +(ns day8.re-frame.trace.utils.macros (:require [clojure.java.io :as io])) (defmacro slurp-macro