Skip to content

Commit

Permalink
More list APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Jul 21, 2021
1 parent 4dd128c commit 797ad1a
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 10 deletions.
6 changes: 4 additions & 2 deletions conbench/api/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def _201_created(example, schema=None):
spec.components.response("DistributionEntity", _200_ok(ex.DISTRIBUTION_ENTITY))
spec.components.response("DistributionList", _200_ok([ex.DISTRIBUTION_ENTITY]))
spec.components.response("ContextEntity", _200_ok(ex.CONTEXT_ENTITY))
spec.components.response("ContextList", _200_ok([ex.CONTEXT_ENTITY]))
spec.components.response("MachineEntity", _200_ok(ex.MACHINE_ENTITY))
spec.components.response("MachineList", _200_ok([ex.MACHINE_ENTITY]))
spec.components.response("RunEntity", _200_ok(ex.RUN_ENTITY))
spec.components.response("RunList", _200_ok(ex.RUN_LIST))
spec.components.response("UserEntity", _200_ok(ex.USER_ENTITY))
Expand All @@ -84,8 +86,8 @@ def _201_created(example, schema=None):
{"name": "Index", "description": "List of endpoints"},
{"name": "Users", "description": "Manage users"},
{"name": "Benchmarks", "description": "Record benchmarks"},
{"name": "Commits", "description": "Benchmarked Commits"},
{"name": "Compare", "description": "Compare benchmarks"},
{"name": "Commits", "description": "Benchmarked commits"},
{"name": "Comparisons", "description": "Benchmark comparisons"},
{"name": "Contexts", "description": "Benchmark contexts"},
{"name": "Distributions", "description": "Benchmark distributions"},
{"name": "Machines", "description": "Benchmark machines"},
Expand Down
2 changes: 2 additions & 0 deletions conbench/api/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def _api_context_entity(context_id, links=True):
"benchmark_language_version": "Python 3.8.5",
"benchmark_language": "Python",
"links": {
"list": "http://localhost/api/contexts/",
"self": "http://localhost/api/contexts/%s/" % context_id,
},
}
Expand Down Expand Up @@ -273,6 +274,7 @@ def _api_machine_entity(machine_id, links=True):
"os_name": "macOS",
"os_version": "10.15.7",
"links": {
"list": "http://localhost/api/machines/",
"self": "http://localhost/api/machines/%s/" % machine_id,
},
}
Expand Down
23 changes: 23 additions & 0 deletions conbench/api/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
from ..entities.context import Context, ContextSerializer


class ContextListAPI(ApiEndpoint):
serializer = ContextSerializer()

def get(self):
"""
---
description: Get a list of contexts.
responses:
"200": "ContextList"
"401": "401"
tags:
- Contexts
"""
contexts = Context.all(order_by=Context.id.asc(), limit=500)
return self.serializer.many.dump(contexts)


class ContextEntityAPI(ApiEndpoint):
serializer = ContextSerializer()

Expand Down Expand Up @@ -35,9 +52,15 @@ def get(self, context_id):


context_entity_view = ContextEntityAPI.as_view("context")
context_list_view = ContextListAPI.as_view("contexts")

rule(
"/contexts/<context_id>/",
view_func=context_entity_view,
methods=["GET"],
)
rule(
"/contexts/",
view_func=context_list_view,
methods=["GET"],
)
24 changes: 24 additions & 0 deletions conbench/api/machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
from ..entities.machine import Machine, MachineSerializer


class MachineListAPI(ApiEndpoint):
serializer = MachineSerializer()

def get(self):
"""
---
description: Get a list of machines.
responses:
"200": "MachineList"
"401": "401"
tags:
- Machines
"""
machines = Machine.all(order_by=Machine.name.asc(), limit=500)
return self.serializer.many.dump(machines)


class MachineEntityAPI(ApiEndpoint):
serializer = MachineSerializer()

Expand Down Expand Up @@ -35,9 +52,16 @@ def get(self, machine_id):


machine_entity_view = MachineEntityAPI.as_view("machine")
machine_list_view = MachineListAPI.as_view("machines")


