Skip to content

Commit

Permalink
Merge pull request #92 from codecov/359-adjust-monthly-upload-limit-f…
Browse files Browse the repository at this point in the history
…or-trialled-customers

Adjust logic for trialing customers monthly upload limit
adrian-codecov authored Sep 7, 2023
2 parents b360665 + 17c53d1 commit 4a4bb80
Showing 3 changed files with 85 additions and 18 deletions.
4 changes: 4 additions & 0 deletions database/tests/factories/core.py
Original file line number Diff line number Diff line change
@@ -44,6 +44,9 @@ class Meta:
service = factory.Iterator(["gitlab", "github", "bitbucket"])
free = 0
unencrypted_oauth_token = factory.LazyFunction(lambda: uuid4().hex)
trial_start_date = datetime.now()
trial_end_date = datetime.now()
trial_status = enums.TrialStatus.NOT_STARTED.value

oauth_token = factory.LazyAttribute(
lambda o: encrypt_oauth_token(o.unencrypted_oauth_token)
@@ -222,6 +225,7 @@ class Meta:
upload_extras = {}
upload_type = "uploaded"
storage_path = "storage/path.txt"
created_at = datetime.now()


class UploadLevelTotalsFactory(Factory):
49 changes: 32 additions & 17 deletions services/decoration.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@

from sqlalchemy import func

from database.enums import Decoration
from conftest import dbsession
from database.enums import Decoration, TrialStatus
from database.models import Commit, Owner, Repository
from database.models.reports import CommitReport, Upload
from services.billing import BillingPlan, is_pr_billing_plan
@@ -22,6 +23,7 @@
"29139614+renovate[bot]@users.noreply.github.com",
]
BOT_USER_IDS = ["29139614"] # renovate[bot] github
USER_BASIC_LIMIT_UPLOAD = 250


@dataclass
@@ -37,6 +39,34 @@ def _is_bot_account(author: Owner) -> bool:
return author.email in BOT_USER_EMAILS or author.service_id in BOT_USER_IDS


def determine_uploads_used(db_session, org: Owner) -> int:
query = (
db_session.query(Upload)
.join(CommitReport)
.join(Commit)
.join(Repository)
.filter(
Upload.upload_type == "uploaded",
Repository.ownerid == org.ownerid,
Repository.private == True,
Upload.created_at >= (datetime.now() - timedelta(days=30)),
Commit.timestamp >= (datetime.now() - timedelta(days=60)),
)
)

if (
org.trial_status == TrialStatus.EXPIRED.value
and org.trial_start_date
and org.trial_end_date
):
query = query.filter(
(Upload.created_at >= org.trial_end_date)
| (Upload.created_at <= org.trial_start_date)
)

return query.limit(USER_BASIC_LIMIT_UPLOAD).count()


def determine_decoration_details(
enriched_pull: EnrichedPull, empty_upload=None
) -> dict:
@@ -118,22 +148,7 @@ def determine_decoration_details(
)

# TODO declare this to be shared between codecov-api and worker
USER_BASIC_LIMIT_UPLOAD = 250
uploads_used = (
db_session.query(Upload)
.join(CommitReport)
.join(Commit)
.join(Repository)
.filter(
Upload.upload_type == "uploaded",
Repository.ownerid == org.ownerid,
Repository.private == True,
Upload.created_at >= (datetime.now() - timedelta(days=30)),
Commit.timestamp >= (datetime.now() - timedelta(days=60)),
)
.limit(USER_BASIC_LIMIT_UPLOAD)
.count()
)
uploads_used = determine_uploads_used(db_session=db_session, org=org)

if (
org.plan == BillingPlan.users_basic.value
50 changes: 49 additions & 1 deletion services/tests/test_decoration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
from datetime import datetime, timedelta

import pytest

from database.enums import TrialStatus
from database.models.reports import Upload
from database.tests.factories import (
CommitFactory,
OwnerFactory,
@@ -15,6 +17,7 @@
Decoration,
_is_bot_account,
determine_decoration_details,
determine_uploads_used,
)
from services.repository import EnrichedPull

@@ -240,6 +243,51 @@ def test_decoration_type_unlimited_upload_on_enterprise(
assert decoration_details.decoration_type != Decoration.upload_limit
assert decoration_details.reason != "Org has exceeded the upload limit"

def test_uploads_used_with_expired_trial(self, dbsession):
owner = OwnerFactory.create(
service="github",
trial_status=TrialStatus.EXPIRED.value,
trial_start_date=datetime.now() + timedelta(days=-10),
trial_end_date=datetime.now() + timedelta(days=-2),
)
dbsession.add(owner)
dbsession.flush()

repository = RepositoryFactory.create(
owner=owner,
private=True,
)
dbsession.add(repository)
dbsession.flush()

commit = CommitFactory.create(
repository=repository,
author__service="github",
timestamp=datetime.now(),
)

report = ReportFactory.create(commit=commit)
report_before_trial = UploadFactory.create(report=report, storage_path="url")
report_before_trial.created_at += timedelta(days=-12)
dbsession.add(report_before_trial)
dbsession.flush()

report_during_trial = UploadFactory.create(report=report, storage_path="url")
report_during_trial.created_at += timedelta(days=-5)
dbsession.add(report_during_trial)
dbsession.flush()

report_after_trial = UploadFactory.create(report=report, storage_path="url")
dbsession.add(report_after_trial)
dbsession.flush()

uploads_present = dbsession.query(Upload).all()
assert len(uploads_present) == 3

uploads_used = determine_uploads_used(dbsession, owner)

assert uploads_used == 2

def test_get_decoration_type_no_pull(self, mocker):
decoration_details = determine_decoration_details(None)

0 comments on commit 4a4bb80

Please sign in to comment.