-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(anta): Add support of CSV file export (#672)
--------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Guillaume Mulocher <[email protected]> Co-authored-by: Carl Baillargeon <[email protected]>
- Loading branch information
Showing
10 changed files
with
309 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Copyright (c) 2023-2024 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the LICENSE file. | ||
"""CSV Report management for ANTA.""" | ||
|
||
# pylint: disable = too-few-public-methods | ||
from __future__ import annotations | ||
|
||
import csv | ||
import logging | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from anta.logger import anta_log_exception | ||
|
||
if TYPE_CHECKING: | ||
import pathlib | ||
|
||
from anta.result_manager import ResultManager | ||
from anta.result_manager.models import TestResult | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ReportCsv: | ||
"""Build a CSV report.""" | ||
|
||
@dataclass() | ||
class Headers: | ||
"""Headers for the CSV report.""" | ||
|
||
device: str = "Device" | ||
test_name: str = "Test Name" | ||
test_status: str = "Test Status" | ||
messages: str = "Message(s)" | ||
description: str = "Test description" | ||
categories: str = "Test category" | ||
|
||
@classmethod | ||
def split_list_to_txt_list(cls, usr_list: list[str], delimiter: str = " - ") -> str: | ||
"""Split list to multi-lines string. | ||
Parameters | ||
---------- | ||
usr_list: List of string to concatenate | ||
delimiter: A delimiter to use to start string. Defaults to None. | ||
Returns | ||
------- | ||
str: Multi-lines string | ||
""" | ||
return f"{delimiter}".join(f"{line}" for line in usr_list) | ||
|
||
@classmethod | ||
def convert_to_list(cls, result: TestResult) -> list[str]: | ||
""" | ||
Convert a TestResult into a list of string for creating file content. | ||
Args: | ||
---- | ||
results: A TestResult to convert into list. | ||
""" | ||
message = cls.split_list_to_txt_list(result.messages) if len(result.messages) > 0 else "" | ||
categories = cls.split_list_to_txt_list(result.categories) if len(result.categories) > 0 else "None" | ||
return [ | ||
str(result.name), | ||
result.test, | ||
result.result, | ||
message, | ||
result.description, | ||
categories, | ||
] | ||
|
||
@classmethod | ||
def generate(cls, results: ResultManager, csv_filename: pathlib.Path) -> None: | ||
"""Build CSV flle with tests results. | ||
Parameter | ||
--------- | ||
results: A ResultManager instance. | ||
csv_filename: File path where to save CSV data. | ||
Raise | ||
----- | ||
OSError if any is raised while writing the CSV file. | ||
""" | ||
headers = [ | ||
cls.Headers.device, | ||
cls.Headers.test_name, | ||
cls.Headers.test_status, | ||
cls.Headers.messages, | ||
cls.Headers.description, | ||
cls.Headers.categories, | ||
] | ||
|
||
try: | ||
with csv_filename.open(mode="w", encoding="utf-8") as csvfile: | ||
csvwriter = csv.writer( | ||
csvfile, | ||
delimiter=",", | ||
) | ||
csvwriter.writerow(headers) | ||
for entry in results.results: | ||
csvwriter.writerow(cls.convert_to_list(entry)) | ||
except OSError as exc: | ||
message = f"OSError caught while writing the CSV file '{csv_filename.resolve()}'." | ||
anta_log_exception(exc, message, logger) | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.