Query pages and sort by incoming link count and number of blocks on page

I’m trying to make a query that

  • Gets all extant pages, excluding a few utility pages
  • Counts the number of incoming links
  • Counts the number of blocks in the page
  • sorts by a function of both counts (TDB)

My query looks like this:

  #+BEGIN_QUERY
  {:title [ :h2 "undersized Pages" ]
    :query [:find ?page (count ?linkcount) (count ?blockcount)
        :keys name linkcount blockcount
        :where
            ; Get pages
            [?pid :block/name ?page]
            ; Filter out journals, missing files, and utility/internal pages, and aliases
            [?pid :block/journal? false]
            [?pid :block/file _]
            (not (page-property ?pid :page-type "utility"))
            (not (page-property ?pid :page-type "internal"))
            [(missing? $ ?pid :block/alias)]
            ; Get incoming links and page-block lists (counted above)
            [_ :block/refs ?pid ?linkcount]
            [_ :block/page ?pid ?blockcount]
     ]
  
    :result-transform (fn [result] (sort-by (fn [r] (get-in r [:linkcount])) > result))
  
    ;:view pprint
    :view (fn [rows] [:table [:thead [:tr [:th "Page"] [:th "Link Count"] [:th "Block Count"] ] ]
        [:tbody (for [r rows] [:tr
            [:td [:a
                {:href (str "#/page/" (clojure.string/replace (get-in r [:name]) "/" "%2F"))}
                (get-in r [:name])]
             ]
            [:td (get-in r [:linkcount]) ]
            [:td (get-in r [:blockcount]) ]
        ])]
    ])
  
  }
  #+END_QUERY

Problems I’m having:

  1. I’m not sure which properties to use.
    • What’s the difference between :block/page and block/parent?
    • Should I be using :block/refs to count number of references to a page?
  2. I need to separate out the ?linkcount and ?blackcount vectors, so they don’t limit the general list of pages, and don’t limit each other. Is this possible in one query?