aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Wood <michael.g.wood@intel.com>2015-06-05 17:04:13 +0100
committerMichael Wood <michael.g.wood@intel.com>2015-06-05 17:04:13 +0100
commitf66d78b26ab4d88c0c00ea6b1f2f8897daca852a (patch)
treef6c1c49b5011620a40e8a1b0f2118f2cf504c502
parent2543753ff9b5fff56e48005c0f189fef5847fdf6 (diff)
downloaderror-report-web-f66d78b26ab4d88c0c00ea6b1f2f8897daca852a.tar.gz
error-report-web-f66d78b26ab4d88c0c00ea6b1f2f8897daca852a.tar.bz2
error-report-web-f66d78b26ab4d88c0c00ea6b1f2f8897daca852a.zip
migrations: Add a data migration function to generate the LEV_DISTANCE
This adds a migration path forward and back for migration of data in migration 0003 (add the LEV_DISTANCE field). Signed-off-by: Michael Wood <michael.g.wood@intel.com>
-rw-r--r--Post/migrations/0003_auto_20150603_0913.py44
1 files changed, 41 insertions, 3 deletions
diff --git a/Post/migrations/0003_auto_20150603_0913.py b/Post/migrations/0003_auto_20150603_0913.py
index 46729ee..321f09f 100644
--- a/Post/migrations/0003_auto_20150603_0913.py
+++ b/Post/migrations/0003_auto_20150603_0913.py
@@ -3,10 +3,47 @@ from __future__ import unicode_literals
from django.db import models, migrations
+import Levenshtein
+
def add_lev_distance_data(apps, schema_editor):
+
BuildFailure = apps.get_model("Post", "BuildFailure")
- for buildfail in BuildFailure.objects.all():
- buildfailure.save(recalc_lev_distance=True)
+
+ # copied from model see
+ # https://docs.djangoproject.com/en/1.8/topics/migrations/#historical-models
+
+ def calc_lev_distance(obj):
+ if BuildFailure.objects.all().count() == 0:
+ return 0
+
+ # Use the last 400 characters of the ERROR_DETAILS.
+ # This is where the error message is likely to occour and
+ # reduces the computational load on calculating the Levenshtein
+ # distance.
+ seed = BuildFailure.objects.first().ERROR_DETAILS[-400:]
+ lv = Levenshtein.distance(str(seed), str(obj.ERROR_DETAILS[-400:]))
+
+ # Offset the distance calculated against the length of the error.
+ return lv + len (obj.ERROR_DETAILS)
+
+
+ offset = 0
+ pagesize = 1000
+ count = BuildFailure.objects.all().count()
+
+ while offset < count:
+ objs = BuildFailure.objects.all()[offset : offset + pagesize]
+ for f in objs:
+ if f.LEV_DISTANCE is None:
+ f.LEV_DISTANCE = calc_lev_distance(f)
+ f.save()
+
+ del objs
+ offset = offset + pagesize
+
+def remove_lev_distance_data(apps, schema_editor):
+ # Nothing to do at this point
+ pass
class Migration(migrations.Migration):
@@ -20,5 +57,6 @@ class Migration(migrations.Migration):
name='LEV_DISTANCE',
field=models.IntegerField(null=True, blank=True),
preserve_default=True,
- ),
+ ),
+ migrations.RunPython(add_lev_distance_data, remove_lev_distance_data),
]