code: def _report_intermediates_and_final(
      query_result: list[Any],
      metric: str,
      query: str,
      scale: float = 1.0,
  ) -> tuple[float, list[float]]:
      """Convert benchmark results from database to results reported to NNI.
  
      Utility function for :meth:`BenchmarkEvaluator.evaluate`.
      """
      if not query_result:
          raise ValueError(
              "Invalid query. Results from benchmark is empty: " + query
          )
      if len(query_result) > 1:
          query_result = random.choice(query_result)
      else:
          query_result = query_result[0]
      query_dict = cast(dict, query_result)
      for i in query_dict.get("intermediates", []):
          if i[metric] is not None:
              nni.report_intermediate_result(i[metric] * scale)
      nni.report_final_result(query_dict[metric] * scale)
      return query_dict[metric]
  
imports: from __future__ import annotations
  import itertools
  import logging
  import random
  import warnings
  from typing import Any, cast
  import nni
  from nni.mutable import Sample, Mutable, LabeledMutable, Categorical, CategoricalMultiple, label_scope
  from nni.nas.evaluator import Evaluator
  from nni.nas.space import ExecutableModelSpace
  from .space import SlimBenchmarkSpace
label_desc: Use image node_id 1 for calling a global function with example usage: _report_intermediates_and_final(query_result, metric, query, scale) and returns: query_dict