Skip to content

Commit

Permalink
feat: users filters and load more button to requests list
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmr0x committed Mar 4, 2025
1 parent 88d8ca0 commit 2ece3e5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 50 deletions.
17 changes: 13 additions & 4 deletions client/src/webclient/events/requests.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@
:filters (-> db :requests :filters)
:data (:data requests)})))

(rf/reg-event-fx
:requests->load-more
(fn [{:keys [db]} [_ proxy-id]]
(let [filters (-> db :requests :filters)
limit (get filters "limit")]
{:fx [[:dispatch [:requests->filter
{"limit" (if limit (+ (int limit) 100) 200)}
proxy-id]]]})))

(rf/reg-event-fx
:requests->filter
(fn [_ [_ filters proxy-id]]
(let [url (js/URL. js/window.location.href)
search-params (.-searchParams url)]
(.forEach search-params
(fn [_ k] (.delete search-params k)))
(doseq [[k v] filters]
search-params (.-searchParams url)
search-params-clj (into {} (js->clj (for [q search-params] q)))
merged-params (merge search-params-clj filters)]
(doseq [[k v] merged-params]
(.set search-params (name k) v))
(.pushState js/history nil "" url))
{:fx [[:dispatch [:requests->get-by-proxy-id proxy-id]]]}))
Expand Down
47 changes: 23 additions & 24 deletions client/src/webclient/proxies/requests/panel.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
[clojure.string :as string]
["dayjs" :as dayjs]
["@mantine/core" :refer [Text Group Stack Divider Box Badge
Anchor MultiSelect TagsInput]]
Anchor MultiSelect TagsInput Button]]
[webclient.components.h :as h]
[webclient.routes :as routes]
[bidi.bidi :as bidi]))
[webclient.routes :as routes]))

(defn- request-list-item [request proxy-id]
[:> Anchor {:href (routes/url-for :request-details
Expand All @@ -32,21 +31,39 @@
[:> Divider {:variant :dashed}]])

(defn- requests-list [requests proxy-id]
[:section
[:> Stack {:gap :xl}
[:> Stack {:gap :xs}
(for [request requests]
^{:key (:id request)}
[request-list-item request proxy-id])]])
[request-list-item request proxy-id])]
[:> Group {:justify :center}
[:> Button
{:variant :default
:onClick #(rf/dispatch [:requests->load-more proxy-id])}
"Load more"]]])

