Skip to content

Commit 87b6d4f

Browse files
committed
ft-orchestration jq command use no intermediate file
1 parent 54c4ee5 commit 87b6d4f

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

  • tools/ft-orchestration/src/collector

tools/ft-orchestration/src/collector/jq.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,12 @@ def __init__(self, executor: Executor, file: str):
6969

7070
self._executor = executor
7171
self._file = stdout.strip()
72-
tmp_file = path.join(self._rsync.get_data_directory(), "flows.json")
7372
self._cmd_json = f"ipfixcol2 -c {Path(self._conf_dir, self.CONFIG_FILE)}"
7473
fields = ", ".join(
7574
[f'.\\"{field}\\"' for field in CSV_HEADER_TO_ANALYZER_HEADER.keys()]
7675
)
77-
header = ",".join(
78-
[f'"{field}"' for field in CSV_HEADER_TO_ANALYZER_HEADER.keys()]
79-
)
8076
self._cmd_csv = f"""set -e
81-
ipfixcol2 -c {Path(self._conf_dir, self.CONFIG_FILE)} > {tmp_file}
82-
echo {header}
83-
cat {tmp_file} | jq -r "
77+
ipfixcol2 -c {Path(self._conf_dir, self.CONFIG_FILE)} | jq -r "
8478
.[\\"iana:sourceIPAddress\\"] = (.\\"iana:sourceIPv4Address\\" // .\\"iana:sourceIPv6Address\\") |
8579
.[\\"iana:destinationIPAddress\\"] = (.\\"iana:destinationIPv4Address\\" // .\\"iana:destinationIPv6Address\\") |
8680
del(
@@ -89,9 +83,8 @@ def __init__(self, executor: Executor, file: str):
8983
.\\"iana:destinationIPv4Address\\",
9084
.\\"iana:destinationIPv6Address\\"
9185
) |
92-
[{fields}] | @csv
93-
"
94-
rm {tmp_file}"""
86+
[{fields}] | @csv"
87+
"""
9588
"""Reads fds file and output as json with ipfixcol2, then converts json with `jq` to csv\\
9689
In the csv output the columns `iana:sourceIPv4Address` and `iana:sourceIPv6Address` are merged to `iana:sourceIPAddress`\\
9790
The same is done for `iana:destinationIPAddress`
@@ -286,15 +279,17 @@ def save_csv(self, csv_file: str):
286279
rsync = Rsync(self._executor)
287280
filename = path.basename(csv_file)
288281
tmp_file = path.join(rsync.get_data_directory(), filename)
282+
header = ",".join(
283+
[f'"{field}"' for field in CSV_HEADER_TO_ANALYZER_HEADER.keys()]
284+
)
289285

290286
logging.getLogger().info(
291287
"Preparing CSV output by calling ipfixcol2 + jq command..."
292288
)
293289
start = time.time()
290+
Tool(f"echo '{header}' > {tmp_file}", executor=self._executor).run()
294291
# write csv
295-
stdout, stderr = Tool(
296-
f"({self._cmd_csv}) >> {tmp_file}", executor=self._executor
297-
).run()
292+
Tool(f"({self._cmd_csv}) >> {tmp_file}", executor=self._executor).run()
298293
end = time.time()
299294
logging.getLogger().info("CSV output saved in %.2f seconds.", (end - start))
300295

0 commit comments

Comments
 (0)