-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prep the terrain for reports with label compression. (#188)
context: codecov/engineering-team #768 Creates a rollout for label compression. This is gonna help us to test and safely release the feature in the wild. Notice that currently the label compression does nothing. There are comments similar to `TODO: needs shared update`. The update in question is codecov/shared#79 So these changes mostly prep the terrain for future changes that will actually do something, and add the guardrails to avoid issues when deploying. In particular it adds some helper methods to the `ArchiveService`, creates a kinda-stubbed `LabelsIndexService`, passes more data to the `_adjust_sessions` portion of `raw_upload_processor` where most changes will occur. If you're curious as to what the end result will probably look like see #180
- Loading branch information
1 parent
8e60c86
commit f751090
Showing
9 changed files
with
368 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Dict | ||
|
||
from shared.reports.resources import Report | ||
|
||
from database.models.reports import CommitReport | ||
from services.archive import ArchiveService | ||
|
||
|
||
class LabelsIndexService(object): | ||
_archive_client: ArchiveService | ||
|
||
def __init__(self, commit_report: CommitReport) -> None: | ||
self.commit_report = commit_report | ||
self._archive_client = ArchiveService( | ||
repository=commit_report.commit.repository | ||
) | ||
self.commit_sha = commit_report.commit.commitid | ||
|
||
def set_label_idx(self, report: Report): | ||
# TODO: Needs shared update. | ||
# if report._labels_index is not None: | ||
# raise Exception( | ||
# "Trying to set labels_index of Report, but it's already set" | ||
# ) | ||
# Load label index from storage | ||
# JSON uses strings are keys, but we are using ints. | ||
map_with_str_keys = self._archive_client.read_label_index( | ||
self.commit_sha, self.commit_report.code | ||
) | ||
loaded_index = {int(k): v for k, v in map_with_str_keys.items()} | ||
return loaded_index | ||
# TODO: Needs shared update. | ||
# report.set_label_idx(loaded_index) | ||
|
||
def unset_label_idx(self, report: Report, label_index: Dict[str, str]): | ||
# Write the updated index back into storage | ||
# TODO: Needs shared update | ||
# self._archive_client.write_label_index( | ||
# self.commit_sha, report._labels_index, self.commit_report.code | ||
# ) | ||
self._archive_client.write_label_index( | ||
self.commit_sha, label_index, self.commit_report.code | ||
) | ||
# Remove reference to it | ||
# TODO: Needs shared update | ||
# report.unset_label_idx() |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import pytest | ||
from shared.reports.resources import Report | ||
|
||
from database.tests.factories.core import ReportFactory | ||
from helpers.labels import SpecialLabelsEnum | ||
from services.report.labels_index import ArchiveService, LabelsIndexService | ||
|
||
|
||
class TestLabelsIndex(object): | ||
def test_init(self, dbsession, mocker): | ||
commit_report = ReportFactory() | ||
dbsession.add(commit_report) | ||
dbsession.flush() | ||
|
||
labels_index_service = LabelsIndexService(commit_report) | ||
assert labels_index_service._archive_client is not None | ||
assert labels_index_service.commit_sha == commit_report.commit.commitid | ||
|
||
def test_set_label_idx(self, dbsession, mocker): | ||
commit_report = ReportFactory() | ||
dbsession.add(commit_report) | ||
dbsession.flush() | ||
# Notice that the keys are strings | ||
# because self._archive_client.read_label_index returns the contents of a JSON file, | ||
# and JSON can only have string keys. | ||
sample_label_index = { | ||
"0": SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label, | ||
"1": "some_label", | ||
"2": "another_label", | ||
} | ||
mocker.patch.object( | ||
ArchiveService, "read_label_index", return_value=sample_label_index | ||
) | ||
report = Report() | ||
# TODO: Needs shared update | ||
# assert report._labels_index == None | ||
label_service = LabelsIndexService(commit_report) | ||
res = label_service.set_label_idx(report) | ||
assert res == { | ||
0: SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label, | ||
1: "some_label", | ||
2: "another_label", | ||
} | ||
|
||
# TODO: Needs shared update | ||
# def test_set_label_idx_already_set(self, dbsession, mocker): | ||
# commit_report = ReportFactory() | ||
# dbsession.add(commit_report) | ||
# dbsession.flush() | ||
# mock_read = mocker.patch.object(ArchiveService, "read_label_index") | ||
# sample_label_index = { | ||
# 0: SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label, | ||
# 1: "some_label", | ||
# 2: "another_label", | ||
# } | ||
# report = Report() | ||
# report._labels_index = sample_label_index | ||
# with pytest.raises(Exception) as exp: | ||
# label_service = LabelsIndexService(commit_report) | ||
# label_service.set_label_idx(report) | ||
# mock_read.assert_not_called() | ||
# assert ( | ||
# str(exp.value) | ||
# == "Trying to set labels_index of Report, but it's already set" | ||
# ) | ||
|
||
def test_unset_label_idx(self, dbsession, mocker): | ||
commit_report = ReportFactory() | ||
dbsession.add(commit_report) | ||
dbsession.flush() | ||
sample_label_index = { | ||
0: SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label, | ||
1: "some_label", | ||
2: "another_label", | ||
} | ||
mock_write = mocker.patch.object(ArchiveService, "write_label_index") | ||
report = Report() | ||
# TODO: Needs shared update | ||
# report._labels_index = sample_label_index | ||
label_service = LabelsIndexService(commit_report) | ||
label_service.unset_label_idx(report, sample_label_index) | ||
# assert report._labels_index == None | ||
mock_write.assert_called_with( | ||
commit_report.commit.commitid, sample_label_index, commit_report.code | ||
) |
Oops, something went wrong.