Skip to content

Commit

Permalink
Persist run info
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Mar 4, 2021
1 parent 42869d1 commit f719b6f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 3 deletions.
1 change: 1 addition & 0 deletions conbench/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
from .contexts import *
from .index import *
from .machines import *
from .runs import *
from .users import *
2 changes: 2 additions & 0 deletions conbench/api/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def _201_created(example, schema=None):
spec.components.response("CompareList", _200_ok(ex.COMPARE_LIST))
spec.components.response("ContextEntity", _200_ok(ex.CONTEXT_ENTITY))
spec.components.response("MachineEntity", _200_ok(ex.MACHINE_ENTITY))
spec.components.response("RunEntity", _200_ok(ex.RUN_ENTITY))
spec.components.response("UserEntity", _200_ok(ex.USER_ENTITY))
spec.components.response("UserList", _200_ok(ex.USER_LIST))
spec.components.response("UserCreated", _201_created(ex.USER_ENTITY))
Expand All @@ -82,6 +83,7 @@ def _201_created(example, schema=None):
{"name": "Compare", "description": "Compare benchmarks"},
{"name": "Contexts", "description": "Benchmark contexts"},
{"name": "Machines", "description": "Benchmark machines"},
{"name": "Runs", "description": "Benchmark runs"},
{"name": "Ping", "description": "Monitor status"},
]

Expand Down
11 changes: 11 additions & 0 deletions conbench/api/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ def _api_machine_entity(machine_id):
}


def _api_run_entity(run_id, machine_id):
return {
"id": run_id,
"links": {
"self": "http://localhost/api/runs/%s/" % run_id,
"machine": "http://localhost/api/machines/%s/" % machine_id,
},
}


BENCHMARK_ENTITY = _api_benchmark_entity(
"some-benchmark-uuid-1",
"some-machine-uuid-1",
Expand Down Expand Up @@ -249,6 +259,7 @@ def _api_machine_entity(machine_id):
)
CONTEXT_ENTITY = _api_context_entity("some-context-uuid-1")
MACHINE_ENTITY = _api_machine_entity("some-machine-uuid-1")
RUN_ENTITY = _api_run_entity("some-run-uuid-1", "some-machine-uuid-1")
USER_ENTITY = _api_user_entity(FakeUser1())
USER_LIST = [
_api_user_entity(FakeUser1()),
Expand Down
43 changes: 43 additions & 0 deletions conbench/api/runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from ..api import rule
from ..api._endpoint import ApiEndpoint
from ..entities._entity import NotFound
from ..entities.run import Run, RunSerializer


class RunEntityAPI(ApiEndpoint):
serializer = RunSerializer()

def _get(self, run_id):
try:
run = Run.one(id=run_id)
except NotFound:
self.abort_404_not_found()
return run

def get(self, run_id):
"""
---
description: Get a run.
responses:
"200": "RunEntity"
"401": "401"
"404": "404"
parameters:
- name: run_id
in: path
schema:
type: string
tags:
- Runs
"""
run = self._get(run_id)
return self.serializer.one.dump(run)


run_entity_view = RunEntityAPI.as_view("run")

rule(
"/runs/<run_id>/",
view_func=run_entity_view,
methods=["GET"],
)
26 changes: 24 additions & 2 deletions conbench/entities/run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import flask as f
import sqlalchemy as s
from sqlalchemy.orm import relationship

from ..entities._entity import Base, EntityMixin, NotNull
from ..entities._entity import Base, EntityMixin, EntitySerializer, NotNull


class Run(Base, EntityMixin):
__tablename__ = "run"
id = NotNull(s.String(50), primary_key=True)
timestamp = NotNull(s.DateTime(timezone=False), server_default=s.sql.func.now())
github_id = NotNull(s.String(50), s.ForeignKey("github.id"))
github = relationship("GitHub", lazy="joined")
github = relationship("GitHub", lazy="select")
machine_id = NotNull(s.String(50), s.ForeignKey("machine.id"))
machine = relationship("Machine", lazy="select")


class _Serializer(EntitySerializer):
def _dump(self, run):
result = {
"id": run.id,
"links": {
"self": f.url_for("api.run", run_id=run.id, _external=True),
"machine": f.url_for(
"api.machine", machine_id=run.machine_id, _external=True
),
},
}
return result


class RunSerializer:
one = _Serializer()
many = _Serializer(many=True)
8 changes: 7 additions & 1 deletion conbench/entities/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ def create(data):
run_id = data["stats"]["run_id"]
run = Run.first(id=run_id)
if not run:
run = Run.create({"id": run_id, "github_id": github.id})
run = Run.create(
{
"id": run_id,
"github_id": github.id,
"machine_id": machine.id,
}
)

stats = data["stats"]
values = stats.pop("data")
Expand Down
34 changes: 34 additions & 0 deletions conbench/tests/api/_expected_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@
},
"description": "OK",
},
"RunEntity": {
"content": {
"application/json": {
"example": {
"id": "some-run-uuid-1",
"links": {
"machine": "http://localhost/api/machines/some-machine-uuid-1/",
"self": "http://localhost/api/runs/some-run-uuid-1/",
},
}
}
},
"description": "OK",
},
"UserCreated": {
"content": {
"application/json": {
Expand Down Expand Up @@ -909,6 +923,25 @@
"tags": ["Authentication"],
}
},
"/api/runs/{run_id}/": {
"get": {
"description": "Get a run.",
"parameters": [
{
"in": "path",
"name": "run_id",
"required": True,
"schema": {"type": "string"},
}
],
"responses": {
"200": {"$ref": "#/components/responses/RunEntity"},
"401": {"$ref": "#/components/responses/401"},
"404": {"$ref": "#/components/responses/404"},
},
"tags": ["Runs"],
}
},
"/api/users/": {
"get": {
"description": "Get a list of users.",
Expand Down Expand Up @@ -1005,6 +1038,7 @@
{"description": "Compare benchmarks", "name": "Compare"},
{"description": "Benchmark contexts", "name": "Contexts"},
{"description": "Benchmark machines", "name": "Machines"},
{"description": "Benchmark runs", "name": "Runs"},
{"description": "Monitor status", "name": "Ping"},
],
}
31 changes: 31 additions & 0 deletions conbench/tests/api/test_runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import copy

from ...api._examples import _api_run_entity
from ...entities.summary import Summary
from ...tests.api import _asserts
from ...tests.api.test_benchmarks import VALID_PAYLOAD


def _expected_entity(run):
return _api_run_entity(run.id, run.machine_id)


def create_benchmark_summary():
data = copy.deepcopy(VALID_PAYLOAD)
summary = Summary.create(data)
return summary


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

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

def test_get_run(self, client):
self.authenticate(client)
run = self._create()
response = client.get(f"/api/runs/{run.id}/")
self.assert_200_ok(response, _expected_entity(run))

0 comments on commit f719b6f

Please sign in to comment.