Log Ingestion and Query
Problem statement
Implement a simplified logging system that supports log ingestion and conditional queries.
Each operation is one of the following strings, with single spaces between its parts:
ADD timestamp service level message: append one log entry. The message is everything after the level and may contain spaces.QUERY startTs endTs service level keyword: count the logs added so far that match. Each ofservice,levelandkeywordmay be*, which matches every log.
A log matches a query if all of these hold:
startTs ≤ timestamp ≤ endTs.- The log's service is exactly the query's
service, unless the filter is*. - The log's level is exactly the query's
level, unless the filter is*. - The query's
keywordappears as a contiguous substring of the log's message (not only as a whole word), unless the filter is*.
Process all operations in order and return an array with the count from each QUERY operation, in the order the queries appear.
Function
runLogIngestionQueries(operations: String[]) → int[] Complete the function runLogIngestionQueries in the editor below.
runLogIngestionQueries has the following parameter:
String[] operations: the operations to execute in order
Examples
Example 1
operations = ["ADD 1 api INFO hello world", "ADD 2 api ERROR something failed", "ADD 3 web INFO user login", "QUERY 1 3 * * *", "QUERY 1 2 api * *", "QUERY 2 3 api ERROR failed"]return = [3, 2, 1]The three queries count all logs, then all api logs in the first two timestamps, then only the single error log whose message contains failed.
Example 2
operations = ["ADD 1 api INFO login ok", "ADD 2 api INFO logout ok", "ADD 3 api INFO login failed", "QUERY 1 3 api INFO login", "QUERY 1 3 api INFO ok", "QUERY 1 2 api INFO login", "QUERY 3 3 api INFO login"]return = [2, 2, 1, 1]Substring matching on the message field allows both successful and failed login events to match the keyword login.
Constraints
1 ≤ operations.length ≤ 2 * 105- Each operation is
ADD timestamp service level messageorQUERY startTs endTs service level keyword, with single spaces between its parts. - At least one operation is a
QUERY. 0 ≤ timestamp ≤ 109and0 ≤ startTs ≤ endTs ≤ 109, written as plain decimal integers.- A service name has
1to10characters, each a lowercase English letter or a digit. - A level has
1to10uppercase English letters. - A message has
1to20characters: lowercase English letters, digits and single spaces, with no space at the start or end. - In a query,
serviceandlevelare*or follow the rules above, andkeywordis*or1to10lowercase English letters or digits.