Fixed-in-time basic TODO aggregation

(Kind of worn out, so pardon if this isn’t coherent.)

My use case is simple: I would like a query to pull together all TODO items written on a given journal page, or referencing that page, without having to repeatedly “call out” (link or tag) the journal page. A property like :journal-created-data would be great, but I’m struggling to string together the Clojure to make that happen.

It would be nice to extend this view to be (say) 5-7 days back from the journal, so each journal page has a snapshot in time of the items left undone at that point. (I intend to close out these lingering items over the winter holiday break). As such, there are a few things I’d like to avoid in a query.

I do not want to use :today because that updates everyday and I regularly look back at my old notes.
I do not want to use :current-page for the exact same reason.
I do want to leverage :query-page, which seems to return the correct string - I tested this by trial-and-error once I generated malformed query wrt :inputs - but the query results are always empty when I swap it in for a functioning query based around :current-page (example of one such is here ).

As an example, the following returns nothing.

{:title "What am I working on i.e. TODOs generated for today"
:query [:find (pull ?block [*])
    :in $ ?page
    :where
        [?block :block/marker "TODO"]
        [?h     :block/page ?page]
        [?block :block/ref-pages ?h]
]
:inputs [:current-page]
}

while the following gives me all the TODO blocks, but it’s still bound to :today, so it will eventually not give me anything

#+BEGIN_QUERY
{:title "TODO blocks in last 7 days"
 :query [:find (pull ?b [*])
         :in $ ?start ?today ?page-name
         :where
        [?b :block/marker "TODO"]
         (between ?b ?start ?today)]
 :inputs [:-7d :today :query-page]}
#+END_QUERY

Why does this matter?

  • Maybe my workflow is terrible, and I’m equally bad at finishing items on my TODO list, but
    1. I often have epiphanies in my diary-writing-style journaling, and I write TODOs inline. It would be very jarring to
    2. If I’m on another page, it’s trivial to link out to the journal page (or wherever) and see the linked TODO in the footer of the page; but I don’t want to litter my journaling with references to a page I’m already on
      • If this is the best for performance, please let me know.
    3. I have a lot of dangling TODOs. There’s redundancy that I’ll clean up eventually, but it’s more valuable for me to be able to see what was left dangling when, so I can root-cause analyze the failure in ability to execute rather than just continue with the never-ending list.

Pardon the tirade.

Many thanks in advance!


If you might also reference the full, unambiguous schema for pages and blocks in LogSeq, I would really appreciate it. I’ve felt for hours that I know what query to write if I just knew the schema, but I have yet to find it (the Database schema link from this tutorial is a 404)

The actual schema is here: https://github.com/logseq/logseq/blob/master/deps/db/src/logseq/db/schema.cljs

I’ll get back to you about the query itself when I have some more time.
Found time quicker than expected lol

1 Like

So they’re not too different.
Query-page refers to the page the query is written on. The current-page refers to the page you are viewing in the main window of logseq. For example if the query is on a page open in the right side bar, current-page refers to the page you are viewing and query-page to the page open in the sidebar. If you’re viewing the query-page in the main window, they are the same.

This is how you get todo that are either on the current journal page or referencing it. Assuming the query is on the same page.

#+BEGIN_QUERY
{:title [:b "TODO for this journal page"]
 :inputs [:query-page]
 :query [:find (pull ?b [*])
  :in $ ?page
  :where
   [?p :block/name ?page]
   [?b :block/marker ?m]
   [(contains? #{"TODO"} ?m)]
   (or
     [?b :block/page ?p]
     [?b :block/refs ?p]
   )
 ]
}
#+END_QUERY

Welcome.

This is not Clojure (neither ClojureScript), but Datalog.

Are you interested in capturing a static snapshot of the query’s results at that point, without re-running the query each time you visit that page? (Could still re-run the query, but only to compare the new results with the captured ones)

Great question.
I’m interested in query wrt the journal day such that the result on journal page 2023-12-19 always shows me

  1. the TODO items created in that journal entry
  2. referencing that entry (a la #[[2023-12-19]] or [[2023-12-19]]), and
  3. any TODO items written on one of the 5 preceding days of journal page only (:-5d) (no references. I’m not looking for a recursive query)

It doesn’t have to be idempotent, as I regularly reference future days in current TODOs and might reference a previous journal page in a future note.

Building off of @Siferiax 's response, I’ve crafted the following, but it returns on results

#+BEGIN_QUERY
{:title [:b "TODO for this journal page"]
 :inputs [:query-page]
 :query [:find (pull ?b [*])
  :in $ ?page
  :where
   [?p :block/name ?page]
   [?b :block/marker ?m]
   [(contains? #{"TODO"} ?m)]
   (or
     [?b :block/page ?p]
     [?b :block/refs ?p]
   )
   (between ?b :-7d-start (?b :block/created-at ?page) )
 ]
}
#+END_QUERY

For block/created-at to work it needs to either be a page, or block timestamps need to be turned on specifically in the config.

Secondly this is not how that works…
:-7d-start can only be used as an input. It also always uses today.
:-Xd - Date for X number of days before today’s date”
https://docs.logseq.com/#/page/advanced%20queries/block/query%20inputs

And it is a timestamp whereas the between simple query syntax uses journal dates. So that’s also not compatible.

To put it very bluntly we don’t know which date is x days before a certain journal date.
Because in datalog we don’t know what a date is.
Not something you can see in the schema apparently, but journal-day is a number in format yyyymmdd.
There is no date operations.
I believe @mentaloid had some complicated math elsewhere on the forum?

So number 3 of your requirements is hard to do. The other two you should now have.

I’m linking the math for Datalog and Clojure respectively. But these are complicated mostly because of calculating the day of the week. For extracting the needed values out of the yyyymmdd format, quotient and modulo are enough.