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