~gandelman-a/+junk/test-catalog

« back to all changes in this revision

Viewing changes to test_catalog/db/api.py

  • Committer: Adam Gandelman
  • Date: 2013-11-26 18:53:56 UTC
  • Revision ID: adamg@canonical.com-20131126185356-nhpion0cbcnmobxs
0.0.8

* Removes 'extra' json blob column, replaces with test_job_attribute table.

* Moves openstack-specific attributes from main build attributes
  to extra build attributes, allowing catalog to be used outside of Openstack
  build-driven CI.

* Drops some duplicate code, relies on db api instead of local pipeline
  db manager.

* Improved logging

* Improved searching for config files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import logging
2
 
import json
 
2
from copy import copy
3
3
 
4
4
from sqlalchemy import and_, distinct, desc
5
5
import sqlalchemy.exc as sqlexc
14
14
from test_catalog import jenkins
15
15
 
16
16
 
 
17
none = None
 
18
 
 
19
 
17
20
class Manager(Backend):
18
21
    def __init__(self):
19
22
        if not CONF:
21
24
        super(Manager, self).__init__()
22
25
        self.session = self.get_backend()
23
26
 
 
27
    def _attribute_update(self, pipeline_id, key, value):
 
28
        pass
 
29
 
 
30
    def _attribute_create(self, pipeline_id, key, value):
 
31
        attr = {
 
32
            'pipeline_id': pipeline_id,
 
33
            'key': key,
 
34
            'value': value,
 
35
        }
 
36
        row = models.TestJobAttribute()
 
37
        row.update(attr)
 
38
        row.save(self.session)
 
39
 
 
40
    def create_test_job_attributes(self, pipeline_id, **attrs):
 
41
        for k, v in attrs.iteritems():
 
42
            self._attribute_create(pipeline_id, k, v)
 
43
 
24
44
    def create_test_job(self, test_job_dict):
 
45
        attrs = {}
25
46
        if ('extra' in test_job_dict and
26
47
           isinstance(test_job_dict['extra'], dict)):
27
 
            test_job_dict['extra'] = json.dumps(test_job_dict['extra'])
 
48
            attrs = copy(test_job_dict['extra'])
 
49
            del test_job_dict['extra']
28
50
 
29
51
        test_job = models.TestJob()
30
52
        test_job.update(test_job_dict)
36
58
            raise exception.TestCatalogException(msg)
37
59
 
38
60
        logging.info('Test job saved: %s' % test_job_dict['build_tag'])
 
61
        self.create_test_job_attributes(test_job.pipeline_id, **attrs)
39
62
        return test_job
40
63
 
41
64
    def create_pipeline_parent(self, parent_dict):
55
78
 
56
79
    def get_pipeline_parent_id_by_pid(self, pipeline_id):
57
80
        parents = self.session.query(models.TestJob)
58
 
        parents = parents.filter(
59
 
            and_(models.TestJob.parent_build_tag == None,
60
 
                 models.TestJob.pipeline_id == pipeline_id)
61
 
        )
 
81
        parents = parents.filter(and_(models.TestJob.parent_build_tag == none,
 
82
                                 models.TestJob.pipeline_id == pipeline_id))
62
83
        try:
63
84
            parent = parents[0]
64
85
        except IndexError:
68
89
 
69
90
        return parent.id
70
91
 
 
92
    def load_attributes(self, pipeline_id):
 
93
        job_attributes = self.session.query(models.TestJobAttribute)
 
94
        attributes = job_attributes.filter_by(pipeline_id=pipeline_id).all()
 
95
        out = {}
 
96
        for attr in attributes:
 
97
            if attr.key not in out:
 
98
                out[attr.key] = attr.value
 
99
        return out
 
100
 
71
101
    def get_test_job_by_btag(self, build_tag):
72
102
        jobs = self.session.query(models.TestJob)
73
103
        job = jobs.filter_by(build_tag=build_tag).first()
 
104
        if job:
 
105
            job.extra = self.load_attributes(job.pipeline_id)
74
106
        return job
75
107
 
76
108
    def get_test_job_by_id(self, id):
91
123
                logging.error('Could not update status in database '
92
124
                              '(from Jenkins): %s' % e)
93
125
                pass
 
126
        if job:
 
127
            job.extra = self.load_attributes(job.pipeline_id)
94
128
        return job
95
129
 
96
130
    def get_test_jobs_by_parent(self, parent_build_tag):
102
136
 
103
137
    def get_parent_by_pid(self, pipeline_id):
104
138
        jobs = self.session.query(models.TestJob)
105
 
        query = jobs.filter(
106
 
            and_(models.TestJob.parent_build_tag == None,
107
 
                 models.TestJob.pipeline_id == pipeline_id))
 
139
        query = jobs.filter(and_(models.TestJob.parent_build_tag == none,
 
140
                            models.TestJob.pipeline_id == pipeline_id))
108
141
        return query.first()
109
142
 
110
143
    def get_all_parents(self, limit=None):
111
144
        order = desc(models.TestJob.timestamp)
112
145
        jobs = self.session.query(models.TestJob).order_by(order)
 
146
 
113
147
        if limit:
114
 
            return jobs.filter(models.TestJob.parent_build_tag == None)[:limit]
 
148
            parents = jobs.filter(models.TestJob.parent_build_tag == none)
 
149
            parents = parents[:limit]
115
150
        else:
116
 
            return jobs.filter(models.TestJob.parent_build_tag == None)
 
151
            parents = jobs.filter(models.TestJob.parent_build_tag == none)
 
152
 
 
153
        # TODO: Optimize this
 
154
        out = []
 
155
        for p in parents:
 
156
            p.extra = self.load_attributes(p.pipeline_id)
 
157
            out.append(p)
 
158
        return out