-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (64 loc) · 2.39 KB
/
utils.py
File metadata and controls
86 lines (64 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import re
from qdrant_client import QdrantClient, models
import pandas as pd
SEED = 42
def to_tradional(template: str):
pattern = r"{(.*?)}"
replaced_string = re.sub(pattern, r"<*>", template)
return replaced_string
def traditional_to_regex(template: str):
template = re.escape(template)
template = template.replace(r"<\*>", r"(.*?)")
return template
def validate_template(log_message, template):
template = to_tradional(template)
template = traditional_to_regex(template)
regex = re.compile(template)
return regex.match(log_message) is not None
def init_qdrant(
client: QdrantClient, ndim, collection_name="logs", from_scratch=True
) -> bool:
existance = client.collection_exists(collection_name)
if not existance:
operation_info = client.create_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(
size=ndim, distance=models.Distance.COSINE
),
)
return operation_info
if from_scratch and existance:
client.delete_collection(collection_name)
operation_info = client.create_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(
size=ndim, distance=models.Distance.COSINE
),
)
return operation_info
return True
def records_to_df(records: list[models.Record]):
df = pd.DataFrame([{**record.payload, "id": record.id} for record in records])
df = df.set_index("id")
return df
def load_project(project: str):
df = pd.read_csv(
f"logs/{project}/{project}_2k.log_structured_corrected.csv",
index_col="LineId",
usecols=["LineId", "Content", "EventTemplate"],
)
df = df.rename(columns={"Content": "log", "EventTemplate": "template"})
df["seen"] = False
df["semantic_template"] = None
df["log"] = df["log"].apply(change_brackets)
df["template"] = df["template"].apply(change_brackets)
# Replace whitespace with a single space
df["log"] = df["log"].str.replace(r"\s+", " ", regex=True)
df["template"] = df["template"].str.replace(r"\s+", " ", regex=True)
# strip
df["log"] = df["log"].str.strip()
df["template"] = df["template"].str.strip()
return df
def change_brackets(text):
# '{' -> '(' and '}' -> ')'
return text.replace("{", "(").replace("}", ")")