Added afe_job_id column to the jobs table in TKO. Changed link in AFE
View Job tab to use afe_job_id instead of job_tag.

Signed-off-by: James Ren <jamesren@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@3665 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/client/src/autotest/afe/JobDetailView.java b/frontend/client/src/autotest/afe/JobDetailView.java
index 212b0ea..ef5eba5 100644
--- a/frontend/client/src/autotest/afe/JobDetailView.java
+++ b/frontend/client/src/autotest/afe/JobDetailView.java
@@ -303,7 +303,7 @@
     
     private String getResultsURL(int jobId) {
         return "/new_tko/#tab_id=spreadsheet_view&row=hostname&column=test_name&" +
-               "condition=job_tag+LIKE+'" + Integer.toString(jobId) + "-%2525'&" +
+               "condition=afe_job_id+%253d+" + Integer.toString(jobId) + "&" +
                "show_incomplete=true";
     }
     
diff --git a/new_tko/tko/models.py b/new_tko/tko/models.py
index 30644dd..2873b31 100644
--- a/new_tko/tko/models.py
+++ b/new_tko/tko/models.py
@@ -162,6 +162,7 @@
     queued_time = dbmodels.DateTimeField(null=True, blank=True)
     started_time = dbmodels.DateTimeField(null=True, blank=True)
     finished_time = dbmodels.DateTimeField(null=True, blank=True)
+    afe_job_id = dbmodels.IntegerField(null=True, default=None)
 
     class Meta:
         db_table = 'jobs'
@@ -515,6 +516,7 @@
     job_queued_time = dbmodels.DateTimeField(null=True, blank=True)
     job_started_time = dbmodels.DateTimeField(null=True, blank=True)
     job_finished_time = dbmodels.DateTimeField(null=True, blank=True)
+    afe_job_id = dbmodels.IntegerField(null=True)
     hostname = dbmodels.CharField(blank=True, max_length=300)
     platform = dbmodels.CharField(blank=True, max_length=240)
     machine_owner = dbmodels.CharField(blank=True, max_length=240)
diff --git a/new_tko/tko/rpc_interface_unittest.py b/new_tko/tko/rpc_interface_unittest.py
index 35bbdee..27ddff1 100755
--- a/new_tko/tko/rpc_interface_unittest.py
+++ b/new_tko/tko/rpc_interface_unittest.py
@@ -28,6 +28,7 @@
         jobs.queued_time AS job_queued_time,
         jobs.started_time AS job_started_time,
         jobs.finished_time AS job_finished_time,
+        jobs.afe_job_id AS afe_job_id,
         machines.hostname AS hostname,
         machines.machine_group AS platform,
         machines.owner AS machine_owner,
diff --git a/tko/db.py b/tko/db.py
index ec83355..4b3f82a 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -318,13 +318,20 @@
         else:
             self.update_machine_information(job, commit=commit)
 
+        afe_job_id = None
+        pattern = re.compile('^([0-9]+)-.+/.+$')
+        match = pattern.match(tag)
+        if match:
+            afe_job_id = match.group(1)
+
         self.insert('jobs', {'tag':tag,
                              'label': job.label,
                              'username': job.user,
                              'machine_idx': job.machine_idx,
                              'queued_time': job.queued_time,
                              'started_time': job.started_time,
-                             'finished_time': job.finished_time},
+                             'finished_time': job.finished_time,
+                             'afe_job_id': afe_job_id},
                              commit=commit)
         job.index = self.get_last_autonumber_value()
         for test in job.tests:
diff --git a/tko/migrations/030_add_afe_job_id_to_jobs.py b/tko/migrations/030_add_afe_job_id_to_jobs.py
new file mode 100644
index 0000000..24427af
--- /dev/null
+++ b/tko/migrations/030_add_afe_job_id_to_jobs.py
@@ -0,0 +1,77 @@
+UP_SQL = """
+ALTER TABLE jobs
+ADD COLUMN afe_job_id INT DEFAULT NULL;
+
+UPDATE jobs
+SET afe_job_id = SUBSTRING_INDEX(tag, '-', 1)
+WHERE tag REGEXP '^[0-9]+-.+/.+$';
+
+CREATE INDEX afe_job_id
+ON jobs(afe_job_id);
+
+ALTER VIEW test_view_2 AS
+SELECT  tests.test_idx,
+        tests.job_idx,
+        tests.test AS test_name,
+        tests.subdir,
+        tests.kernel_idx,
+        tests.status AS status_idx,
+        tests.reason,
+        tests.machine_idx,
+        tests.started_time AS test_started_time,
+        tests.finished_time AS test_finished_time,
+        jobs.tag AS job_tag,
+        jobs.label AS job_name,
+        jobs.username AS job_owner,
+        jobs.queued_time AS job_queued_time,
+        jobs.started_time AS job_started_time,
+        jobs.finished_time AS job_finished_time,
+        jobs.afe_job_id AS afe_job_id,
+        machines.hostname AS hostname,
+        machines.machine_group AS platform,
+        machines.owner AS machine_owner,
+        kernels.kernel_hash,
+        kernels.base AS kernel_base,
+        kernels.printable AS kernel,
+        status.word AS status
+FROM tests
+INNER JOIN jobs ON jobs.job_idx = tests.job_idx
+INNER JOIN machines ON machines.machine_idx = jobs.machine_idx
+INNER JOIN kernels ON kernels.kernel_idx = tests.kernel_idx
+INNER JOIN status ON status.status_idx = tests.status;
+"""
+
+DOWN_SQL = """
+ALTER VIEW test_view_2 AS
+SELECT  tests.test_idx,
+        tests.job_idx,
+        tests.test AS test_name,
+        tests.subdir,
+        tests.kernel_idx,
+        tests.status AS status_idx,
+        tests.reason,
+        tests.machine_idx,
+        tests.started_time AS test_started_time,
+        tests.finished_time AS test_finished_time,
+        jobs.tag AS job_tag,
+        jobs.label AS job_name,
+        jobs.username AS job_owner,
+        jobs.queued_time AS job_queued_time,
+        jobs.started_time AS job_started_time,
+        jobs.finished_time AS job_finished_time,
+        machines.hostname AS hostname,
+        machines.machine_group AS platform,
+        machines.owner AS machine_owner,
+        kernels.kernel_hash,
+        kernels.base AS kernel_base,
+        kernels.printable AS kernel,
+        status.word AS status
+FROM tests
+INNER JOIN jobs ON jobs.job_idx = tests.job_idx
+INNER JOIN machines ON machines.machine_idx = jobs.machine_idx
+INNER JOIN kernels ON kernels.kernel_idx = tests.kernel_idx
+INNER JOIN status ON status.status_idx = tests.status;
+
+ALTER TABLE jobs
+DROP COLUMN afe_job_id;
+"""