Skip to content

Commit

Permalink
Start adding tenant information
Browse files Browse the repository at this point in the history
  • Loading branch information
angelbarrera92 committed Mar 6, 2021
1 parent 150b2e6 commit 030e842
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 13 deletions.
17 changes: 17 additions & 0 deletions k8spin_reporter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def debug():
return render_template("debug.html")


@app.route("/organizations/<organization_id>/tenants/<tenant_id>")
def debug_tenant(organization_id, tenant_id):
return render_template("tenant.html", organization_id=organization_id, tenant_id=tenant_id)


@app.route("/report")
def report():
return render_template("report.html")
Expand Down Expand Up @@ -60,12 +65,24 @@ def tenants(organization_id):
return jsonify(tenants)


@app.route("/api/organizations/<organization_id>/tenants/<tenant_id>")
def tenant(organization_id, tenant_id):
t = data.tenant(db.engine, organization_id, tenant_id)
return jsonify(t)


@app.route("/api/organizations/<organization_id>/tenants/<tenant_id>/spaces")
def spaces(organization_id, tenant_id):
spaces = data.spaces(db.engine, tenant_id)
return jsonify(spaces)


@app.route("/api/organizations/<organization_id>/tenants/<tenant_id>/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)
Expand Down
22 changes: 11 additions & 11 deletions k8spin_reporter/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def gen_org_resources(orgs, since, hour_step):
data.append({
"reported": data_date,
"organization_id": org.get("id"),
"cpu": random.randrange(1000, 20000, 1000),
"memory": random.randrange(8589934592, 17179869184, 1073741824),
"cpu": random.randrange(5, 10, 1),
"memory": random.randrange(512, 1024, 128),
})
return data

Expand All @@ -57,8 +57,8 @@ def gen_tenant_resources(tenants, since, hour_step):
data.append({
"reported": data_date,
"tenant_id": tenant.get("id"),
"cpu": random.randrange(1000, 2000, 1000),
"memory": random.randrange(4294967296, 8589934592, 1073741824),
"cpu": random.randrange(3, 5, 1),
"memory": random.randrange(128, 512, 128),
})
return data

Expand Down Expand Up @@ -86,8 +86,8 @@ def gen_space_resources(spaces, since, hour_step):
data.append({
"reported": data_date,
"space_id": space.get("id"),
"cpu": random.randrange(0, 1000, 1000),
"memory": random.randrange(1073741824, 4294967296, 1073741824),
"cpu": random.randrange(1, 3, 1),
"memory": random.randrange(128, 512, 128),
})
return data

Expand All @@ -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(1, 2, 1),
"memory": random.randrange(16, 256, 16),
})
return data

Expand All @@ -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)

Expand Down
63 changes: 63 additions & 0 deletions k8spin_reporter/k8spin_reporter/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -119,6 +160,28 @@ def tenants(db_engine, organization_id):
return data


def tenant(db_engine, organization_id, tenant_id):
data = []
query = f"""
SELECT id,name
FROM tenant
WHERE
organization_id = "{organization_id}"
AND
id = "{tenant_id}"
"""
rows = db.query(db_engine, query)
for r in rows:
data.append({
"id": r[0],
"name": r[1]
})
if len(data) == 1:
return data[0]
else:
raise Exception("TBD: Multiple results")


