Sentiment data query
October 2025
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Negative
Slightly negative
Neutral
Positive
Very positive
Hide code
-- Query all blocks that have sentiment metadata and aggregate by date
local current_page = nb.current_page
local blocks

-- Check if we're on a journal page and extract date range
if current_page and current_page:match("^journal/") then
  local date_str = current_page:match("[^/]+$")  -- extract "2025-10-31"
  local year, month, day = date_str:match("(%d%d%d%d)%-(%d%d)%-(%d%d)")
  if year and month then
    year, month = tonumber(year), tonumber(month)
    local first_day = string.format("%04d-%02d-01", year, month)

    -- Compute last day of month
    local last_day = string.format("%04d-%02d-%02d", year, month, 31)

    blocks = nb.query_all({
      tags = {"dayone"},
      date_from = first_day,
      date_to = last_day
    })
  end
else
  -- Not on journal page, query February 2026
  blocks = nb.query_all({
    tags = {"dayone"},
    date_from = "2026-03-01",
    date_to = "2026-03-31"
  })
end

local by_date = {}
local counts = {}

for _, b in ipairs(blocks) do
  local d = b.metadata.date or b.date
  local s = tonumber(b.metadata.sentiment)
  if d and s then
    local key = string.sub(d, 1, 10)
    by_date[key] = (by_date[key] or 0) + s
    counts[key] = (counts[key] or 0) + 1
  end
end

local result = {}
for date, total in pairs(by_date) do
  result[date] = total / counts[date]
end

-- Extract month label and days in month from current context
local month_label = "Monthly View"
local days_in_month = 31

if current_page and current_page:match("^journal/") then
  local date_str = current_page:match("[^/]+$")
  local year, month = date_str:match("(%d%d%d%d)%-(%d%d)")
  if year and month then
    local month_names = {"January", "February", "March", "April", "May", "June",
                         "July", "August", "September", "October", "November", "December"}
    month_label = month_names[tonumber(month)] .. " " .. year
    
    -- Calculate days in month
    local days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    local y = tonumber(year)
    if tonumber(month) == 2 and (y % 4 == 0 and (y % 100 ~= 0 or y % 400 == 0)) then
      days_in_month = 29
    else
      days_in_month = days[tonumber(month)]
    end
  end
end

ui.render("sentiment-calendar-heatmap-v2", {
  data = result,
  monthLabel = month_label,
  daysInMonth = days_in_month
})