|
31 | 31 | from sqlalchemy.engine import Result
|
32 | 32 | import re
|
33 | 33 | from datetime import datetime
|
| 34 | +import multiprocessing as mp |
| 35 | +import asyncio |
34 | 36 |
|
35 | 37 | from anms.components.schemas import ARIs
|
36 | 38 | from anms.models.relational import get_async_session, get_session
|
@@ -102,6 +104,37 @@ async def find_var_type(obj_metadata):
|
102 | 104 |
|
103 | 105 | return data_type_id
|
104 | 106 |
|
| 107 | +async def _process_report_entries(x): |
| 108 | + entry, ac_types_and_id = x |
| 109 | + # for entry in entries: |
| 110 | + curr_values = [] |
| 111 | + time = datetime.fromtimestamp(int(entry.time)).strftime('%Y-%m-%d %H:%M:%S') |
| 112 | + |
| 113 | + string_values = list(filter(None, re.split(r",|'(.*?)'", entry.string_values))) if entry.string_values else [] |
| 114 | + uint_values = entry.uint_values.split(',') if entry.uint_values else [] |
| 115 | + int_values = entry.int_values.split(',') if entry.int_values else [] |
| 116 | + real32_values = entry.real32_values.split(',') if entry.real32_values else [] |
| 117 | + real64_values = entry.real64_values.split(',') if entry.real64_values else [] |
| 118 | + uvast_values = entry.uvast_values.split(',') if entry.uvast_values else [] |
| 119 | + vast_values = entry.vast_values.split(',') if entry.vast_values else [] |
| 120 | + value_matchup = {18: string_values, 19: int_values, 20: uint_values, 21: vast_values, 22: uvast_values, |
| 121 | + 23: real32_values, 24: real64_values} |
| 122 | + curr_values.append(time) |
| 123 | + for type_id, obj_id in ac_types_and_id: |
| 124 | + # find the type of ari |
| 125 | + curr_type = type_id |
| 126 | + if value_matchup[curr_type]: |
| 127 | + curr_values.append(value_matchup[curr_type].pop(0)) |
| 128 | + if not ac_types_and_id: |
| 129 | + if string_values: curr_values.append(','.join(string_values)) |
| 130 | + if uint_values: curr_values.append(','.join(uint_values)) |
| 131 | + if int_values: curr_values.append(','.join(int_values)) |
| 132 | + if real32_values: curr_values.append(','.join(real32_values)) |
| 133 | + if real64_values: curr_values.append(','.join(real64_values)) |
| 134 | + if uvast_values: curr_values.append(','.join(uvast_values)) |
| 135 | + if vast_values: curr_values.append(','.join(vast_values)) |
| 136 | + return curr_values |
| 137 | + |
105 | 138 |
|
106 | 139 | # entries tabulated returns header and values in correct order
|
107 | 140 | @router.get("/entries/table/{agent_id}/{adm}/{report_name}", status_code=status.HTTP_200_OK,
|
@@ -147,52 +180,30 @@ async def report_ac(agent_id: str, adm: str, report_name: str):
|
147 | 180 | curr_name = result.one_or_none()
|
148 | 181 |
|
149 | 182 | ac_names.append(curr_name)
|
150 |
| - ac_types_and_id.append((entry.data_type_id, entry.obj_metadata_id)) |
151 |
| - # unknown template |
152 |
| - if ac_names == []: |
153 |
| - ac_names = ["time","string_values", "uint_values", "int_values", "real32_values", "real64_values", "uvast_values","vast_values"] |
154 |
| - |
| 183 | + curr_type = entry.data_type_id |
| 184 | + if curr_type == 2: |
| 185 | + curr_type = await find_edd_type(entry.obj_metadata_id) |
| 186 | + elif curr_type == 12: |
| 187 | + curr_type = await find_var_type(entry.obj_metadata_id) |
| 188 | + ac_types_and_id.append((curr_type, entry.obj_metadata_id)) |
| 189 | + |
155 | 190 | stmt = select(Report).where(Report.agent_id == agent_id , Report.ADM == adm_name
|
156 | 191 | , Report.report_name == report_name)
|
157 |
| - # find the type of ari |
158 |
| - type_matchup = {2: find_edd_type, 12: find_var_type, } |
| 192 | + # if a none formal report |
| 193 | + if ac_id == None: |
| 194 | + ac_names.append(report_name) |
| 195 | + |
159 | 196 | final_values = []
|
160 | 197 | final_values.append(ac_names)
|
161 | 198 | async with get_async_session() as session:
|
162 | 199 | result: Result = await session.scalars(stmt)
|
163 | 200 | entries = result.all()
|
164 |
| - |
| 201 | + args_to_use = [] |
165 | 202 | for entry in entries:
|
166 |
| - curr_values = [] |
167 |
| - time = datetime.fromtimestamp(int(entry.time)).strftime('%Y-%m-%d %H:%M:%S') |
168 |
| - |
169 |
| - string_values = list(filter(None, re.split(r",|'(.*?)'", entry.string_values))) if entry.string_values else [] |
170 |
| - uint_values = entry.uint_values.split(',') if entry.uint_values else [] |
171 |
| - int_values = entry.int_values.split(',') if entry.int_values else [] |
172 |
| - real32_values = entry.real32_values.split(',') if entry.real32_values else [] |
173 |
| - real64_values = entry.real64_values.split(',') if entry.real64_values else [] |
174 |
| - uvast_values = entry.uvast_values.split(',') if entry.uvast_values else [] |
175 |
| - vast_values = entry.vast_values.split(',') if entry.vast_values else [] |
176 |
| - value_matchup = {18: string_values, 19: int_values, 20: uint_values, 21: vast_values, 22: uvast_values, |
177 |
| - 23: real32_values, 24: real64_values} |
178 |
| - curr_values.append(time) |
179 |
| - for type_id, obj_id in ac_types_and_id: |
180 |
| - if type_id in type_matchup: |
181 |
| - curr_type = await type_matchup[type_id](obj_id) |
182 |
| - else: |
183 |
| - curr_type = type_id |
184 |
| - if value_matchup[curr_type]: |
185 |
| - curr_values.append(value_matchup[curr_type].pop(0)) |
186 |
| - if ac_types_and_id is []: |
187 |
| - curr_values.append(','.join(string_values)) |
188 |
| - curr_values.append(','.join(uint_values)) |
189 |
| - curr_values.append(','.join(int_values)) |
190 |
| - curr_values.append(','.join(real32_values)) |
191 |
| - curr_values.append(','.join(real64_values)) |
192 |
| - curr_values.append(','.join(uvast_values)) |
193 |
| - curr_values.append(','.join(vast_values)) |
194 |
| - |
195 |
| - final_values.append(curr_values) |
| 203 | + args_to_use.append(_process_report_entries([entry, ac_types_and_id])) |
| 204 | + result = await asyncio.gather(*args_to_use) |
| 205 | + for res in result: |
| 206 | + final_values.append(res) |
196 | 207 |
|
197 | 208 | return final_values
|
198 | 209 |
|
0 commit comments