Log Ingestion and Query
DatadogOnsiteMedium
A logging service stores log lines and answers count queries. Implement a class LogStore that starts empty and supports adding logs and querying counts of stored logs.
Part 1: Add logs and count matches
You are given a logging service that stores log lines and answers count queries. Implement a class LogStore with two methods.
add(timestamp, service, level, message) stores one log. service and level are single words, and message is text that may contain spaces.
query(start, end, service, level, keyword) returns how many stored logs satisfy all of these conditions:
start <= timestamp <= end- the log's
serviceequalsservice - the log's
levelequalslevel keywordis a substring of the log'smessage
Matching is case-sensitive. The value * for service, level or keyword means that condition is not applied.
Logs can be added with timestamps in any order, and query may be called between adds. A log with the same fields as an earlier one counts separately.
Start from this skeleton:
python class LogStore: def __init__(self) -> None: pass
def add(self, timestamp: int, service: str, level: str, message: str) -> None: raise NotImplementedError
def query(self, start: int, end: int, service: str, level: str, keyword: str) -> int: raise NotImplementedError
- Example 1
- init
[]- 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"]]]- Output
[null,null,null,3,2,1]
Why: The three logs sit in times 1 to 3, so the unfiltered query counts 3. The second query keeps the two
apilogs. The third needs serviceapi, levelERRORandfailedin the message, which only the log at time 2 satisfies.
Constraints
- 1 <= total number of
addandquerycalls <= 2 * 10^5 0 <= timestamp <= 10^90 <= start <= end <= 10^9serviceandlevelof an added log are non-empty, contain no spaces, and are not*1 <= len(message) <= 200keywordis*or1 <= len(keyword) <= 200