generated from konveyor-ecosystem/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP logging updates Signed-off-by: John Matthews <jwmatthews@gmail.com> * Logging to disk via podman compose working Signed-off-by: John Matthews <jwmatthews@gmail.com> * Tweak to log levels Signed-off-by: John Matthews <jwmatthews@gmail.com> * Cleanup tweaks Signed-off-by: John Matthews <jwmatthews@gmail.com> * Ensure 'load-data' from podman compose up can log to disk Signed-off-by: John Matthews <jwmatthews@gmail.com> --------- Signed-off-by: John Matthews <jwmatthews@gmail.com>
- Loading branch information
1 parent
74efb3d
commit ece6b12
Showing
17 changed files
with
138 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1 @@ | ||
__version__ = "0.0.1" | ||
|
||
from typing import Annotated, List | ||
|
||
import typer | ||
|
||
from kai.report import Report | ||
from kai.result import LLMResult | ||
from kai.service.incident_store.incident_store import IncidentStore | ||
|
||
# from typing_extensions import Annotated | ||
|
||
|
||
app = typer.Typer() | ||
|
||
# report_app = typer.Typer() | ||
# result_app = typer.Typer() | ||
|
||
# app.add_typer(report_app, name="report", help="Generate a markdown report from a raw analysis yaml.") | ||
# app.add_typer(result_app, name="result", help="Generate patches for given violations and incidents from an analysis yaml") | ||
|
||
|
||
@app.command() | ||
def report(analysis_path: str, output_dir: str): | ||
""" | ||
Generate a Markdown report of a given analysis | ||
YAML to be read by a human | ||
""" | ||
report = Report.load_report_from_file(analysis_path) | ||
r = dict(report) | ||
print(f"We have results from {len(r.keys())} RuleSet(s) in {analysis_path}\n") | ||
report.write_markdown(output_dir) | ||
|
||
|
||
@app.command() | ||
def generate( | ||
path_to_report: str, | ||
path_to_source: str, | ||
example_initial_branch: str, | ||
example_solved_branch: str, | ||
path_to_output: str, | ||
limit_rulesets: Annotated[List[str], typer.Option("--ruleset", "-r")] = None, | ||
limit_violations: Annotated[List[str], typer.Option("--violation", "-v")] = None, | ||
model: Annotated[str, typer.Option("--model", "-m")] = "gpt-3.5-turbo-16k", | ||
): | ||
# model: Annotated[Optional[str], typer.Argument()] = "gpt-3.5-turbo-16k"): | ||
""" | ||
Generate patches for given violations and incidents from an analysis yaml report | ||
- path_to_report: Path to the analysis yaml report | ||
- path_to_source: Path to the source code to be patched | ||
- example_initial_branch: Branch name for the initial state of the source code | ||
- example_solved_branch: Branch name for the solved state of the source code | ||
- path_to_output: Path to the output directory for the patches | ||
- limit_rulesets: Limit to specific rulesets (defaults to 'None', meaning to run all) | ||
- limit_violations: Limit to specific violations (defaults to 'None', meaning to run all) | ||
- model: Model name to use for generating the patches (defaults to 'gpt-3.5-turbo-16k', 'gpt-4-1106-preview' is another good option) | ||
""" | ||
print( | ||
f"Generating patches for {path_to_report} for example app at {path_to_source}" | ||
) | ||
print(f"Initial branch: {example_initial_branch}") | ||
print(f"Solved branch: {example_solved_branch}") | ||
print(f"Output directory: {path_to_output}") | ||
print(f"Model: {model}") | ||
print(f"Limit to ruleset(s): {limit_rulesets}") | ||
print(f"Limit to violation(s): {limit_violations}") | ||
|
||
llmResult = LLMResult(path_to_source, example_initial_branch, example_solved_branch) | ||
llmResult.parse_report(path_to_report) | ||
llmResult.process(path_to_output, model, limit_rulesets, limit_violations) | ||
print(f"Completed processing, output written to {path_to_output}\n") | ||
|
||
|
||
@app.command() | ||
def load(report_path: str, output_dir: str): | ||
""" | ||
Load the incident store with the given applications | ||
write the cached_violations to a file for later use | ||
""" | ||
incident_store = IncidentStore(report_path, output_dir) | ||
incident_store.load_incident_store() | ||
|
||
|
||
@app.command() | ||
def patch(ruleset: str, violation: str, report_path: str, output_dir: str): | ||
""" | ||
Generate patches for a specific violation | ||
""" | ||
print(f"Generating patches for {ruleset} - {violation}") | ||
incident_store = IncidentStore(report_path, output_dir) | ||
patches = incident_store.get_solved_issue(ruleset, violation) | ||
if len(patches) == 0: | ||
print(f"No patches found for {ruleset} - {violation}") | ||
else: | ||
for patch in patches: | ||
print(f"Patch: {patch}") | ||
return patches | ||
|
||
|
||
@app.command() | ||
def common(ruleset: str, violation: str, report_path: str, output_dir: str): | ||
""" | ||
Find common violations for a specific violation | ||
""" | ||
print(f"Finding common violations for {ruleset} - {violation}") | ||
incident_store = IncidentStore(report_path, output_dir) | ||
violations = incident_store.find_common_violations(ruleset, violation) | ||
if violations is None: | ||
print(f"No common violations found for {ruleset} - {violation}") | ||
for violation in violations: | ||
print(f"Violation: {violation}") | ||
return violations |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
log_level = "info" | ||
file_log_level = "debug" | ||
log_dir = "$pwd/logs" | ||
demo_mode = false | ||
trace_enabled = true | ||
|
||
|
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 |
---|---|---|
@@ -1,10 +1,63 @@ | ||
import logging | ||
import os | ||
|
||
KAI_LOG = logging.getLogger(__name__) | ||
console_handler = logging.StreamHandler() | ||
from kai.models.kai_config import KaiConfig | ||
|
||
parent_log = logging.getLogger("kai") | ||
|
||
# console_handler = logging.StreamHandler() | ||
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
formatter = logging.Formatter( | ||
"%(levelname)s - %(asctime)s - [%(filename)20s:%(lineno)-4s - %(funcName)20s()] - %(message)s" | ||
"%(levelname)s - %(asctime)s - %(name)s - [%(filename)20s:%(lineno)-4s - %(funcName)20s()] - %(message)s" | ||
) | ||
console_handler.setFormatter(formatter) | ||
KAI_LOG.addHandler(console_handler) | ||
|
||
|
||
def process_log_dir_replacements(log_dir: str): | ||
## | ||
# We want to replace $pwd with the location of the Kai project directory, | ||
# this is needed to help with specifying from configuration | ||
## | ||
if log_dir.startswith("$pwd"): | ||
log_dir = log_dir.replace( | ||
"$pwd", os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") | ||
) | ||
return log_dir | ||
|
||
|
||
def setup_console_handler(logger, log_level: str = "INFO"): | ||
console_handler = logging.StreamHandler() | ||
console_handler.setLevel(log_level) | ||
console_handler.setFormatter(formatter) | ||
logger.addHandler(console_handler) | ||
print(f"Console logging for '{parent_log.name}' is set to level '{log_level}'") | ||
|
||
|
||
def setup_file_handler( | ||
logger, log_file_name: str, log_dir: str, log_level: str = "DEBUG" | ||
): | ||
# Ensure any needed log directories exist | ||
log_dir = process_log_dir_replacements(log_dir) | ||
log_file_path = os.path.join(log_dir, log_file_name) | ||
if log_dir.startswith("$pwd"): | ||
log_dir = os.path.join(os.getcwd(), log_dir[5:]) | ||
os.makedirs(os.path.dirname(log_file_path), exist_ok=True) | ||
|
||
file_handler = logging.FileHandler(log_file_path) | ||
file_handler.setLevel(log_level) | ||
file_handler.setFormatter(formatter) | ||
logger.addHandler(file_handler) | ||
print( | ||
f"File logging for '{logger.name}' is set to level '{log_level}' writing to file: '{log_file_path}'" | ||
) | ||
|
||
|
||
def initLogging(console_log_level, file_log_level, log_dir, log_file="kai_server.log"): | ||
setup_console_handler(parent_log, console_log_level) | ||
setup_file_handler(parent_log, log_file, log_dir, file_log_level) | ||
# Attempt to set the parent log level to | ||
# most persmissive and allow child loggers to control what is filtered or not | ||
parent_log.setLevel("DEBUG") | ||
|
||
|
||
def initLoggingFromConfig(config: KaiConfig): | ||
initLogging(config.log_level.upper(), config.file_log_level.upper(), config.log_dir) |
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
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.