Skip to content

Commit 5f297e4

Browse files
author
Jonathan Visser
committed
Add support for creating Insights annotations (POST /v2/insights-annotation/create/)
1 parent 99e8fc2 commit 5f297e4

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/Service/InsightsAnnotation.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class InsightsAnnotation extends AbstractService
1111
{
1212
public const V2_INSIGHTS_ANNOTATION_LIST_URL = "/v2/insights-annotation/";
13+
public const V2_INSIGHTS_ANNOTATION_CREATE_URL = "/v2/insights-annotation/create/";
1314

1415
/**
1516
* List all custom Insights annotations for the Hypernodes you have access
@@ -45,4 +46,30 @@ public function getList(array $params = []): array
4546

4647
return $annotations;
4748
}
49+
50+
/**
51+
* Create a custom Insights annotation for a Hypernode. The annotation is
52+
* shown in the Insights graphs at the point in time given by `x_axis` (a
53+
* unix timestamp in seconds). Optionally restrict the annotation to
54+
* specific graphs with `metrics`, a comma-separated list of metric names;
55+
* when omitted, the annotation applies to all metrics.
56+
*
57+
* @param array $data Annotation data, requires at least the keys
58+
* `name`, `x_axis` and `app`.
59+
* @return array The created annotation
60+
* @throws HypernodeApiClientException
61+
* @throws HypernodeApiServerException
62+
*/
63+
public function create(array $data): array
64+
{
65+
$response = $this->client->api->post(
66+
self::V2_INSIGHTS_ANNOTATION_CREATE_URL,
67+
[],
68+
json_encode($data)
69+
);
70+
71+
$this->client->maybeThrowApiExceptions($response);
72+
73+
return $this->client->getJsonFromResponse($response);
74+
}
4875
}

tests/unit/Service/InsightsAnnotationTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,55 @@ public function testGetListRaisesServerExceptions()
124124

125125
$this->client->insightsAnnotation->getList();
126126
}
127+
128+
public function testCreate()
129+
{
130+
$annotationData = [
131+
'name' => 'Deployed release 1.2.3',
132+
'x_axis' => 1756384957,
133+
'app' => 'johndoe',
134+
'metrics' => 'memory_usage',
135+
];
136+
$createdAnnotation = array_merge($annotationData, [
137+
'id' => 123,
138+
'created' => '2026-07-15T12:00:00Z',
139+
'modified' => '2026-07-15T12:00:00Z',
140+
]);
141+
$this->responses->append(
142+
new Response(201, [], json_encode($createdAnnotation)),
143+
);
144+
145+
$result = $this->client->insightsAnnotation->create($annotationData);
146+
147+
$request = $this->responses->getLastRequest();
148+
$this->assertEquals('POST', $request->getMethod());
149+
$this->assertEquals('/v2/insights-annotation/create/', $request->getUri());
150+
$this->assertJson((string)$request->getBody());
151+
$this->assertEquals($annotationData, json_decode((string)$request->getBody(), true));
152+
$this->assertEquals($createdAnnotation, $result);
153+
}
154+
155+
public function testCreateRaisesClientExceptions()
156+
{
157+
$badRequestResponse = new Response(400, [], json_encode([
158+
'non_field_errors' => ['Your request was invalid.']
159+
]));
160+
$this->responses->append($badRequestResponse);
161+
162+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
163+
164+
$this->client->insightsAnnotation->create(['name' => 'Deployed release 1.2.3']);
165+
}
166+
167+
public function testCreateRaisesServerExceptions()
168+
{
169+
$badRequestResponse = new Response(500, [], json_encode([
170+
'non_field_errors' => ['Something went wrong processing your request.']
171+
]));
172+
$this->responses->append($badRequestResponse);
173+
174+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
175+
176+
$this->client->insightsAnnotation->create(['name' => 'Deployed release 1.2.3']);
177+
}
127178
}

0 commit comments

Comments
 (0)