diff --git a/k8spin_reporter/app.py b/k8spin_reporter/app.py index 5cdbea6c..05bcc19f 100644 --- a/k8spin_reporter/app.py +++ b/k8spin_reporter/app.py @@ -66,6 +66,12 @@ def spaces(organization_id, tenant_id): return jsonify(spaces) +@app.route("/api/organizations//tenants//resources") +def tenant_resources(organization_id, tenant_id): + resources = data.tenant_current_resources(db.engine, tenant_id) + return jsonify(resources) + + if __name__ == "__main__": health = HealthCheck(app, "/health") scheduler.init_app(app) diff --git a/k8spin_reporter/gen.py b/k8spin_reporter/gen.py index 9f632d20..71c2a3af 100644 --- a/k8spin_reporter/gen.py +++ b/k8spin_reporter/gen.py @@ -86,7 +86,7 @@ def gen_space_resources(spaces, since, hour_step): data.append({ "reported": data_date, "space_id": space.get("id"), - "cpu": random.randrange(0, 1000, 1000), + "cpu": random.randrange(10, 1000, 1000), "memory": random.randrange(1073741824, 4294967296, 1073741824), }) return data @@ -103,8 +103,8 @@ def gen_space_usage(spaces, since, hour_step): "organization_id": space.get("organization_id"), "tenant_id": space.get("tenant_id"), "space_id": space.get("id"), - "cpu": random.randrange(0, 1000, 100), - "memory": random.randrange(0, 2147483648, 104857600), + "cpu": random.randrange(10, 1000, 100), + "memory": random.randrange(104857600, 2147483648, 104857600), }) return data @@ -123,11 +123,11 @@ def gen_sql(data, table_name): if __name__ == "__main__": since = datetime.datetime.now() - datetime.timedelta(days=10) - orgs = gen_orgs(5) + orgs = gen_orgs(1) org_resources = gen_org_resources(orgs, since, 1) - tenants = gen_tenant(orgs, 20) + tenants = gen_tenant(orgs, 1) tenant_resources = gen_tenant_resources(tenants, since, 1) - spaces = gen_spaces(tenants, 100) + spaces = gen_spaces(tenants, 2) space_resources = gen_space_resources(spaces, since, 1) space_usage = gen_space_usage(spaces, since, 1) diff --git a/k8spin_reporter/k8spin_reporter/data.py b/k8spin_reporter/k8spin_reporter/data.py index 1c1358e6..89e632bd 100644 --- a/k8spin_reporter/k8spin_reporter/data.py +++ b/k8spin_reporter/k8spin_reporter/data.py @@ -57,6 +57,47 @@ def org_current_resources(db_engine, organization_id): return data[0] +def tenant_current_resources(db_engine, tenant_id): + data = [] + query = f""" + SELECT t1.allocated_cpu, t1.allocated_memory, t2.used_cpu, t2.used_memory + FROM + ( + SELECT t2.tenant_id as tenant_id, t2.cpu as allocated_cpu, t2.memory as allocated_memory FROM + ( + SELECT cpu,memory, max(id) id + FROM tenant_resources + GROUP BY tenant_id + ) t1, tenant_resources t2 + WHERE t1.id = t2.id + ) t1, + ( + SELECT t2.tenant_id as tenant_id, sum(t2.cpu) as used_cpu, sum(t2.memory) as used_memory FROM + ( + SELECT tenant_id, space_id, max(id) id + FROM space_usage + GROUP BY tenant_id, space_id + ) + t1, space_usage t2 + WHERE t1.id = t2.id + GROUP BY t2.tenant_id + ) t2 + WHERE t1.tenant_id = t2.tenant_id + AND t1.tenant_id = "{tenant_id}" + """ + rows = db.query(db_engine, query, False) + for r in rows: + data.append({ + "allocated_cpu": r[0], + "allocated_memory": r[1], + "used_cpu": r[2], + "used_memory": r[3], + }) + if len(data) != 1: + return None + return data[0] + + # TODO: Currently it returns up to 7 days of data. Aggregated by day. # TODO: Make it configurable by day, hour... def org_history_resouces(db_engine, organization_id): diff --git a/k8spin_reporter/queries.sql b/k8spin_reporter/queries.sql index a58bb06a..9769ba99 100644 --- a/k8spin_reporter/queries.sql +++ b/k8spin_reporter/queries.sql @@ -199,4 +199,26 @@ LIMIT 24; --- WHERE organization_id = "d831bb55-b4fd-4971-8230-4fdd0141d3b2" \ No newline at end of file +-- WHERE organization_id = "d831bb55-b4fd-4971-8230-4fdd0141d3b2" + + +-- Tenant CPU and Memory configurable + +SELECT * FROM tenant_resources WHERE +tenant_id = "31fc7679-3ba5-4983-812d-f2340bc03233" +ORDER BY id desc LIMIT 1; + + +SELECT id FROM space WHERE +tenant_id = "31fc7679-3ba5-4983-812d-f2340bc03233"; + + +SELECT * FROM space_resources WHERE +space_id = "78aa18f1-4526-4d08-9720-5970e7259a81" +ORDER BY id desc LIMIT 1; + + +SELECT * FROM space_resources WHERE +space_id = "26c13dbb-569e-414f-907a-ce473ac43f8a" +ORDER BY id desc LIMIT 1; + diff --git a/k8spin_reporter/static/debug.js b/k8spin_reporter/static/debug.js index 30a8843e..8c4889e1 100644 --- a/k8spin_reporter/static/debug.js +++ b/k8spin_reporter/static/debug.js @@ -27,14 +27,64 @@ function organizations() {
\
\ \ +
\ +
\ +
\ +
\ "; $("#orgs").append(new_box); organization_resources(org); + organization_tenants(org); organization_history(org); } }); } +function organization_tenants(org) { + api_org_tenants = "/api/organizations/" + org.id + "/tenants"; + $.getJSON(api_org_tenants, function (data) { + tbody = "" + for (var i = 0; i < data.length; i++) { + tenant = data[i] + tbody = tbody + "\ + \ + "+ tenant.name + "\ + \ + \ + \ + \ + "; + tenant_resources(org, tenant); + } + + content = "\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + "+ tbody + "\ + \ +
NameMemoryCPUDetails
"; + $("#" + org.id + "-tenants").append(content); + }); +} + +function tenant_resources(org, tenant) { + api_tenant_resources = "/api/organizations/" + org.id + "/tenants/" + tenant.id + "/resources"; + $.getJSON(api_tenant_resources, function (data) { + cpu = data.used_cpu + "/" + data.allocated_cpu + memory = data.used_memory + "/" + data.allocated_memory + $("#" + tenant.id + "-cpu").append(cpu); + $("#" + tenant.id + "-memory").append(memory); + }); +} + function organization_resources(org) { api_org_resources = "/api/organizations/" + org.id + "/resources"; $.getJSON(api_org_resources, function (data) { @@ -90,7 +140,7 @@ function organization_history(org) { label: "Memory Allocated", data: [] } - for (var i = (data.length-1); i >= 0; i--) { + for (var i = (data.length - 1); i >= 0; i--) { record = data[i] chartCPUData.labels.push(record.day);