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

Reporting Module: Add Basic Model/Query/Mutation #1357

Merged
merged 9 commits into from
Nov 8, 2023
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
98 changes: 98 additions & 0 deletions apps/analysis/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
from .models import (
Analysis,
AnalysisPillar,
AnalysisReport,
AnalyticalStatement,
AnalyticalStatementEntry,
DiscardedEntry,
TopicModelCluster,
AnalysisReportUpload,
AnalysisReportContainerData,
AnalysisReportContainer,
AnalysisReportSnapshot,
)


Expand Down Expand Up @@ -140,6 +145,75 @@ def batch_load_fn(self, keys):
return Promise.resolve([_map.get(key, []) for key in keys])


# -------------- Report Module -------------------------------
class AnalysisReportUploadsLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportUpload.objects.filter(
id__in=keys,
)
_map = {
item.pk: item
for item in qs
}
return Promise.resolve([_map.get(key, []) for key in keys])


class AnalysisReportContainerDataByContainerLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportContainerData.objects.filter(
container__in=keys,
)
_map = defaultdict(list)
for item in qs:
_map[item.container_id].append(item)
return Promise.resolve([_map.get(key, []) for key in keys])


class OrganizationByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReport.organizations.through.objects.filter(
analysisreport__in=keys,
).select_related('organization')
_map = defaultdict(list)
for item in qs:
_map[item.analysisreport_id].append(item.organization)
return Promise.resolve([_map[key] for key in keys])


class ReportUploadByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportUpload.objects.filter(
report__in=keys,
)
_map = defaultdict(list)
for item in qs:
_map[item.report_id].append(item)
return Promise.resolve([_map[key] for key in keys])


class AnalysisReportContainerByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportContainer.objects.filter(
report__in=keys,
)
_map = defaultdict(list)
for item in qs:
_map[item.report_id].append(item)
return Promise.resolve([_map[key] for key in keys])


class LatestReportSnapshotByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportSnapshot.objects.filter(
report__in=keys,
).order_by('report_id', '-published_on').distinct('report_id')
_map = {
snapshot.report_id: snapshot
for snapshot in qs
}
return Promise.resolve([_map.get(key) for key in keys])


class DataLoaders(WithContextMixin):
@cached_property
def analysis_publication_date(self):
Expand Down Expand Up @@ -176,3 +250,27 @@ def analytical_statement_entries(self):
@cached_property
def topic_model_cluster_entries(self):
return AnalysisTopicModelClusterEntryLoader(context=self.context)

@cached_property
def analysis_report_uploads(self):
return AnalysisReportUploadsLoader(context=self.context)

@cached_property
def analysis_report_container_data_by_container(self):
return AnalysisReportContainerDataByContainerLoader(context=self.context)

@cached_property
def organization_by_analysis_report(self):
return OrganizationByAnalysisReportLoader(context=self.context)

@cached_property
def analysis_report_uploads_by_analysis_report(self):
return ReportUploadByAnalysisReportLoader(context=self.context)

@cached_property
def analysis_report_container_by_analysis_report(self):
return AnalysisReportContainerByAnalysisReportLoader(context=self.context)

@cached_property
def latest_report_snapshot_by_analysis_report(self):
return LatestReportSnapshotByAnalysisReportLoader(context=self.context)
45 changes: 45 additions & 0 deletions apps/analysis/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
AutomaticSummary,
AnalyticalStatementNGram,
AnalyticalStatementGeoTask,
AnalysisReportUpload,
AnalysisReportContainer,
)
from .serializers import (
ReportEnum,
AnalysisReportVariableSerializer,
AnalysisReportTextStyleSerializer,
AnalysisReportBorderStyleSerializer,
AnalysisReportImageContentStyleSerializer,
AnalysisReportHeadingConfigurationSerializer,
)


DiscardedEntryTagTypeEnum = convert_enum_to_graphene_enum(DiscardedEntry.TagType, name='DiscardedEntryTagTypeEnum')

Expand All @@ -21,6 +32,26 @@
AnalyticalStatementGeoTaskStatusEnum = convert_enum_to_graphene_enum(
AnalyticalStatementGeoTask.Status, name='AnalyticalStatementGeoTaskStatusEnum')

