mirror of
https://github.com/EDCD/EDDN.git
synced 2025-04-25 20:52:13 +03:00
Monitor: Re-format with black
This commit is contained in:
parent
03525efe82
commit
83fa055722
@ -26,6 +26,7 @@ app = Bottle()
|
|||||||
# This import must be done post-monkey-patching!
|
# This import must be done post-monkey-patching!
|
||||||
if Settings.RELAY_DUPLICATE_MAX_MINUTES:
|
if Settings.RELAY_DUPLICATE_MAX_MINUTES:
|
||||||
from eddn.core.DuplicateMessages import DuplicateMessages
|
from eddn.core.DuplicateMessages import DuplicateMessages
|
||||||
|
|
||||||
duplicate_messages = DuplicateMessages()
|
duplicate_messages = DuplicateMessages()
|
||||||
duplicate_messages.start()
|
duplicate_messages.start()
|
||||||
|
|
||||||
@ -33,19 +34,20 @@ if Settings.RELAY_DUPLICATE_MAX_MINUTES:
|
|||||||
def parse_cl_args():
|
def parse_cl_args():
|
||||||
"""Parse command-line arguments."""
|
"""Parse command-line arguments."""
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog='Gateway',
|
prog="Gateway",
|
||||||
description='EDDN Gateway server',
|
description="EDDN Gateway server",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--loglevel',
|
"--loglevel",
|
||||||
help='CURRENTLY NO EFFECT - Logging level to output at',
|
help="CURRENTLY NO EFFECT - Logging level to output at",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-c', '--config',
|
"-c",
|
||||||
metavar='config filename',
|
"--config",
|
||||||
nargs='?',
|
metavar="config filename",
|
||||||
|
nargs="?",
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,33 +56,33 @@ def parse_cl_args():
|
|||||||
|
|
||||||
def date(__format) -> str:
|
def date(__format) -> str:
|
||||||
"""
|
"""
|
||||||
Make a 'now' datetime as per the supplied format.
|
Make a "now" datetime as per the supplied format.
|
||||||
|
|
||||||
:param __format:
|
:param __format:
|
||||||
:return: String representation of 'now'
|
:return: String representation of "now"
|
||||||
"""
|
"""
|
||||||
d = datetime.datetime.utcnow()
|
d = datetime.datetime.utcnow()
|
||||||
return d.strftime(__format)
|
return d.strftime(__format)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/ping', method=['OPTIONS', 'GET'])
|
@app.route("/ping", method=["OPTIONS", "GET"])
|
||||||
def ping() -> str:
|
def ping() -> str:
|
||||||
"""Respond to a ping request."""
|
"""Respond to a ping request."""
|
||||||
return 'pong'
|
return "pong"
|
||||||
|
|
||||||
|
|
||||||
@app.route('/getTotalSoftwares/', method=['OPTIONS', 'GET'])
|
@app.route("/getTotalSoftwares/", method=["OPTIONS", "GET"])
|
||||||
def get_total_softwares() -> str:
|
def get_total_softwares() -> str:
|
||||||
"""Respond with data about total uploading software counts."""
|
"""Respond with data about total uploading software counts."""
|
||||||
response.set_header("Access-Control-Allow-Origin", "*")
|
response.set_header("Access-Control-Allow-Origin", "*")
|
||||||
db = mariadb.connect(
|
db = mariadb.connect(
|
||||||
user=Settings.MONITOR_DB['user'],
|
user=Settings.MONITOR_DB["user"],
|
||||||
password=Settings.MONITOR_DB['password'],
|
password=Settings.MONITOR_DB["password"],
|
||||||
database=Settings.MONITOR_DB['database']
|
database=Settings.MONITOR_DB["database"],
|
||||||
)
|
)
|
||||||
softwares = collections.OrderedDict()
|
softwares = collections.OrderedDict()
|
||||||
|
|
||||||
max_days = request.GET.get('maxDays', '31').strip()
|
max_days = request.GET.get("maxDays", "31").strip()
|
||||||
max_days = int(max_days) - 1
|
max_days = int(max_days) - 1
|
||||||
|
|
||||||
query = """SELECT name, SUM(hits) AS total, MAX(dateStats) AS maxDate
|
query = """SELECT name, SUM(hits) AS total, MAX(dateStats) AS maxDate
|
||||||
@ -90,29 +92,29 @@ def get_total_softwares() -> str:
|
|||||||
ORDER BY total DESC"""
|
ORDER BY total DESC"""
|
||||||
|
|
||||||
results = db.cursor()
|
results = db.cursor()
|
||||||
results.execute(query, (max_days, ))
|
results.execute(query, (max_days,))
|
||||||
|
|
||||||
for row in results:
|
for row in results:
|
||||||
softwares[row[0].encode('utf8')] = str(row[1])
|
softwares[row[0].encode("utf8")] = str(row[1])
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
return simplejson.dumps(softwares)
|
return simplejson.dumps(softwares)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/getSoftwares/', method=['OPTIONS', 'GET'])
|
@app.route("/getSoftwares/", method=["OPTIONS", "GET"])
|
||||||
def get_softwares() -> str:
|
def get_softwares() -> str:
|
||||||
"""Respond with hit stats for all uploading software."""
|
"""Respond with hit stats for all uploading software."""
|
||||||
response.set_header("Access-Control-Allow-Origin", "*")
|
response.set_header("Access-Control-Allow-Origin", "*")
|
||||||
db = mariadb.connect(
|
db = mariadb.connect(
|
||||||
user=Settings.MONITOR_DB['user'],
|
user=Settings.MONITOR_DB["user"],
|
||||||
password=Settings.MONITOR_DB['password'],
|
password=Settings.MONITOR_DB["password"],
|
||||||
database=Settings.MONITOR_DB['database']
|
database=Settings.MONITOR_DB["database"],
|
||||||
)
|
)
|
||||||
softwares: OrderedDict = collections.OrderedDict()
|
softwares: OrderedDict = collections.OrderedDict()
|
||||||
|
|
||||||
date_start = request.GET.get('dateStart', str(date('%Y-%m-%d'))).strip()
|
date_start = request.GET.get("dateStart", str(date("%Y-%m-%d"))).strip()
|
||||||
date_end = request.GET.get('dateEnd', str(date('%Y-%m-%d'))).strip()
|
date_end = request.GET.get("dateEnd", str(date("%Y-%m-%d"))).strip()
|
||||||
|
|
||||||
query = """SELECT *
|
query = """SELECT *
|
||||||
FROM `softwares`
|
FROM `softwares`
|
||||||
@ -123,7 +125,7 @@ def get_softwares() -> str:
|
|||||||
results.execute(query, (date_start, date_end))
|
results.execute(query, (date_start, date_end))
|
||||||
|
|
||||||
for row in results:
|
for row in results:
|
||||||
current_date = row[2].strftime('%Y-%m-%d')
|
current_date = row[2].strftime("%Y-%m-%d")
|
||||||
if current_date not in softwares.keys():
|
if current_date not in softwares.keys():
|
||||||
softwares[current_date] = collections.OrderedDict()
|
softwares[current_date] = collections.OrderedDict()
|
||||||
|
|
||||||
@ -134,14 +136,14 @@ def get_softwares() -> str:
|
|||||||
return simplejson.dumps(softwares)
|
return simplejson.dumps(softwares)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/getTotalSchemas/', method=['OPTIONS', 'GET'])
|
@app.route("/getTotalSchemas/", method=["OPTIONS", "GET"])
|
||||||
def get_total_schemas() -> str:
|
def get_total_schemas() -> str:
|
||||||
"""Respond with total hit stats for all schemas."""
|
"""Respond with total hit stats for all schemas."""
|
||||||
response.set_header("Access-Control-Allow-Origin", "*")
|
response.set_header("Access-Control-Allow-Origin", "*")
|
||||||
db = mariadb.connect(
|
db = mariadb.connect(
|
||||||
user=Settings.MONITOR_DB['user'],
|
user=Settings.MONITOR_DB["user"],
|
||||||
password=Settings.MONITOR_DB['password'],
|
password=Settings.MONITOR_DB["password"],
|
||||||
database=Settings.MONITOR_DB['database']
|
database=Settings.MONITOR_DB["database"],
|
||||||
)
|
)
|
||||||
schemas = collections.OrderedDict()
|
schemas = collections.OrderedDict()
|
||||||
|
|
||||||
@ -161,19 +163,19 @@ def get_total_schemas() -> str:
|
|||||||
return simplejson.dumps(schemas)
|
return simplejson.dumps(schemas)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/getSchemas/', method=['OPTIONS', 'GET'])
|
@app.route("/getSchemas/", method=["OPTIONS", "GET"])
|
||||||
def get_schemas() -> str:
|
def get_schemas() -> str:
|
||||||
"""Respond with schema hit stats between given datetimes."""
|
"""Respond with schema hit stats between given datetimes."""
|
||||||
response.set_header("Access-Control-Allow-Origin", "*")
|
response.set_header("Access-Control-Allow-Origin", "*")
|
||||||
db = mariadb.connect(
|
db = mariadb.connect(
|
||||||
user=Settings.MONITOR_DB['user'],
|
user=Settings.MONITOR_DB["user"],
|
||||||
password=Settings.MONITOR_DB['password'],
|
password=Settings.MONITOR_DB["password"],
|
||||||
database=Settings.MONITOR_DB['database']
|
database=Settings.MONITOR_DB["database"],
|
||||||
)
|
)
|
||||||
schemas: OrderedDict = collections.OrderedDict()
|
schemas: OrderedDict = collections.OrderedDict()
|
||||||
|
|
||||||
date_start = request.GET.get('dateStart', str(date('%Y-%m-%d'))).strip()
|
date_start = request.GET.get("dateStart", str(date("%Y-%m-%d"))).strip()
|
||||||
date_end = request.GET.get('dateEnd', str(date('%Y-%m-%d'))).strip()
|
date_end = request.GET.get("dateEnd", str(date("%Y-%m-%d"))).strip()
|
||||||
|
|
||||||
query = """SELECT *
|
query = """SELECT *
|
||||||
FROM `schemas`
|
FROM `schemas`
|
||||||
@ -184,7 +186,7 @@ def get_schemas() -> str:
|
|||||||
results.execute(query, (date_start, date_end))
|
results.execute(query, (date_start, date_end))
|
||||||
|
|
||||||
for row in results:
|
for row in results:
|
||||||
current_date = row[2].strftime('%Y-%m-%d')
|
current_date = row[2].strftime("%Y-%m-%d")
|
||||||
if current_date not in schemas.keys():
|
if current_date not in schemas.keys():
|
||||||
schemas[current_date] = collections.OrderedDict()
|
schemas[current_date] = collections.OrderedDict()
|
||||||
|
|
||||||
@ -203,38 +205,37 @@ class Monitor(Thread):
|
|||||||
context = zmq.Context()
|
context = zmq.Context()
|
||||||
|
|
||||||
receiver = context.socket(ZMQ_SUB)
|
receiver = context.socket(ZMQ_SUB)
|
||||||
receiver.setsockopt_string(ZMQ_SUBSCRIBE, '')
|
receiver.setsockopt_string(ZMQ_SUBSCRIBE, "")
|
||||||
|
|
||||||
for binding in Settings.MONITOR_RECEIVER_BINDINGS:
|
for binding in Settings.MONITOR_RECEIVER_BINDINGS:
|
||||||
receiver.connect(binding)
|
receiver.connect(binding)
|
||||||
|
|
||||||
def monitor_worker(message: bytes) -> None:
|
def monitor_worker(message: bytes) -> None:
|
||||||
db = mariadb.connect(
|
db = mariadb.connect(
|
||||||
user=Settings.MONITOR_DB['user'],
|
user=Settings.MONITOR_DB["user"],
|
||||||
password=Settings.MONITOR_DB['password'],
|
password=Settings.MONITOR_DB["password"],
|
||||||
database=Settings.MONITOR_DB['database']
|
database=Settings.MONITOR_DB["database"],
|
||||||
)
|
)
|
||||||
|
|
||||||
message_text = zlib.decompress(message)
|
message_text = zlib.decompress(message)
|
||||||
json = simplejson.loads(message_text)
|
json = simplejson.loads(message_text)
|
||||||
|
|
||||||
# Default variables
|
# Default variables
|
||||||
schema_id = json['$schemaRef']
|
schema_id = json["$schemaRef"]
|
||||||
software_id = f'{json["header"]["softwareName"]} | {json["header"]["softwareVersion"]}'
|
software_id = f"{json['header']['softwareName']} | {json['header']['softwareVersion']}"
|
||||||
|
|
||||||
# Duplicates?
|
# Duplicates?
|
||||||
if Settings.RELAY_DUPLICATE_MAX_MINUTES:
|
if Settings.RELAY_DUPLICATE_MAX_MINUTES:
|
||||||
if duplicate_messages.is_duplicated(json):
|
if duplicate_messages.is_duplicated(json):
|
||||||
schema_id = 'DUPLICATE MESSAGE'
|
schema_id = "DUPLICATE MESSAGE"
|
||||||
|
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
c.execute(
|
c.execute(
|
||||||
'UPDATE `schemas` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()',
|
"UPDATE `schemas` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()",
|
||||||
(schema_id, )
|
(schema_id,),
|
||||||
)
|
)
|
||||||
c.execute(
|
c.execute(
|
||||||
'INSERT IGNORE INTO `schemas` (`name`, `dateStats`) VALUES (%s, UTC_DATE())',
|
"INSERT IGNORE INTO `schemas` (`name`, `dateStats`) VALUES (%s, UTC_DATE())", (schema_id,)
|
||||||
(schema_id, )
|
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@ -245,25 +246,18 @@ class Monitor(Thread):
|
|||||||
# Update software count
|
# Update software count
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
c.execute(
|
c.execute(
|
||||||
'UPDATE `softwares` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()',
|
"UPDATE `softwares` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()",
|
||||||
(software_id, )
|
(software_id,),
|
||||||
)
|
|
||||||
c.execute(
|
|
||||||
'INSERT IGNORE INTO `softwares` (`name`, `dateStats`) VALUES (%s, UTC_DATE())',
|
|
||||||
(software_id, )
|
|
||||||
)
|
)
|
||||||
|
c.execute("INSERT IGNORE INTO `softwares` (`name`, `dateStats`) VALUES (%s, UTC_DATE())", (software_id,))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
# Update schemas count
|
# Update schemas count
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
c.execute(
|
c.execute(
|
||||||
'UPDATE `schemas` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()',
|
"UPDATE `schemas` SET `hits` = `hits` + 1 WHERE `name` = %s AND `dateStats` = UTC_DATE()", (schema_id,)
|
||||||
(schema_id, )
|
|
||||||
)
|
|
||||||
c.execute(
|
|
||||||
'INSERT IGNORE INTO `schemas` (`name`, `dateStats`) VALUES (%s, UTC_DATE())',
|
|
||||||
(schema_id, )
|
|
||||||
)
|
)
|
||||||
|
c.execute("INSERT IGNORE INTO `schemas` (`name`, `dateStats`) VALUES (%s, UTC_DATE())", (schema_id,))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
@ -279,18 +273,9 @@ def apply_cors() -> None:
|
|||||||
|
|
||||||
Ref: <https://stackoverflow.com/a/17262900>
|
Ref: <https://stackoverflow.com/a/17262900>
|
||||||
"""
|
"""
|
||||||
response.set_header(
|
response.set_header("Access-Control-Allow-Origin", "*")
|
||||||
'Access-Control-Allow-Origin',
|
response.set_header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS")
|
||||||
'*'
|
response.set_header("Access-Control-Allow-Headers", "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token")
|
||||||
)
|
|
||||||
response.set_header(
|
|
||||||
'Access-Control-Allow-Methods',
|
|
||||||
'GET, POST, PUT, OPTIONS'
|
|
||||||
)
|
|
||||||
response.set_header(
|
|
||||||
'Access-Control-Allow-Headers',
|
|
||||||
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
@ -300,15 +285,15 @@ def main() -> None:
|
|||||||
|
|
||||||
m = Monitor()
|
m = Monitor()
|
||||||
m.start()
|
m.start()
|
||||||
app.add_hook('after_request', apply_cors)
|
app.add_hook("after_request", apply_cors)
|
||||||
app.run(
|
app.run(
|
||||||
host=Settings.MONITOR_HTTP_BIND_ADDRESS,
|
host=Settings.MONITOR_HTTP_BIND_ADDRESS,
|
||||||
port=Settings.MONITOR_HTTP_PORT,
|
port=Settings.MONITOR_HTTP_PORT,
|
||||||
server='gevent',
|
server="gevent",
|
||||||
certfile=Settings.CERT_FILE,
|
certfile=Settings.CERT_FILE,
|
||||||
keyfile=Settings.KEY_FILE
|
keyfile=Settings.KEY_FILE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user