Skip to content

Commit 6a71c7d

Browse files
committed
ft-analyzer make read host stats csv more robust
1 parent a7d719a commit 6a71c7d

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

tools/ft-analyzer/ftanalyzer/events/events.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
22
import atexit
33
import heapq
4+
import logging
45
import math
56
import os
67
import shutil
@@ -184,22 +185,36 @@ def create_event_queue(
184185
sorted_by_end_path = os.path.join(out_dir, "flows_sorted_by_end.csv")
185186
sorted_by_export_path = os.path.join(out_dir, "flows_sorted_by_export.csv")
186187

188+
with open(flows_path, "r") as f:
189+
header_line = f.readline()
190+
191+
wanted_columns = set(CSV_COLUMN_TYPES.keys())
192+
available_columns = set(header_line.strip().split(","))
193+
187194
# Load and split
188195
df = pd.read_csv(
189196
flows_path,
190197
dtype=CSV_COLUMN_TYPES,
191-
usecols=CSV_COLUMN_TYPES.keys(),
198+
usecols=wanted_columns & available_columns,
192199
engine="pyarrow",
193200
)
194201

195202
# Filer host stats file by time
196203
start = math.floor(df["START_TIME"].min() / 1000)
197204
end = math.ceil(df["END_TIME"].max() / 1000)
198205

199-
stats_df: pd.DataFrame = pd.read_csv(
200-
hosts_stats_file, sep=";", dtype=STATS_CSV_COLUMN_TYPES, engine="pyarrow"
201-
)
202-
stats_df = stats_df[(stats_df["Time"] >= start) & (stats_df["Time"] <= end)]
206+
try:
207+
stats_df: pd.DataFrame = pd.read_csv(
208+
hosts_stats_file,
209+
sep=";",
210+
dtype=STATS_CSV_COLUMN_TYPES,
211+
engine="pyarrow",
212+
usecols=STATS_CSV_COLUMN_TYPES.keys(),
213+
)
214+
stats_df = stats_df[(stats_df["Time"] >= start) & (stats_df["Time"] <= end)]
215+
except Exception as e:
216+
logging.error(e)
217+
stats_df = pd.DataFrame([], columns=STATS_CSV_COLUMN_TYPES.keys())
203218

204219
agg_dict = {col: "sum" for col in SUM_PIDSTAT_COLS}
205220
agg_dict.update({col: "first" for col in TAKE_PIDSTAT_COLS})

0 commit comments

Comments
 (0)