Queries for task management

Post 0.9.2 update, all my queries have gone back to show references and they are no more a list of TODO things. Did others also face similar issues?

I believe this is a solution for the issue: :result-transform and grouping of results · Issue #6641 · logseq/logseq · GitHub
Which a lot of people have asked for / complained about. (myself included lol, even though I often used result-transform to get rid of it)
There should be a way to remove the grouping, but I’m unaware if this is now available or not.
Created a feature request topic for it:

1 Like

Thanks for the info.
I have problem also with :collapsed? Function.
Do you have any idea and solution?

Thanks

From what I read about it, seems that’s an actual bug. So would need a bug report I guess.

ok. I hope the fix it quickly.
Everyone is talking about it even in discord. :sweat_smile:

Tasks that do or do not repeat with specific tag

1 Like

That thread is a goldmine! Thank you so much @Siferiax I wonder if you could help me out. I managed to put together a query that looks for “next actions” as per the GTD terminology and keep only these “next actions” related to projects with deadlines. I just can’t seem to sort the list by :block/deadline of these ancestor blocks in the :result-transform section as the “next actions” don’t really inherit the deadlines themselves from the ancestor tasks… Here’s my query so far:

#+BEGIN_QUERY
{:title [:h2 "NEXT ACTIONS of projects w/ deadlines"]
:query [:find (pull ?b [*])
        :in $ % 
        :where
        [?b :block/marker ?marker]
        [(contains? #{"LATER" "NOW"} ?marker)]
        ;; descendant is true when ?descendant is an descendant of b

        (not [?descendant :block/parent ?b]
             [?descendant :block/marker ?amarker]
             [(contains? #{"LATER" "NOW"} ?amarker)])

(ancestor ?b ?ancestor)
[?ancestor :block/deadline ?d]
]


:inputs
   
  [[

    [(ancestor ?b ?ancestor)
				[?b :block/parent ?ancestor]]
	
    [(ancestor ?b ?ancestor)
	            [?child :block/parent ?ancestor]
	            (ancestor ?b ?child)]
]]

:result-transform  (fn [result]
                            (sort-by  
(juxt
(fn [d] (get d :block/deadline)) 
(fn [h] (get-in h [:block/properties :time]))
) < result)
)
:collapsed? false
}
#+END_QUERY#

We need to associate that deadline property with the result you get.
This happens in the :find, :keys and most importantly :result-transform of the below query.

#+BEGIN_QUERY
{:title [:h2 "NEXT ACTIONS of projects w/ deadlines"]
 :query [:find (pull ?b [*]) ?d
   :keys block deadline
   :in $ % 
   :where
     [?b :block/marker ?marker]
     [(contains? #{"LATER" "NOW"} ?marker)]
     ;; descendant is true when ?descendant is an descendant of b
     (not 
       [?descendant :block/parent ?b]
       [?descendant :block/marker ?amarker]
       [(contains? #{"LATER" "NOW"} ?amarker)]
     )
     (ancestor ?b ?ancestor)
     [?ancestor :block/deadline ?d]
 ]
 :inputs [ [
   [(ancestor ?b ?ancestor)
     [?b :block/parent ?ancestor]
   ]
   [(ancestor ?b ?ancestor)
     [?child :block/parent ?ancestor]
     (ancestor ?b ?child)
   ]
 ] ]
 :result-transform (fn [result]
   (sort-by  
     (juxt
       (fn [d] (get-in d [:block/properties :deadline])) 
       (fn [h] (get-in h [:block/properties :time]))
     )
     < 
; the below associates the deadline key from the query result with the :block/properties attribute from the block key. Which we use above in the sort.
     (map 
       (fn [m]
         (update (:block m) :block/properties
           (fn [u] (assoc u :deadline (get-in m [:deadline]) ) )
         )
       ) 
       result 
     )
   )
 )
 :collapsed? false
}
#+END_QUERY

Things are broken in version 0.9.2, but older versions should work correctly.
Let me know if you encounter any issues!

@Siferiax - Sorry to bother you, but I just wanted to follow up on this in case you missed it! With table view, the only columns I see are “block” and “page” but is there any way to see more columns like “deadline”, “priority”, etc.?

Yes I had that on my backlog, but a bunch of other things came on my plate at the same time and I sort of lost track of it. Thanks for the reminder!
I put it off as it is not a simple answer :slight_smile: but definitely should be possible with some magic trickery!
Table view in default will only show block, page and any used properties, not other values. However we can build our own tables in advanced queries!

Here’s the whole thing:

#+BEGIN_QUERY
{:title "🟠 SLIPPING"
 :query [:find (pull ?b [*])
   :in $ ?start ?today
   :where
     (task ?b #{"NOW" "LATER" "TODO" "DOING"})
     (between ?b ?start ?today)
 ]
 :inputs [:-7d :today]
 :result-transform (fn [result] (sort-by (fn [h] (get h :block/created-at)) result ) )
 :view (fn [rows] [:table 
   [:thead [:tr 
     [:th "Page"]
     [:th "Task"]
     [:th "Priority"]
     [:th "Deadline"]
   ]] 
   [:tbody (for 
     [r rows
       :let [content (str (get r :block/content))]
       :let [firstline (first (str/split-lines content))] ;this will show only the first line of the task.
     ]
     [:tr
       [:td [:a {:href (str "#/page/" (clojure.string/replace (get-in r [:block/page :block/original-name]) "/" "%2F"))}
 (get-in r [:block/page :block/original-name])] ] ;make a workable link to the page, even when it is in a namespace
       [:td [:a {:href (str "#/page/" (get-in r [:block/uuid]))} firstline] ] ;link to the block and show the first line
       [:td (get r :block/priority) ] ;get the priority from the results
       [:td (get r :block/deadline)] ;get the deadline from the results
     ]
   )]
 ])
 :collapsed? false
}
#+END_QUERY

You can expand it with any block attributes available basically.

1 Like

Thank you so much! I’ll try it out ASAP. You’re very knowlegeable in Datalog. I would love to get to that level of mastery and give back to the Logseq community. What ressources would you recommend based on your own journey to go beyond a simple introduction to Datalog just to get by? I have no background in programming but I would love to learn. Thank you!

Start here: http://www.learndatalogtoday.org/
Then check out these resources: https://siferiax.github.io/#/page/logseq%2Fadvanced%20queries/block/sources%20(external)
Especially Datomic Queries and Rules | Datomic I still reference a lot to this day.
As well as the Clojure docs for the functions.
I also have a collection of references I use frequently: https://siferiax.github.io/#/page/logseq%2Fadvanced%20queries
As well as a collection of examples: https://siferiax.github.io/#/page/logseq%2Fquery%20tests
I do admit I haven’t updated my site in awhile with new queries though.

3 Likes

As usual, I can’t thank you enough for your help. Thank you for being one of the most amazing people on this forum! :blush:

2 Likes

Hello! This works perfectly. I just wonder how can I make the query results collapsed by default? Thanks for your help!

add to your advanced query

:collapsed? true

I don’r want to collapse the query itself, just block results! Thank though

@Matthieu_B changed the result-transform for this.

 :result-transform (fn [result]
   (sort-by  
     (juxt
       (fn [d] (get-in d [:block/properties :deadline])) 
       (fn [h] (get-in h [:block/properties :time]))
     )
     < 
     (map (fn [m] (assoc m :block/collapsed? true))
     (map 
       (fn [m]
         (update (:block m) :block/properties
           (fn [u] (assoc u :deadline (get-in m [:deadline]) ) )
         )
       ) 
       result
     ) )
   )
 )
1 Like

Thank you so much! It works as expected!

Hmmm with 0.9.4 this will probably do the same?

Add :remove-block-children? query option for advanced queries

Is there a way to show tasks results in a table, where I can click in their checkboxes?