Skip to content

Commit

Permalink
Switch to analyzer brief
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Sep 12, 2023
1 parent a1dd6e4 commit 97697ea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
4 changes: 2 additions & 2 deletions duetector/analyzer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def brief(
]

return AnalyzerBrief(
tracers=[brief.tracer for brief in briefs],
collector_ids=[brief.collector_id for brief in briefs],
tracers=set([brief.tracer for brief in briefs]),
collector_ids=set([brief.collector_id for brief in briefs]),
briefs=briefs,
)
36 changes: 31 additions & 5 deletions duetector/analyzer/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Set

import pydantic

Expand Down Expand Up @@ -67,20 +67,46 @@ class Brief(pydantic.BaseModel):
count: Optional[int] = None
fields: Dict[str, Any] = {}

def __repr__(self):
fields_repr = ", ".join([f"{k}: {v.__name__}" for k, v in self.fields.items()])

s = f"""
{self.tracer}@{self.collector_id} with {self.count} records
from {self.start} to {self.end}
available fields: [{fields_repr}]
"""

return s

def __str__(self):
return self.__repr__()


class AnalyzerBrief(pydantic.BaseModel):
"""
Brief of analyzer.
"""

tracers: List[str]
tracers: Set[str]
"""
List of tracers
Set of tracers
"""

collector_ids: List[str]
collector_ids: Set[str]
"""
List of collector ids
Set of collector ids
"""

briefs: List[Brief]

def __repr__(self):
briefs_repr = "\n".join([f"\n----------------{b}----------------" for b in self.briefs])
s = f"""
Available tracers: {self.tracers}
Available collector ids: {self.collector_ids}
briefs: {briefs_repr}
"""
return s

def __str__(self):
return self.__repr__()
21 changes: 18 additions & 3 deletions duetector/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import click

from duetector.analyzer.db import DBAnalyzer
from duetector.config import CONFIG_PATH, ConfigLoader
from duetector.log import logger
from duetector.monitors import BccMonitor, ShMonitor
Expand Down Expand Up @@ -94,7 +95,9 @@ def generate_config(path):

@click.command()
@click.option(
"--config", default=CONFIG_PATH, help=f"Config file path, default: ``{CONFIG_PATH}``."
"--config",
default=CONFIG_PATH,
help=f"Config file path, default: ``{CONFIG_PATH}``.",
)
@click.option(
"--load_env",
Expand Down Expand Up @@ -124,13 +127,19 @@ def generate_config(path):
default=True,
help=f"Set false or False to disable shell monitor, default: ``True``.",
)
@click.option(
"--brief",
default=True,
help=f"Print brief when exit, default: ``True``.",
)
def start(
config,
load_env,
dump_when_load,
config_dump_dir,
enable_bcc_monitor,
enable_sh_monitor,
brief,
):
"""
Start A bcc monitor and wait for KeyboardInterrupt
Expand All @@ -157,8 +166,14 @@ def _shutdown(sig=None, frame=None):
logger.info("Exiting...")
for m in monitors:
m.shutdown()
for m in monitors:
logger.info(m.summary())
logger.info("All monitors shutdown.")
if brief:
try:
logger.info("Generating brief...")
logger.info(str(DBAnalyzer(c).brief()))
except Exception as e:
logger.error("Exception when generating brief")
logger.exception(e)
exit(0)

signal.signal(signal.SIGINT, _shutdown)
Expand Down

0 comments on commit 97697ea

Please sign in to comment.