
At Datadog, performance counters are emitted as log lines in the format name=value. Write a function that parses a list of counter strings and flags anomalous counters based on recent history.
A line is valid only if it contains exactly one = and the value is a valid integer. Ignore invalid lines. For each counter name, keep the sequence of parsed values in input order. A counter value is considered anomalous if it is at least threshold greater than the average of the previous window_size valid values for the same counter. If fewer than window_size previous values exist for that counter, do not evaluate anomaly status for that occurrence.
Return a list of anomalies in the order they are detected. Each anomaly should be represented as [name, index, value], where index is the original position in the input list.
lines as a list of strings, window_size as an integer, threshold as an integer[counter_name, original_index, counter_value]Example 1
lines = ["cpu=10", "cpu=12", "cpu=11", "cpu=30"], window_size = 3, threshold = 10[["cpu", 3, 30]]cpu values average to 11, and 30 >= 11 + 10.Example 2
lines = ["mem=5", "bad", "mem=7", "mem=x", "mem=20"], window_size = 2, threshold = 10[]mem values are 5, 7, and 20. The average of the previous 2 values is 6, so 20 is not at least 16.1 <= len(lines) <= 10^51 <= window_size <= 10^40 <= threshold <= 10^9= in valid lines[-10^9, 10^9]lines = ["cpu=10", "cpu=12", "cpu=11", "cpu=30"], window_size = 3, threshold = 10Output[["cpu", 3, 30]]WhyThe previous three valid `cpu` values are 10, 12, and 11, with average 11. Since 30 is at least 21, it is flagged.lines = ["mem=5", "bad", "mem=7", "mem=x", "mem=20"], window_size = 2, threshold = 10Output[]WhyInvalid lines are ignored. The previous two valid `mem` values are 5 and 7, average 6, so 20 is not at least 16.lines = ["cpu=1", "mem=100", "cpu=2", "mem=105", "cpu=20", "mem=130"], window_size = 2, threshold = 10Output[["cpu", 4, 20], ["mem", 5, 130]]WhyEach counter is tracked independently. `cpu=20` is compared to average of 1 and 2, and `mem=130` is compared to average of 100 and 105.1 <= len(lines) <= 10^51 <= window_size <= 10^40 <= threshold <= 10^9Each valid line has exactly one '=' and a non-empty counter nameParsed integer values are in the range [-10^9, 10^9]def flag_counter_anomalies(lines, window_size, threshold):