Access metric values in a custom python check

marinos_r
marinos_r Registered Posts: 5 ✭✭✭✭
edited July 16 in Using Dataiku

Hi everyone, this is my first community post so please let me know if I need to change anything about the formatting.

I am trying to set up a check on a dataset that has to do with the modified date of the underlying file. I have successfully set up a metric to get the information of the file using the following code

# Define here a function that returns the metric.
import datetime
def process(dataset, partition_id):
    meta = dataset.get_files_info()
    for filepath in meta["globalPaths"]:
        file_size = filepath["size"]
        file_date = filepath["lastModified"]
    
    date = datetime.datetime.fromtimestamp(file_date / 1e3)
    return {'file_size' : file_size, 'file_date' : file_date, 'date_mod' : date}

Next, I want to be able to access the variables file_date and date_mod on the checks python recipe. The built in comment in the code editor says that last_values is a dict but I'm not sure what the format looks like or how to access the contents of the dict.

When I try to access the values by doing following this link: https://doc.dataiku.com/dss/latest/python-api/metrics.html

def process(last_values, dataset, partition_id):
    # last_values is a dict of the last values of the metrics,
    # with the values as a dataiku.metrics.MetricDataPoint.
    # dataset is a dataiku.Dataset object
    
    vals = last_values.get_value()

, I get an error "PackagedPythonException : <type 'exceptions.AttributeError'> : 'dict' object has no attribute 'get_value' "

How can I get the values of the metrics that I have computer so that I can assign them to custom variables in the custom checks editor?

Best Answer

  • marinos_r
    marinos_r Registered Posts: 5 ✭✭✭✭
    Answer ✓

    I used

    var = last_values['python:>metric_data<:>metric_name<'].get_value()

    or in my case

    date_str = last_values['python:file_date:meta_info'].get_value()

    and that seems to work in accessing the data from my custom metric.

Setup Info
    Tags
      Help me…