Add a put result method to modify trust_indicator
[releng.git] / utils / test / result_collection_api / opnfv_testapi / resources / result_models.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from opnfv_testapi.tornado_swagger import swagger
10
11
12 @swagger.model()
13 class TIHistory(object):
14     """
15         @ptype step: L{float}
16     """
17     def __init__(self, date=None, step=0):
18         self.date = date
19         self.step = step
20
21     def format(self):
22         return {
23             "date": self.date,
24             "step": self.step
25         }
26
27     @staticmethod
28     def from_dict(a_dict):
29         if a_dict is None:
30             return None
31
32         return TIHistory(a_dict.get('date'), a_dict.get('step'))
33
34
35 @swagger.model()
36 class TI(object):
37     """
38         @property histories: trust_indicator update histories
39         @ptype histories: C{list} of L{TIHistory}
40         @ptype current: L{float}
41     """
42     def __init__(self, current=0):
43         self.current = current
44         self.histories = list()
45
46     def format(self):
47         hs = []
48         for h in self.histories:
49             hs.append(h.format())
50
51         return {
52             "current": self.current,
53             "histories": hs
54         }
55
56     @staticmethod
57     def from_dict(a_dict):
58         if a_dict is None:
59             return None
60         t = TI()
61         t.current = a_dict.get('current')
62         if 'histories' in a_dict.keys():
63             for history in a_dict.get('histories', None):
64                 t.histories.append(TIHistory.from_dict(history))
65         else:
66             t.histories = []
67         return t
68
69
70 @swagger.model()
71 class ResultCreateRequest(object):
72     """
73         @property trust_indicator:
74         @ptype trust_indicator: L{TI}
75     """
76     def __init__(self,
77                  pod_name=None,
78                  project_name=None,
79                  case_name=None,
80                  installer=None,
81                  version=None,
82                  start_date=None,
83                  stop_date=None,
84                  details=None,
85                  build_tag=None,
86                  scenario=None,
87                  criteria=None,
88                  trust_indicator=None):
89         self.pod_name = pod_name
90         self.project_name = project_name
91         self.case_name = case_name
92         self.installer = installer
93         self.version = version
94         self.start_date = start_date
95         self.stop_date = stop_date
96         self.details = details
97         self.build_tag = build_tag
98         self.scenario = scenario
99         self.criteria = criteria
100         self.trust_indicator = trust_indicator
101
102     def format(self):
103         return {
104             "pod_name": self.pod_name,
105             "project_name": self.project_name,
106             "case_name": self.case_name,
107             "installer": self.installer,
108             "version": self.version,
109             "start_date": self.start_date,
110             "stop_date": self.stop_date,
111             "details": self.details,
112             "build_tag": self.build_tag,
113             "scenario": self.scenario,
114             "criteria": self.criteria,
115             "trust_indicator": self.trust_indicator.format()
116         }
117
118
119 @swagger.model()
120 class ResultUpdateRequest(object):
121     """
122         @property trust_indicator:
123         @ptype trust_indicator: L{TI}
124     """
125     def __init__(self, trust_indicator=None):
126         self.trust_indicator = trust_indicator
127
128     def format(self):
129         return {
130             "trust_indicator": self.trust_indicator.format(),
131         }
132
133
134 @swagger.model()
135 class TestResult(object):
136     """
137         @property trust_indicator: used for long duration test case
138         @ptype trust_indicator: L{TI}
139     """
140     def __init__(self, _id=None, case_name=None, project_name=None,
141                  pod_name=None, installer=None, version=None,
142                  start_date=None, stop_date=None, details=None,
143                  build_tag=None, scenario=None, criteria=None,
144                  trust_indicator=None):
145         self._id = _id
146         self.case_name = case_name
147         self.project_name = project_name
148         self.pod_name = pod_name
149         self.installer = installer
150         self.version = version
151         self.start_date = start_date
152         self.stop_date = stop_date
153         self.details = details
154         self.build_tag = build_tag
155         self.scenario = scenario
156         self.criteria = criteria
157         self.trust_indicator = trust_indicator
158
159     @staticmethod
160     def from_dict(a_dict):
161
162         if a_dict is None:
163             return None
164
165         t = TestResult()
166         t._id = a_dict.get('_id')
167         t.case_name = a_dict.get('case_name')
168         t.pod_name = a_dict.get('pod_name')
169         t.project_name = a_dict.get('project_name')
170         t.start_date = str(a_dict.get('start_date'))
171         t.stop_date = str(a_dict.get('stop_date'))
172         t.details = a_dict.get('details')
173         t.version = a_dict.get('version')
174         t.installer = a_dict.get('installer')
175         t.build_tag = a_dict.get('build_tag')
176         t.scenario = a_dict.get('scenario')
177         t.criteria = a_dict.get('criteria')
178         t.trust_indicator = TI.from_dict(a_dict.get('trust_indicator'))
179         return t
180
181     def format(self):
182         return {
183             "case_name": self.case_name,
184             "project_name": self.project_name,
185             "pod_name": self.pod_name,
186             "start_date": str(self.start_date),
187             "stop_date": str(self.stop_date),
188             "version": self.version,
189             "installer": self.installer,
190             "details": self.details,
191             "build_tag": self.build_tag,
192             "scenario": self.scenario,
193             "criteria": self.criteria,
194             "trust_indicator": self.trust_indicator.format()
195         }
196
197     def format_http(self):
198         return {
199             "_id": str(self._id),
200             "case_name": self.case_name,
201             "project_name": self.project_name,
202             "pod_name": self.pod_name,
203             "start_date": str(self.start_date),
204             "stop_date": str(self.stop_date),
205             "version": self.version,
206             "installer": self.installer,
207             "details": self.details,
208             "build_tag": self.build_tag,
209             "scenario": self.scenario,
210             "criteria": self.criteria,
211             "trust_indicator": self.trust_indicator.format()
212         }
213
214
215 @swagger.model()
216 class TestResults(object):
217     """
218         @property results:
219         @ptype results: C{list} of L{TestResult}
220     """
221     def __init__(self):
222         self.results = list()
223
224     @staticmethod
225     def from_dict(a_dict):
226         if a_dict is None:
227             return None
228
229         res = TestResults()
230         for result in a_dict.get('results'):
231             res.results.append(TestResult.from_dict(result))
232         return res