Skip to content

feat(analytics): switch reporter to *database.Client + Ent - #1217

Merged
kalbasit merged 1 commit into
mainfrom
feat-analytics-to-ent
May 23, 2026
Merged

kalbasit merged 1 commit into
mainfrom
feat-analytics-to-ent

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

pkg/analytics held the last in-tree consumer of database.Querier
outside of test fixtures. The reporter now takes a *database.Client,
and the ncps_store_nar_files_total_size_bytes gauge callback issues
the SUM via the Ent fluent API with the null-safe sql.NullInt64
unpacking pattern already used elsewhere in the codebase.

pkg/ncps/serve.go threads dbClient into analytics.New in place of the
legacy db Querier.

Co-Authored-By: Claude Opus 4.7 [email protected]

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label May 21, 2026
@dosubot dosubot Bot added enhancement New feature or request go Pull requests that update go code labels May 21, 2026
@kalbasit

kalbasit commented May 21, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 1742471 to 7883536 Compare May 22, 2026 21:04
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from 727302e to dd54658 Compare May 22, 2026 21:04
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 7883536 to 51fd46c Compare May 22, 2026 21:17
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from dd54658 to 0868ec4 Compare May 22, 2026 21:17
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 51fd46c to 828ac31 Compare May 22, 2026 21:29
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from 0868ec4 to ffd8eb7 Compare May 22, 2026 21:29
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 828ac31 to 194f960 Compare May 22, 2026 21:42
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from ffd8eb7 to 770eace Compare May 22, 2026 21:42
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 194f960 to 86d9c0f Compare May 22, 2026 21:54
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from 770eace to 93b30ab Compare May 22, 2026 21:54
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor-cache-drop-legacy-tx-helpers branch from 86d9c0f to 364285f Compare May 22, 2026 23:02
@kalbasit
kalbasit force-pushed the feat-analytics-to-ent branch from 93b30ab to 0875a49 Compare May 22, 2026 23:02
kalbasit added a commit that referenced this pull request May 22, 2026
Ent's Aggregate(ent.Sum(...)) emits a raw SUM(...) expression without an
alias; scanning into []struct{ Sum sql.NullInt64 \`sql:"sum"\` } relied
on tag-name resolution that may not match the column header. Scan
positionally into a single sql.NullInt64 instead, which is more robust
and also correctly handles the NULL result that SUM returns on an empty
table.

Addresses gemini-code-assist comments on PR #1217
(PRRT_kwDONV6tZs6D3Um1, PRRT_kwDONV6tZs6D3Um6).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1217 swapped the working []struct{Sum sql.NullInt64 \`sql:"sum"\`}
pattern for &sql.NullInt64, but Ent's Select.Scan calls sql.ScanSlice
which rejects non-slice destinations ("expected slice as an argument"),
so the gauge callback was effectively broken.

Confirmed against a live SQLite database:
- []sql.NullInt64 fails: scanStruct can't map SUM(...) to NullInt64
  fields (the Int64/Valid fields don't carry the tag).
- []*int64 (the suggestion in PR #1229's review) fails on NULL — when
  SUM returns NULL on an empty table the driver errors with
  "converting NULL to int64 is unsupported".
- []struct{ Sum sql.NullInt64 \`sql:"sum"\` } works: scanStruct splits
  the column name at "(" and lowercases the leading token, so
  "SUM(file_size)" → "sum" matches the field tag, and NullInt64
  captures the NULL/empty-table case via sql.Scanner.

This is the same pattern already in use at pkg/cache/cache.go:711.

Addresses gemini-code-assist comments on PR #1229
(PRRT_kwDONV6tZs6D5Lec, PRRT_kwDONV6tZs6D5Lem) — the underlying bug is
fixed even though the specific []*int64 suggestion would not have
worked.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
pkg/analytics held the last in-tree consumer of database.Querier
outside of test fixtures. The reporter now takes a *database.Client,
and the ncps_store_nar_files_total_size_bytes gauge callback issues
the SUM via the Ent fluent API with the null-safe sql.NullInt64
unpacking pattern already used elsewhere in the codebase.

pkg/ncps/serve.go threads dbClient into analytics.New in place of the
legacy db Querier.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/analytics/analytics.go`:
- Around line 268-270: The aggregation scan struct currently expects a column
alias "sum" via `sql:"sum"` but the query may not be aliasing the aggregate
accordingly; keep the struct definition with Sum sql.NullInt64 `sql:"sum"` and
ensure the ent aggregation call that produces this value uses an explicit alias
(e.g., use ent.As(...) or alias the aggregate as "sum") so
Aggregate(...).Scan(&sums) receives a column named "sum"; also verify the
scanned type (`sql.NullInt64` on the `Sum` field) matches the aggregate type
returned by the query.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d830060a-f8a0-4611-b6cc-d1652ecb36e3

📥 Commits

Reviewing files that changed from the base of the PR and between 05bb01b and f2ee0b7.

📒 Files selected for processing (2)
  • pkg/analytics/analytics.go
  • pkg/ncps/serve.go

Comment thread pkg/analytics/analytics.go Resolved
@codecov

codecov Bot commented May 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.68%. Comparing base (05bb01b) to head (f2ee0b7).

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1217   +/-   ##
=====================================
  Coverage   5.68%   5.68%           
=====================================
  Files          6       6           
  Lines        387     387           
=====================================
  Hits          22      22           
  Misses       361     361           
  Partials       4       4           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code merge-stack size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant