|
28 | 28 | import asyncio
|
29 | 29 | from pathlib import Path
|
30 | 30 | from typing import Optional, List
|
| 31 | +import webbrowser |
| 32 | +import re |
| 33 | +from enum import Enum |
31 | 34 |
|
32 | 35 | import rich
|
33 | 36 | import typer
|
@@ -138,3 +141,83 @@ async def download_with_progress(filename) -> Path:
|
138 | 141 |
|
139 | 142 | for path in result_files:
|
140 | 143 | print(path.as_posix())
|
| 144 | + |
| 145 | + |
| 146 | +class TimeFrame(str, Enum): |
| 147 | + r""" |
| 148 | + Time Frame levels: 'day', 'week' & 'month' |
| 149 | + """ |
| 150 | + day = ("day",) |
| 151 | + week = ("week",) |
| 152 | + month = ("month") |
| 153 | + |
| 154 | + |
| 155 | +class LogLevel(str, Enum): |
| 156 | + r""" |
| 157 | + Level of the log messages: INFO & ERROR |
| 158 | + """ |
| 159 | + info = ("INFO",) |
| 160 | + error = ("ERROR",) |
| 161 | + |
| 162 | + |
| 163 | +def add_query(key, value): |
| 164 | + """ |
| 165 | + Creates query string from the key and value pairs |
| 166 | + """ |
| 167 | + query_string = "(query:(match_phrase:({0}:'{1}')))".format(key, value) |
| 168 | + return query_string |
| 169 | + |
| 170 | + |
| 171 | +def select_time(time_frame=TimeFrame.day): |
| 172 | + """ |
| 173 | + Takes input as 'day','week','month' and returns the time filter |
| 174 | + """ |
| 175 | + time_string = time_frame |
| 176 | + if time_frame.lower() == TimeFrame.day: |
| 177 | + time_string = "time:(from:now%2Fd,to:now%2Fd)" |
| 178 | + elif time_frame.lower() == TimeFrame.week: |
| 179 | + time_string = "time:(from:now%2Fw,to:now%2Fw)" |
| 180 | + elif time_frame.lower() == TimeFrame.month: |
| 181 | + time_string = "time:(from:now-30d%2Fd,to:now)" |
| 182 | + else: |
| 183 | + rich.print("Got a time frame apart from 'day', 'week', 'month'") |
| 184 | + return time_string |
| 185 | + |
| 186 | + |
| 187 | +def create_kibana_link_parameters(log_url, transform_id=None, log_level=None, time_frame=None): |
| 188 | + """ |
| 189 | + Create the _a and _g parameters for the kibana dashboard link |
| 190 | + """ |
| 191 | + a_parameter = f"&_a=(filters:!({add_query('requestId', transform_id)},"\ |
| 192 | + f"{add_query('level', log_level.value.lower())}))" |
| 193 | + g_parameter = f"&_g=({select_time(time_frame.value.lower())})" |
| 194 | + kibana_link = re.sub(r"\&\_g\=\(\)", g_parameter + a_parameter, log_url) |
| 195 | + return kibana_link |
| 196 | + |
| 197 | + |
| 198 | +@transforms_app.command(no_args_is_help=True) |
| 199 | +def logs( |
| 200 | + url: Optional[str] = url_cli_option, |
| 201 | + backend: Optional[str] = backend_cli_option, |
| 202 | + transform_id: str = typer.Option(None, "-t", "--transform-id", help="Transform ID"), |
| 203 | + log_level: Optional[LogLevel] = typer.Option("ERROR", "-l", "--log-level", |
| 204 | + help="Level of Logs", |
| 205 | + case_sensitive=False), |
| 206 | + time_frame: Optional[TimeFrame] = typer.Option("month", "-f", "--time-frame", |
| 207 | + help="Time Frame", |
| 208 | + case_sensitive=False) |
| 209 | +): |
| 210 | + """ |
| 211 | + Open the URL to the Kibana dashboard of the logs of a tranformer |
| 212 | + """ |
| 213 | + sx = ServiceXClient(backend=backend, url=url) |
| 214 | + transforms = sx.get_transform_status(transform_id) |
| 215 | + if transforms and transforms.request_id == transform_id: |
| 216 | + kibana_link = create_kibana_link_parameters(transforms.log_url, |
| 217 | + transform_id=transform_id, |
| 218 | + log_level=log_level, |
| 219 | + time_frame=time_frame) |
| 220 | + print(kibana_link) |
| 221 | + webbrowser.open(kibana_link) |
| 222 | + else: |
| 223 | + rich.print("Invalid Request ID") |
0 commit comments