Skip to content

Commit 9bfb7be

Browse files
committed
Added comments, moved report to models directory
Signed-off-by: JonahSussman <[email protected]>
1 parent 035dfd5 commit 9bfb7be

19 files changed

+123
-78
lines changed

example/run_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Ensure that we have 'kai' in our import path
2020
sys.path.append("../../kai")
2121
from kai.kai_logging import formatter
22-
from kai.report import Report
22+
from kai.models.report import Report
2323

2424
KAI_LOG = logging.getLogger(__name__)
2525

kai/evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from kai.constants import PATH_BENCHMARKS
1111
from kai.models.file_solution import guess_language, parse_file_solution_content
1212
from kai.models.kai_config import KaiConfig, KaiConfigIncidentStoreSQLiteArgs
13+
from kai.models.report import Report
1314
from kai.models.report_types import ExtendedIncident
14-
from kai.report import Report
1515
from kai.service.incident_store.backend import SQLiteBackend
1616
from kai.service.incident_store.incident_store import Application, IncidentStore
1717
from kai.service.kai_application.util import get_prompt

kai/hub_importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pydantic import BaseModel, Field
1616

1717
from kai.models.kai_config import KaiConfig
18-
from kai.report import Report
18+
from kai.models.report import Report
1919
from kai.service.incident_store import Application, IncidentStore
2020
from kai.service.kai_application.kai_application import KaiApplication
2121

kai/models/kai_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class KaiConfigModels(BaseModel):
135135
# Main config
136136

137137

138+
# TODO: Evaluate the usage of pydantic-settings to simplify environment variable
139+
# and command line argument management.
138140
class KaiConfig(BaseModel):
139141
log_level: str = "info"
140142
file_log_level: str = "info"

kai/report.py renamed to kai/models/report.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,13 @@ def _write_markdown_snippet(
146146

147147
for count, (key, items) in enumerate(ruleset.violations.items()):
148148
f.write(f"### #{count} - {key}\n")
149-
# Break out below for violation
150-
# Then we can weave in an example perhaps?
151-
# Or should there be a Markdown class that is responsible for blending
152-
# . - Report
153-
# . - Per Violation create a prompt/run/example
149+
# Break out below for violation. Then we can weave in an example
150+
# perhaps?
151+
#
152+
# Or should there be a Markdown class that is responsible for
153+
# blending:
154+
# - Report
155+
# - Per Violation create a prompt/run/example
154156

155157
f.write(f"* Category: {items.category}\n")
156158
if items.effort is not None:
@@ -189,7 +191,10 @@ def get_violation_snippet(self, ruleset_name: str, violation_name: str):
189191
buffer.write("* Source of rules:")
190192

191193
def should_we_skip_incident(self, incident: Incident) -> bool:
192-
# Filter out known issues
194+
"""
195+
Filter out known issues
196+
"""
197+
193198
file_path = remove_known_prefixes(urlparse(incident.uri).path)
194199

195200
if file_path.startswith("target/"):

kai/models/report_types.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import os
44
from enum import StrEnum
55
from pathlib import Path
6-
from typing import Any, Dict, List, Optional
6+
from typing import Any, Optional
77

88
import yaml
99
from pydantic import AliasChoices, BaseModel, Field, RootModel
1010

11+
"""
12+
Report types ripped straight from analyzer-lsp.
13+
"""
14+
1115

1216
class Category(StrEnum):
1317
potential = "potential"
@@ -16,6 +20,10 @@ class Category(StrEnum):
1620

1721

1822
class Incident(BaseModel):
23+
"""
24+
An Incident is a specific instance of a rule being violated.
25+
"""
26+
1927
# NOTE: `str` is the best equivalent of Go's `json.RawMessage`
2028
uri: str
2129

@@ -38,14 +46,14 @@ class Incident(BaseModel):
3846
serialization_alias="lineNumber",
3947
)
4048

