|
26 | 26 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 | 27 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | -from typing import List, Type |
30 | | -from powerapi.report import Report |
| 29 | + |
31 | 30 | from powerapi.exception import PowerAPIExceptionWithMessage |
| 31 | +from powerapi.report import Report |
32 | 32 |
|
33 | 33 |
|
34 | 34 | class DBError(PowerAPIExceptionWithMessage): |
35 | 35 |
|
36 | 36 | """ |
37 | | - Error raised when an error occuried when using a database |
| 37 | + Error raised when an error occurred when using a database. |
38 | 38 | """ |
39 | 39 | def __init__(self, msg: str): |
40 | 40 | PowerAPIExceptionWithMessage.__init__(self, msg) |
41 | 41 |
|
42 | 42 |
|
43 | 43 | class IterDB: |
44 | 44 | """ |
45 | | - IterDB class |
46 | | -
|
47 | | - This class allows to browse a database as an iterable |
| 45 | + This class define the interface of a database results iterator. |
48 | 46 | """ |
49 | 47 |
|
50 | | - def __init__(self, db, report_type, stream_mode): |
| 48 | + def __init__(self, db, report_type: type[Report], stream_mode: bool): |
51 | 49 | """ |
| 50 | + :param db: Database instance |
| 51 | + :param report_type: Report type to convert the database results into |
| 52 | + :param stream_mode: Define if the iterator should stop when there is no data |
52 | 53 | """ |
53 | 54 | self.db = db |
54 | | - self.stream_mode = stream_mode |
55 | 55 | self.report_type = report_type |
| 56 | + self.stream_mode = stream_mode |
56 | 57 |
|
57 | 58 | def __iter__(self): |
58 | 59 | """ |
| 60 | + Return an iterator for the database results. |
59 | 61 | """ |
60 | 62 | raise NotImplementedError() |
61 | 63 |
|
62 | 64 | def __next__(self) -> Report: |
63 | 65 | """ |
| 66 | + Return and consume the next database result available. |
64 | 67 | """ |
65 | 68 | raise NotImplementedError() |
66 | 69 |
|
67 | 70 |
|
68 | 71 | class BaseDB: |
69 | 72 | """ |
70 | | - Abstract class which define every common function for database uses. |
71 | | -
|
72 | | - This class define every common function that need to be implemented |
73 | | - by each DB module. A database module correspond to a kind of BDD. |
74 | | - For example, Mongodb, influxdb, csv are different kind of BDD. |
| 73 | + This class define the interface needed to fetch/save reports from/to a database. |
75 | 74 | """ |
76 | | - def __init__(self, report_type: Type[Report], exceptions: List[Type[Exception]] = None, asynchrone: bool = False): |
77 | | - self.exceptions = exceptions or [] |
78 | | - self.asynchrone = asynchrone |
| 75 | + |
| 76 | + def __init__(self, report_type: type[Report], exceptions: list[type[Exception]] = None, is_async: bool = False): |
| 77 | + """ |
| 78 | + :param report_type: The type of report expected |
| 79 | + :param exceptions: List of exception type raised by the database module |
| 80 | + :param is_async: Whether the database use asyncio or not |
| 81 | + """ |
79 | 82 | self.report_type = report_type |
| 83 | + self.exceptions = exceptions or [] |
| 84 | + self.is_async = is_async |
80 | 85 |
|
81 | 86 | def connect(self): |
82 | 87 | """ |
83 | | - Function that allow to load the database. Depending of the type, |
84 | | - different process can happen. |
| 88 | + Connect to the database. |
| 89 | + """ |
| 90 | + raise NotImplementedError() |
85 | 91 |
|
86 | | - .. note:: Need to be overrided |
| 92 | + def disconnect(self): |
| 93 | + """ |
| 94 | + Disconnect from the database. |
87 | 95 | """ |
88 | 96 | raise NotImplementedError() |
89 | 97 |
|
90 | | - def iter(self, stream_mode: bool) -> IterDB: |
| 98 | + def iter(self, stream_mode: bool = False) -> IterDB: |
91 | 99 | """ |
92 | | - Create the iterator for get the data |
93 | | - :param stream_mode: Define if we read in stream mode |
| 100 | + Create and returns a database results iterator. |
| 101 | + :param stream_mode: Define if we read continuously (streaming) or stop when no data is available |
94 | 102 | """ |
95 | 103 | raise NotImplementedError() |
96 | 104 |
|
97 | 105 | def save(self, report: Report): |
98 | 106 | """ |
99 | | - Allow to save a json input in the db |
100 | | -
|
101 | | - :param report: Report |
| 107 | + Save a report to the database. |
| 108 | + :param report: Report to be saved |
102 | 109 | """ |
103 | 110 | raise NotImplementedError() |
104 | 111 |
|
105 | | - def save_many(self, reports: List[Report]): |
| 112 | + def save_many(self, reports: list[Report]): |
106 | 113 | """ |
107 | | - Allow to save a batch of data |
108 | | -
|
109 | | - :param reports: Batch of Serialized Report |
| 114 | + Save multiple reports to the database. (batch mode) |
| 115 | + :param reports: List of Report to be saved |
110 | 116 | """ |
111 | 117 | raise NotImplementedError() |
0 commit comments