Example #1
0
def track_metric(name: str, value: Any, label: dict = None) -> None:
    # store the metric
    if value:
        value, valid = prepare_and_validate_value(value)
        if not valid:
            click.echo(
                f"AskAnna cannot store this datatype. Metric not stored for {name}, {value}, {label}.",
                err=True
            )
            return
    # add value to track queue
    if value:
        datapair = MetricDataPair(name=name, value=value, dtype=translate_dtype(value))
    else:
        datapair = MetricDataPair(name=name, value=None, dtype="tag")
    labels = labels_to_type(label, labelclass=MetricLabel)

    metric = Metric(metric=datapair, label=labels)
    mc.add(metric)
Example #2
0
def track_variable(name: str, value: Any, label: dict = None) -> None:
    # store the variable
    if value:
        value, valid = prepare_and_validate_value(value)
        if not valid:
            click.echo(
                f"AskAnna cannot store this datatype. Variable not stored for {name}, {value}, {label}.",
                err=True
            )
            return

    # we don't want to store secrets, so filter them and replace value if it is sensitive information
    variable_name = name.upper()
    is_masked = any(
        [
            "KEY" in variable_name,
            "TOKEN" in variable_name,
            "SECRET" in variable_name,
            "PASSWORD" in variable_name,
        ]
    )
    if is_masked:
        value = "***masked***"

    # add value to track queue
    if value:
        datapair = VariableDataPair(name=name, value=value, dtype=translate_dtype(value))
    else:
        datapair = VariableDataPair(name=name, value=None, dtype="tag")
    labels = [
        VariableLabel(name="source", value="run", dtype="string")
    ] + labels_to_type(label, labelclass=VariableLabel)

    if is_masked:
        labels.append(
            VariableLabel(**{"name": "is_masked", "value": None, "dtype": "tag"})
        )

    variable = VariableTracked(variable=datapair, label=labels)
    vc.add(variable)
Example #3
0
 def test_prepare_and_validate_value_range(self):
     value, valid = prepare_and_validate_value(range(0, 3))
     self.assertEqual(value, [0, 1, 2])
     self.assertTrue(valid)
Example #4
0
 def test_prepare_and_validate_value_numpy_float(self):
     value, valid = prepare_and_validate_value(np.float(5.21))
     self.assertEqual(value, 5.21)
     self.assertTrue(valid)
Example #5
0
 def test_prepare_and_validate_value_string(self):
     value, valid = prepare_and_validate_value("some text")
     self.assertEqual(value, "some text")
     self.assertTrue(valid)