(defn- request-list-filters [proxy-id]
(let [requests (rf/subscribe [:requests])]
(let [requests (rf/subscribe [:requests])
customers (rf/subscribe [:customers])]
(rf/dispatch [:customers->get])
(fn []
(let [filters (-> @requests :filters)
methods-value (when-not (string/blank? (get filters "methods"))
(string/split (get filters "methods") #","))
customers-value (when-not (string/blank? (get filters "customers_subs"))
(string/split (get filters "customers_subs") #","))
status-codes-value (when-not (string/blank? (get filters "status_codes"))
(string/split (get filters "status_codes") #","))]
[:> Group
[:> MultiSelect {:label "Customers"
:onChange #(rf/dispatch [:requests->filter {"customers_subs" %} proxy-id])
:clearable true
:searchable true
:value (or customers-value [])
:nothingFoundMessage "Nothing found..."
:checkIconPosition :right
:data (or (map :sub (-> @customers :data :list)) [])
:placeholder "Select methods"}]
[:> MultiSelect {:label "Methods"
:onChange #(rf/dispatch [:requests->filter {"methods" %} proxy-id])
:clearable true
Expand All @@ -64,20 +81,6 @@
:data []
:placeholder "Select methods"}]]))))

(defmulti timeframe-dates identity)
(defmethod timeframe-dates "24 hours" [_]
(let [today (.toISOString (.utc dayjs))
last-24-hours (.toISOString (.utc dayjs (- (js/Date.now) 86400000)))]
{:start last-24-hours :end today}))
(defmethod timeframe-dates "7 days" [_]
(let [today (.toISOString (dayjs))
seven-days-ago (.toISOString (dayjs (- (js/Date.now) 604800000)))]
{:start seven-days-ago :end today}))
(defmethod timeframe-dates "30 days" [_]
(let [today (.toISOString (dayjs))
thirty-days-ago (.toISOString (dayjs (- (js/Date.now) 2592000000)))]
{:start thirty-days-ago :end today}))

(defn main []
(let [requests (rf/subscribe [:requests])
proxies (rf/subscribe [:proxies])]
Expand All @@ -88,10 +91,6 @@
selected-proxy (->> (:data @proxies)
(filter #(= (:id %) proxy-id))
first)]
;(println :selected-proxy selected-proxy)
(println :proxy-id proxy-id)
(when-not (= (:id selected-proxy) proxy-id)
(println :hue selected-proxy proxy-id))
[:> Box {:p :md}
[:> Stack {:gap :lg}
[:header
Expand Down
8 changes: 4 additions & 4 deletions server/src/server/api/proxies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
(merge
(when-let [name (:name body)] {:name name})
(when-let [description (:description body)] {:description description})
(when-let [request-headers-keys (:request-headers-keys body)]
{:request-headers-keys request-headers-keys})
(when-let [response-headers-keys (:response-headers-keys body)]
{:response-headers-keys response-headers-keys})
(when-let [request-headers-config (:request-headers-config body)]
{:request-headers-config request-headers-config})
(when-let [response-headers-config (:response-headers-config body)]
{:response-headers-config response-headers-config})
(when-let [target-url (:target-url body)] {:target-url target-url})
(when-let [host-url (:host-url body)] {:host-url host-url})))})))

Expand Down
21 changes: 12 additions & 9 deletions server/src/server/api/requests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,26 @@
(string/split
(string/lower-case http-methods)
#",")))
:start-date (when-let [start-date (get query-params "start_date")]
(Instant/parse start-date))
:end-date (when-let [end-date (get query-params "end_date")]
(Instant/parse end-date))
:customer-ids (when-let [customer-ids (get query-params "customer_ids")]
(when-not (string/blank? customer-ids)
(map parse-long
(string/split customer-ids #","))))
:customer-subs (when-let [customer-subs (get query-params "customer_subs")]
(when-not (string/blank? customer-subs)
(map parse-long
(string/split customer-subs #","))))
(string/split customer-ids #",")))
:customers-subs (when-let [customers-subs (get query-params "customers_subs")]
(when-not (string/blank? customers-subs)
(string/split customers-subs #",")))
:endpoints-ids (when-let [endpoints-ids (get query-params "endpoints_ids")]
(when-not (string/blank? endpoints-ids)
(map parse-long
(string/split endpoints-ids #","))))
(string/split endpoints-ids #",")))
:order-by (get query-params "order_by") ;; field
:order (get query-params "order") ;; :asc or :desc
:customer-id (get query-params "customer_id")
:endpoint-id (get query-params "endpoint_id")
:limit (or (get query-params "limit") 50)
:limit (if-let [limit (get query-params "limit")]
(parse-long limit)
100)
:offset (or (get query-params "offset") 0)})]
(response {:status "ok"
:data requests})))
Expand Down
17 changes: 11 additions & 6 deletions server/src/server/models/proxies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,21 @@
:request_headers_config]})))

(defn update-proxy-by-id
[workspace-id proxy-id {:keys [name description
target-url host-url]}]
[workspace-id proxy-id {:keys [name description target-url host-url
response-headers-config request-headers-config]}]
(t/log! :debug (str "Updating proxy model for " proxy-id))
(with-open [conn (db/connection)]
(pg-honey/update
conn :proxies
{:name name
:description description
:target_url target-url
:host_url host-url}
(merge
(when response-headers-config
{:response_headers_config response-headers-config})
(when request-headers-config
{:request_headers_config request-headers-config})
{:name name
:description description
:target_url target-url
:host_url host-url})
{:where [:and
[:= :id proxy-id]
[:= :workspace_id workspace-id]]
Expand Down
6 changes: 3 additions & 3 deletions server/src/server/models/requests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

(defn get-requests-by-proxy-id
[{:keys [proxy-id workspace-id limit offset
order order-by customer-ids customer-subs
order order-by customer-ids customers-subs
start-date end-date
status-codes http-methods endpoints-ids]}]
(t/log! :debug "Getting requests by proxy id")
Expand All @@ -130,8 +130,8 @@
[:in :endpoint_id endpoints-ids])
(when customer-ids
[:in :customer_id customer-ids])
(when customer-subs
[:in :customer_sub customer-subs])
(when customers-subs
[:in :customer_sub customers-subs])
(when start-date
[:>= :created_at start-date])
(when end-date
Expand Down

0 comments on commit 2ece3e5

Please sign in to comment.