Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(data-modeling): sort usage by model columns (#125028)

Jovan Sakovic44ef5f4e7b6a
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index ce45530470d2..a36a67f5e73b 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -7417,25 +7417,25 @@ snapshots:
products-data-modeling-models-usage--error--light:
hash: v1.k794b7964.7ad357a4263212e6a943643e84f1b1c4a8fba5688aa88fab825e7c5fea86786c.OgmCM-dKcZI9B2a3jdciMu9KgNFrOJ2YvEYPdzyK9Yc
products-data-modeling-models-usage--filtered-by-failed-models--dark:
- hash: v1.k794b7964.5227230e2c87030d8ab6d9eb085e338c0559d0519a97df50ee5289465eed6bd1.OpRh3Oj0Tgpq2rgK1mggpK97nNK-3acxpsrAijWCjE0
+ hash: v1.k794b7964.d86155a022ceb20146a8e3205658363719405340ba6b12ef192f185a2ed94517.psnvg2y41RTvk23YHh-Yw83qEmHJQX2yN9e29Hzwj38
products-data-modeling-models-usage--filtered-by-failed-models--light:
- hash: v1.k794b7964.a911a34c17d14853b0fcf2d4c5ca123ce67e6a0eceaebe49b4091f47476f2b95.NAguVEpH6uFZkqvaN67N6T30hy_GwLEB_UaqZGlipns
+ hash: v1.k794b7964.e84292c0ba7e56506892fcb4b9a277e3eae2b329a6b6223b6344a5a7a73b9095.vUeKLNLoFEq-wVgm57zS4Pp_jMfg62B7rksPkMxfDBc
products-data-modeling-models-usage--narrow--dark:
- hash: v1.k794b7964.d45bc3ec3b99d69f263bea5e240b82fb8b7558a004a1a1d7960c094397070c1e.mn8t0WeIeeFY6hXdVeIF1cdEqBVfEIF3eRDX719qk-U
+ hash: v1.k794b7964.8fdf7fdef92c582579c0d0c958231a45beeee74a24eacaf796465436b1be6968.IbckVCux4Vhe7hhhO9iCjrHDUiz1ZWVdwE-pOMNuo5E
products-data-modeling-models-usage--narrow--light:
- hash: v1.k794b7964.41825b5b570acc1e3a06f7f9a7fc9f2aac5395a537b78334d80f346c47212b45.CXoYoaKXkuVxOZ_wQpXOzXeHtBnJ-uQ1C1ELununJiE
+ hash: v1.k794b7964.8115b14f7d80acc03914f792e673433c6e64fba5438b39efafaf205d9e7aa1e4.gzkRECLOqF3IhyposzV7NTpI2_k5aFgFTWxPTaqmebA
products-data-modeling-models-usage--populated--dark:
- hash: v1.k794b7964.7347905329382de9904c8dd27995860efae5d121e98ec972b8984a9f71a23f61.W6L5AajpwdhVrG2yMbsOqa_Fh35v2Uezh77MD-ATLUY
+ hash: v1.k794b7964.1bbf2fd5c17b5bd2a0f27b06ba4911bc15c26e1fca920eb6fc6ad1889d700799.VweHdsi2TlMYDa8G34T9TId6v-tmBb2gpzHUGknP9DA
products-data-modeling-models-usage--populated--light:
- hash: v1.k794b7964.8db475e78d40495f09a63dd5eed350a48e57c2d00706b11b6f70c65f19dbed91.bCgqP3mABo0Sm5euLM96_RFSHs7Xpb-ZbX2rsT7ZYmU
+ hash: v1.k794b7964.642aa21c66df3b8fe4bd681fe4acec4e1fe99ba02c0cf01ec4aa9343ca1baaf6.m8qBq7vWZnwLfK8-kHB_TDsxQHA2jZxNTUrzTHJPNh8
products-data-modeling-models-usage--status-only--dark:
hash: v1.k794b7964.843b703ee6891ee34efe1197bb6ebd590ea566cba1132f20ae2443ab60799419.MNAGtHUc7xtCMLOn1s_pcXnukRUTHHQCINhQ2rh_foM
products-data-modeling-models-usage--status-only--light:
hash: v1.k794b7964.867c9559e44d0e09c4917c577ba9dc1e9e3187ac9a42a34bc6395682e67166f1.F1qdz52LZQrOMsDI-Hbfy-oOd5I9E7Lwgn7NsJFy9x4
products-data-modeling-models-usage--unmeasured--dark:
- hash: v1.k794b7964.83cfe7f1b96aab125b156e2d22e59b15dc18da9dbf19e7f151fe8b8e8b1527e3.c4MIrGF6GxHQlbYm9Vr7Sq_ZTsTFUCZvxPN9CrDbtu0
+ hash: v1.k794b7964.38c8554a1f7e97e18b9c0a06222e5839152027d31c929a6dc32bf0e1e78fd0e4.xm4AiJf4LwQ0xojfJ6OwiCeCnByTw812pt7YOEiaSN4
products-data-modeling-models-usage--unmeasured--light:
- hash: v1.k794b7964.1943fb020a1510601a2a451da4dd9056e1cf1c27eb08758aee880b5656e03c4e.73PZaJDXWHgo-t9_cM3F4c0Gwpjs9wV11V0dM1_iEog
+ hash: v1.k794b7964.b08020b1fe0448fe2b39f1b96a0c86eebe53f80c943fbf08e1a7901cc36eca2e.wdhIDYyXrkfvQzPpUHQV1HSW6zyJvcz5n2rnAiT0DCY
products-data-modeling-node-detail-scene--deleted-model--dark:
hash: v1.k794b7964.bf008eaa19295f4131e58d639e52c574e724973ac897a20d95c905206ccfe333.8CL_nCzrTvLfheyYXNeMHjX8MI8t_lazxmw_8FtPLu4
products-data-modeling-node-detail-scene--deleted-model--light:
diff --git a/products/data_modeling/backend/logic/usage.py b/products/data_modeling/backend/logic/usage.py
index 7e1851bad76a..22d7ea2164e0 100644
--- a/products/data_modeling/backend/logic/usage.py
+++ b/products/data_modeling/backend/logic/usage.py
@@ -3,8 +3,8 @@ from datetime import UTC, date, datetime, time, timedelta
from typing import TypedDict, cast
from uuid import UUID
-from django.db.models import Count, F, Max, Q, QuerySet, Sum
-from django.db.models.functions import TruncDate
+from django.db.models import Count, F, Max, Q, QuerySet, Sum, Value
+from django.db.models.functions import Coalesce, Lower, NullIf, TruncDate
from products.data_modeling.backend.models.data_modeling_job import (
DataModelingJob,
@@ -139,6 +139,8 @@ class ModelUsage:
start_date: date,
end_date: date,
engines: tuple[DataModelingJobEngine, ...],
+ sort_by: str = "read_bytes",
+ sort_direction: str = "desc",
) -> dict[str, object]:
start = datetime.combine(start_date, time.min, tzinfo=UTC)
end = datetime.combine(end_date, time.min, tzinfo=UTC)
@@ -176,12 +178,21 @@ class ModelUsage:
}
)
models = jobs.values("saved_query_id", "saved_query__name", "saved_query__deleted").annotate(
- **cls.engine_aggregates(engines), **cls.aggregates()
+ **cls.engine_aggregates((DataModelingJobEngine.CLICKHOUSE, DataModelingJobEngine.MANAGED_WAREHOUSE)),
+ **cls.aggregates(),
)
count = models.count()
offset = (page - 1) * page_size
+ sort_expression = (
+ Lower(Coalesce(NullIf("saved_query__name", Value("")), Value("Deleted model")))
+ if sort_by == "name"
+ else F(sort_by)
+ )
+ ordering = (
+ sort_expression.asc(nulls_last=True) if sort_direction == "asc" else sort_expression.desc(nulls_last=True)
+ )
rows: list[Mapping[str, object]] = list(
- models.order_by(F("read_bytes").desc(nulls_last=True), "saved_query_id")[offset : offset + page_size]
+ models.order_by(ordering, "saved_query_id")[offset : offset + page_size]
)
return {
"date_from": start.date(),
@@ -212,6 +223,8 @@ def get_model_usage(
start_date: date,
end_date: date,
engines: tuple[DataModelingJobEngine, ...],
+ sort_by: str = "read_bytes",
+ sort_direction: str = "desc",
) -> dict[str, object]:
return ModelUsage.get(
team_id=team_id,
@@ -221,4 +234,6 @@ def get_model_usage(
start_date=start_date,
end_date=end_date,
engines=engines,
+ sort_by=sort_by,
+ sort_direction=sort_direction,
)
diff --git a/products/data_modeling/backend/presentation/views/usage.py b/products/data_modeling/backend/presentation/views/usage.py
index 457763f519d5..2eeb45cdb6ba 100644
--- a/products/data_modeling/backend/presentation/views/usage.py
+++ b/products/data_modeling/backend/presentation/views/usage.py
@@ -20,6 +20,20 @@ class ModelUsageEngine(models.TextChoices):
MANAGED_WAREHOUSE = "managed_warehouse", "Managed warehouse"
+class ModelUsageSortBy(models.TextChoices):
+ NAME = "name", "Model"
+ READ_BYTES = "read_bytes", "Bytes read"
+ CLICKHOUSE_READ_BYTES = "clickhouse_read_bytes", "ClickHouse bytes"
+ MANAGED_WAREHOUSE_READ_BYTES = "managed_warehouse_read_bytes", "Managed warehouse bytes"
+ CPU_MICROSECONDS = "cpu_microseconds", "CPU time"
+ RUNS = "runs", "Runs"
+
+
+class ModelUsageSortDirection(models.TextChoices):
+ ASC = "asc", "Ascending"
+ DESC = "desc", "Descending"
+
+
class ModelUsageQuerySerializer(serializers.Serializer):
days = serializers.IntegerField(
required=False, min_value=1, max_value=90, help_text="Number of complete UTC days, up to 90."
@@ -34,6 +48,14 @@ class ModelUsageQuerySerializer(serializers.Serializer):
page_size = serializers.IntegerField(
default=20, min_value=1, max_value=100, help_text="Models per page, up to 100."
)
+ sort_by = serializers.ChoiceField(
+ choices=ModelUsageSortBy.choices, default=ModelUsageSortBy.READ_BYTES, help_text="Column to sort models by."
+ )
+ sort_direction = serializers.ChoiceField(
+ choices=ModelUsageSortDirection.choices,
+ default=ModelUsageSortDirection.DESC,
+ help_text="Sort direction. Unmeasured values are always last.",
+ )
engine = serializers.ChoiceField(
choices=ModelUsageEngine.choices,
default=ModelUsageEngine.CLICKHOUSE,
@@ -134,7 +156,7 @@ class ModelUsageSerializer(serializers.Serializer):
date_to = serializers.DateField(help_text="Exclusive UTC end date. Today's incomplete usage is excluded.")
overview = ModelUsageTotalsSerializer(help_text="Totals over the whole date range.")
trends = ModelUsageDaySerializer(many=True, help_text="Daily usage, attributed to when each run finished.")
- models = ModelUsageRowSerializer(many=True, help_text="Model breakdown, highest measured bytes first.")
+ models = ModelUsageRowSerializer(many=True, help_text="Model breakdown in the requested sort order.")
count = serializers.IntegerField(help_text="Total number of models in the breakdown.")
status_breakdown = ModelStatusBreakdownSerializer(
many=True, help_text="Latest run status for each model and engine in the selected complete-day range."
@@ -175,6 +197,8 @@ class ModelUsageViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
start_date=query_data["start_date"],
end_date=query_data["end_date"],
engines=engines,
+ sort_by=query_data["sort_by"],
+ sort_direction=query_data["sort_direction"],
)
).data
)
diff --git a/products/data_modeling/backend/tests/api/test_usage.py b/products/data_modeling/backend/tests/api/test_usage.py
index 6625f9b28f03..665a37794f6e 100644
--- a/products/data_modeling/backend/tests/api/test_usage.py
+++ b/products/data_modeling/backend/tests/api/test_usage.py
@@ -22,10 +22,11 @@ class TestModelUsageValidation(SimpleTestCase):
self.assertFalse(serializer.is_valid())
self.assertIn(field, serializer.errors)
- def test_rejects_unknown_engine(self) -> None:
- serializer = ModelUsageQuerySerializer(data={"engine": "duckgres"})
+ @parameterized.expand([("engine", "duckgres"), ("sort_by", "saved_query__team_id"), ("sort_direction", "up")])
+ def test_rejects_unknown_choice(self, field: str, value: str) -> None:
+ serializer = ModelUsageQuerySerializer(data={field: value})
self.assertFalse(serializer.is_valid())
- self.assertIn("engine", serializer.errors)
+ self.assertIn(field, serializer.errors)
@parameterized.expand(
[
@@ -273,6 +274,63 @@ class TestModelUsage(WarehouseAccessControlTestMixin):
self.assertEqual([data["overview"]["read_bytes"] for data in results], [2000] * 4)
self.assertEqual([data["count"] for data in results], [4] * 4)
+ def test_sorts_all_columns_before_pagination(self) -> None:
+ finished = datetime(2026, 10, 15, 20, tzinfo=UTC)
+ for index, name in enumerate(["zebra", "Alpha", "beta", ""]):
+ model = DataWarehouseSavedQuery.objects.create(team=self.team, name=name)
+ for engine, read_bytes in [
+ ("clickhouse", [10, 30, 20, None][index]),
+ ("managed_warehouse", [60, 10, 25, None][index]),
+ ]:
+ DataModelingJob.objects.create(
+ team=self.team,
+ saved_query=model,
+ engine=engine,
+ status="Completed",
+ last_run_at=finished,
+ read_bytes=read_bytes,
+ cpu_microseconds=[200, 100, 300, None][index],
+ usage_updated_at=finished if read_bytes is not None else None,
+ )
+ for _ in range(index):
+ DataModelingJob.objects.create(team=self.team, saved_query=model, status="Failed", last_run_at=finished)
+
+ ascending = {
+ "name": ["Alpha", "beta", "Deleted model", "zebra"],
+ "read_bytes": ["Alpha", "beta", "zebra", "Deleted model"],
+ "clickhouse_read_bytes": ["zebra", "beta", "Alpha", "Deleted model"],
+ "managed_warehouse_read_bytes": ["Alpha", "beta", "zebra", "Deleted model"],
+ "cpu_microseconds": ["Alpha", "zebra", "beta", "Deleted model"],
+ "runs": ["zebra", "Alpha", "beta", "Deleted model"],
+ }
+ for sort_by, names in ascending.items():
+ for direction in ["asc", "desc"]:
+ expected = (
+ names
+ if direction == "asc"
+ else (
+ list(reversed(names))
+ if sort_by in ["name", "runs"]
+ else [*reversed(names[:-1]), "Deleted model"]
+ )
+ )
+ with self.subTest(sort_by=sort_by, direction=direction):
+ results = [
+ self.client.get(
+ f"/api/projects/{self.team.id}/data_modeling_usage/",
+ {
+ "engine": "all",
+ "sort_by": sort_by,
+ "sort_direction": direction,
+ "page_size": 2,
+ "page": page,
+ },
+ ).json()
+ for page in [1, 2]
+ ]
+ self.assertEqual([row["name"] for data in results for row in data["models"]], expected)
+ self.assertEqual(results[0]["overview"], results[1]["overview"])
+
def test_custom_date_range_is_inclusive_and_excludes_today(self) -> None:
model = DataWarehouseSavedQuery.objects.create(team=self.team, name="custom_range")
for day in [9, 10, 12, 13, 16]:
diff --git a/products/data_modeling/frontend/ModelsUsageByModelTable.tsx b/products/data_modeling/frontend/ModelsUsageByModelTable.tsx
index e3ed5804d2c5..af905e4ccb4c 100644
--- a/products/data_modeling/frontend/ModelsUsageByModelTable.tsx
+++ b/products/data_modeling/frontend/ModelsUsageByModelTable.tsx
@@ -1,7 +1,11 @@
import { useActions, useValues } from 'kea'
+import { IconSort } from '@posthog/icons'
+
+import { IconArrowDown, IconArrowUp } from 'lib/lemon-ui/icons'
import { LinkPrimitive } from 'lib/lemon-ui/Link/Link'
import {
+ Button,
Card,
CardContent,
CardDescription,
@@ -30,7 +34,7 @@ import { pluralize } from 'lib/utils/strings'
import { urls } from 'scenes/urls'
import { DataModelingUsageListEngine, type ModelUsageRowApi } from './generated/api.schemas'
-import { modelsUsageLogic } from './modelsUsageLogic'
+import { modelsUsageLogic, type ModelsUsageSortBy } from './modelsUsageLogic'
import { UsageNotAvailable } from './UsageNotAvailable'
const PAGE_SIZE = 20
@@ -44,8 +48,9 @@ export function ModelsUsageByModelTable({
}: {
savedQueryIdToNodeId: Record<string, string>
}): JSX.Element | null {
- const { usage, usageLoading, page, managedWarehouseEnabled, selectedEngine } = useValues(modelsUsageLogic)
- const { setPage, openModel } = useActions(modelsUsageLogic)
+ const { usage, usageLoading, page, managedWarehouseEnabled, selectedEngine, sortBy, sortDirection } =
+ useValues(modelsUsageLogic)
+ const { setPage, openModel, sortModels } = useActions(modelsUsageLogic)
if (!usage || usage.overview.runs === 0) {
return null
@@ -58,6 +63,18 @@ export function ModelsUsageByModelTable({
const lastRow = Math.min(firstRow + usage.models.length - 1, usage.count)
const showEngineColumns = managedWarehouseEnabled && selectedEngine === DataModelingUsageListEngine.All
+ const columns: { key: ModelsUsageSortBy; label: string; className?: string }[] = [
+ { key: 'name', label: 'Model' },
+ ...(showEngineColumns
+ ? [
+ { key: 'clickhouse_read_bytes' as ModelsUsageSortBy, label: 'ClickHouse bytes' },
+ { key: 'managed_warehouse_read_bytes' as ModelsUsageSortBy, label: 'Managed warehouse bytes' },
+ ]
+ : [{ key: 'read_bytes' as ModelsUsageSortBy, label: 'Bytes read' }]),
+ { key: 'cpu_microseconds', label: 'CPU time', className: 'hidden @min-[48rem]/main-content:table-cell' },
+ { key: 'runs', label: 'Runs' },
+ ]
+
return (
<Card size="sm" className="min-w-0 gap-0">
<CardHeader className="border-b border-border pb-3">
@@ -65,26 +82,47 @@ export function ModelsUsageByModelTable({
Usage by model
</CardTitle>
<CardDescription>
- Highest bytes read first. Review refresh schedules and query filters for models with high usage.
+ Review refresh schedules and query filters for models with high usage.
</CardDescription>
</CardHeader>
<CardContent className="overflow-x-auto p-0">
<Table fullWidth aria-busy={usageLoading}>
<TableHeader>
<TableRow>
- <TableHead expand>Model</TableHead>
- {showEngineColumns ? (
- <>
- <TableHead align="right">ClickHouse bytes</TableHead>
- <TableHead align="right">Managed warehouse bytes</TableHead>
- </>
- ) : (
- <TableHead align="right">Bytes read</TableHead>
- )}
- <TableHead align="right" className="hidden @min-[48rem]/main-content:table-cell">
- CPU time
- </TableHead>
- <TableHead align="right">Runs</TableHead>
+ {columns.map((column) => (
+ <TableHead
+ key={column.key}
+ expand={column.key === 'name'}
+ align={column.key === 'name' ? 'left' : 'right'}
+ className={column.className}
+ aria-sort={
+ sortBy === column.key
+ ? sortDirection === 'asc'
+ ? 'ascending'
+ : 'descending'
+ : 'none'
+ }
+ >
+ <Button
+ size="sm"
+ className="h-auto gap-1.5 whitespace-normal"
+ disabled={usageLoading}
+ onClick={() => sortModels(column.key)}
+ data-attr={`models-usage-sort-${column.key}`}
+ >
+ <span>{column.label}</span>
+ {sortBy === column.key ? (
+ sortDirection === 'asc' ? (
+ <IconArrowUp className="size-3" />
+ ) : (
+ <IconArrowDown className="size-3" />
+ )
+ ) : (
+ <IconSort className="size-3 opacity-50" />
+ )}
+ </Button>
+ </TableHead>
+ ))}
</TableRow>
</TableHeader>
<TableBody>
@@ -92,7 +130,10 @@ export function ModelsUsageByModelTable({
const nodeId = row.saved_query_id ? savedQueryIdToNodeId[row.saved_query_id] : undefined
return (
<TableRow key={row.saved_query_id ?? 'deleted'}>
- <TableCell expand className="min-w-40 max-w-96 break-all">
+ <TableCell
+ expand
+ className="min-w-32 max-w-96 break-all @min-[48rem]/main-content:min-w-40"
+ >
{row.deleted || !nodeId ? (
<Text variant="muted" render={<span />}>
{row.name}
diff --git a/products/data_modeling/frontend/ModelsUsageTab.stories.tsx b/products/data_modeling/frontend/ModelsUsageTab.stories.tsx
index 0890b6b68133..ef370e144b6e 100644
--- a/products/data_modeling/frontend/ModelsUsageTab.stories.tsx
+++ b/products/data_modeling/frontend/ModelsUsageTab.stories.tsx
@@ -113,7 +113,7 @@ const populated: ModelUsageApi = {
}),
}
-const getUsage = ({ request }: MockResolverInfo): [number, ModelUsageApi] => {
+const getUnsortedUsage = ({ request }: MockResolverInfo): [number, ModelUsageApi] => {
const savedQueryIds = new Set(
(new URL(request.url).searchParams.get('saved_query_ids') ?? '').split(',').filter(Boolean)
)
@@ -143,6 +143,46 @@ const getUsage = ({ request }: MockResolverInfo): [number, ModelUsageApi] => {
},
]
}
+const getUsage = (info: MockResolverInfo): [number, ModelUsageApi] => {
+ const [status, usage] = getUnsortedUsage(info)
+ const params = new URL(info.request.url).searchParams
+ const sortBy = params.get('sort_by') ?? 'read_bytes'
+ const direction = params.get('sort_direction') === 'asc' ? 1 : -1
+ const value = (row: ModelUsageRowApi): number | string | null => {
+ switch (sortBy) {
+ case 'name':
+ return row.name.toLowerCase()
+ case 'clickhouse_read_bytes':
+ return row.engines.find((engine) => engine.engine === 'clickhouse')?.read_bytes ?? null
+ case 'managed_warehouse_read_bytes':
+ return row.engines.find((engine) => engine.engine === 'managed_warehouse')?.read_bytes ?? null
+ case 'cpu_microseconds':
+ return row.cpu_microseconds
+ case 'runs':
+ return row.runs
+ default:
+ return row.read_bytes
+ }
+ }
+ return [
+ status,
+ {
+ ...usage,
+ models: [...usage.models].sort((a, b) => {
+ const left = value(a)
+ const right = value(b)
+ if (left === null) {
+ return right === null ? 0 : 1
+ }
+ if (right === null) {
+ return -1
+ }
+ return (left < right ? -1 : left > right ? 1 : 0) * direction
+ }),
+ },
+ ]
+}
+
const unknown: ModelUsageTotalsApi = {
runs: 8,
measured_runs: 0,
diff --git a/products/data_modeling/frontend/generated/api.schemas.ts b/products/data_modeling/frontend/generated/api.schemas.ts
index 1bfe3a848a83..abe815bb3dd5 100644
--- a/products/data_modeling/frontend/generated/api.schemas.ts
+++ b/products/data_modeling/frontend/generated/api.schemas.ts
@@ -428,7 +428,7 @@ export interface ModelUsageApi {
overview: ModelUsageTotalsApi
/** Daily usage, attributed to when each run finished. */
trends: ModelUsageDayApi[]
- /** Model breakdown, highest measured bytes first. */
+ /** Model breakdown in the requested sort order. */
models: ModelUsageRowApi[]
/** Total number of models in the breakdown. */
count: number
@@ -535,6 +535,26 @@ export type DataModelingUsageListParams = {
* @minLength 1
*/
saved_query_ids?: string
+ /**
+ * Column to sort models by.
+ *
+ * * `name` - Model
+ * * `read_bytes` - Bytes read
+ * * `clickhouse_read_bytes` - ClickHouse bytes
+ * * `managed_warehouse_read_bytes` - Managed warehouse bytes
+ * * `cpu_microseconds` - CPU time
+ * * `runs` - Runs
+ * @minLength 1
+ */
+ sort_by?: DataModelingUsageListSortBy
+ /**
+ * Sort direction. Unmeasured values are always last.
+ *
+ * * `asc` - Ascending
+ * * `desc` - Descending
+ * @minLength 1
+ */
+ sort_direction?: DataModelingUsageListSortDirection
}
export type DataModelingUsageListEngine = (typeof DataModelingUsageListEngine)[keyof typeof DataModelingUsageListEngine]
@@ -544,3 +564,22 @@ export const DataModelingUsageListEngine = {
Clickhouse: 'clickhouse',
ManagedWarehouse: 'managed_warehouse',
} as const
+
+export type DataModelingUsageListSortBy = (typeof DataModelingUsageListSortBy)[keyof typeof DataModelingUsageListSortBy]
+
+export const DataModelingUsageListSortBy = {
+ Name: 'name',
+ ReadBytes: 'read_bytes',
+ ClickhouseReadBytes: 'clickhouse_read_bytes',
+ ManagedWarehouseReadBytes: 'managed_warehouse_read_bytes',
+ CpuMicroseconds: 'cpu_microseconds',
+ Runs: 'runs',
+} as const
+
+export type DataModelingUsageListSortDirection =
+ (typeof DataModelingUsageListSortDirection)[keyof typeof DataModelingUsageListSortDirection]
+
+export const DataModelingUsageListSortDirection = {
+ Asc: 'asc',
+ Desc: 'desc',
+} as const
diff --git a/products/data_modeling/frontend/modelsUsageLogic.test.ts b/products/data_modeling/frontend/modelsUsageLogic.test.ts
index 075ad77f4341..3f22288f04e2 100644
--- a/products/data_modeling/frontend/modelsUsageLogic.test.ts
+++ b/products/data_modeling/frontend/modelsUsageLogic.test.ts
@@ -95,6 +95,35 @@ describe('modelsUsageLogic', () => {
expect(logic.values.page).toBe(1)
})
+ it('sorts all models, resets pagination, and keeps the sort on the next page', async () => {
+ await expectLogic(logic).toDispatchActions(['loadUsageSuccess', 'loadMaterializationJobsSuccess'])
+ await expectLogic(logic, () => logic.actions.setPage(3)).toDispatchActions(['loadUsageSuccess'])
+ const jobRequestCount = jobRequests.length
+
+ await expectLogic(logic, () => logic.actions.sortModels('runs')).toDispatchActions(['loadUsageSuccess'])
+ expect(usageRequests.at(-1)?.get('page')).toBe('1')
+ expect(usageRequests.at(-1)?.get('sort_by')).toBe('runs')
+ expect(usageRequests.at(-1)?.get('sort_direction')).toBe('desc')
+ await expectLogic(logic, () => logic.actions.sortModels('runs')).toDispatchActions(['loadUsageSuccess'])
+ expect(usageRequests.at(-1)?.get('sort_direction')).toBe('asc')
+ await expectLogic(logic, () => logic.actions.setPage(2)).toDispatchActions(['loadUsageSuccess'])
+ expect(usageRequests.at(-1)?.get('page')).toBe('2')
+ expect(usageRequests.at(-1)?.get('sort_by')).toBe('runs')
+ expect(usageRequests.at(-1)?.get('sort_direction')).toBe('asc')
+ await expectLogic(logic, () => logic.actions.sortModels('name')).toDispatchActions(['loadUsageSuccess'])
+ expect(usageRequests.at(-1)?.get('sort_direction')).toBe('asc')
+ expect(jobRequests).toHaveLength(jobRequestCount)
+
+ await expectLogic(logic, () => logic.actions.sortModels('managed_warehouse_read_bytes')).toDispatchActions([
+ 'loadUsageSuccess',
+ ])
+ await expectLogic(logic, () =>
+ logic.actions.setSelectedEngine(DataModelingUsageListEngine.Clickhouse)
+ ).toDispatchActions(['loadUsageSuccess'])
+ expect(usageRequests.at(-1)?.get('sort_by')).toBe('read_bytes')
+ expect(usageRequests.at(-1)?.get('sort_direction')).toBe('desc')
+ })
+
it('builds a status breakdown by engine', async () => {
await expectLogic(logic).toDispatchActions(['loadUsageSuccess'])
diff --git a/products/data_modeling/frontend/modelsUsageLogic.ts b/products/data_modeling/frontend/modelsUsageLogic.ts
index 34f9c0f9448a..8cf0fd3e8404 100644
--- a/products/data_modeling/frontend/modelsUsageLogic.ts
+++ b/products/data_modeling/frontend/modelsUsageLogic.ts
@@ -21,7 +21,11 @@ import {
} from 'products/data_warehouse/frontend/generated/api.schemas'
import * as api from './generated/api'
-import { DataModelingUsageListEngine, type ModelUsageApi } from './generated/api.schemas'
+import {
+ DataModelingUsageListEngine,
+ type DataModelingUsageListParams,
+ type ModelUsageApi,
+} from './generated/api.schemas'
export const MATERIALIZATION_JOBS_PAGE_SIZE = 20
export const DEFAULT_MODELS_USAGE_DATE_FROM = '-30d'
@@ -107,6 +111,9 @@ function materializationJobsDateFrom(dateFrom: string, dateTo: string | null): s
return start.toISOString()
}
+export type ModelsUsageSortBy = NonNullable<DataModelingUsageListParams['sort_by']>
+export type ModelsUsageSortDirection = NonNullable<DataModelingUsageListParams['sort_direction']>
+
export interface MaterializedViewOption {
id: string
name: string
@@ -140,6 +147,8 @@ export interface modelsUsageLogicValues {
selectedJobStatus: DataModelingJobsListStatus | null
selectedMaterializedViewOptions: MaterializedViewOption[]
selectedSavedQueryIds: string[]
+ sortBy: ModelsUsageSortBy
+ sortDirection: ModelsUsageSortDirection
statusChartLabels: string[]
statusChartSeries: Series[]
usage: ModelUsageApi | null
@@ -211,6 +220,10 @@ export interface modelsUsageLogicActions {
setSelectedSavedQueryIds: (savedQueryIds: string[]) => {
savedQueryIds: string[]
}
+ sortModels: (sortBy: ModelsUsageSortBy) => {
+ sortBy: ModelsUsageSortBy
+ sortDirection: ModelsUsageSortDirection
+ }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -256,10 +269,20 @@ export const modelsUsageLogic = kea<modelsUsageLogicType>([
],
actions: [dataWarehouseViewsLogic, ['loadDataWarehouseSavedQueries']],
})),
- actions({
+ actions(({ values }) => ({
setDateRange: (dateFrom: string | null, dateTo: string | null) =>
normalizeModelsUsageDateRange(dateFrom, dateTo),
setPage: (page: number) => ({ page }),
+ sortModels: (sortBy: ModelsUsageSortBy) => ({
+ sortBy,
+ sortDirection: (sortBy === values.sortBy
+ ? values.sortDirection === 'asc'
+ ? 'desc'
+ : 'asc'
+ : sortBy === 'name'
+ ? 'asc'
+ : 'desc') as ModelsUsageSortDirection,
+ }),
setJobsPage: (page: number) => ({ page }),
setSelectedEngine: (engine: DataModelingUsageListEngine) => ({ engine }),
setSelectedJobStatus: (status: DataModelingJobsListStatus | null) => ({ status }),
@@ -267,7 +290,7 @@ export const modelsUsageLogic = kea<modelsUsageLogicType>([
openModel: true,
refresh: true,
refreshJobs: true,
- }),
+ })),
reducers({
dateFrom: [
DEFAULT_MODELS_USAGE_DATE_FROM as string,
@@ -278,11 +301,23 @@ export const modelsUsageLogic = kea<modelsUsageLogicType>([
1,
{
setPage: (_, { page }) => page,
+ sortModels: () => 1,
setDateRange: () => 1,
setSelectedEngine: () => 1,
setSelectedSavedQueryIds: () => 1,
},
],
+ sortBy: [
+ 'read_bytes' as ModelsUsageSortBy,
+ { sortModels: (_, { sortBy }) => sortBy, setSelectedEngine: () => 'read_bytes' },
+ ],
+ sortDirection: [
+ 'desc' as ModelsUsageSortDirection,
+ {
+ sortModels: (_, { sortDirection }) => sortDirection,
+ setSelectedEngine: () => 'desc',
+ },
+ ],
jobsPage: [
1,
{
@@ -323,6 +358,8 @@ export const modelsUsageLogic = kea<modelsUsageLogicType>([
: DataModelingUsageListEngine.Clickhouse,
page: values.page,
page_size: 20,
+ sort_by: values.sortBy,
+ sort_direction: values.sortDirection,
saved_query_ids: values.selectedSavedQueryIds.join(',') || undefined,
})
breakpoint()
@@ -459,6 +496,13 @@ export const modelsUsageLogic = kea<modelsUsageLogicType>([
actions.loadMaterializationJobs()
},
setPage: () => actions.loadUsage(),
+ sortModels: () => {
+ posthog.capture('models usage sort changed', {
+ sort_by: values.sortBy,
+ sort_direction: values.sortDirection,
+ })
+ actions.loadUsage()
+ },
setJobsPage: () => actions.loadMaterializationJobs(),
setSelectedEngine: () => {
actions.loadUsage()
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 189621c2a1de..025b10d8e00f 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -73094,7 +73094,7 @@ export namespace Schemas {
overview: ModelUsageTotals;
/** Daily usage, attributed to when each run finished. */
trends: ModelUsageDay[];
- /** Model breakdown, highest measured bytes first. */
+ /** Model breakdown in the requested sort order. */
models: ModelUsageRow[];
/** Total number of models in the breakdown. */
count: number;
@@ -132240,6 +132240,26 @@ export namespace Schemas {
* @minLength 1
*/
saved_query_ids?: string;
+ /**
+ * Column to sort models by.
+ *
+ * * `name` - Model
+ * * `read_bytes` - Bytes read
+ * * `clickhouse_read_bytes` - ClickHouse bytes
+ * * `managed_warehouse_read_bytes` - Managed warehouse bytes
+ * * `cpu_microseconds` - CPU time
+ * * `runs` - Runs
+ * @minLength 1
+ */
+ sort_by?: DataModelingUsageListSortBy;
+ /**
+ * Sort direction. Unmeasured values are always last.
+ *
+ * * `asc` - Ascending
+ * * `desc` - Descending
+ * @minLength 1
+ */
+ sort_direction?: DataModelingUsageListSortDirection;
};
export type DataModelingUsageListEngine = typeof DataModelingUsageListEngine[keyof typeof DataModelingUsageListEngine];
@@ -132251,6 +132271,26 @@ export namespace Schemas {
ManagedWarehouse: 'managed_warehouse',
} as const;
+ export type DataModelingUsageListSortBy = typeof DataModelingUsageListSortBy[keyof typeof DataModelingUsageListSortBy];
+
+
+ export const DataModelingUsageListSortBy = {
+ Name: 'name',
+ ReadBytes: 'read_bytes',
+ ClickhouseReadBytes: 'clickhouse_read_bytes',
+ ManagedWarehouseReadBytes: 'managed_warehouse_read_bytes',
+ CpuMicroseconds: 'cpu_microseconds',
+ Runs: 'runs',
+ } as const;
+
+ export type DataModelingUsageListSortDirection = typeof DataModelingUsageListSortDirection[keyof typeof DataModelingUsageListSortDirection];
+
+
+ export const DataModelingUsageListSortDirection = {
+ Asc: 'asc',
+ Desc: 'desc',
+ } as const;
+
export type DataQualityChecksListParams = {
/**
* Only the checks that make this assertion. See /check_types/.