# Analysis Report
AnalysisReportUploadTypeEnum = convert_enum_to_graphene_enum(AnalysisReportUpload.Type, name='AnalysisReportUploadTypeEnum')
AnalysisReportContainerContentTypeEnum = convert_enum_to_graphene_enum(
AnalysisReportContainer.ContentType, name='AnalysisReportContainerContentTypeEnum')


# Client Side Enums

AnalysisReportVariableTypeEnum = convert_enum_to_graphene_enum(
ReportEnum.VariableType, name='AnalysisReportVariableTypeEnum')
AnalysisReportTextStyleAlignEnum = convert_enum_to_graphene_enum(
ReportEnum.TextStyleAlign, name='AnalysisReportTextStyleAlignEnum')
AnalysisReportBorderStyleStyleEnum = convert_enum_to_graphene_enum(
ReportEnum.BorderStyleStyle, name='AnalysisReportBorderStyleStyleEnum')
AnalysisReportImageContentStyleFitEnum = convert_enum_to_graphene_enum(
ReportEnum.ImageContentStyleFit, name='AnalysisReportImageContentStyleFitEnum')
AnalysisReportHeadingConfigurationVariantEnum = convert_enum_to_graphene_enum(
ReportEnum.HeadingConfigurationVariant, name='AnalysisReportHeadingConfigurationVariantEnum')

# Model field mapping
enum_map = {
# Need to pass model with abstract base class
get_enum_name_from_django_field(field, model_name=model.__name__): enum
Expand All @@ -30,5 +61,19 @@
(AutomaticSummary, AutomaticSummary.status, AutomaticSummaryStatusEnum),
(AnalyticalStatementNGram, AnalyticalStatementNGram.status, AnalyticalStatementNGramStatusEnum),
(AnalyticalStatementGeoTask, AnalyticalStatementGeoTask.status, AnalyticalStatementGeoTaskStatusEnum),
(AnalysisReportUpload, AnalysisReportUpload.type, AnalysisReportUploadTypeEnum),
(AnalysisReportContainer, AnalysisReportContainer.content_type, AnalysisReportContainerContentTypeEnum),
)
}

# Serializers field mapping
enum_map.update({
get_enum_name_from_django_field(serializer().fields[field]): enum
for serializer, field, enum in [
(AnalysisReportVariableSerializer, 'type', AnalysisReportVariableTypeEnum),
(AnalysisReportTextStyleSerializer, 'align', AnalysisReportTextStyleAlignEnum),
(AnalysisReportBorderStyleSerializer, 'style', AnalysisReportBorderStyleStyleEnum),
(AnalysisReportImageContentStyleSerializer, 'fit', AnalysisReportImageContentStyleFitEnum),
(AnalysisReportHeadingConfigurationSerializer, 'variant', AnalysisReportHeadingConfigurationVariantEnum),
]
})
16 changes: 16 additions & 0 deletions apps/analysis/factories.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import factory
from factory.django import DjangoModelFactory

from gallery.factories import FileFactory
from .models import (
Analysis,
AnalysisPillar,
AnalyticalStatement,
AnalyticalStatementEntry,
DiscardedEntry,
AnalysisReport,
AnalysisReportUpload,
)


Expand Down Expand Up @@ -44,3 +47,16 @@ class AnalyticalStatementEntryFactory(DjangoModelFactory):

class Meta:
model = AnalyticalStatementEntry


class AnalysisReportFactory(DjangoModelFactory):
class Meta:
model = AnalysisReport


class AnalysisReportUploadFactory(DjangoModelFactory):
type = AnalysisReportUpload.Type.CSV
file = factory.SubFactory(FileFactory)

class Meta:
model = AnalysisReportUpload
53 changes: 52 additions & 1 deletion apps/analysis/filter_set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import django_filters
from django.db import models
from django.db.models.functions import Coalesce

from utils.graphene.filters import IDListFilter, MultipleInputFilter
from user_resource.filters import UserResourceGqlFilterSet
Expand All @@ -9,8 +11,14 @@
AnalysisPillar,
DiscardedEntry,
AnalyticalStatement,
AnalysisReport,
AnalysisReportUpload,
AnalysisReportSnapshot,
)
from .enums import (
DiscardedEntryTagTypeEnum,
AnalysisReportUploadTypeEnum,
)
from .enums import DiscardedEntryTagTypeEnum


class AnalysisFilterSet(django_filters.FilterSet):
Expand Down Expand Up @@ -93,3 +101,46 @@ class AnalysisPillarDiscardedEntryGqlFilterSet(django_filters.FilterSet):
class Meta:
model = DiscardedEntry
fields = []


