Skip to content

Commit

Permalink
Merge pull request foundriesio#88 from doanac/artifact-size
Browse files Browse the repository at this point in the history
Add the ability give artifact sizes
  • Loading branch information
doanac authored Jun 6, 2023
2 parents 8f756a4 + 10bd6e2 commit 5eb8019
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jobserv/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _promoted_as_json(storage, build):
test["name"] = "%s-%s" % (run.name, test["name"])
rv["tests"].append(test)
for a in storage.list_artifacts(run):
rv["artifacts"].append("%s/%s" % (run.name, a))
rv["artifacts"].append("%s/%s" % (run.name, a["name"]))
return rv


Expand Down
8 changes: 6 additions & 2 deletions jobserv/api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ def run_get(proj, build_id, run):
r = _get_run(proj, build_id, run)
data = r.as_json(detailed=True)
artifacts = []
v2 = request.args.get("version") == "v2"
for a in Storage().list_artifacts(r):
u = url_for(
"api_run.run_get_artifact",
proj=proj,
build_id=build_id,
run=run,
path=a,
path=a["name"],
_external=True,
)
artifacts.append(u)
if v2:
artifacts.append({"url": u, "size_bytes": a["size_bytes"]})
else:
artifacts.append(u)
data["artifacts"] = artifacts
return jsendify({"run": data})

Expand Down
5 changes: 4 additions & 1 deletion jobserv/storage/gce_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def _get_as_string(self, storage_path):
def list_artifacts(self, run):
name = "%s/%s/%s/" % (run.build.project.name, run.build.build_id, run.name)
return [
x.name[len(name) :]
{
"name": x.name[len(name) :],
"size_bytes": x.size,
}
for x in self.bucket.list_blobs(prefix=name)
if not x.name.endswith(".rundef.json")
]
Expand Down
8 changes: 7 additions & 1 deletion jobserv/storage/local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ def list_artifacts(self, run):
for base, _, names in os.walk(path):
for name in names:
if name != ".rundef.json":
yield os.path.join(base, name)[len(path) :]
name = os.path.join(base, name)
size = os.stat(name).st_size
item = {
"name": name[len(path) :],
"size_bytes": size,
}
yield item

def delete_build(self, build):
path = os.path.join(self.artifacts, build.project.name, str(build.build_id))
Expand Down
10 changes: 8 additions & 2 deletions tests/test_local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ def test_list(self):
self.storage._create_from_string(os.path.join(path, "file1.txt"), "a")
self.storage._create_from_string(os.path.join(path, "file2.txt"), "b")
self.storage._create_from_string(os.path.join(path, "subdir/1"), "c")
expected = ["file1.txt", "file2.txt", "subdir/1"]
found = list(sorted(self.storage.list_artifacts(self.run)))
expected = [
{"name": "file1.txt", "size_bytes": 1},
{"name": "file2.txt", "size_bytes": 1},
{"name": "subdir/1", "size_bytes": 1},
]
found = list(
sorted(self.storage.list_artifacts(self.run), key=lambda x: x["name"])
)
self.assertEqual(expected, found)

@mock.patch("jobserv.api.run.Storage")
Expand Down

0 comments on commit 5eb8019

Please sign in to comment.