Skip to content

Commit

Permalink
Commit API
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Jul 21, 2021
1 parent 57928cf commit 16ac362
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 166 deletions.
1 change: 1 addition & 0 deletions conbench/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ._errors import * # noqa
from .auth import * # noqa
from .benchmarks import * # noqa
from .commits import * # noqa
from .compare import * # noqa
from .contexts import * # noqa
from .distributions import * # noqa
Expand Down
7 changes: 5 additions & 2 deletions conbench/api/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ def _201_created(example, schema=None):
spec.components.response("Ping", _200_ok(ex.API_PING, "Ping"))
spec.components.response("Index", _200_ok(ex.API_INDEX))
spec.components.response("BenchmarkEntity", _200_ok(ex.BENCHMARK_ENTITY))
spec.components.response("BenchmarkList", _200_ok(ex.BENCHMARK_LIST))
spec.components.response("BenchmarkList", _200_ok([ex.BENCHMARK_ENTITY]))
spec.components.response("BenchmarkCreated", _201_created(ex.BENCHMARK_ENTITY))
spec.components.response("CommitEntity", _200_ok(ex.COMMIT_ENTITY))
spec.components.response("CommitList", _200_ok([ex.COMMIT_ENTITY]))
spec.components.response("CompareEntity", _200_ok(ex.COMPARE_ENTITY))
spec.components.response("CompareList", _200_ok(ex.COMPARE_LIST))
spec.components.response("DistributionEntity", _200_ok(ex.DISTRIBUTION_ENTITY))
spec.components.response("DistributionList", _200_ok(ex.DISTRIBUTION_LIST))
spec.components.response("DistributionList", _200_ok([ex.DISTRIBUTION_ENTITY]))
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))
Expand All @@ -82,6 +84,7 @@ 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": "Contexts", "description": "Benchmark contexts"},
{"name": "Distributions", "description": "Benchmark distributions"},
Expand Down
59 changes: 13 additions & 46 deletions conbench/api/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def _api_benchmark_entity(summary_id, context_id, case_id, batch_id, run_id, nam
}


