48c73600727e46fbd315991a7903fcaacdb97a07
3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 class StateHandler(tornado
.web
.RequestHandler
):
24 def options(self
, *args
, **kargs
):
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')
33 def initialize(self
, log
, loop
):
37 def success(self
, messages
):
38 success
= self
.__class
__.SUCCESS
39 return any(isinstance(msg
, success
) for msg
in messages
)
41 def failure(self
, messages
):
42 failure
= self
.__class
__.FAILURE
43 return any(isinstance(msg
, failure
) for msg
in messages
)
45 def started(self
, messages
):
46 started
= self
.__class
__.STARTED
47 return any(isinstance(msg
, started
) for msg
in messages
)
49 def status(self
, messages
):
50 if self
.failure(messages
):
52 elif self
.success(messages
):
56 def notifications(self
, messages
):
64 if isinstance(msg
, message
.StatusMessage
):
65 notifications
["events"].append({
68 'timestamp': msg
.timestamp
,
72 elif isinstance(msg
, message
.WarningMessage
):
73 notifications
["warnings"].append({
75 'timestamp': msg
.timestamp
,
79 elif isinstance(msg
, message
.ErrorMessage
):
80 notifications
["errors"].append({
82 'timestamp': msg
.timestamp
,
86 elif isinstance(msg
, message
.FilenameMessage
):
87 notifications
["filename"] = msg
.text
90 self
.log
.warning('unrecognized message: {}'.format(msg
))
94 def get(self
, transaction_id
):
95 if transaction_id
not in self
.application
.messages
:
96 raise tornado
.web
.HTTPError(404, "unrecognized transaction ID")
98 messages
= self
.application
.messages
[transaction_id
]
99 messages
.sort(key
=lambda m
: m
.timestamp
)
101 if not self
.started(messages
):
102 raise tornado
.web
.HTTPError(404, "unrecognized transaction ID")
104 notifications
= self
.notifications(messages
)
105 notifications
["status"] = self
.status(messages
)
107 self
.write(tornado
.escape
.json_encode(notifications
))