Coverage Report

Created: 2024-12-03 16:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/build/source/nativelink-scheduler/src/platform_property_manager.rs
Line
Count
Source
1
// Copyright 2024 The NativeLink Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
use std::collections::HashMap;
16
17
use nativelink_config::schedulers::PropertyType;
18
use nativelink_error::{make_input_err, Code, Error, ResultExt};
19
use nativelink_metric::{
20
    group, MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
21
};
22
use nativelink_util::platform_properties::{PlatformProperties, PlatformPropertyValue};
23
24
/// Helps manage known properties and conversion into `PlatformPropertyValue`.
25
pub struct PlatformPropertyManager {
26
    known_properties: HashMap<String, PropertyType>,
27
}
28
29
// TODO(allada) We cannot use the `MetricsComponent` trait here because
30
// the `PropertyType` lives in the `nativelink-config` crate which is not
31
// a dependency of the `nativelink-metric-collector` crate.
32
impl MetricsComponent for PlatformPropertyManager {
33
0
    fn publish(
34
0
        &self,
35
0
        _kind: MetricKind,
36
0
        field_metadata: MetricFieldData,
37
0
    ) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
38
0
        let _enter = group!("known_properties").entered();
39
0
        for (k, v) in &self.known_properties {
40
0
            group!(k).in_scope(|| {
41
0
                format!("{v:?}").publish(MetricKind::String, field_metadata.clone())
42
0
            })?;
43
        }
44
0
        Ok(MetricPublishKnownKindData::Component)
45
0
    }
46
}
47
48
impl PlatformPropertyManager {
49
    #[must_use]
50
27
    pub const fn new(known_properties: HashMap<String, PropertyType>) -> Self {
51
27
        Self { known_properties }
52
27
    }
53
54
    /// Returns the `known_properties` map.
55
    #[must_use]
56
0
    pub const fn get_known_properties(&self) -> &HashMap<String, PropertyType> {
57
0
        &self.known_properties
58
0
    }
59
60
    /// Given a map of key-value pairs, returns a map of `PlatformPropertyValue` based on the
61
    /// configuration passed into the `PlatformPropertyManager` constructor.
62
43
    pub fn make_platform_properties(
63
43
        &self,
64
43
        properties: HashMap<String, String>,
65
43
    ) -> Result<PlatformProperties, Error> {
66
43
        let mut platform_properties = HashMap::with_capacity(properties.len());
67
54
        for (
key, value11
) in properties {
68
11
            let prop_value = self.make_prop_value(&key, &value)
?0
;
69
11
            platform_properties.insert(key, prop_value);
70
        }
71
43
        Ok(PlatformProperties::new(platform_properties))
72
43
    }
73
74
    /// Given a specific key and value, returns the translated `PlatformPropertyValue`. This will
75
    /// automatically convert any strings to the type-value pairs of `PlatformPropertyValue` based
76
    /// on the configuration passed into the `PlatformPropertyManager` constructor.
77
11
    pub fn make_prop_value(&self, key: &str, value: &str) -> Result<PlatformPropertyValue, Error> {
78
11
        if let Some(prop_type) = self.known_properties.get(key) {
  Branch (78:16): [True: 11, False: 0]
  Branch (78:16): [Folded - Ignored]
79
11
            return match prop_type {
80
                PropertyType::minimum => Ok(PlatformPropertyValue::Minimum(
81
9
                    value.parse::<u64>().err_tip_with_code(|e| {
82
0
                        (
83
0
                            Code::InvalidArgument,
84
0
                            format!("Cannot convert to platform property to u64: {value} - {e}"),
85
0
                        )
86
9
                    })
?0
,
87
                )),
88
2
                PropertyType::exact => Ok(PlatformPropertyValue::Exact(value.to_string())),
89
0
                PropertyType::priority => Ok(PlatformPropertyValue::Priority(value.to_string())),
90
            };
91
0
        }
92
0
        Err(make_input_err!("Unknown platform property '{}'", key))
93
11
    }
94
}