From 8c34a255eb6c849626100267d268723ba70daed8 Mon Sep 17 00:00:00 2001
From: Jaden Diefenbaugh <blakcap@users.noreply.github.com>
Date: Tue, 11 Apr 2017 10:59:34 +0200
Subject: [PATCH] added data migration script for reports

---
 .../migrations/0004_auto_20170410_1121.py     | 121 ++++++++++++++++++
 1 file changed, 121 insertions(+)
 create mode 100644 beat/web/reports/migrations/0004_auto_20170410_1121.py

diff --git a/beat/web/reports/migrations/0004_auto_20170410_1121.py b/beat/web/reports/migrations/0004_auto_20170410_1121.py
new file mode 100644
index 000000000..aae102538
--- /dev/null
+++ b/beat/web/reports/migrations/0004_auto_20170410_1121.py
@@ -0,0 +1,121 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.5 on 2017-04-10 11:21
+from __future__ import unicode_literals
+
+from django.db import migrations
+import json
+import re
+
+def parse_table(table, precision):
+    conv_table = {
+            'fields': ['Experiment'],
+            'precision': precision
+            }
+
+    for row in table:
+        name = row['name']
+        name = re.sub(r'\[.*\]$', '', name)
+
+        if name == 'experiment':
+            continue
+
+        if name == 'experiment.execution_time' or name == 'execution_time':
+            name = 'total_execution_time'
+        elif re.match(r'^execution_time\.', name):
+            name = re.sub(r'execution_time', 'linear_execution_time', name)
+
+        if name.startswith('experiment.'):
+            name = re.sub(r'experiment\.', '', name)
+
+        if '.' in name:
+            segs = name.split('.')
+            name = segs[1] + '.' + segs[0]
+        conv_table['fields'].append(name)
+
+    return conv_table
+
+def parse_plot(plot):
+    conv_plot = {
+        'name': plot['data']['output'][0],
+        'type': plot['required_plotter'][0]
+            }
+    return conv_plot
+
+def experiment_fullname(exp):
+    return '%s/%s/%s/%s/%s' % (exp.author.username, exp.toolchain.author.username, exp.toolchain.name, exp.toolchain.version, exp.name)
+
+def analyzer_fullname(report):
+    return '%s/%s/%s' % (report.analyzer.author.username, report.analyzer.name, report.analyzer.version)
+
+def move_content_to_groups_format(apps, schema_editor):
+    Report = apps.get_model('reports', 'Report')
+
+    for report in Report.objects.all():
+        report_content = json.loads(report.content)
+        # convert to groups format
+        if 'groups' not in report_content:
+            exps = report.experiments.all()
+            # groups: {
+            # 	group1 : {
+            # 		experiments: [],
+            # 		reportItems: [],
+            # 		analyzer: '',
+            # 		aliases: {},
+            # 		idx: 1
+            # 		},
+            # 	...
+            # }
+            obj = {}
+            groups = {}
+            group1 = {
+                    'experiments': [ experiment_fullname(e) for e in exps ],
+                    'reportItems': [],
+                    'analyzer': analyzer_fullname(report) if report.analyzer else '',
+                    'aliases': {},
+                    'idx': 1
+                    }
+            for e in exps:
+                group1['aliases'][experiment_fullname(e)] = e.name
+
+            count_tables = 0
+            count_plots = 0
+            for item_name in report_content:
+                if item_name == 'floating_point_precision' or item_name == 'alias_experiments':
+                    continue
+
+                item = report_content[item_name]
+                item_type = 'table' if item_name.startswith('table') else 'plot'
+                fpp = report_content['floating_point_precision'] if 'floating_point_precision' in report_content else 10
+
+                converted_content = parse_table(item, fpp) if item_type == 'table' else parse_plot(item)
+
+                converted_id = ''
+                if item_type == 'table':
+                    converted_id = 'table_' + str(count_tables)
+                    count_tables += 1
+                else:
+                    converted_id = 'plot_' + str(count_plots)
+                    count_plots += 1
+
+                converted_item = {
+                        'id': converted_id,
+                        'content': converted_content
+                        }
+
+                group1['reportItems'].append(converted_item)
+
+            groups['group1'] = group1
+            obj['groups'] = groups
+            report.content = json.dumps(obj)
+            report.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('reports', '0003_report_last_edited_date'),
+    ]
+
+    operations = [
+            migrations.RunPython(move_content_to_groups_format)
+    ]
-- 
GitLab