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

Code before:
        f"max_over_time(({normalized_expr})[$__range:$__interval])"
    )

@section
def _(outer_panels: Panels):
    # The actor_id can be masked due to metrics level settings.
    # We use this filter to suppress the actor-level panels if applicable.
    actor_level_filter = "actor_id!=''"
    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}"
    )
    idle_duration_rate_expr = (
        f"sum(rate({metric('stream_actor_idle_duration')}[$__rate_interval])) by (fragment_id)"
    )
    idle_duration_expr = (
        f"{idle_duration_rate_expr} / on(fragment_id) {actor_count_expr}"
    )
    scheduled_duration_rate_expr = (
        f"sum(rate({metric('stream_actor_scheduled_duration')}[$__rate_interval])) by (fragment_id)"
    )
    scheduled_duration_expr = (
        f"{scheduled_duration_rate_expr} / on(fragment_id) {actor_count_expr}"
    )
    return [
        outer_panels.row_collapsed(
            "Streaming Fragments",
            [
                panels.subheader("Overview"),
                panels.table_info(
                    "Top Fragments by Busy Rate",
                    "Top 10 fragments with the highest peak busy rate (%).",
                    [
                        panels.table_target(
                            topk_percent_expr(
                                _fragment_peak_rate_with_id_at_step(
                                    _actor_busy_rate_expr("$__rate_interval")
                                )
                            )
                        )
                    ],
                    ["id", "Value"],
                    dict.fromkeys(["Time", "fragment_id"], True),
                    {"Value": "rate"},
                    {"rate": "percent"},

Review comment (suggestion):
Review note: these are moved out because they are reused by both the `topk` panels and the `trend` panels.

Code after:
def _(outer_panels: Panels):
    # The actor_id can be masked due to metrics level settings.
    # We use this filter to suppress the actor-level panels if applicable.
    actor_level_filter = "actor_id!=''"
    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} / 1000000000"
    )
    idle_duration_rate_expr = (
        f"sum(rate({metric('stream_actor_idle_duration')}[$__rate_interval])) by (fragment_id)"
    )
    idle_duration_expr = (
        f"{idle_duration_rate_expr} / on(fragment_id) {actor_count_expr} / 1000000000"
    )
    scheduled_duration_rate_expr = (
        f"sum(rate({metric('stream_actor_scheduled_duration')}[$__rate_interval])) by (fragment_id)"
    )
    scheduled_duration_expr = (
        f"{scheduled_duration_rate_expr} / on(fragment_id) {actor_count_expr} / 1000000000"
    )
    return [
        outer_panels.row_collapsed(
            "Streaming Fragments",
            [
                panels.subheader("Overview"),
                panels.table_info(
                    "Top Fragments by Busy Rate",
                    "Top 10 fragments with the highest peak busy rate (%).",
                    [
                        panels.table_target(
                            topk_percent_expr(
                                _fragment_peak_rate_with_id_at_step(
                                    _actor_busy_rate_expr("$__rate_interval")
                                )
                            )
                        )
                    ],
                    ["id", "Value"],
                    dict.fromkeys(["Time", "fragment_id"], True),
                    {"Value": "rate"},
                    {"rate": "percent"},
                ),
                panels.table_info(
                    "Top Fragments by CPU Rate",
                    "Top 10 fragments with the highest peak CPU rate (%).",