Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom metadata to /packit/metadata endpoint #69

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Run all tests with `cargo test`.
## API
### GET /

```
```json
{
"status": "succcess",
"data": {
Expand All @@ -66,17 +66,17 @@ Returns hash of all current packet ids, ordered alphanumerically and concatenate
in the `outpack` config, unless a query parameter specifying an alternative is passed:
e.g. `/checksum?alg=md5`.

```
```json
{
"status": "succcess",
"data": "md5:117723186364b4b409081b1bd347d406"
"data": "md5:117723186364b4b409081b1bd347d406",
"errors": null
}
```

### GET /metadata/list

```
```json
{
"status": "success",
"errors": null,
Expand Down Expand Up @@ -113,22 +113,30 @@ from which to return results. This will filter packets by the `time` property of
location metadata, i.e. the point at which they were inserted into the index.
e.g. `/packit/metadata?known_since=1683117048`.

```
```json
{
"status": "success",
"errors": null,
"data": [
{
"id": "20220812-155808-c873e405",
"name": "depends",
"custom": { "orderly": { "display": "Report with dependencies" }}
"parameters": null
"parameters": null,
"time": {
"end": 1503074545.8687,
"start": 1503074545.8687
},
"custom": { "orderly": { "description": { "display": "Report with dependencies" }}},
},
{
"id": "20220812-155808-d5747caf",
"name": "params",
"custom": { "orderly": { "display": "Report with parameters" }},
"parameters": { "alpha": 1 }
"parameters": { "alpha": 1 },
"time": {
"start": 1722267993.0676,
"end": 1722267993.0971
},
"custom": { "orderly": { "description": { "display": "Report with parameters" }}},
}
]
}
Expand All @@ -137,7 +145,7 @@ e.g. `/packit/metadata?known_since=1683117048`.

### GET /metadata/\<id\>/json

```
```json
{
"status": "success",
"errors": null,
Expand Down Expand Up @@ -223,7 +231,7 @@ Given a list of ids, returns those that are missing in the current root. If `unp
returns missing unpacked packets, otherwise just looks at missing metadata.

#### Response
```
```json
{
"status": "success",
"errors": null,
Expand All @@ -246,7 +254,7 @@ returns missing unpacked packets, otherwise just looks at missing metadata.
Given a list of file hashes, returns those that are missing in the current root.

#### Response
```
```json
{
"status": "success",
"errors": null,
Expand All @@ -265,7 +273,7 @@ The file contents should be written directly to the request body.

#### Response

```
```json
{
"status": "success",
"errors": null,
Expand All @@ -284,7 +292,7 @@ The metadata should be written directly to the request body.

#### Response

```
```json
{
"status": "success",
"errors": null,
Expand All @@ -302,7 +310,7 @@ Returns an array of branches with their `name`, `commit_hash` (where branch poin

#### Response

```
```json
{
"status": "success",
"data": {
Expand Down
6 changes: 6 additions & 0 deletions schema/server/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"description": "Task parameters, used when running and for querying",
"type": ["null", "object"]
},

"time": {
"description": "Information about the running time",
"start": {
Expand All @@ -27,6 +28,11 @@
"description": "Time that the report was completed, in seconds since 1970-01-01",
"type": "number"
}
},

"custom": {
"description": "Optional custom metadata, grouped under application keys",
"type": ["null", "object"]
}
},
"required": ["id", "name"],
Expand Down
2 changes: 2 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct PackitPacket {
pub name: String,
pub parameters: Option<HashMap<String, serde_json::Value>>,
pub time: PacketTime,
pub custom: Option<serde_json::Value>,
}

impl PackitPacket {
Expand All @@ -28,6 +29,7 @@ impl PackitPacket {
name: packet.name.to_string(),
parameters: packet.parameters.clone(),
time: packet.time.clone(),
custom: packet.custom.clone(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ async fn can_list_metadata() {
entries[0].get("time").unwrap().get("end").unwrap(),
1503074938.2232
);
assert_eq!(
entries[0]["custom"]["orderly"]["artefacts"][3]["description"],
"Projected Coverage for routine immunisation in PINE countries"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the rest of the file is written in this style and you are just following that, but that is way more verbose than it needs to be.

assert_eq!(entries[0]["custom"]["orderly"]["artefacts"][3]["description"], "Projected Coverage ...");

serde_json::Value implements the Index trait for both integers and string keys. If the value isn't of the right kind, the indexing returns a Null variant and the equality assertion would eventually fail. It also has a PartialEq<&str> implementation, which means that you shouldn't need the final .as_str().unwrap() call.

Alternatively, there is also the .pointer method, eg.

entries[0].pointer("/custom/orderly/artefacts/3/description")

Pointer syntax comes from https://www.rfc-editor.org/rfc/rfc6901

https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html#impl-Index%3CI%3E-for-Value
https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html#method.pointer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I hadn't seen pointer syntax before, that's handy!

I will use the index syntax.

assert_eq!(
entries[1].get("id").unwrap().as_str().unwrap(),
"20170818-164847-7574883b"
Expand Down
Loading