48c73600727e46fbd315991a7903fcaacdb97a07
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / state.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import tornado.web
19
20 from . import message
21
22
23 class StateHandler(tornado.web.RequestHandler):
24 def options(self, *args, **kargs):
25 pass
26
27 def set_default_headers(self):
28 self.set_header('Access-Control-Allow-Origin', '*')
29 self.set_header('Access-Control-Allow-Headers',
30 'Content-Type, Cache-Control, Accept, X-Requested-With, Authorization')
31 self.set_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE')
32
33 def initialize(self, log, loop):
34 self.log = log
35 self.loop = loop
36
37 def success(self, messages):
38 success = self.__class__.SUCCESS
39 return any(isinstance(msg, success) for msg in messages)
40
41 def failure(self, messages):
42 failure = self.__class__.FAILURE
43 return any(isinstance(msg, failure) for msg in messages)
44
45 def started(self, messages):
46 started = self.__class__.STARTED
47 return any(isinstance(msg, started) for msg in messages)
48
49 def status(self, messages):
50 if self.failure(messages):
51 return "failure"
52 elif self.success(messages):
53 return "success"
54 return "pending"
55
56 def notifications(self, messages):
57 notifications = {
58 "errors": list(),
59 "events": list(),
60 "warnings": list(),
61 }
62
63 for msg in messages:
64 if isinstance(msg, message.StatusMessage):
65 notifications["events"].append({
66 'value': msg.name,
67 'text': msg.text,
68 'timestamp': msg.timestamp,
69 })
70 continue
71
72 elif isinstance(msg, message.WarningMessage):
73 notifications["warnings"].append({
74 'value': msg.text,
75 'timestamp': msg.timestamp,
76 })
77 continue
78
79 elif isinstance(msg, message.ErrorMessage):
80 notifications["errors"].append({
81 'value': msg.text,
82 'timestamp': msg.timestamp,
83 })
84 continue
85
86 elif isinstance(msg, message.FilenameMessage):
87 notifications["filename"] = msg.text
88 continue
89
90 self.log.warning('unrecognized message: {}'.format(msg))
91
92 return notifications
93
94 def get(self, transaction_id):
95 if transaction_id not in self.application.messages:
96 raise tornado.web.HTTPError(404, "unrecognized transaction ID")
97
98 messages = self.application.messages[transaction_id]
99 messages.sort(key=lambda m: m.timestamp)
100
101 if not self.started(messages):
102 raise tornado.web.HTTPError(404, "unrecognized transaction ID")
103
104 notifications = self.notifications(messages)
105 notifications["status"] = self.status(messages)
106
107 self.write(tornado.escape.json_encode(notifications))