def _api_commit_entity(commit_id):
return {
def _api_commit_entity(commit_id, links=True):
result = {
"author_avatar": "https://avatars.githubusercontent.com/u/878798?v=4",
"author_login": "dianaclarke",
"author_name": "Diana Clarke",
Expand All @@ -99,7 +99,14 @@ def _api_commit_entity(commit_id):
"parent_sha": "4beb514d071c9beec69b8917b5265e77ade22fb3",
"parent_url": "https://github.com/apache/arrow/commit/4beb514d071c9beec69b8917b5265e77ade22fb3",
"timestamp": "2021-02-25T01:02:51",
"links": {
"list": "http://localhost/api/commits/",
"self": "http://localhost/api/commits/%s/" % commit_id,
},
}
if not links:
result.pop("links", None)
return result


def _api_compare_entity(benchmark_ids, batch_ids, run_ids, batch, benchmark, tags):
Expand Down Expand Up @@ -239,6 +246,7 @@ def _api_distribution_entity(
"observations": observations,
"repository": "https://github.com/apache/arrow",
"links": {
"list": "http://localhost/api/distributions/",
"self": "http://localhost/api/distributions/%s/" % distribution_id,
},
}
Expand Down Expand Up @@ -278,9 +286,10 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"id": run_id,
"name": "commit: 02addad336ba19a654f9c857ede546331be7b631",
"timestamp": now,
"commit": _api_commit_entity(commit_id),
"commit": _api_commit_entity(commit_id, links=False),
"machine": _api_machine_entity(machine_id, links=False),
"links": {
"list": "http://localhost/api/runs/",
"self": "http://localhost/api/runs/%s/" % run_id,
},
}
Expand All @@ -298,24 +307,7 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"some-run-uuid-1",
"file-write",
)
BENCHMARK_LIST = [
_api_benchmark_entity(
"some-benchmark-uuid-1",
"some-context-uuid-1",
"some-case-uuid-1",
"some-batch-uuid-1",
"some-run-uuid-1",
"file-write",
),
_api_benchmark_entity(
"some-benchmark-uuid-2",
"some-context-uuid-1",
"some-case-uuid-1",
"some-batch-uuid-1",
"some-run-uuid-1",
"file-write",
),
]
COMMIT_ENTITY = _api_commit_entity("some-commit-uuid-1")
COMPARE_ENTITY = _api_compare_entity(
["some-benchmark-uuid-1", "some-benchmark-uuid-2"],
["some-batch-uuid-1", "some-batch-uuid-2"],
Expand Down Expand Up @@ -369,24 +361,6 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"some-machine-hash-1",
33,
)
DISTRIBUTION_LIST = [
_api_distribution_entity(
"some-distribution-uuid-1",
"some-sha-1",
"some-case-uuid-1",
"some-context-uuid-1",
"some-machine-hash-1",
33,
),
_api_distribution_entity(
"some-distribution-uuid-2",
"some-sha-2",
"some-case-uuid-2",
"some-context-uuid-2",
"some-machine-hash-2",
33,
),
]
MACHINE_ENTITY = _api_machine_entity("some-machine-uuid-1")
RUN_ENTITY = _api_run_entity(
"some-run-uuid-1",
Expand All @@ -403,13 +377,6 @@ def _api_run_entity(run_id, commit_id, machine_id, now, baseline_id):
"2021-02-04T17:22:05.225583",
None,
),
_api_run_entity(
"some-run-uuid-2",
"some-commit-uuid-1",
"some-machine-uuid-1",
"2021-03-04T17:18:05.715583",
None,
),
]
USER_ENTITY = _api_user_entity(FakeUser1())
USER_LIST = [
Expand Down
66 changes: 66 additions & 0 deletions conbench/api/commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from ..api import rule
from ..api._endpoint import ApiEndpoint
from ..entities._entity import NotFound
from ..entities.commit import Commit, CommitSerializer


class CommitListAPI(ApiEndpoint):
serializer = CommitSerializer()

def get(self):
"""
---
description: Get a list of commits.
responses:
"200": "CommitList"
"401": "401"
tags:
- Commits
"""
commits = Commit.all(order_by=Commit.timestamp.desc(), limit=500)
return self.serializer.many.dump(commits)


class CommitEntityAPI(ApiEndpoint):
serializer = CommitSerializer()

def _get(self, commit_id):
try:
commit = Commit.one(id=commit_id)
except NotFound:
self.abort_404_not_found()
return commit

def get(self, commit_id):
"""
---
description: Get a commit.
responses:
"200": "CommitEntity"
"401": "401"
"404": "404"
parameters:
- name: commit_id
in: path
schema:
type: string
tags:
- Commits
"""
commit = self._get(commit_id)
return self.serializer.one.dump(commit)


commit_entity_view = CommitEntityAPI.as_view("commit")
commit_list_view = CommitListAPI.as_view("commits")

rule(
"/commits/<commit_id>/",
view_func=commit_entity_view,
methods=["GET"],
)
rule(
"/commits/",
view_func=commit_list_view,
methods=["GET"],
)
4 changes: 3 additions & 1 deletion conbench/api/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def get(self):
tags:
- Distributions
"""
distributions = Distribution.all(limit=500)
distributions = Distribution.all(
order_by=Distribution.last_timestamp.desc(), limit=500
)
return self.serializer.many.dump(distributions)


Expand Down
5 changes: 5 additions & 0 deletions conbench/entities/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

import dateutil.parser
import flask as f
import requests
import sqlalchemy as s

Expand Down Expand Up @@ -50,6 +51,10 @@ def _dump(self, commit):
"author_login": commit.author_login,
"author_avatar": commit.author_avatar,
"timestamp": commit.timestamp.isoformat(),
"links": {
"list": f.url_for("api.commits", _external=True),
"self": f.url_for("api.commit", commit_id=commit.id, _external=True),
},
}


Expand Down
1 change: 1 addition & 0 deletions conbench/entities/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _dump(self, distribution):
"last_timestamp": distribution.last_timestamp.isoformat(),
"observations": distribution.observations,
"links": {
"list": f.url_for("api.distributions", _external=True),
"self": f.url_for(
"api.distribution", distribution_id=distribution.id, _external=True
),
Expand Down
1 change: 1 addition & 0 deletions conbench/entities/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _dump(self, run):
"commit": commit,
"machine": machine,
"links": {
"list": f.url_for("api.runs", _external=True),
"self": f.url_for("api.run", run_id=run.id, _external=True),
},
}
Expand Down

0 comments on commit 16ac362

Please sign in to comment.