Repository: risingwavelabs/risingwave
File: grafana/dashboard/dev/streaming_relations.py (Python)

Code before:
        "name {{name}} id {{id}} type {{type}} fragment {{fragment_id}}",
    )


def _sum_fragment_metric_by_mv(expr: str) -> str:
    return (
        f"sum(({expr})"
        f"* on(fragment_id) group_left(materialized_view_id)"
        f"max by (fragment_id, materialized_view_id) ({metric('table_info')}))"
        f"by (materialized_view_id)"
    )

@section
def _(outer_panels: Panels):
    panels = outer_panels.sub_panel()
    poll_duration_expr = (
        f"sum(rate({metric('stream_actor_poll_duration')}[$__rate_interval])) by (fragment_id) "
        f"/ on(fragment_id) sum({metric('stream_actor_count')}) by (fragment_id)"
    )
    return [
        outer_panels.row_collapsed(
            "Streaming Relation Metrics",
            [
                panels.subheader("Overview"),
                panels.table_info(
                    "Top Relations by Busy Time",
                    "Top 10 relations with the highest busy time rate (%).",
                    [
                        panels.table_target(
                            f"topk(10, ({_relation_busy_rate_expr('$__rate_interval')}) * 100)"
                        )
                    ],
                    ["name", "id", "type", "Value"],
                    dict.fromkeys(["Time"], True),
                    {"Value": "rate"},
                    "",
                    {"rate": "percent"},
                ),
                panels.table_info(
                    "Top Relations by CPU Time",
                    "Top 10 relations with the highest CPU time rate (%).",
                    [
                        panels.table_target(
                            _relation_topk_percent_expr(
                                f"{_sum_fragment_metric_by_mv(poll_duration_expr)} / 1000000000"
                            )
                        )
                    ],
                    ["name", "id", "type", "Value"],
                    dict.fromkeys(["Time"], True),
                    {"Value": "rate"},

Review comment (suggestion):
The table title "Top Relations by Busy Time" is misleading because it measures busy time rate (as a percentage), not absolute busy time. Consider renaming to "Top Relations by Busy Rate" to match the description and be consistent with the fragment tables.
```suggestion
                    "Top Relations by Busy Rate",
```

Code after:

def _relation_busy_peak_topk_expr(rate_interval: str) -> str:
    """Compute top-k peak busy rate for relations over the dashboard range."""
    per_mv_busy_expr = _relation_busy_rate_expr_by_mv(rate_interval)
    per_mv_busy_with_id_expr = relabel_materialized_view_id_as_id(per_mv_busy_expr)
    with_metadata_expr = _relation_metric_with_metadata(per_mv_busy_with_id_expr)
    peak_expr = f"max_over_time(({with_metadata_expr})[$__range:$__interval])"
    return topk_percent_expr(peak_expr)

def _relation_cpu_peak_topk_expr(
    poll_duration_rate_expr: str, actor_count_expr: str
) -> str:
    """Compute top-k peak CPU rate for relations, normalized by actor count."""
    per_fragment_rate_expr = (
        f"({poll_duration_rate_expr}) / on(fragment_id) {actor_count_expr}"
    )
    per_mv_rate_expr = _sum_fragment_metric_by_mv(per_fragment_rate_expr)
    per_mv_rate_expr = f"({per_mv_rate_expr}) / 1000000000"
    per_mv_rate_with_id_expr = relabel_materialized_view_id_as_id(per_mv_rate_expr)
    with_metadata_expr = (
        f"({per_mv_rate_with_id_expr}) * on (id) group_left (name, type)"
        f" {metric('relation_info')}"
    )
    peak_expr = (
        f"max_over_time(({with_metadata_expr})[$__range:$__interval])"
    )
    return topk_percent_expr(peak_expr)

def _relation_busy_rate_target(panels: Panels, rate_interval: str):
    return panels.target(
        _relation_busy_rate_expr(rate_interval),
        "name {{name}} id {{id}} type {{type}} fragment {{fragment_id}}",
    )


def _sum_fragment_metric_by_mv(expr: str) -> str:
    return (
        f"sum(({expr})"
        f"* on(fragment_id) group_left(materialized_view_id)"
        f"max by (fragment_id, materialized_view_id) ({metric('table_info')}))"
        f"by (materialized_view_id)"
    )

@section
def _(outer_panels: Panels):
    panels = outer_panels.sub_panel()
    actor_count_expr = (
        f"clamp_min(sum({metric('stream_actor_count')}) by (fragment_id), 1)"
    )
    poll_duration_rate_expr = (
        f"sum(rate({metric('stream_actor_poll_duration')}[$__rate_interval])) by (fragment_id)"