41-
variables: Dict[str, Any] = Field(
49+
variables: dict[str, Any] = Field(
4250
{}, validation_alias=AliasChoices("variables", "incident_variables")
4351
)
4452

4553

4654
class ExtendedIncident(Incident):
4755
"""
48-
A "flattened" incident, containing its ruleset and violation names.
56+
An Incident with extra metadata.
4957
"""
5058

5159
ruleset_name: str
@@ -54,29 +62,37 @@ class ExtendedIncident(Incident):
5462
violation_description: Optional[str] = None
5563

5664

57-
# Link defines an external hyperlink
5865
class Link(BaseModel):
66+
"""
67+
Link defines an external hyperlink.
68+
"""
69+
5970
url: str
6071

6172
# Title optional description
6273
title: str = ""
6374

6475

6576
class Violation(BaseModel):
77+
"""
78+
A Violation is a specific rule being broken, i.e. a rule being "violated".
79+
It may have many different incidents throughout the codebase.
80+
"""
81+
6682
# Description text description about the violation
6783
description: str = ""
6884

6985
# Category category of the violation
7086
category: Category = "potential"
7187

7288
# Labels list of labels for the violation
73-
labels: List[str] = []
89+
labels: list[str] = []
7490

7591
# Incidents list of instances of violation found
76-
incidents: List[Incident] = []
92+
incidents: list[Incident] = []
7793

7894
# ExternalLinks hyperlinks to external sources of docs, fixes, etc.
79-
links: List[Link] = []
95+
links: list[Link] = []
8096

8197
# Extras reserved for additional data
8298
# NOTE: `str` is the best equivalent of Go's `json.RawMessage`
@@ -87,34 +103,44 @@ class Violation(BaseModel):
87103

88104

89105
class RuleSet(BaseModel):
106+
"""
107+
A RuleSet is a collection of rules that are evaluated together. It different
108+
data on its rules: which rules were unmatched, which rules where skipped,
109+
and which rules generated errors or violations.
110+
"""
111+
90112
# Name is a name for the ruleset.
91113
name: Optional[str] = None
92114

93115
# Description text description for the ruleset.
94116
description: str = ""
95117

96118
# Tags list of generated tags from the rules in this ruleset.
97-
tags: Optional[List[str]] = None
119+
tags: Optional[list[str]] = None
98120

99121
# Violations is a map containing violations generated for the matched rules
100122
# in this ruleset. Keys are rule IDs, values are their respective generated
101123
# violations.
102-
violations: Dict[str, Violation] = {}
124+
violations: dict[str, Violation] = {}
103125

104126
# Errors is a map containing errors generated during evaluation of rules in
105127
# this ruleset. Keys are rule IDs, values are their respective generated
106128
# errors.
107-
errors: Optional[Dict[str, str]] = None
129+
errors: Optional[dict[str, str]] = None
108130

109131
# Unmatched is a list of rule IDs of the rules that weren't matched.
110-
unmatched: Optional[List[str]] = None
132+
unmatched: Optional[list[str]] = None
111133

112134
# Skipped is a list of rule IDs that were skipped
113-
skipped: Optional[List[str]] = None
135+
skipped: Optional[list[str]] = None
136+
114137

138+
class AnalysisReport(RootModel[list[RuleSet]]):
139+
"""
140+
An analysis report is simply a list of rule sets.
141+
"""
115142

116-
class AnalysisReport(RootModel[List[RuleSet]]):
117-
root: List[RuleSet] = Field(..., title="AnalysisReport")
143+
root: list[RuleSet] = Field(..., title="AnalysisReport")
118144

119145

120146
def generate_json_schema():

kai/result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from kai.constants import PATH_TEMPLATES
1111

12-
from .report import Report
12+
from .models.report import Report
1313
from .scm import GitDiff
1414

1515
KAI_LOG = logging.getLogger(__name__)

kai/routes/load_analysis_report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from aiohttp.web_request import Request
55
from pydantic import BaseModel
66

7-
from kai.report import Report
7+
from kai.models.report import Report
88
from kai.routes.util import to_route
99
from kai.service.incident_store.incident_store import Application
1010

kai/server.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
log = logging.getLogger(__name__)
1818

19-
# TODO: Make openapi spec for everything
2019

2120
# TODO: Repo lives both on client and on server. Determine either A) Best way to
2221
# rectify differences or B) Only have the code on one and pass stuff between

kai/service/incident_store/incident_store.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from kai.constants import PATH_GIT_ROOT, PATH_KAI, PATH_LOCAL_REPO
1515
from kai.kai_logging import initLogging
1616
from kai.models.kai_config import KaiConfig
17+
from kai.models.report import Report
1718
from kai.models.util import filter_incident_vars
18-
from kai.report import Report
1919
from kai.service.incident_store.backend import IncidentStoreBackend
2020
from kai.service.incident_store.sql_types import (
2121
SQLAcceptedSolution,
@@ -157,6 +157,7 @@ def load_reports_from_directory(store: "IncidentStore", path: str):
157157
KAI_LOG.info(f"Loaded application - solved {app}\n")
158158

159159

160+
# NOTE: This application object is more like metadata than anything.
160161
@dataclass
161162
class Application:
162163
application_name: str
@@ -186,10 +187,9 @@ def load_report(self, app: Application, report: Report) -> tuple[int, int, int]:
186187
tuple containing (# of new incidents, # of unsolved incidents, # of
187188
solved incidents) in that order.
188189
189-
NOTE: This application object is more like metadata than anything.
190-
"""
191190
192-
# FIXME: Only does stuff within the same application. Maybe fixed?
191+
TODO: Only does stuff within the same application. Maybe fixed?
192+
"""
193193

194194
# NEW: Store whole report in table
195195
# - if we get the same report again, we should skip adding it. Have some identifier
@@ -200,14 +200,6 @@ def load_report(self, app: Application, report: Report) -> tuple[int, int, int]:
200200
# Iterate through all incidents in the report
201201
# - change so theres an identified like "commit application ruleset violation"
202202

203-
# create entries if not exists
204-
# reference the old-new matrix
205-
# old
206-
# | NO | YES
207-
# --------|--------+-----------------------------
208-
# new NO | - | update (SOLVED, embeddings)
209-
# YES | insert | update (line number, etc...)
210-
211203
repo = Repo(unquote(urlparse(app.repo_uri_local).path))
212204
old_commit: str
213205
new_commit = app.current_commit
@@ -304,15 +296,24 @@ def load_report(self, app: Application, report: Report) -> tuple[int, int, int]:
304296

305297
categorized_incidents = self.solution_detector(solution_detector_ctx)
306298

307-
for new_incident in categorized_incidents.new:
308-
session.add(new_incident)
299+
# create entries if not exists
300+
# old
301+
# | NO | YES
302+
# --------|--------+-----------------------------
303+
# new NO | - | update (SOLVED, embeddings)
304+
# YES | insert | update (line number, etc...)
309305

306+
# Add new incidents
307+
308+
session.add_all(categorized_incidents.new)
310309
session.commit()
311310

312311
KAI_LOG.debug(
313312
f"Number of solved incidents: {len(categorized_incidents.solved)}"
314313
)
315314

315+
# Update solved incidents with their respective solutions
316+
316317
for solved_incident in categorized_incidents.solved:
317318
solution = self.solution_producer.produce_one(
318319
solved_incident, repo, old_commit, new_commit

0 commit comments

Comments
 (0)