def spaces(db_engine, tenant_id):
data = []
query = f"""
Expand Down
24 changes: 23 additions & 1 deletion k8spin_reporter/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,26 @@ LIMIT 24;



-- WHERE organization_id = "d831bb55-b4fd-4971-8230-4fdd0141d3b2"
-- 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;

54 changes: 53 additions & 1 deletion k8spin_reporter/static/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,66 @@ function organizations() {
<div class=\"column\" id=\""+ org.id + "-memory-history\">\
</div>\
</div>\
<div class=\"columns\"> \
<div class=\"column\" id=\""+ org.id + "-tenants\">\
</div>\
</div>\
</div>";
$("#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]
tenant_page_url = "organizations/" + org.id + "/tenants/" + tenant.id
tbody = tbody + "\
<tr>\
<td>"+ tenant.name + "</td>\
<td id=\""+ tenant.id + "-memory\"></td>\
<td id=\""+ tenant.id + "-cpu\"></td>\
<td><a href=\""+ tenant_page_url + "\"<i class=\"fas fa-info\"></i></a></td>\
</tr>\
";
tenant_resources(org, tenant);
}

content = "\
<table class=\"table\">\
<thead>\
<tr>\
<th>Name</th>\
<th>Memory</th>\
<th>CPU</th>\
<th>Details</th>\
</tr>\
</thead>\
<tbody id=\""+ org.id + "-tenants-table\">\
"+ tbody + "\
</tbody>\
</table>";
$("#" + 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) {
Expand Down Expand Up @@ -90,7 +142,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);
Expand Down
100 changes: 100 additions & 0 deletions k8spin_reporter/static/tenant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
$(document).ready(function () {
refresh();
$("#refresh").click(function () {
refresh();
});
});

function refresh() {
tenants();
}

function tenants() {
organization_id = $("#organization_id").val();
tenant_id = $("#tenant_id").val();
endpoint = "/api/organizations/" + organization_id + "/tenants/" + tenant_id
$.getJSON(endpoint, function (data) {
$("#tenant").empty();
tenant = data;
var new_box = "\
<div class=\"box\">\
<div class=\"columns\"> \
<div class=\"column is-one-fifth\">\
"+ tenant.name + "\
</div>\
<div class=\"column\" id=\""+ tenant.id + "-resources\">\
</div>\
<div class=\"column\" id=\""+ tenant.id + "-cpu-history\">\
</div>\
<div class=\"column\" id=\""+ tenant.id + "-memory-history\">\
</div>\
</div>\
<div class=\"columns\"> \
<div class=\"column\" id=\""+ tenant.id + "-spaces\">\
</div>\
</div>\
</div>";
$("#tenant").append(new_box);
tenant_resources(organization_id, tenant);
tenant_spaces(organization_id, tenant);
// organization_history(org);
});
}

function tenant_resources(organization_id, tenant) {
api_tenant_resources = "/api/organizations/" + organization_id + "/tenants/" + tenant.id + "/resources";
$.getJSON(api_tenant_resources, function (data) {
content = "\
<p>\
<span class=\"icon-text\"> \
<span class=\"icon\"> \
<i class=\"fas fa-memory\"></i> \
</span> \
<span>"+ data.used_memory + "/" + data.allocated_memory + "</span> \
</span></p> \
<p>\
<span class=\"icon-text\"> \
<span class=\"icon\"> \
<i class=\"fas fa-microchip\"></i> \
</span> \
<span>"+ data.used_cpu + "/" + data.allocated_cpu + "</span> \
</span></p>"
$("#" + tenant.id + "-resources").append(content);
});
}

function tenant_spaces(organization_id, tenant) {
api_tenant_spaces = "/api/organizations/" + organization_id + "/tenants/" + tenant.id + "/spaces";
$.getJSON(api_tenant_spaces, function (data) {
tbody = ""
for (var i = 0; i < data.length; i++) {
space = data[i]
space_page_url = "organizations/" + organization_id + "/tenants/" + tenant.id + "/spaces/" + space.id
tbody = tbody + "\
<tr>\
<td>"+ space.name + "</td>\
<td id=\""+ space.id + "-memory\"></td>\
<td id=\""+ space.id + "-cpu\"></td>\
<td><a href=\""+ space_page_url + "\"<i class=\"fas fa-info\"></i></a></td>\
</tr>\
";
// tenant_resources(org, tenant);
}

content = "\
<table class=\"table\">\
<thead>\
<tr>\
<th>Name</th>\
<th>Memory</th>\
<th>CPU</th>\
<th>Details</th>\
</tr>\
</thead>\
<tbody id=\""+ tenant.id + "-spaces-table\">\
"+ tbody + "\
</tbody>\
</table>";
$("#" + tenant.id + "-spaces").append(content);
});
}
Loading

0 comments on commit 030e842

Please sign in to comment.