rename result_collection_api to testapi
[releng.git] / utils / test / testapi / 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         t = TI()
59         if a_dict:
60             t.current = a_dict.get('current')
61             if 'histories' in a_dict.keys():
62                 for history in a_dict.get('histories', None):
63                     t.histories.append(TIHistory.from_dict(history))
64             else:
65                 t.histories = []
66         return t
67
68
69 @swagger.model()
70 class ResultCreateRequest(object):
71     """
72         @property trust_indicator:
73         @ptype trust_indicator: L{TI}
74     """
75     def __init__(self,
76                  pod_name=None,
77                  project_name=None,
78                  case_name=None,
79                  installer=None,
80                  version=None,
81                  start_date=None,
82                  stop_date=None,
83                  details=None,
84                  build_tag=None,
85                  scenario=None,
86                  criteria=None,
87                  trust_indicator=None):
88         self.pod_name = pod_name
89         self.project_name = project_name
90         self.case_name = case_name
91         self.installer = installer
92         self.version = version
93         self.start_date = start_date
94         self.stop_date = stop_date
95         self.details = details
96         self.build_tag = build_tag
97         self.scenario = scenario
98         self.criteria = criteria
99         self.trust_indicator = trust_indicator if trust_indicator else TI(0)
100
101     def format(self):
102         return {
103             "pod_name": self.pod_name,
104             "project_name": self.project_name,
105             "case_name": self.case_name,
106             "installer": self.installer,
107             "version": self.version,
108             "start_date": self.start_date,
109             "stop_date": self.stop_date,
110             "details": self.details,
111             "build_tag": self.build_tag,
112             "scenario": self.scenario,
113             "criteria": self.criteria,
114             "trust_indicator": self.trust_indicator.format()
115         }
116
117
118 @swagger.model()
119 class ResultUpdateRequest(object):
120     """
121         @property trust_indicator:
122         @ptype trust_indicator: L{TI}
123     """
124     def __init__(self, trust_indicator=None):
125         self.trust_indicator = trust_indicator
126
127     def format(self):
128         return {
129             "trust_indicator": self.trust_indicator.format(),
130         }
131
132
133 @swagger.model()
134 class TestResult(object):
135     """
136         @property trust_indicator: used for long duration test case
137         @ptype trust_indicator: L{TI}
138     """
139     def __init__(self, _id=None, case_name=None, project_name=None,
140                  pod_name=None, installer=None, version=None,
141                  start_date=None, stop_date=None, details=None,
142                  build_tag=None, scenario=None, criteria=None,
143                  trust_indicator=None):
144         self._id = _id
145         self.case_name = case_name
146         self.project_name = project_name
147         self.pod_name = pod_name
148         self.installer = installer
149         self.version = version
150         self.start_date = start_date
151         self.stop_date = stop_date
152         self.details = details
153         self.build_tag = build_tag
154         self.scenario = scenario
155         self.criteria = criteria
156         self.trust_indicator = trust_indicator
157
158     @staticmethod
159     def from_dict(a_dict):
160
161         if a_dict is None:
162             return None
163
164         t = TestResult()
165         t._id = a_dict.get('_id')
166         t.case_name = a_dict.get('case_name')
167         t.pod_name = a_dict.get('pod_name')
168         t.project_name = a_dict.get('project_name')
169         t.start_date = str(a_dict.get('start_date'))
170         t.stop_date = str(a_dict.get('stop_date'))
171         t.details = a_dict.get('details')
172         t.version = a_dict.get('version')
173         t.installer = a_dict.get('installer')
174         t.build_tag = a_dict.get('build_tag')
175         t.scenario = a_dict.get('scenario')
176         t.criteria = a_dict.get('criteria')
177         t.trust_indicator = TI.from_dict(a_dict.get('trust_indicator'))
178         return t
179
180     def format(self):
181         return {
182             "case_name": self.case_name,
183             "project_name": self.project_name,
184             "pod_name": self.pod_name,
185             "start_date": str(self.start_date),
186             "stop_date": str(self.stop_date),
187             "version": self.version,
188             "installer": self.installer,
189             "details": self.details,
190             "build_tag": self.build_tag,
191             "scenario": self.scenario,
192             "criteria": self.criteria,
193             "trust_indicator": self.trust_indicator.format()
194         }
195
196     def format_http(self):
197         return {
198             "_id": str(self._id),
199             "case_name": self.case_name,
200             "project_name": self.project_name,
201             "pod_name": self.pod_name,
202             "start_date": str(self.start_date),
203             "stop_date": str(self.stop_date),
204             "version": self.version,
205             "installer": self.installer,
206             "details": self.details,
207             "build_tag": self.build_tag,
208             "scenario": self.scenario,
209             "criteria": self.criteria,
210             "trust_indicator": self.trust_indicator.format()
211         }
212
213
214 @swagger.model()
215 class TestResults(object):
216     """
217         @property results:
218         @ptype results: C{list} of L{TestResult}
219     """
220     def __init__(self):
221         self.results = list()
222
223     @staticmethod
224     def from_dict(a_dict):
225         if a_dict is None:
226             return None
227
228         res = TestResults()
229         for result in a_dict.get('results'):
230             res.results.append(TestResult.from_dict(result))
231         return res