Skip to content
Snippets Groups Projects
Commit 8c34a255 authored by Jaden Diefenbaugh's avatar Jaden Diefenbaugh
Browse files

added data migration script for reports

parent 50d9581e
No related branches found
No related tags found
1 merge request!223Reports overhaul
# -*- 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)
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment