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 5, 2021
1 parent 150b2e6 commit 8040773
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
6 changes: 6 additions & 0 deletions k8spin_reporter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def spaces(organization_id, 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
12 changes: 6 additions & 6 deletions k8spin_reporter/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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(10, 1000, 100),
"memory": random.randrange(104857600, 2147483648, 104857600),
})
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
41 changes: 41 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
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;

52 changes: 51 additions & 1 deletion k8spin_reporter/static/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,64 @@ 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]
tbody = tbody + "\
<tr>\
<td>"+ tenant.name + "</td>\
<td id=\""+ tenant.id + "-memory\"></td>\
<td id=\""+ tenant.id + "-cpu\"></td>\
<td><a href=\"debug/"+ tenant.id + "\"<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 +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);
Expand Down

0 comments on commit 8040773

Please sign in to comment.