Skip to content

Commit 4cb823e

Browse files
committed
added separate write route, changed return format for api route
1 parent 96ee580 commit 4cb823e

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

codeflash/api/cfapi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,17 @@ def is_function_being_optimized_again(owner: str, repo: str, pr_number: int, cod
209209
)
210210
response.raise_for_status()
211211
return response.json()
212+
213+
def add_code_context_hash(owner: str, repo: str, pr_number: int, code_context_hash: str) -> Response:
214+
"""Add code context to the DB cache"""
215+
response = make_cfapi_request(
216+
"/add-code-hash",
217+
"POST",
218+
{
219+
"owner": owner,
220+
"repo": repo,
221+
"pr_number": pr_number,
222+
"code_context_hash": code_context_hash
223+
}
224+
)
225+
return response

codeflash/discovery/functions_to_optimize.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,17 @@ def filter_functions(
532532
disable_logs: bool = False, # noqa: FBT001, FBT002
533533
) -> tuple[dict[Path, list[FunctionToOptimize]], int]:
534534
blocklist_funcs = get_blocklisted_functions()
535+
already_optimized_count = 0
536+
path_based_functions = {Path(k): v for k, v in modified_functions.items() if v}
537+
try:
538+
repository = git.Repo(Path.cwd(), search_parent_directories=True)
539+
owner, repo = get_repo_owner_and_name(repository)
540+
except git.exc.InvalidGitRepositoryError:
541+
logger.warning("No git repository found")
542+
owner, repo = None, None
543+
pr_number = get_pr_number()
544+
if owner and repo and pr_number is not None:
545+
path_based_functions, functions_count = check_optimization_status(path_based_functions, owner, repo, pr_number)
535546
logger.debug(f"Blocklisted functions: {blocklist_funcs}")
536547
# Remove any function that we don't want to optimize
537548

@@ -606,19 +617,10 @@ def filter_functions(
606617
functions_count += len(_functions)
607618

608619
# Convert to Path keys for optimization check
609-
path_based_functions = {Path(k): v for k, v in filtered_modified_functions.items() if v}
620+
610621

611622
# Check optimization status if repository info is provided
612-
already_optimized_count = 0
613-
try:
614-
repository = git.Repo(Path.cwd(), search_parent_directories=True)
615-
owner, repo = get_repo_owner_and_name(repository)
616-
except git.exc.InvalidGitRepositoryError:
617-
logger.warning("No git repository found")
618-
owner, repo = None, None
619-
pr_number = get_pr_number()
620-
if owner and repo and pr_number is not None:
621-
path_based_functions, functions_count = check_optimization_status(path_based_functions, owner, repo, pr_number)
623+
622624
initial_count = sum(len(funcs) for funcs in filtered_modified_functions.values())
623625
already_optimized_count = initial_count - functions_count
624626

codeflash/optimization/function_optimizer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import ast
4+
import git
45
import concurrent.futures
56
import os
67
import subprocess
@@ -18,6 +19,7 @@
1819
from rich.tree import Tree
1920

2021
from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
22+
from codeflash.api.cfapi import add_code_context_hash
2123
from codeflash.benchmarking.utils import process_benchmark_data
2224
from codeflash.cli_cmds.console import code_print, console, logger, progress_bar
2325
from codeflash.code_utils import env_utils
@@ -50,6 +52,8 @@
5052
from codeflash.code_utils.line_profile_utils import add_decorator_imports
5153
from codeflash.code_utils.static_analysis import get_first_top_level_function_or_method_ast
5254
from codeflash.code_utils.time_utils import humanize_runtime
55+
from codeflash.code_utils.env_utils import get_pr_number
56+
from codeflash.code_utils.git_utils import get_repo_owner_and_name
5357
from codeflash.context import code_context_extractor
5458
from codeflash.context.unused_definition_remover import detect_unused_helper_functions, revert_unused_helper_functions
5559
from codeflash.either import Failure, Success, is_successful
@@ -370,6 +374,19 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
370374
)
371375
self.log_successful_optimization(explanation, generated_tests, exp_type)
372376

377+
# Add function to code context hash if in gh actions
378+
try:
379+
repository = git.Repo(Path.cwd(), search_parent_directories=True)
380+
owner, repo = get_repo_owner_and_name(repository)
381+
except git.exc.InvalidGitRepositoryError:
382+
logger.warning("No git repository found")
383+
owner, repo = None, None
384+
pr_number = get_pr_number()
385+
386+
if owner and repo and pr_number is not None:
387+
code_context_hash = self.function_to_optimize.get_code_context_hash()
388+
add_code_context_hash(owner, repo, pr_number, code_context_hash)
389+
373390
if self.args.override_fixtures:
374391
restore_conftest(original_conftest_content)
375392
if not best_optimization:

0 commit comments

Comments
 (0)