def record_bundle_stats(job_id: UUID): create_or_update( ItemStat, where={"item_id": job_id, "name": "bundle.total_asset_size"}, values={ "value": db.session.query( func.coalesce(func.sum(BundleAsset.size), 0) ).filter( BundleAsset.job_id == job_id ).as_scalar() }, ) db.session.flush()
def record_style_violation_stats(job_id: UUID): create_or_update( ItemStat, where={"item_id": job_id, "name": "style_violations.count"}, values={ "value": db.session.query( func.coalesce(func.count(StyleViolation.id), 0) ).filter( StyleViolation.job_id == job_id ).as_scalar() }, ) db.session.flush()
def record_style_violation_stats(job_id: UUID): create_or_update( ItemStat, where={ 'item_id': job_id, 'name': 'style_violations.count', }, values={ 'value': db.session.query(func.count(StyleViolation.id)).filter( StyleViolation.job_id == job_id, ).as_scalar(), }) db.session.flush()
def _record_test_duration(self, test_list): create_or_update( ItemStat, where={ 'item_id': self.job.id, 'name': 'tests.duration', }, values={ 'value': db.session.query(func.coalesce( func.sum(TestCase.duration), 0)).filter(TestCase.job_id == self.job.id, ).as_scalar(), })
def record_bundle_stats(job_id: UUID): create_or_update( ItemStat, where={ 'item_id': job_id, 'name': 'bundle.total_asset_size', }, values={ 'value': db.session.query(func.coalesce( func.sum(BundleAsset.size), 0)).filter(BundleAsset.job_id == job_id, ).as_scalar(), }) db.session.flush()
def _record_test_failures(self, test_list): create_or_update(ItemStat, where={ 'item_id': self.job.id, 'name': 'tests.failures', }, values={ 'value': db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == self.job.id, TestCase.result == Result.failed, ).as_scalar(), }) db.session.commit()
def _record_test_counts(self, test_list): create_or_update( ItemStat, where={ "item_id": self.job.id, "name": "tests.count" }, values={ "value": db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == self.job.id).as_scalar() }, ) db.session.flush()
def _record_test_duration(self, test_list): create_or_update( ItemStat, where={ "item_id": self.job.id, "name": "tests.duration" }, values={ "value": db.session.query(func.coalesce( func.sum(TestCase.duration), 0)).filter(TestCase.job_id == self.job.id).as_scalar() }, )
def _record_test_counts(self, test_list): create_or_update( ItemStat, where={ 'item_id': self.job.id, 'name': 'tests.count', }, values={ 'value': db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == self.job.id, ).as_scalar(), } ) db.session.flush()
def config_set(repository, option): provider, owner_name, repo_name = repository.split("/", 2) repo = Repository.query.unrestricted_unsafe().filter( Repository.provider == RepositoryProvider(provider), Repository.owner_name == owner_name, Repository.name == repo_name, ).first() for key, value in [o.split("=", 1) for o in option]: create_or_update(ItemOption, where={ "item_id": repo.id, "name": key }, values={"value": value}) db.session.commit()
def save(self, repository: Repository) -> Tuple[Revision, bool]: author = self._get_author(repository, self.author) if self.author == self.committer: committer = author else: committer = self._get_author(repository, self.committer) revision, created = create_or_update( Revision, where={ 'repository': repository, 'sha': self.sha, }, values={ 'author': author, 'committer': committer, 'message': self.message, 'parents': self.parents, 'branches': self.branches, 'date_created': self.author_date, 'date_committed': self.committer_date, } ) # we also want to create a source for this item as it's the canonical # representation in the UI try_create(Source, { 'revision_sha': self.sha, 'repository': repository, 'author': author, }) return (revision, created)
def test_create_or_update_existing_instance(db_session, default_user): another_user = factories.UserFactory.create() instance, created = create_or_update(User, {"id": default_user.id}, {"email": "*****@*****.**"}) assert not created assert instance.email == "*****@*****.**" db_session.refresh(another_user) assert another_user.email != "*****@*****.**"
def aggregate_stat_for_build(build: Build, name: str, func_=func.sum): """ Aggregates a single stat for all jobs the given build. """ value = db.session.query(func.coalesce(func_(ItemStat.value), 0), ).filter( ItemStat.item_id.in_( db.session.query(Job.id).filter(Job.build_id == build.id, )), ItemStat.name == name, ).as_scalar() create_or_update( model=ItemStat, where={ 'item_id': build.id, 'name': name, }, values={'value': value}, )
def config_set(repository, option): provider, owner_name, repo_name = repository.split('/', 2) repo = Repository.query.unrestricted_unsafe().filter( Repository.provider == RepositoryProvider(provider), Repository.owner_name == owner_name, Repository.name == repo_name, ).first() for key, value in [o.split('=', 1) for o in option]: create_or_update(ItemOption, where={ 'item_id': repo.id, 'name': key, }, values={ 'value': value, }) db.session.commit()
def test_create_or_update_new_instance(db_session): another_user = factories.UserFactory.create() instance, created = create_or_update(User, {"email": "*****@*****.**"}) assert created assert instance.email == "*****@*****.**" db_session.refresh(another_user) assert another_user.email != "*****@*****.**"
def sync_repos_for_owner( provider: GitHubRepositoryProvider, user: User, owner_name: str ): repo_permissions = { r["name"]: r["permission"] for r in provider.get_repos_for_owner(user, owner_name) } if not repo_permissions: remove_access_to_owner_repos(user.id, owner_name) return # first clear any access to repos which are no longer part of the organization remove_access_to_owner_repos( user.id, owner_name, ~Repository.name.in_(repo_permissions.keys()) ) # now identify any repos which might need access granted or updated matches = list( Repository.query.unrestricted_unsafe().filter( Repository.provider == RepositoryProvider.github, Repository.owner_name == owner_name, Repository.name.in_(repo_permissions.keys()), ) ) for repo in matches: permission = repo_permissions.get(repo.name) if permission: create_or_update( RepositoryAccess, where={"repository_id": repo.id, "user_id": user.id}, values={"permission": permission}, ) else: # revoke permissions -- this path shouldnt really get hit RepositoryAccess.query.filter( RepositoryAccess.repository_id == repo.id, RepositoryAccess.user_id == user.id, ).delete()
def record_test_stats(job_id: UUID): create_or_update( ItemStat, where={"item_id": job_id, "name": "tests.count"}, values={ "value": db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == job_id ).as_scalar() }, ) create_or_update( ItemStat, where={"item_id": job_id, "name": "tests.failures"}, values={ "value": db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == job_id, TestCase.result == Result.failed ).as_scalar() }, ) create_or_update( ItemStat, where={"item_id": job_id, "name": "tests.duration"}, values={ "value": db.session.query( func.coalesce(func.sum(TestCase.duration), 0) ).filter( TestCase.job_id == job_id ).as_scalar() }, ) db.session.flush()
def record_coverage_stats(build_id: UUID): """ Aggregates all FileCoverage stats for the given build. """ coverage_stats = db.session.query( func.sum(FileCoverage.lines_covered).label('coverage.lines_covered'), func.sum( FileCoverage.lines_uncovered).label('coverage.lines_uncovered'), func.sum(FileCoverage.diff_lines_covered).label( 'coverage.diff_lines_covered'), func.sum(FileCoverage.diff_lines_uncovered).label( 'coverage.diff_lines_uncovered'), ).filter(FileCoverage.build_id == build_id, ).group_by( FileCoverage.build_id, ).first() # TODO(dcramer): it'd be safer if we did this query within SQL stat_list = ( 'coverage.lines_covered', 'coverage.lines_uncovered', 'coverage.diff_lines_covered', 'coverage.diff_lines_uncovered', ) if not any( getattr(coverage_stats, n, None) is not None for n in stat_list): ItemStat.query.filter( ItemStat.item_id == build_id, ItemStat.name.in_(stat_list)).delete(synchronize_session=False) else: for name in stat_list: create_or_update( model=ItemStat, where={ 'item_id': build_id, 'name': name, }, values={ 'value': getattr(coverage_stats, name, 0) or 0, }, )
def save(self, repository: Repository) -> Tuple[Revision, bool]: author = self._get_author(repository, self.author) if self.author == self.committer: committer = author else: committer = self._get_author(repository, self.committer) revision, created = create_or_update( Revision, where={ "repository": repository, "sha": self.sha }, values={ "author": author, "committer": committer, "message": self.message, "parents": self.parents, "branches": self.branches, "date_created": self.author_date, "date_committed": self.committer_date, }, ) # we also want to create a source for this item as it's the canonical # representation in the UI try_create( Source, { "revision_sha": self.sha, "repository": repository, "author": author }, ) return RevisionSaveResult(revision, created)
def record_test_stats(job_id: UUID): create_or_update(ItemStat, where={ 'item_id': job_id, 'name': 'tests.count', }, values={ 'value': db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == job_id, ).as_scalar(), }) create_or_update(ItemStat, where={ 'item_id': job_id, 'name': 'tests.failures', }, values={ 'value': db.session.query(func.count(TestCase.id)).filter( TestCase.job_id == job_id, TestCase.result == Result.failed, ).as_scalar(), }) create_or_update( ItemStat, where={ 'item_id': job_id, 'name': 'tests.duration', }, values={ 'value': db.session.query(func.coalesce( func.sum(TestCase.duration), 0)).filter(TestCase.job_id == job_id, ).as_scalar(), }) db.session.flush()