0004_auto_20170410_1121.py 5.17 KB
Newer Older
1
2
3
4
5
6
7
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2017-04-10 11:21
from __future__ import unicode_literals

import json
import re

8
9
10
from django.db import migrations


11
12
# parses a table from the old representation (a list of column objects)
# and returns a table in the new representation
13
def parse_table(table, precision):
14
    conv_table = {"itemName": "Table", "fields": ["Experiment"], "precision": precision}
15
16

    for row in table:
17
        if not row["selected"]:
18
19
            continue

20
21
        name = row["name"]
        name = re.sub(r"\[.*\]$", "", name)
22

23
        if name == "experiment":
24
25
            continue

26
27
28
29
        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)
30

31
32
        if name.startswith("experiment."):
            name = re.sub(r"experiment\.", "", name)
33

34
35
36
37
        if "." in name:
            segs = name.split(".")
            name = segs[1] + "." + segs[0]
        conv_table["fields"].append(name)
38
39
40

    return conv_table

41

42
43
# parses a plot from the old representation
# and returns a plot in the new representation
44
45
def parse_plot(plot):
    conv_plot = {
46
47
48
        "itemName": plot["required_plotter"][0],
        "name": plot["data"]["output"][0],
        "type": plot["required_plotter"][0],
Philip ABBET's avatar
Philip ABBET committed
49
    }
50
51
    return conv_plot

52

53
# helper func to build the experiment's full name
54
def experiment_fullname(exp):
55
56
57
58
59
60
61
62
    return "%s/%s/%s/%s/%s" % (
        exp.author.username,
        exp.toolchain.author.username,
        exp.toolchain.name,
        exp.toolchain.version,
        exp.name,
    )

63

64
# helper func to build the analyzer's full name
65
def analyzer_fullname(report):
66
67
68
69
70
71
    return "%s/%s/%s" % (
        report.analyzer.author.username,
        report.analyzer.name,
        report.analyzer.version,
    )

72

73
# converts an old report into the new report format
74
def move_content_to_groups_format(apps, schema_editor):
75
    Report = apps.get_model("reports", "Report")
76
77

    for report in Report.objects.all():
78
        # all of the changes are in the report's content field
79
        report_content = json.loads(report.content)
80
81
82

        # convert to groups format, but don't touch any report thats
        # already using the new system
83
        if "groups" not in report_content:
84
85
            # format:
            # ----
86
87
88
89
90
91
92
93
94
95
            # groups: {
            # 	group1 : {
            # 		experiments: [],
            # 		reportItems: [],
            # 		analyzer: '',
            # 		aliases: {},
            # 		idx: 1
            # 		},
            # 	...
            # }
96
97
            # ----
            exps = report.experiments.all()
98
99
            obj = {}
            groups = {}
100
            # default to just one group that contains all experiments/items
101
            group1 = {
Philip ABBET's avatar
Philip ABBET committed
102
                # list of experiments in the group
103
104
105
106
107
108
                "experiments": [experiment_fullname(e) for e in exps],
                "reportItems": [],
                # analyzer of the report
                "analyzer": analyzer_fullname(report) if report.analyzer else "",
                "aliases": {},
                "idx": 1,
Philip ABBET's avatar
Philip ABBET committed
109
            }
110

111
112
113
114
115
            old_aliases = (
                report_content["alias_experiments"]
                if "alias_experiments" in report_content
                else {}
            )
116
            # assign aliases
117
118
119
120
121
            get_alias = (
                lambda exp_name: old_aliases[exp_name]
                if exp_name in old_aliases
                else None
            )
122
            for e in exps:
123
                fullname = experiment_fullname(e)
124
                group1["aliases"][fullname] = get_alias(fullname) or e.name
125
126
127
128

            count_tables = 0
            count_plots = 0
            for item_name in report_content:
129
130
131
132
                if (
                    item_name == "floating_point_precision"
                    or item_name == "alias_experiments"
                ):
133
134
135
                    continue

                item = report_content[item_name]
136
137
138
139
140
141
142
143
144
145
146
147
148
149
                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)
150
151
                    count_tables += 1
                else:
152
                    converted_id = "plot_" + str(count_plots)
153
154
                    count_plots += 1

155
                converted_item = {"id": converted_id, "content": converted_content}
156

157
                group1["reportItems"].append(converted_item)
158

159
160
            groups["group1"] = group1
            obj["groups"] = groups
161
162
163
164
165
166
167
            report.content = json.dumps(obj)
            report.save()


class Migration(migrations.Migration):

    dependencies = [
168
        ("reports", "0003_report_last_edited_date"),
169
170
    ]

171
    operations = [migrations.RunPython(move_content_to_groups_format)]