class AnalysisReportGQFilterSet(django_filters.FilterSet):
search = django_filters.CharFilter(method='search_filter')
analyses = IDListFilter(field_name='analysis')
is_public = django_filters.BooleanFilter(method='filter_discarded')
organizations = IDListFilter(method='organizations_filter')

class Meta:
model = AnalysisReport
fields = []

def organizations_filter(self, qs, _, value):
if value:
qs = qs.annotate(
authoring_organizations=Coalesce('authors__parent_id', 'authors__id')
).filter(authoring_organizations__in=value).distinct()
return qs

def search_filter(self, qs, _, value):
if value:
qs = qs.filter(
models.Q(slug__icontains=value) |
models.Q(title__icontains=value)
).distinct()
return qs


class AnalysisReportUploadGQFilterSet(django_filters.FilterSet):
report = IDListFilter(field_name='report')
types = MultipleInputFilter(AnalysisReportUploadTypeEnum, field_name='type')

class Meta:
model = AnalysisReportUpload
fields = []


class AnalysisReportSnapshotGQFilterSet(django_filters.FilterSet):
report = IDListFilter(field_name='report')

class Meta:
model = AnalysisReportSnapshot
fields = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Generated by Django 3.2.17 on 2023-08-23 12:24

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('organization', '0012_organization_popularity'),
('gallery', '0020_merge_0019_auto_20210120_0443_0019_auto_20210503_0431'),
('analysis', '0010_analyticalstatementgeoentry_analyticalstatementgeotask'),
]

operations = [
migrations.CreateModel(
name='AnalysisReport',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('modified_at', models.DateTimeField(auto_now=True)),
('client_id', models.CharField(blank=True, default=None, max_length=128, null=True, unique=True)),
('is_public', models.BooleanField(default=False, help_text='A report should be public for "shareable link" to be accessible by')),
('slug', models.CharField(db_index=True, help_text='For sharing we use this slug to get the permalink', max_length=255, unique=True)),
('title', models.CharField(max_length=255)),
('sub_title', models.CharField(max_length=255)),
('configuration', models.JSONField(default=dict)),
('analysis', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='analysis.analysis')),
('created_by', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='analysisreport_created', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='analysisreport_modified', to=settings.AUTH_USER_MODEL)),
('organizations', models.ManyToManyField(blank=True, to='organization.Organization')),
],
options={
'ordering': ['-created_at'],
'abstract': False,
},
),
migrations.CreateModel(
name='AnalysisReportContainer',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('row', models.SmallIntegerField()),
('column', models.SmallIntegerField()),
('width', models.SmallIntegerField(validators=[django.core.validators.MaxValueValidator(12), django.core.validators.MinValueValidator(1)])),
('height', models.SmallIntegerField(blank=True, null=True)),
('style', models.JSONField(default=dict)),
('content_type', models.SmallIntegerField(choices=[(1, 'Text'), (2, 'Heading'), (3, 'Image'), (4, 'URL')])),
('content_style', models.JSONField(default=dict)),
('content_configuration', models.JSONField(default=dict)),
('report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='analysis.analysisreport')),
],
),
migrations.CreateModel(
name='AnalysisReportUpload',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('type', models.SmallIntegerField(choices=[(1, 'CSV'), (2, 'XLSX'), (3, 'GeoJson'), (4, 'Image')])),
('metadata', models.JSONField(default=dict)),
('file', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='gallery.File')),
('report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='analysis.analysisreport')),
],
),
migrations.CreateModel(
name='AnalysisReportSnapshot',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('modified_at', models.DateTimeField(auto_now=True)),
('client_id', models.CharField(blank=True, default=None, max_length=128, null=True, unique=True)),
('published_on', models.DateTimeField(auto_now_add=True)),
('report_data_file', models.FileField(upload_to='analysis_report_snapshot/')),
('created_by', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='analysisreportsnapshot_created', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='analysisreportsnapshot_modified', to=settings.AUTH_USER_MODEL)),
('published_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='analysis.analysisreport')),
],
options={
'ordering': ['-created_at'],
'abstract': False,
},
),
migrations.CreateModel(
name='AnalysisReportContainerData',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('data', models.JSONField(default=dict)),
('container', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='analysis.analysisreportcontainer')),
('upload', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='analysis.analysisreportupload')),
],
),
]
Loading
Loading