def get(self, imo): """ Queries ShipData table on given imo_id and returns list of all matching rows in desc order of datetime :param imo: Integer :return: list of dicts of {latitude, longitude} in DESC order """ try: result = db.session.query(ShipData).filter( ShipData.imo_id == imo).order_by(ShipData.datetimestamp.desc()) # log.info(result) data = [] for row in result: data.append({ 'latitude': row.latitude, 'longitude': row.longitude }) # return {'count': len(data), 'positions': data}, 200 return data, 200 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def __exit__(self, exc_type, exc_value, exc_traceback): self.error=None if exc_type: log.exception(exc_value) self.error = traceback.format_exception(exc_type, exc_value, exc_traceback) if self.error: log.error("TEST `{0}` failed".format(self.test_name)) raise Exception(str(self.error)) else: log.info("TEST `{0}` passed".format(self.test_name)) exit(0)
def delete(self): """Empty IMO table (only) """ try: res = delete_all_table_rows(IMO.__tablename__) msg = "Deleted all rows of table '{}': {} rows".format( IMO.__tablename__, res) log.warn(msg) return {'message': "Rows Deleted: {}".format(res)}, 200 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def delegateRendering(graphType, graphOptions): start = time() postData = graphType + '\n' + pickle.dumps(graphOptions) servers = settings.RENDERING_HOSTS[:] # make a copy so we can shuffle it safely shuffle(servers) for server in servers: start2 = time() try: # Get a connection try: pool = connectionPools[server] except KeyError: # happens the first time pool = connectionPools[server] = set() try: connection = pool.pop() except KeyError: # No available connections, have to make a new one connection = HTTPConnectionWithTimeout(server) connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT # Send the request try: connection.request('POST', '/render/local/', postData) except CannotSendRequest: connection = HTTPConnectionWithTimeout(server) # retry once connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT connection.request('POST', '/render/local/', postData) # Read the response response = connection.getresponse() assert response.status == 200, "Bad response code %d from %s" % ( response.status, server) contentType = response.getheader('Content-Type') imageData = response.read() assert contentType == 'image/png', "Bad content type: \"%s\" from %s" % ( contentType, server) assert imageData, "Received empty response from %s" % server # Wrap things up log.rendering('Remotely rendered image on %s in %.6f seconds' % (server, time() - start2)) log.rendering( 'Spent a total of %.6f seconds doing remote rendering work' % (time() - start)) pool.add(connection) return imageData except: log.exception( "Exception while attempting remote rendering request on %s" % server) log.rendering( 'Exception while remotely rendering on %s wasted %.6f' % (server, time() - start2)) continue
def get(self): """ get all the IMOs from the db """ # todo: should return paginated responses try: self.create_base_data() imodata = IMO.query.all() data = [] for imo in imodata: data.append(imo.to_json()) return data, 200 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def delete(self): """ Empty ShipData table (only) """ try: res = delete_all_table_rows(ShipData.__tablename__) msg = "Deleted all rows of table '{}'".format( ShipData.__tablename__) log.warn(msg) return { 'message': "Table: {}, Rows Deleted: {}".format(ShipData.__tablename__, res) }, 200 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def phrase(self, phrase: str) -> list: """ Perform a search for the phrase provided. Args: phrase: phrase to search for. Returns: List of 3 potential matches. """ words = phrase.split(" ") try: return self._match_words(words) except Exception as e: log.error("Search error.") log.exception(e) return []
def mygraph(request): profile = getProfile(request, allowDefault=False) if not profile: return HttpResponse("You are not logged in!") action = request.GET['action'] graphName = request.GET['graphName'] if not graphName: return HttpResponse("You must type in a graph name.") if action == 'save': url = request.GET['url'] try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.url = url existingGraph.save() except ObjectDoesNotExist: try: newGraph = MyGraph(profile=profile, name=graphName, url=url) newGraph.save() except: log.exception( "Failed to create new MyGraph in /composer/mygraph/, graphName=%s" % graphName) return HttpResponse("Failed to save graph %s" % graphName) return HttpResponse("SAVED") elif action == 'delete': try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.delete() except ObjectDoesNotExist: return HttpResponse("No such graph '%s'" % graphName) return HttpResponse("DELETED") else: return HttpResponse("Invalid operation '%s'" % action)
def renderLocalView(request): try: start = time() reqParams = StringIO(request.body) graphType = reqParams.readline().strip() optionsPickle = reqParams.read() reqParams.close() graphClass = GraphTypes[graphType] options = unpickle.loads(optionsPickle) image = doImageRender(graphClass, options) log.rendering("Delegated rendering request took %.6f seconds" % (time() - start)) response = buildResponse(image) add_never_cache_headers(response) return response except: log.exception("Exception in graphite.render.views.rawrender") return HttpResponseServerError()
def get(self): """ get all the ships into the db """ # todo: should return paginated responses try: # import pdb; pdb.set_trace() # shipdata = ShipData.query.all() result = db.session.query(ShipData).all() data = [] for sd in result: data.append(sd.to_json()) if len(data) == 0: return {'message': 'No data in shipdata table.'}, 204 return {'shipsdata': data}, 200 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def get(self): """ read csv and write to database """ try: # load Shipdata table resp_ship, status_ship = csv_reader(self._default_csv_filename) # load imo table imo_resource = IMOResource() resp_imo, status_imo = imo_resource.create_base_data() if status_ship == status_imo == 200: return {'ShipData': resp_ship, 'IMO': resp_imo}, status_ship else: return { 'message': 'unsuccessful', 'ShipData': resp_ship, 'IMO': resp_imo }, 500 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def post(self): """ PRODEED WITH CAUTION: deletes the entire database file itself - unrecoverable """ try: data = parser.parse_args() action = data['action'] if action.lower() == 'delete': wipe_all_db_files() return {"message": "Database DELETED"}, 200 elif action.lower() == 'count': shipdata = ShipData.query.all() return {"row count": len(shipdata)}, 200 else: msg = "Invalid value supplied. Valid values are ['delete']" log.error(msg) return {'error': msg}, 500 except Exception as e: log.exception(e) return {"message": str(e)}, 500
def create_base_data(self): """ Creates 3 rows on the fly and inserts into IMO table. Drops all rows before re-inserting. """ try: log.info("Dropping all rows of 'imo' table first...") self.delete() a = IMO(id=9632179, name="Mathilde Maersk") b = IMO(id=9247455, name="Australian Spirit") c = IMO(id=9595321, name="MSC Preziosa") db.session.add(a) db.session.add(b) db.session.add(c) db.session.commit() log.info("Inserted 3 base IMOs into 'imo' table.") return "{} rows inserted to database table '{}'".format( 3, self.__model__.__tablename__), 200 except Exception as e: log.exception(e) return str(e), 500
class HttpRequest: """二次封装requests方法""" http_method_names = 'get', 'post', 'put', 'delete', 'patch', 'head', 'options' def __init__(self): self.r = requests.session() self.reg = Regular() def deserialize(self, headers): """反序列化字典""" results = {} for i in headers.split('\n'): res = i.find(':') results[i[:res].strip()] = i[res + 1:].strip() return results def request(self, case, **kwargs): """发送请求 :param case: 测试用例 :param kwargs: 其他参数 :return: request响应 """ if case[CF.URL]: VariablePool.set('url', case[CF.URL]) if case[CF.HEADERS]: self.r.headers = self.deserialize(case[CF.HEADERS]) method = case[CF.METHOD].upper() url = VariablePool.get('url') + case[CF.ROUTE] kwargs = replace_param(case[CF.PARAMETER]) try: log.info("Request Url: {}".format(url)) log.info("Request Method: {}".format(method)) log.info("Request Data: {}".format(kwargs)) def dispatch(method, *args, **kwargs): if method in self.http_method_names: handler = getattr(self.r, method) return handler(*args, **kwargs) else: raise AttributeError('request method is ERROR!') response = dispatch(method.lower(), url, **kwargs) log.info(response) log.info("Response Data: {}".format(response.text)) return response except RequestException as e: log.exception(format(e)) except Exception as e: raise e
for direct in order_direction: for act in active: if by == "creator": start = "" elif by == "start_date" or by == "end_date": start = "2019-03-01T00:00:00" else: start = 0 call_args = OrderedDict() call_args["start"]=start call_args["order_by"]=by call_args["order_direction"]=direct call_args["limit"]=10 call_args["status"]=act call_args["last_id"]="" resp = last_message_as_json(call_and_check(wallet.list_proposals, call_args, "args")) if not "result" in resp: raise ArgsCheckException("No `result` in response") except Exception as _ex: log.exception(str(_ex)) error = True finally: if error: log.error("TEST `{0}` failed".format(__file__)) exit(1) else: log.info("TEST `{0}` passed".format(__file__)) exit(0)
def fetchData(requestContext, pathExpr): seriesList = [] startTime = int(epoch(requestContext['startTime'])) endTime = int(epoch(requestContext['endTime'])) def _fetchData(pathExpr, startTime, endTime, requestContext, seriesList): matching_nodes = STORE.find(pathExpr, startTime, endTime, local=requestContext['localOnly']) fetches = [(node, node.fetch(startTime, endTime)) for node in matching_nodes if node.is_leaf] for node, results in fetches: if isinstance(results, FetchInProgress): results = results.waitForResults() if not results: log.info( "render.datalib.fetchData :: no results for %s.fetch(%s, %s)" % (node, startTime, endTime)) continue try: (timeInfo, values) = results except ValueError as e: raise Exception( "could not parse timeInfo/values from metric '%s': %s" % (node.path, e)) (start, end, step) = timeInfo series = TimeSeries(node.path, start, end, step, values) series.pathExpression = pathExpr # hack to pass expressions through to render functions seriesList.append(series) # Prune empty series with duplicate metric paths to avoid showing empty graph elements for old whisper data names = set([s.name for s in seriesList]) for name in names: series_with_duplicate_names = [ s for s in seriesList if s.name == name ] empty_duplicates = [ s for s in series_with_duplicate_names if not nonempty(s) ] if series_with_duplicate_names == empty_duplicates and len( empty_duplicates) > 0: # if they're all empty empty_duplicates.pop() # make sure we leave one in seriesList for series in empty_duplicates: seriesList.remove(series) return seriesList retries = 1 # start counting at one to make log output and settings more readable while True: try: seriesList = _fetchData(pathExpr, startTime, endTime, requestContext, seriesList) return seriesList except Exception, e: if retries >= settings.MAX_FETCH_RETRIES: log.exception("Failed after %s retry! Root cause:\n%s" % (settings.MAX_FETCH_RETRIES, format_exc())) raise e else: log.exception( "Got an exception when fetching data! Try: %i of %i. Root cause:\n%s" % (retries, settings.MAX_FETCH_RETRIES, format_exc())) retries += 1
"Assertion `{0}` is required.".format(error_msg_x)) error_msg_y = "The value `y` for `_last_id` argument is invalid, it should be integer type." resp_error_y = wallet.list_voter_proposals(args.creator, "creator", "asc", 50, "all", "y") log.info(resp_error_y) if resp_error_y.find(error_msg_y) == -1: raise ArgsCheckException( "Assertion `{0}` is required.".format(error_msg_y)) error_msg_10 = "The value `10` for `_last_id` argument is invalid, it should be integer type." resp_10 = wallet.list_voter_proposals(args.creator, "creator", "asc", 50, "all", "10") log.info(resp_10) if resp_10.find(error_msg_10) != -1: raise ArgsCheckException( "There should be no assertion `{0}`.".format(error_msg_10)) if not "result" in last_message_as_json(resp_10): raise ArgsCheckException("No `result` in response") except Exception as _ex: log.exception(str(_ex)) error = True finally: if error: log.error("TEST `{0}` failed".format(__file__)) exit(1) else: log.info("TEST `{0}` passed".format(__file__)) exit(0)
api.add_resource(health, "/test/") api.add_resource(CSVResource, "/api/init_db/") api.add_resource(ShipResource, "/api/shipsdata/") ## asked in assignment api.add_resource(IMOResource, "/api/ships/") api.add_resource(PositionResource, "/api/positions/<int:imo>") def create_app(): # app = Flask(__name__) db.create_all() db.init_app(app) return app if __name__ == "__main__": # from db import db # db.init_app(app) # wipe_all_db_files() default_port = 8010 try: app = create_app() if len(sys.argv) == 2: port = int(sys.argv[1]) app.run(host="0.0.0.0", port=port, debug=True) else: app.run(host="0.0.0.0", port=default_port, debug=True) except Exception as e: log.exception(e)