Could I get a little Rust help please? I’m trying ...
# development
h
Could I get a little Rust help please? I’m trying to get
scheduler.metrics()
to return
Dict[str, int]
, not its current
Dict[bytes, int]
. I could simply coerce the keys in Python code, but I think it’s more idiomatic for us to fix it on the Rust side this is
<http://scheduler.rs|scheduler.rs>
Copy code
///
  /// Return Scheduler and per-Session metrics.
  ///
  pub fn metrics(&self, session: &Session) -> HashMap<&str, i64> {
    let mut m = HashMap::new();
    m.insert(
      "affected_file_count",
      self
        .core
        .graph
        .reachable_digest_count(&session.root_nodes()) as i64,
    );
    m.insert("preceding_graph_size", session.preceding_graph_size as i64);
    m.insert("resulting_graph_size", self.core.graph.len() as i64);
    m
  }
and
engine/src/lib.rs
Copy code
///
/// Returns a Handle representing a tuple of tuples of metric name string and metric value int.
///
#[no_mangle]
pub extern "C" fn scheduler_metrics(
  scheduler_ptr: *mut Scheduler,
  session_ptr: *mut Session,
) -> Handle {
  with_scheduler(scheduler_ptr, |scheduler| {
    with_session(session_ptr, |session| {
      let values = scheduler
        .metrics(session)
        .into_iter()
        .map(|(metric, value)| {
          externs::store_tuple(&[
            externs::store_bytes(metric.as_bytes()),
            externs::store_i64(value),
          ])
        }).collect::<Vec<_>>();
      externs::store_tuple(&values).into()
    })
  })
}
@average-vr-56795’s patch back in July to allow returning UTF-8 might be relevant? https://github.com/pantsbuild/pants/pull/6108