Commit e1755ff1 authored by Jerome Touvier's avatar Jerome Touvier
Browse files

add client number

parent 1396d088
......@@ -32,10 +32,6 @@ def is_like_or_equal(params, key):
return " OR ".join(subquery)
def get_table():
return "sent_data_summary_weekly"
def sql_common_string(params, begin=""):
s = begin
# network, station, location, channel parameters
......@@ -72,13 +68,18 @@ def sql_request(params):
s = f"""{s} AND ({is_like_or_equal(params, "year")})"""
s = f"""{s} ORDER BY {columns}, date DESC"""
return s.replace("?", "_").replace("*", "%")
table = "sent_data_summary_weekly"
if params["request"] == "country":
s = f"SELECT country, sum(requests)::INTEGER FROM {get_table()}"
if "all" in params["country"]:
s = f"SELECT sum(requests)::INTEGER, sum(hll_cardinality(clients))::INTEGER FROM {table}"
s = f"SELECT country, sum(requests)::INTEGER, sum(hll_cardinality(clients))::INTEGER FROM {table}"
elif params["request"] == "send":
s = f"SELECT sum(bytes) FROM {get_table()}"
s = f"SELECT sum(bytes)::INTEGER FROM {table}"
elif params["request"] == "timeseries":
s = f"SELECT date, country, sum(bytes), sum(hll_cardinality(clients))::INTEGER FROM {get_table()}"
s = f"SELECT date, country, sum(bytes)::INTEGER, sum(hll_cardinality(clients))::INTEGER FROM {table}"
s = sql_common_string(params, s)
if "all" not in params["country"]:
......@@ -91,7 +92,7 @@ def sql_request(params):
elif "dataselect" in params["media"]:
s = f"{s} AND protocol = 'dataselect'"
if params["request"] == "country":
if params["request"] == "country" and "all" not in params["country"]:
s = f"""{s} GROUP BY country"""
elif params["request"] == "timeseries":
if "seedlink" in params["media"] and "dataselect" in params["media"]:
......@@ -116,35 +117,20 @@ def collect_data(params):
return curs.fetchall()
def sum_results(params, data):
""" Adds the results from the different media tables. """
newdata = list()
def format_results(params, data):
data = list(map(list, data))
if params["request"] == "country":
result = dict()
for row in data:
if row[1]:
if row[0] in result: # Is this country already there ?
result[row[0]] = result[row[0]] + int(row[1])
result[row[0]] = int(row[1])
if "all" in params["country"]:
result = {"all": sum([int(val) for val in result.values()])}
for key, val in result.items():
newdata.append([key, "{:_}".format(val)])
data = [["all", data[0][0], data[0][1]]]
for row in data:
row[:] = [row[0], "{:_}".format(row[1]), "{:_}".format(row[2])]
elif params["request"] == "send":
result = 0
for row in data:
if row[0]:
result = result + int(row[0])
["{:_}".format(result) + f" bytes ({human_readable_size(result)})"]
result = data[0][0]
data = [["{:_}".format(result) + f" bytes ({human_readable_size(result)})"]]
return newdata
return data
def get_header(params):
......@@ -152,7 +138,7 @@ def get_header(params):
header = ["size", "year", "network", "station", "channel", "quality", "type"]
elif params["request"] == "country":
header = ["country", "requests"]
header = ["country", "requests", "clients"]
elif params["request"] == "send":
if len(params["media"]) == 1 and "all" not in params["media"]:
if "seedlink" in params["media"]:
......@@ -222,15 +208,12 @@ def get_output(params):
if data is None:
return data
if params["request"] in ("send", "country"):
data = sum_results(params, data)
if not data:
code = params["nodata"]
return error_request(msg=f"HTTP._{code}_", details=Error.NODATA, code=code)"Number of collected rows: {len(data)}")
data = [[str(val) for val in row] for row in data]
data = format_results(params, data)
return get_response(params, data)
except Exception as ex:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment