feat(mcp): port the get_dataset_usage_history tool

Merged
NW
4d ago
+0-0

Stack. This is one PR in a series that adds a built-in MCP server to DataHub Core. It is based on na--mcp-get-dataset-queries (the previous PR in the stack); review only its own commit. It changes only the shared datahub-core-mcp-tools package and datahub-ai-service. It is one tool port, so it stays one PR even where it runs past the stack's usual 1,000-line size. Everything user-facing is behind a GMS feature flag, MCP_SERVER_ENABLED, off by default (added in #21349).

Stack so far:

1–22. Merged to master as #21332.
23. #21435
24. #21436
25. #21437
26. #21438
27. #21439
28. #21440
29. #21441
30. #21442
31. #21443
32. #21444 ← this PR
33. #21445
34. #21447
35. #21501
36. #21502
37. #21503
38. #21504
39. #21505
40. #21506
41. #21507
42. #21508
43. #21509
44. #21510
45. #21511
46. #21512

What this does

get_dataset_usage_history returns how often a dataset was queried over the
last DAY, WEEK, MONTH or QUARTER: the total SQL query count and one
UTC-dated point per day, sorted, ready to summarize or chart. It is in the
read group and read-only.

From DataHub Cloud's integrations service (where it is registered beside
the shared tools, not in the package): the tool, its description and
dataset_usage.gql (the dataset's usageStats with totalSqlQueries per bucket,
in UTC), the closed TimeRange choice, and the response shape. Buckets
without a date or a count are left out rather than plotted as zero. The
legacy OSS server has no such tool.

Adapted to this package: reads go through ReadBackend (new dataset_usage),
and every failure is a ToolError instead of ValueError. The URN must be a
dataset URN (a name gets the search hint, another entity type says usage is
recorded for datasets only) and must exist: GMS answers dataset(urn:) with
an object even for a URN it has never seen, so a typo would otherwise read
as "never queried". It also takes the usual urn aliases.

Version gates: none. usageStats(range:, timeZone:) and totalSqlQueries
validate on GMS 1.7.0, 1.8.0 and master.

Testing

dated, sorted points and the total from a test ReadBackend
(including incomplete buckets), an unqueried dataset, missing usage, a null
or missing dataset, a non-dataset URN and a name, the GraphQL backend's
operation and variables, and registration (read-only, the four ranges in
the schema). Live on a local OSS DataHub (showcase-ecommerce): the datapack
has no usage statistics, so the Snowflake and Postgres orders tables return
totalQueryCount 0 and no points for MONTH and QUARTER (the query runs on
GMS); a bogus URN is a tool error. Non-empty history is covered by the unit
tests only.

Checklist

  • The PR conforms to DataHub's Contributing Guideline (particularly PR Title Format)
  • Tests for the changes have been added/updated (if applicable)
  • Docs related to the changes have been added/updated (if applicable). The MCP docs come later in the stack.
  • For any breaking change/potential downtime/deprecation/big changes an entry has been made in Breaking Changes. The release notes come later in the stack.

šŸ¤– Generated with Claude Code