Skip to content

Commit

Permalink
Merge pull request #1404 from the-deep/feature/add-project-count-in-a…
Browse files Browse the repository at this point in the history
…nalysisframework

add project count in analysisframework schema
  • Loading branch information
AdityaKhatri authored Jan 10, 2024
2 parents 4468790 + 5fb57f5 commit 49fd3ba
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
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

0 comments on commit 49fd3ba

Please sign in to comment.