Filtering out the data using filter

{:settings {:columns [{:key "seqNo", :active true} {:key "title", :active true} {:key "status", :active true} {:key "dueDate", :active true} {:key "priority", :active true} {:key "parent", :active true} {:key "childCount", :active true} {:key "createdAt", :active true} {:key "updatedAt", :active true} {:key "completedAt", :active true} {:key "completedBy", :active false} {:key "age", :active true} {:key "creator", :active true} {:key "owner", :active true} {:key "members", :active true} {:key "description", :active true} {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false} {:id 5, :key 5, :type "text", :label "Direction", :active false, :settings "{}", :auto-check false} {:id 6, :key 6, :type "text", :label "Cast", :active false, :settings "{}", :auto-check false}]}}
How to show only the data when :active is true?
See if this is the data,
When I did this,

 (->> (The above data)
      :settings
      :columns
      (filter :active)
      (mapv :key))

I got this
[:seq-no :title :status :due-date :priority :parent :child-count :created-at :updated-at :completed-at :age :creator :owner :members :description :4 :5 :6]

Because of (filter :active) it should only true ones right?
So {:key "completedBy", :active false} this is not being shown

but this is {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false}

When I run above code I got what was expected:

;; => ("seqNo" "title" "status" "dueDate" "priority" "parent" "childCount" "createdAt" "updatedAt" "completedAt" "age" "creator" "owner" "members" "description")

No :4 :5 :6?

How did you ran it?

Just copy&paste to the REPL

(->> {:settings {:columns [{:key "seqNo", :active true} {:key "title", :active true} {:key "status", :active true} {:key "dueDate", :active true} {:key "priority", :active true} {:key "parent", :active true} {:key "childCount", :active true} {:key "createdAt", :active true} {:key "updatedAt", :active true} {:key "completedAt", :active true} {:key "completedBy", :active false} {:key "age", :active true} {:key "creator", :active true} {:key "owner", :active true} {:key "members", :active true} {:key "description", :active true} {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false} {:id 5, :key 5, :type "text", :label "Direction", :active false, :settings "{}", :auto-check false} {:id 6, :key 6, :type "text", :label "Cast", :active false, :settings "{}", :auto-check false}]}}
     :settings
     :columns
     (filter :active)
     (map :key))

;; => ("seqNo" "title" "status" "dueDate" "priority" "parent" "childCount" "createdAt" "updatedAt" "completedAt" "age" "creator" "owner" "members" "description")

I get that data from this func

(defn get-report
  "Get a particular report"
  [{db-spec :db :as system} org-id uid report-id]
  (try
    (->> (j/query db-spec
                  (sql/sql
                   (sql/select db [:id :org-id :creator :title :settings
                                   :created-at :updated-at :filters :type]
                     (sql/from :report)
                     (sql/where `(and (= :org-id ~org-id)
                                      (= :id ~report-id))))))
         first
         (transform-keys ->kebab-case-keyword))
    (catch SQLException e
      (timbre/error e)
      (throw (IllegalArgumentException. "Failed to fetch the report")))))

This returns:
{:updated-at #inst "2021-06-02T06:21:53.980000000-00:00", :creator "L0QGp8J3P0fFNq7eGeR7Tl61UuL2", :filters {:id 3, :page 1, :sort ["seqNo:asc"]}, :settings {:columns [{:key "seqNo", :active true} {:key "title", :active true} {:key "status", :active true} {:key "dueDate", :active true} {:key "priority", :active true} {:key "parent", :active true} {:key "childCount", :active true} {:key "createdAt", :active true} {:key "updatedAt", :active true} {:key "completedAt", :active true} {:key "completedBy", :active false} {:key "age", :active true} {:key "creator", :active true} {:key "owner", :active true} {:key "members", :active true} {:key "description", :active true} {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false} {:id 5, :key 5, :type "text", :label "Direction", :active false, :settings "{}", :auto-check false} {:id 6, :key 6, :type "text", :label "Cast", :active false, :settings "{}", :auto-check false}]}, :type "process", :org-id 1, :title "Test Three", :id 1, :created-at #inst "2021-05-24T07:10:32.506789000-00:00"}
and doing this,

(->> (get-report {:db db-spec} org-id uid report-id)
                             :settings
                             :columns
                             (filter :active)
                             (mapv (comp :key)))

I’m getting, ["seqNo" "title" "status" "dueDate" "priority" "parent" "childCount" "createdAt" "updatedAt" "completedAt" "age" "creator" "owner" "members" "description" "Screenplay" "Direction" "Cast"]

Well… looks like you paste here something different than you run. Again, copy&paste from above:

(->> {:updated-at #inst "2021-06-02T06:21:53.980000000-00:00", :creator "L0QGp8J3P0fFNq7eGeR7Tl61UuL2", :filters {:id 3, :page 1, :sort ["seqNo:asc"]}, :settings {:columns [{:key "seqNo", :active true} {:key "title", :active true} {:key "status", :active true} {:key "dueDate", :active true} {:key "priority", :active true} {:key "parent", :active true} {:key "childCount", :active true} {:key "createdAt", :active true} {:key "updatedAt", :active true} {:key "completedAt", :active true} {:key "completedBy", :active false} {:key "age", :active true} {:key "creator", :active true} {:key "owner", :active true} {:key "members", :active true} {:key "description", :active true} {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false} {:id 5, :key 5, :type "text", :label "Direction", :active false, :settings "{}", :auto-check false} {:id 6, :key 6, :type "text", :label "Cast", :active false, :settings "{}", :auto-check false}]}, :type "process", :org-id 1, :title "Test Three", :id 1, :created-at #inst "2021-05-24T07:10:32.506789000-00:00"}
     :settings
     :columns
     (filter :active)
     (map :key))

;; => ("seqNo" "title" "status" "dueDate" "priority" "parent" "childCount" "createdAt" "updatedAt" "completedAt" "age" "creator" "owner" "members" "description")

"Screenplay" "Direction" "Cast" are under :label key not :key key. So there is no way to get them from the above code.

I found something weird,
This is the method,

(defn get-report
  "Get a particular report"
  [{db-spec :db :as system} org-id uid report-id]
  (try
    (->> (j/query db-spec
                  (sql/sql
                   (sql/select db [:id :org-id :creator :title :settings
                                   :created-at :updated-at :filters :type]
                     (sql/from :report)
                     (sql/where `(and (= :org-id ~org-id)
                                      (= :id ~report-id))))))
         first
         (transform-keys ->kebab-case-keyword))
    (catch SQLException e
      (timbre/error e)
      (throw (IllegalArgumentException. "Failed to fetch the report")))))

