import statistics


def get_per_second_metric(metric_name, func, stats):
    """ Gets a per second metric out of the docker stats list,
    per second valuates the time difference in time between every two samples
    :param metric_name: The metric name
    :param func: A function which gets the value of the sample out of the stat object
    :param stats: The docker stats list
    :return: Ai metric
    """
    assert metric_name is not None, "metric_name shoud not be None"
    assert func is not None, "func should not be None"
    assert stats is not None and len(stats)>1, "stats should have more than 1 samples in it"
    stats2 = stats[1:]
    stats1 = stats[:len(stats) - 1]
    samples = [(func(s2)-func(s1)) / (time2 - time1) for (time2, s2), (time1, s1) in list(zip(stats2, stats1))]
    return {'name':metric_name,
            'value':statistics.mean(samples),
            'count':len(samples),
            'min':min(samples),
            'max':max(samples),
            'std':statistics.stdev(samples) if len(samples) > 1 else None}