Average latency for the paths

I am using Grafana v10.4.1 (d94d597d84) set up on k8s.
I am trying to get the average latency for the paths. But the latency values are calculated wrong.
I am using bar gauge visualization. I’m using Loki as a datasource.
I have the following query -

sort_desc(topk(20, avg by(path_endpoint) (rate({namespace=~"$namespace", stream=~"$stream", container=~"$container"} | json |= $query| path_endpoint =~.+| path_endpoint !~/api/internal.+| path_endpoint !~/ws | __error__=`` | unwrap latency [$__range]))))

Does anybody have an idea what could be wrong with this query?