Alert After Repeated Column Threshold Breaches
Problem statement
Implement the core of a command-line monitoring script. The function receives the command-line token array args and all standard-input text as stdinText. Parse exactly three required flag-value pairs, in any order: --col C, --threshold T, and --count X. Each flag appears exactly once. C is a one-based column number, T is an integer threshold, and X is the number of consecutive breaches required for an alert.
Read standard input line by line and split each line on whitespace. If a line has fewer than C fields or its selected field is not a nonnegative decimal integer, skip the line without changing the current streak. This naturally skips text headers in a vmstat-style stream. For every valid selected value, a strict breach (value > T) extends the streak by one, while a value at or below T resets the streak to zero.
Return "WARNING" as soon as the streak reaches X, or "" if the stream ends first. Return "ERROR" for missing, repeated, or unknown flags, missing flag values, nondecimal flag values, or out-of-range flag values. This callable interface represents the parsing and standard-input behavior of a script main function.
Function
runThresholdAlert(args: String[], stdinText: String) → StringExamples
Example 1
args = ["--col", "4", "--threshold", "100", "--count", "2"]stdinText = "r b swpd free\n1 0 3 99\n2 0 3 101\n3 0 3 150\n"return = "WARNING"The header is skipped, 99 starts with a zero-length streak, and the consecutive values 101 and 150 form the two strict breaches required for the warning.
Example 2
args = ["--count", "2", "--threshold", "10", "--col", "2"]stdinText = "name value\na 11\nb 3\nc 12\nd 13\n"return = "WARNING"Flag order may vary. The value 3 resets the first one-row streak; the later values 12 and 13 are consecutive breaches.
Example 3
args = ["--col", "2", "--threshold", "8", "--count", "2"]stdinText = "r b\na 9\nb bad\nc 10\n"return = "WARNING"The text header and the nonnumeric selected field are skipped rather than treated as observations. Therefore 9 and 10 are consecutive valid breaches.
Example 4
args = ["--col", "0", "--threshold", "8", "--count", "1"]stdinText = "a 9\n"return = "ERROR"Column zero violates the one-based column range.
Constraints
1 <= C <= 20,0 <= T <= 10^9, and1 <= X <= 10^4.0 <= stdinText.length <= 100000; there are at most10000lines and at most20whitespace-separated fields per line.- A selected field is valid only if all its characters are ASCII digits and its value is at most
10^9. Other selected fields are skipped. - Input text is ASCII; lines use LF or CRLF, and fields are separated by ASCII spaces or tabs.
- Flag values must be nonnegative ASCII decimal integers with no signs or spaces inside them. Leading zeroes are allowed.