Skip to content

Commit

Permalink
generate the project viz stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed May 7, 2024
1 parent 7dbffd4 commit 8aae8b6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
79 changes: 61 additions & 18 deletions apps/analysis_framework/migrations/0041_widget_mapping.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,69 @@
# Generated by Django 3.2.17 on 2024-02-22 05:02
from django.db import migrations
import copy

from django.db import migrations,transaction

@transaction.atomic
def analysis_framework_widgets_mapping(apps, schema_editor):
AnalysisFramework = apps.get_model('analysis_framework', 'AnalysisFramework')
analysis_framework_data = AnalysisFramework.objects.filter(properties__isnull=False)
for af in analysis_framework_data:
if af.properties != {}:
widget_properties = af.properties
widget_properties['stats_config']['widget_1d'] = widget_properties['stats_config'].get('widget1d') or widget_properties['stats_config'].get('widget_1d') or [] # noqa
widget_properties['stats_config']['widget_2d'] = widget_properties['stats_config'].get('widget2d') or widget_properties['stats_config'].get('widget_2d') or [] # noqa
widget_properties['stats_config']['organigram_widgets'] = widget_properties['stats_config'].get('affected_groups_widgets') or widget_properties['stats_config'].get('organigram_widgets') or widget_properties['stats_config'].get('organigram_widget') or [] # noqa
widget_properties['stats_config']['multiselect_widgets'] = widget_properties['stats_config'].get('specific_needs_groups_widgets') or widget_properties['stats_config'].get('multiselect_widgets') or widget_properties['stats_config'].get('multiselect_widget') or [] # noqa
if 'old_stats_config' in widget_properties:
widget_properties.pop('old_stats_config')
remove_key = ['widget2d', 'widget1d', 'affected_groups_widgets', 'specific_needs_groups_widgets', 'affected_groups_widget', 'specific_needs_groups_widget'] # noqa
for widget_key in list(widget_properties['stats_config'].keys()):
if widget_key in remove_key:
del widget_properties['stats_config'][widget_key]
af.properties = widget_properties
af.save()
af_qs = AnalysisFramework.objects.filter(properties__isnull=False)
for af in af_qs:
if af.prop == {}:
continue
af_prop = af.properties
# Remove super legacy config
af_prop.pop('old_stats_config', None)

# For reassurance
af_prop = {
'old_stats_config': copy.deepcopy(af_prop),
}

# Migrate legacy config to latest
# -- Widget1D
af_prop['stats_config']['widget_1d'] = (
af_prop['stats_config'].get('widget_1d') or
af_prop['stats_config'].get('widget1d') or
[]
)

# -- Widget2D
af_prop['stats_config']['widget_2d'] = (
af_prop['stats_config'].get('widget_2d') or
af_prop['stats_config'].get('widget2d') or
[]
)

# -- Organigram
af_prop['stats_config']['organigram_widgets'] = (
af_prop['stats_config'].get('affected_groups_widgets') or
af_prop['stats_config'].get('organigram_widgets') or
af_prop['stats_config'].get('organigram_widget') or
[]
)

# -- Multiselect
af_prop['stats_config']['multiselect_widgets'] = (
af_prop['stats_config'].get('specific_needs_groups_widgets') or
af_prop['stats_config'].get('multiselect_widgets') or
af_prop['stats_config'].get('multiselect_widget') or
af_prop['stats_config'].get('demographic_group_widget') or
[]
)

legacy_widget_keys = [
'widget1d',
'widget2d',
'affected_groups_widgets',
'specific_needs_groups_widgets',
'affected_groups_widget',
'specific_needs_groups_widget',
'demographic_group_widget',
]
for widget_key in legacy_widget_keys:
af_prop['stats_config'].pop(widget_key, None)

af.properties = af_prop
af.save(update_fields=('properties',))


class Migration(migrations.Migration):
Expand Down
20 changes: 20 additions & 0 deletions apps/project/management/commands/generate_projects_viz_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand

from project.models import Project

from project.tasks import generate_viz_stats


class Command(BaseCommand):
help = 'Generate the Project Viz Stats'

def handle(self, *arg, **options):
generate_project_viz_stats()


def generate_project_viz_stats():
project_qs = Project.objects.filter(
is_visualization_enabled=True
)
for project in project_qs:
generate_viz_stats(project.id, force=True)

0 comments on commit 8aae8b6

Please sign in to comment.