简体   繁体   中英

Alerter monitoring tool : How to find if a number greater than average of the consecutive subsets of windowsize

The Alerter is a simple monitoring tool, intended to help detect increases in response time for some process. It does that by computing a few statistics about the process across a 'window' of a certain number of runs, and alerting (returning true) if certain thresholds are met.

It takes the following parameters:

  • inputs: A list of integer times for the process. This list may be very long
  • window size: how many runs long a window is, as an integer
  • allowedIncrease: how far over 'average' a window or value is allowed to be, as a percent.

This is represented as a decimal value based on one, so a 50% allowable increase would be represented as 1.5

Your Alerter should return true if either of the following conditions are met:

  • Any value is more than the allowed increase above the window average in ALL windows in which it appears. For example: alert({1, 2, 100, 2, 2}, 3, 1.5) should alert: the value 100 appears in three windows, and in all cases is more than 50% over the average value alert({1, 2, 4, 2, 2}, 3, 2) should not alert: the largest outlier is 4, and that value appears in a window with average value 2.6, less than 100% of that average

  • Any window's average is more than the acceptable increase over a previous window's average value For example: alert({1,2,100,2,2}, 2, 2.5) should alert: Even though no individual value causes an alert, there is a window with average 1.5 and a later window with an average more than 2.5 times larger

Otherwise, you should return false.

This is my solution, but it is not working.

from decimal import *

def alert(inputs, windowSize, allowedIncrease):
    average = dict()
    increase_average = dict()
    val_list = list()

    ## calculating the average and appending to the dictionary
    for i in range(0, len(inputs)):
        val = sum(inputs[i:i + windowSize])
        avg = Decimal(val) / windowSize
        if i == len(inputs) - windowSize + 1:
            break
        else:
            for j in range(0, windowSize-1):
                try:
                    average[inputs[i + j]] = avg
                except:
                    average[inputs[i + j]].append(avg)

    increase = Decimal(allowedIncrease - 1)

    ##appending increase in the average
    for key, values in average.items():
        data = (Decimal(values) * increase) + Decimal(values)
        try:
            increase_average[key] = data
        except:
            increase_average[key].append(data)

    ##checking if all the average value is  greater than key
    for key, value in increase_average.items():
        if key > value:
            return True

    ##checking if any average value greater than incease*previous average value
    for (k, v) in average.items():
        val_list.append(v)

    for h in range(len(val_list)):
        if any(val_list >= (Decimal(x * increase) + Decimal(x)) for x in val_list[:h]):
            return True

    return False

if __name__ == "__main__":
    inputs = [1, 2, 4, 2, 2]
    windowSize = 3
    allowedIncrease = 2
    res = alert(inputs, windowSize, allowedIncrease)
    print res

There would be a TypeError in the following line:

if any(val_list >= (Decimal(x * increase) + Decimal(x)) for x in val_list[:h]:

You need to change val_list to val_list[k]

change here, and it will start working.

for h in range(len(val_list)):
    if any(val_list[h] >= (Decimal(x * increase) + Decimal(x)) for x in ..

This will work.

def alerter(l,w,inc):
    dic = {}
    lis = []
    for i in range(0,len(l)-w+1):
        avg = sum(l[i:i+w])/w
        lis.append(avg)
        for j in range(0,w):
            if l[i+j] in dic.keys():
                dic[l[i+j]].append(avg)
            else:
                dic[l[i+j]] = [avg]

    for i in range(len(lis)-1):
        if lis[i]*inc < lis[i+1]:
            return True

    for k,v in dic.items():
        if min(v)*inc < k:
            return True
    return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM