Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add project count in analysisframework schema #1404

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/analysis_framework/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from promise import Promise
from django.utils.functional import cached_property
from django.db import models
from project.models import Project

from utils.graphene.dataloaders import DataLoaderWithContext, WithContextMixin

Expand Down Expand Up @@ -102,6 +103,38 @@ def batch_load_fn(self, keys):
return Promise.resolve([_map[key] for key in keys])


class AnalysisFrameworkProjectCountLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
stat_qs = Project.objects\
.filter(analysis_framework__in=keys)\
.order_by('analysis_framework').values('analysis_framework')\
.annotate(
project_count=models.functions.Coalesce(
models.Count(
'id',
filter=models.Q(is_test=False)
),
0,
),
test_project_count=models.functions.Coalesce(
models.Count(
'id',
filter=models.Q(is_test=True)
),
0,
),
).values('analysis_framework', 'project_count', 'test_project_count')
_map = {
stat.pop('analysis_framework'): stat
for stat in stat_qs
}
_dummy = {
'project_count': 0,
'test_project_count': 0,
}
return Promise.resolve([_map.get(key, _dummy) for key in keys])


class DataLoaders(WithContextMixin):
@cached_property
def secondary_widgets(self):
Expand Down Expand Up @@ -130,3 +163,7 @@ def members(self):
@cached_property
def af_tags(self):
return AnalysisFrameworkTagsLoader(context=self.context)

@cached_property
def af_project_count(self):
return AnalysisFrameworkProjectCountLoader(context=self.context)
10 changes: 10 additions & 0 deletions apps/analysis_framework/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class Meta:
icon = graphene.Field(FileFieldType, required=False)


class AnalysisFrameworkProjectCount(graphene.ObjectType):
project_count = graphene.Int(required=True)
test_project_count = graphene.Int(required=True)


# NOTE: We have AnalysisFrameworkDetailType for detailed AF Type.
class AnalysisFrameworkType(DjangoObjectType):
class Meta:
Expand All @@ -122,6 +127,7 @@ class Meta:
),
required=True,
)
used_in_project_count = graphene.Field(AnalysisFrameworkProjectCount, required=True)

@staticmethod
def get_custom_node(_, info, id):
Expand All @@ -147,6 +153,10 @@ def resolve_allowed_permissions(root, info):
def resolve_tags(root, info):
return info.context.dl.analysis_framework.af_tags.load(root.id)

@staticmethod
def resolve_used_in_project_count(root, info):
return info.context.dl.analysis_framework.af_project_count.load(root.id)


class AnalysisFrameworkRoleType(DjangoObjectType):
class Meta:
Expand Down
7 changes: 7 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type AnalysisFrameworkDetailType {
clonedFrom: ID
allowedPermissions: [AnalysisFrameworkPermission!]!
tags: [AnalysisFrameworkTagType!]!
usedInProjectCount: AnalysisFrameworkProjectCount!
primaryTagging: [SectionType!]
secondaryTagging: [WidgetType!]
members: [AnalysisFrameworkMembershipType!]
Expand Down Expand Up @@ -136,6 +137,11 @@ type AnalysisFrameworkPredictionMappingType {
widgetType: WidgetWidgetTypeEnum!
}

type AnalysisFrameworkProjectCount {
projectCount: Int!
testProjectCount: Int!
}

input AnalysisFrameworkPropertiesGqlInputType {
statsConfig: AnalysisFrameworkPropertiesStatsConfigInputType
}
Expand Down Expand Up @@ -218,6 +224,7 @@ type AnalysisFrameworkType {
clonedFrom: ID
allowedPermissions: [AnalysisFrameworkPermission!]!
tags: [AnalysisFrameworkTagType!]!
usedInProjectCount: AnalysisFrameworkProjectCount!
}

type AnalysisFrameworkVisibleProjectType {
Expand Down
Loading