If I run this in repl like this,
(get-report {:db (:myapp/db integrant.repl.state/system)} 1 “L0QGp8J3P0fFNq7eGeR7Tl61UuL2” 1)`

I get:
{:updated-at #inst "2021-06-02T09:44:15.130000000-00:00", :creator "L0QGp8J3P0fFNq7eGeR7Tl61UuL2", :filters {:id 3, :page 1, :sort ["seqNo:asc"]}, :settings {:columns [{:key "seqNo", :active true} {:key "title", :active true} {:key "status", :active true} {:key "dueDate", :active true} {:key "priority", :active true} {:key "parent", :active true} {:key "childCount", :active true} {:key "createdAt", :active true} {:key "updatedAt", :active true} {:key "completedAt", :active true} {:key "completedBy", :active false} {:key "age", :active true} {:key "creator", :active true} {:key "owner", :active true} {:key "members", :active true} {:key "description", :active true} {:id 4, :key 4, :type "text", :label "Screenplay", :active false, :settings "{}", :auto-check false} {:id 5, :key 5, :type "text", :label "Direction", :active false, :settings "{}", :auto-check false} {:id 6, :key 6, :type "text", :label "Cast", :active false, :settings "{}", :auto-check false} {:id 11, :key 11, :type "text", :label "SOME", :active false, :settings "{}", :auto-check false}]}, :type "process", :org-id 1, :title "Test Three", :id 1, :created-at #inst "2021-05-24T07:10:32.506789000-00:00"}

But if call that func and print itlike this in code,
(println "R" (get-report {:db db-spec} org-id uid report-id))
I get:
{:updated-at #inst "2021-06-02T09:44:15.130000000-00:00", :creator L0QGp8J3P0fFNq7eGeR7Tl61UuL2, :filters {:id 3, :page 1, :sort [seqNo:asc]}, :settings {:columns [{:key seqNo, :active true} {:key title, :active true} {:key status, :active true} {:key dueDate, :active true} {:key priority, :active true} {:key parent, :active true} {:key childCount, :active true} {:key createdAt, :active true} {:key updatedAt, :active true} {:key completedAt, :active true} {:key completedBy, :active false} {:key age, :active true} {:key creator, :active true} {:key owner, :active true} {:key members, :active true} {:key description, :active true} {:id 4, :key 4, :type text, :label Screenplay, :active false, :settings {}, :auto-check false} {:id 5, :key 5, :type text, :label Direction, :active false, :settings {}, :auto-check false} {:id 6, :key 6, :type text, :label Cast, :active false, :settings {}, :auto-check false} {:id 11, :key 11, :type text, :label SOME, :active false, :settings {}, :auto-check false}]}, :type process, :org-id 1, :title Test Three, :id 1, :created-at #inst "2021-05-24T07:10:32.506789000-00:00"}

If you print, double quotes are removed:

user> (def a "abc")
#'user/a
user> a
"abc"
user> (println a)
abc

Oh but the main issue is here,
There’s a method and inside it I’m doing this,

columns (into 
                        (->> (get-report {:db db-spec} org-id uid report-id)
                             :settings
                             :columns
                             (filter :active)
                             (mapv (comp keyword :key)))
                      ;; Append checklist columns
                      chk-columns)

Now if I do (println columns) I get, [:seqNo :title :status :dueDate :priority :parent :childCount :createdAt :updatedAt :completedAt :age :creator :owner :members :description :4 :5 :6 :11]

But in repl if I do,

(->> (get-report {:db db-spec} 1 "L0QGp8J3P0fFNq7eGeR7Tl61UuL2" 1)
                             :settings
                             :columns
                             (filter :active)
                             (mapv (comp keyword :key)))

I get
[:seqNo :title :status :dueDate :priority :parent :childCount :createdAt :updatedAt :completedAt :age :creator :owner :members :description]

How is this a possibility?

When you look at your code, you can see (into the-filter-stuff chk-columns). I’m pretty sure that chk-columns contains [:4 :5 :6 :11]

Yeah got that now! thank you so much for your time.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.