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

Code before:
    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"},
                    "",
                    {"rate": "percent"},
                ),
                panels.subheader("CPU Usage By Relation"),
                panels.timeseries_percentage(
                    "CPU Usage Per Streaming Job",
                    "The figure shows the CPU usage of each streaming job",
                    [
                        panels.target(
                            f"label_replace("
                            f"({_sum_fragment_metric_by_mv(poll_duration_expr)}"
                            f"/ 1000000000), "
                            f"'id', '$1', 'materialized_view_id', '(.*)'"
                            f") * on(id) group_left(name, type) {metric('relation_info')}",

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

Code after:
        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)"
    )
    poll_duration_expr = (
        f"{poll_duration_rate_expr} / on(fragment_id) {actor_count_expr}"
    )
    return [
        outer_panels.row_collapsed(
            "Streaming Relation Metrics",
            [
                panels.subheader("Overview"),
                panels.table_info(
                    "Top Relations by Busy Rate",
                    "Top 10 relations with the highest peak busy rate (%).",
                    [
                        panels.table_target(