autotest: Insert skylab tasks into tko_skylab_tasks table

BUG=chromium:833988
TEST=None

Change-Id: I37efa217827612d36984b0e25d3b9837e7c32bcb
Reviewed-on: https://chromium-review.googlesource.com/1018723
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Prathmesh Prabhu <pprabhu@chromium.org>
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
Reviewed-by: Xixuan Wu <xixuan@chromium.org>
diff --git a/tko/db.py b/tko/db.py
index 3a7f087..8fa4976 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -440,20 +440,11 @@
         @param job: The job object.
         @param commit: If commit the transaction .
         """
-        data = {'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,
+        data = self._get_common_job_data(tag, job)
+        data.update({
                 'afe_job_id': job.afe_job_id,
                 'afe_parent_job_id': job.afe_parent_job_id,
-                'build': job.build,
-                'build_version': job.build_version,
-                'board': job.board,
-                'suite': job.suite}
-
+        })
         if job.index is not None:
             self.update('tko_jobs', data, {'job_idx': job.index}, commit=commit)
         else:
@@ -461,19 +452,46 @@
             job.index = self.get_last_autonumber_value()
 
 
-    def insert_or_update_task_reference(self, job, commit=None):
+    def _get_common_job_data(self, tag, job):
+        """Construct a dictionary with the common data to insert in job/task."""
+        return {
+                '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,
+                'build': job.build,
+                'build_version': job.build_version,
+                'board': job.board,
+                'suite': job.suite,
+        }
+
+
+    def insert_or_update_task_reference(self, job, reference_type, commit=None):
         """Insert an entry in the tko_task_references table.
 
         The job should already have been inserted in tko_jobs.
         @param job: tko.models.job object.
+        @param reference_type: The type of reference to insert.
+                One of: {'afe', 'skylab'}
         @param commit: Whether to commit this transaction.
         """
+        assert reference_type in {'afe', 'skylab'}
+        if reference_type == 'afe':
+            task_id = job.afe_job_id
+            parent_task_id = job.afe_parent_job_id
+        else:
+            task_id = job.skylab_task_id
+            parent_task_id = job.skylab_parent_task_id
         data = {
-                'reference_type': 'afe',
+                'reference_type': reference_type,
                 'tko_job_idx': job.index,
-                'task_id': job.afe_job_id,
-                'parent_task_id': job.afe_parent_job_id,
+                'task_id': task_id,
+                'parent_task_id': parent_task_id,
         }
+
         task_reference_id = self._lookup_task_reference(job)
         if task_reference_id is not None:
             self.update('tko_task_references',
@@ -486,7 +504,6 @@
             job.task_reference_id = self.get_last_autonumber_value()
 
 
-
     def update_job_keyvals(self, job, commit=None):
         """Updates the job key values.
 
diff --git a/tko/parse.py b/tko/parse.py
index 66db548..9b61dc6 100755
--- a/tko/parse.py
+++ b/tko/parse.py
@@ -346,7 +346,9 @@
             _delete_tests_from_db(db, unmatched_tests)
 
     job.afe_job_id = tko_utils.get_afe_job_id(jobname)
+    job.skylab_task_id = tko_utils.get_skylab_task_id(jobname)
     job.afe_parent_job_id = str(job_keyval.get(constants.PARENT_JOB_ID))
+    job.skylab_parent_task_id = str(job_keyval.get(constants.PARENT_JOB_ID))
     job.build = None
     job.board = None
     job.build_version = None
@@ -520,7 +522,10 @@
     """
     db.insert_or_update_machine(job)
     db.insert_job(jobname, job)
-    db.insert_or_update_task_reference(job)
+    db.insert_or_update_task_reference(
+            job,
+            'skylab' if tko_utils.is_skylab_task(jobname) else 'afe',
+    )
     db.update_job_keyvals(job)
     for test in job.tests:
         db.insert_test(job, test)
diff --git a/tko/utils.py b/tko/utils.py
index 169f58e..40ecf7b 100644
--- a/tko/utils.py
+++ b/tko/utils.py
@@ -61,9 +61,7 @@
 def get_afe_job_id(tag):
     """ Given a tag return the afe_job_id (if any).
 
-    Extract job id and hostname if tag is in the format of
-    JOB_ID-OWNER/HOSTNAME. JOB_ID and HOSTNAME must both be present
-    to be considered as a match.
+    Tag is in the format of JOB_ID-OWNER/HOSTNAME
 
     @param tag: afe_job_id and hostname are extracted from this tag.
                 e.g. "1234-chromeos-test/chromeos1-row1-host1"
@@ -71,3 +69,20 @@
     """
     match = re.search('^([0-9]+)-.+/(.+)$', tag)
     return match.group(1) if match else None
+
+
+def get_skylab_task_id(tag):
+    """ Given a tag return the skylab_task's id (if any).
+
+    Tag is in the format of swarming-TASK_ID/HOSTNAME
+
+    @param tag: afe_job_id and hostname are extracted from this tag.
+                e.g. "1234-chromeos-test/chromeos1-row1-host1"
+    @return: the afe_job_id as a string if regex matches, else return ''
+    """
+    match = re.search('^swarming-([A-Fa-f0-9]+)/(.+)$', tag)
+    return match.group(1) if match else None
+
+
+def is_skylab_task(tag):
+    return get_skylab_task_id(tag) is not None