Skip to content

Commit

Permalink
Mkerge branch 'main' of github.com:codecov/worker into 692-create-sav…
Browse files Browse the repository at this point in the history
…e_commit_measurement-task
  • Loading branch information
adrian-codecov committed Feb 8, 2024
2 parents 98822c4 + 646126a commit f1bdc86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
15 changes: 11 additions & 4 deletions services/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def create_report_upload(
class ReportService(BaseReportService):
metrics_prefix = "services.report"

def __init__(self, current_yaml: UserYaml):
super().__init__(current_yaml)
self.flag_dict = None

def has_initialized_report(self, commit: Commit) -> bool:
"""Says whether a commit has already initialized its report or not
Expand Down Expand Up @@ -302,25 +306,28 @@ def _attach_flags_to_upload(self, upload: Upload, flag_names: Sequence[str]):
db_session = upload.get_db_session()
repoid = upload.report.commit.repoid

existing_flag_dict = self.get_existing_flag_dict(db_session, repoid)
if self.flag_dict is None:
self.fetch_repo_flags(db_session, repoid)

for individual_flag in flag_names:
flag_obj = existing_flag_dict.get(individual_flag, None)
flag_obj = self.flag_dict.get(individual_flag, None)
if flag_obj is None:
flag_obj = RepositoryFlag(
repository_id=repoid, flag_name=individual_flag
)
db_session.add(flag_obj)
db_session.flush()
self.flag_dict[individual_flag] = flag_obj
all_flags.append(flag_obj)
upload.flags = all_flags
db_session.flush()
return all_flags

def get_existing_flag_dict(self, db_session, repoid):
def fetch_repo_flags(self, db_session, repoid):
existing_flags_on_repo = (
db_session.query(RepositoryFlag).filter_by(repository_id=repoid).all()
)
return {flag.flag_name: flag for flag in existing_flags_on_repo}
self.flag_dict = {flag.flag_name: flag for flag in existing_flags_on_repo}

def build_files(
self, report_details: ReportDetails
Expand Down
16 changes: 12 additions & 4 deletions services/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Mapping, Sequence

from shared.torngit.exceptions import TorngitClientError
from shared.yaml import UserYaml
from sqlalchemy import desc
from test_results_parser import Outcome

Expand All @@ -19,6 +20,10 @@


class TestResultsReportService(BaseReportService):
def __init__(self, current_yaml: UserYaml):
super().__init__(current_yaml)
self.flag_dict = None

async def initialize_and_save_report(
self, commit: Commit, report_code: str = None
) -> CommitReport:
Expand Down Expand Up @@ -69,25 +74,28 @@ def _attach_flags_to_upload(self, upload: Upload, flag_names: Sequence[str]):
db_session = upload.get_db_session()
repoid = upload.report.commit.repoid

existing_flag_dict = self.get_existing_flag_dict(db_session, repoid)
if self.flag_dict is None:
self.fetch_repo_flags(db_session, repoid)

for individual_flag in flag_names:
flag_obj = existing_flag_dict.get(individual_flag, None)
flag_obj = self.flag_dict.get(individual_flag, None)
if flag_obj is None:
flag_obj = RepositoryFlag(
repository_id=repoid, flag_name=individual_flag
)
db_session.add(flag_obj)
db_session.flush()
self.flag_dict[individual_flag] = flag_obj
all_flags.append(flag_obj)
upload.flags = all_flags
db_session.flush()
return all_flags

def get_existing_flag_dict(self, db_session, repoid):
def fetch_repo_flags(self, db_session, repoid):
existing_flags_on_repo = (
db_session.query(RepositoryFlag).filter_by(repository_id=repoid).all()
)
return {flag.flag_name: flag for flag in existing_flags_on_repo}
self.flag_dict = {flag.flag_name: flag for flag in existing_flags_on_repo}


def generate_flags_hash(flag_names):
Expand Down
2 changes: 2 additions & 0 deletions tasks/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ async def run_async_within_lock(
upload_context.prepare_kwargs_for_retry(kwargs)
self.retry(countdown=60, kwargs=kwargs)
argument_list = []

for arguments in upload_context.arguments_list():
normalized_arguments = upload_context.normalize_arguments(commit, arguments)
if "upload_id" in normalized_arguments:
Expand All @@ -492,6 +493,7 @@ async def run_async_within_lock(
upload = report_service.create_report_upload(
normalized_arguments, commit_report
)

normalized_arguments["upload_pk"] = upload.id_
argument_list.append(normalized_arguments)
if argument_list:
Expand Down

0 comments on commit f1bdc86

Please sign in to comment.