Inconsistent Query Issues

Hi all- I am a recent convert over to the silverbullet way of life, but some behavior I’m seeing on a query is utterly baffling and seemingly inconsistent.

I have a large amount of TODOs that I use check boxes to track the completion of. Naturally I am attempting to track to see which are and aren’t completed.

I am using the following query to pull those details from the current page (minus backtick to show single quotes after template block):

```template
project task tracking: 
Not Done: {{count({task where not done and page = @page.name})}}
Done:     {{count({task where done and page = @page.name})}}
Total:    {{count({task where page = @page.name})}}
```/

This displays the following after doing a system reload and page refresh:

But that’s not the oddest behavior, it’s that modifying seemingly irrelevant portions of the query controls the output in almost random patterns.

See below, two newlines are added below the text:

```template
project task tracking: 
Not Done: {{count({task where not done and page = @page.name})}}
Done:     {{count({task where done and page = @page.name})}}
Total:    {{count({task where page = @page.name})}}


\```

The new result is:
https://community.silverbullet.md/uploads/default/original/1X/871951f108c24f2c1c443202e239616f936453b2.png

Reloading the query directly takes it back to 0. Reviewing the logs of my Docker Container running SilverBullet shows no immediate errors hitting the server. (Note: I am using zefhemel/silverbullet:latest for my image)

2024-08-15 16:43:04 Requested file index.md
2024-08-15 16:43:09 Requested file index.md
2024-08-15 16:43:10 Requested file Library/Core/Widget/Table of Contents.md
2024-08-15 16:43:14 Requested file index.md
2024-08-15 16:43:19 Writing file testing/internal.md
2024-08-15 16:43:19 Requested file index.md
2024-08-15 16:43:24 Requested file index.md

And to further convolute my misapprehension of what is going on, placing a random space, such as below:

```template
project task tracking: 
Not Done: {{count({task where not done and page = @page.name})}}
Done:     {{count({task where done and page = @page.name})}}
T  otal:    {{count({task where page = @page.name})}}
\```

Results in a totally different set of numbers:
https://community.silverbullet.md/uploads/default/original/1X/42b7e0098c9df6582576eaf5c30ca8d8450d153a.png

The more I play with it the odder results I get. Utterly lost and looking for guidance from anyone who has seen this kind of behavior! Also sorry for the links instead of images, new users can only use one embedded media at a time.

I understand that you have a very large list of tasks on that page or just those two that we see on the screenshot?

If so, what may be going on is a race condition between the (task) indexer reindexing the tasks while the query at (surprising) times pulling data while that indexing is still ongoing.

To check, can you try the following:

  1. Make a minor change to the page to trigger a save (and reindex)
  2. Wait a few seconds (to be sure this Reindex completed)
  3. Run Alt-q to trigger a recalculation of all queries/templates on the page (or hit reload button on the template, same thing)

Ah I believe that may have been the case! After restarting my docker container, waiting on indexing and then following your steps recalculating the queries on the page worked to show the true answer each time!

I was a little surprised since using {{count({task})}} seems to run immediately without issue. I only have a total of 123 tasks, with 117 on the current page which I’m going to use as a template for more (I’m not sure how many are a lot with this crowd :slight_smile:).

Would you have any recommendation on how to effectively remediate the race condition? I would imagine there’s a way to force the query to wait for the task re-indexing to finish, but it doesn’t sound trivial to adjust.

Also thank you for replying not just to me, but also for being such an active maintainer focused on driving this project forward without losing sight of both community interests and the SBs core utility!

Honestly, it’s a bug. It’s a bit of an edge case because I doubt a lot of people have templates or queries that heavily depend on the content of the page itself, which is why this appears only now. I’ve created an issue for it:

1 Like

I might ask while on the topic of templates depending on contents of the page itself… I’m trying to understand the SB approach to include this query in new pages built from a template if you have any advice.

When initially attempting to include the query, the {{}} gets baked in.


image

I attempted a number of variations of sub-templates, with the latest attempt using the live template below. I think I might be misunderstanding when or how to pull in live templates, as the template doesn’t generate with this text (there also is no error, so likely something happening under the hood I’m not catching).

Is there a better approach to have a live template included in a template page?

Try using {{[[template/taskTracker]]}} so without the template() bit. This should inline the template without processing it.

That did the trick! Thanks again for all the help- hopefully I can help pass it forward in the community as I get more comfortable with SB :slight_smile:

1 Like