rule(
"/machines/<machine_id>/",
view_func=machine_entity_view,
methods=["GET"],
)
rule(
"/machines/",
view_func=machine_list_view,
methods=["GET"],
)
1 change: 1 addition & 0 deletions conbench/entities/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def _dump(self, context):
result = {
"id": context.id,
"links": {
"list": f.url_for("api.contexts", _external=True),
"self": f.url_for("api.context", context_id=context.id, _external=True),
},
}
Expand Down
1 change: 1 addition & 0 deletions conbench/entities/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def _dump(self, machine):
"cpu_frequency_max_hz": machine.cpu_frequency_max_hz,
"memory_bytes": machine.memory_bytes,
"links": {
"list": f.url_for("api.machines", _external=True),
"self": f.url_for("api.machine", machine_id=machine.id, _external=True),
},
}
Expand Down
82 changes: 78 additions & 4 deletions conbench/tests/api/_expected_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,36 @@
"benchmark_language_version": "Python 3.8.5",
"id": "some-context-uuid-1",
"links": {
"self": "http://localhost/api/contexts/some-context-uuid-1/"
"list": "http://localhost/api/contexts/",
"self": "http://localhost/api/contexts/some-context-uuid-1/",
},
}
}
},
"description": "OK",
},
"ContextList": {
"content": {
"application/json": {
"example": [
{
"arrow_compiler_flags": "-fPIC -arch x86_64 -arch x86_64 -std=c++11 -Qunused-arguments -fcolor-diagnostics -O3 -DNDEBUG",
"arrow_compiler_id": "AppleClang",
"arrow_compiler_version": "11.0.0.11000033",
"arrow_version": "2.0.0",
"benchmark_language": "Python",
"benchmark_language_version": "Python 3.8.5",
"id": "some-context-uuid-1",
"links": {
"list": "http://localhost/api/contexts/",
"self": "http://localhost/api/contexts/some-context-uuid-1/",
},
}
]
}
},
"description": "OK",
},
"DistributionEntity": {
"content": {
"application/json": {
Expand Down Expand Up @@ -520,7 +543,8 @@
"id": "some-machine-uuid-1",
"kernel_name": "19.6.0",
"links": {
"self": "http://localhost/api/machines/some-machine-uuid-1/"
"list": "http://localhost/api/machines/",
"self": "http://localhost/api/machines/some-machine-uuid-1/",
},
"memory_bytes": 17179869184,
"name": "diana",
Expand All @@ -531,6 +555,36 @@
},
"description": "OK",
},
"MachineList": {
"content": {
"application/json": {
"example": [
{
"architecture_name": "x86_64",
"cpu_core_count": 2,
"cpu_frequency_max_hz": 3500000000,
"cpu_l1d_cache_bytes": 32768,
"cpu_l1i_cache_bytes": 32768,
"cpu_l2_cache_bytes": 262144,
"cpu_l3_cache_bytes": 4194304,
"cpu_model_name": "Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz",
"cpu_thread_count": 4,
"id": "some-machine-uuid-1",
"kernel_name": "19.6.0",
"links": {
"list": "http://localhost/api/machines/",
"self": "http://localhost/api/machines/some-machine-uuid-1/",
},
"memory_bytes": 17179869184,
"name": "diana",
"os_name": "macOS",
"os_version": "10.15.7",
}
]
}
},
"description": "OK",
},
"Ping": {
"content": {
"application/json": {
Expand Down Expand Up @@ -1059,6 +1113,16 @@
"tags": ["Compare"],
}
},
"/api/contexts/": {
"get": {
"description": "Get a list of contexts.",
"responses": {
"200": {"$ref": "#/components/responses/ContextList"},
"401": {"$ref": "#/components/responses/401"},
},
"tags": ["Contexts"],
}
},
"/api/contexts/{context_id}/": {
"get": {
"description": "Get a context.",
Expand Down Expand Up @@ -1132,6 +1196,16 @@
"tags": ["Authentication"],
}
},
"/api/machines/": {
"get": {
"description": "Get a list of machines.",
"responses": {
"200": {"$ref": "#/components/responses/MachineList"},
"401": {"$ref": "#/components/responses/401"},
},
"tags": ["Machines"],
}
},
"/api/machines/{machine_id}/": {
"get": {
"description": "Get a machine.",
Expand Down Expand Up @@ -1297,8 +1371,8 @@
{"description": "List of endpoints", "name": "Index"},
{"description": "Manage users", "name": "Users"},
{"description": "Record benchmarks", "name": "Benchmarks"},
{"description": "Benchmarked Commits", "name": "Commits"},
{"description": "Compare benchmarks", "name": "Compare"},
{"description": "Benchmarked commits", "name": "Commits"},
{"description": "Benchmark comparisons", "name": "Comparisons"},
{"description": "Benchmark contexts", "name": "Contexts"},
{"description": "Benchmark distributions", "name": "Distributions"},
{"description": "Benchmark machines", "name": "Machines"},
Expand Down
22 changes: 20 additions & 2 deletions conbench/tests/api/test_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@ def _expected_entity(context):
return _api_context_entity(context.id)


def create_context():
summary = _fixtures.create_benchmark_summary()
return summary.context


class TestContextGet(_asserts.GetEnforcer):
url = "/api/contexts/{}/"
public = True

def _create(self):
summary = _fixtures.create_benchmark_summary()
return summary.context
return create_context()

def test_get_context(self, client):
self.authenticate(client)
context = self._create()
response = client.get(f"/api/contexts/{context.id}/")
self.assert_200_ok(response, _expected_entity(context))


class TestContextList(_asserts.ListEnforcer):
url = "/api/contexts/"
public = True

def _create(self):
return create_context()

def test_context_list(self, client):
self.authenticate(client)
context = self._create()
response = client.get("/api/contexts/")
self.assert_200_ok(response, contains=_expected_entity(context))
22 changes: 20 additions & 2 deletions conbench/tests/api/test_machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@ def _expected_entity(machine):
return _api_machine_entity(machine.id)


def create_machine():
summary = _fixtures.create_benchmark_summary()
return summary.run.machine


class TestMachineGet(_asserts.GetEnforcer):
url = "/api/machines/{}/"
public = True

def _create(self):
summary = _fixtures.create_benchmark_summary()
return summary.run.machine
return create_machine()

def test_get_machine(self, client):
self.authenticate(client)
machine = self._create()
response = client.get(f"/api/machines/{machine.id}/")
self.assert_200_ok(response, _expected_entity(machine))


class TestMachineList(_asserts.ListEnforcer):
url = "/api/machines/"
public = True

def _create(self):
return create_machine()

def test_machine_list(self, client):
self.authenticate(client)
machine = self._create()
response = client.get("/api/machines/")
self.assert_200_ok(response, contains=_expected_entity(machine))

0 comments on commit 797ad1a

Please sign in to comment.