Skip to content

Commit 2e8e938

Browse files
authored
Merge pull request #11 from keboola/odin-PS-2972-b
Add warning & long running notifications for phases
2 parents 6a69c95 + f686249 commit 2e8e938

7 files changed

Lines changed: 333 additions & 2 deletions
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostEvent;
6+
7+
use Keboola\NotificationClient\Requests\EventDataInterface;
8+
9+
class PhaseJobProcessingLongEventData implements EventDataInterface
10+
{
11+
private string $projectId;
12+
private string $projectName;
13+
private float $durationOvertimePercentage;
14+
private float $averageDuration;
15+
private float $currentDuration;
16+
private JobData $jobData;
17+
private string $phaseName;
18+
private string $phaseId;
19+
20+
public function __construct(
21+
string $projectId,
22+
string $projectName,
23+
string $phaseName,
24+
string $phaseId,
25+
float $durationOvertimePercentage,
26+
float $averageDuration,
27+
float $currentDuration,
28+
JobData $jobData
29+
) {
30+
$this->projectId = $projectId;
31+
$this->projectName = $projectName;
32+
$this->durationOvertimePercentage = $durationOvertimePercentage;
33+
$this->jobData = $jobData;
34+
$this->averageDuration = $averageDuration;
35+
$this->currentDuration = $currentDuration;
36+
$this->phaseName = $phaseName;
37+
$this->phaseId = $phaseId;
38+
}
39+
40+
public function jsonSerialize(): array
41+
{
42+
return [
43+
'averageDuration' => $this->averageDuration,
44+
'currentDuration' => $this->currentDuration,
45+
'durationOvertimePercentage' => $this->durationOvertimePercentage,
46+
'job' => $this->jobData->jsonSerialize(),
47+
'phase' => [
48+
'id' => $this->phaseId,
49+
'name' => $this->phaseName,
50+
],
51+
'project' => [
52+
'id' => $this->projectId,
53+
'name' => $this->projectName,
54+
],
55+
'uniqueId' => $this->jobData->getJobId(),
56+
];
57+
}
58+
59+
public static function getEventTypeName(): string
60+
{
61+
return 'phase-job-processing-long';
62+
}
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostEvent;
6+
7+
use Keboola\NotificationClient\Requests\EventDataInterface;
8+
9+
class PhaseJobSucceededWithWarningEventData implements EventDataInterface
10+
{
11+
private string $projectId;
12+
private string $projectName;
13+
private string $errorMessage;
14+
private JobData $jobData;
15+
private string $phaseName;
16+
private string $phaseId;
17+
18+
public function __construct(
19+
string $projectId,
20+
string $projectName,
21+
string $phaseName,
22+
string $phaseId,
23+
string $errorMessage,
24+
JobData $jobData
25+
) {
26+
$this->projectId = $projectId;
27+
$this->projectName = $projectName;
28+
$this->errorMessage = $errorMessage;
29+
$this->jobData = $jobData;
30+
$this->phaseName = $phaseName;
31+
$this->phaseId = $phaseId;
32+
}
33+
34+
public function jsonSerialize(): array
35+
{
36+
return [
37+
'errorMessage' => $this->errorMessage,
38+
'job' => $this->jobData->jsonSerialize(),
39+
'phase' => [
40+
'id' => $this->phaseId,
41+
'name' => $this->phaseName,
42+
],
43+
'project' => [
44+
'id' => $this->projectId,
45+
'name' => $this->projectName,
46+
],
47+
];
48+
}
49+
50+
public static function getEventTypeName(): string
51+
{
52+
return 'phase-job-succeeded-with-warning';
53+
}
54+
}

src/Requests/Subscription.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Keboola\NotificationClient\Requests\PostEvent\JobProcessingLongEventData;
1212
use Keboola\NotificationClient\Requests\PostEvent\JobSucceededWithWarningEventData;
1313
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobFailedEventData;
14+
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobProcessingLongEventData;
15+
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobSucceededWithWarningEventData;
1416
use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient;
1517
use Keboola\NotificationClient\Requests\PostSubscription\Filter;
1618

@@ -58,6 +60,8 @@ private function checkEventType(string $eventType): void
5860
JobSucceededWithWarningEventData::getEventTypeName(),
5961
JobProcessingLongEventData::getEventTypeName(),
6062
PhaseJobFailedEventData::getEventTypeName(),
63+
PhaseJobSucceededWithWarningEventData::getEventTypeName(),
64+
PhaseJobProcessingLongEventData::getEventTypeName(),
6165
];
6266

6367
if (!in_array($eventType, $validEventTypes)) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Tests\Requests\PostEvent;
6+
7+
use DateTimeImmutable;
8+
use Keboola\NotificationClient\Requests\PostEvent\JobData;
9+
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobProcessingLongEventData;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class PhaseJobProcessingLongEventDataTest extends TestCase
13+
{
14+
public function testJsonSerialize(): void
15+
{
16+
$jobData = new JobData(
17+
'23456',
18+
'http://someUrl',
19+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
20+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
21+
'keboola.orchestrator',
22+
'Orchestrator',
23+
'my-configuration',
24+
'My configuration'
25+
);
26+
$failedEventData = new PhaseJobProcessingLongEventData(
27+
'1234',
28+
'My project',
29+
'Lithium Extractors',
30+
'123',
31+
12.534,
32+
23.854,
33+
12.0,
34+
$jobData
35+
);
36+
self::assertSame(
37+
[
38+
'averageDuration' => 23.854,
39+
'currentDuration' => 12.0,
40+
'durationOvertimePercentage' => 12.534,
41+
'job' => [
42+
'id' => '23456',
43+
'url' => 'http://someUrl',
44+
'component' => [
45+
'id' => 'keboola.orchestrator',
46+
'name' => 'Orchestrator',
47+
],
48+
'tasks' => [],
49+
'startTime' => '2020-01-01T11:11:00+00:00',
50+
'endTime' => '2020-01-01T12:11:00+00:00',
51+
'configuration' => [
52+
'id' => 'my-configuration',
53+
'name' => 'My configuration',
54+
],
55+
],
56+
'phase' => [
57+
'id' => '123',
58+
'name' => 'Lithium Extractors',
59+
],
60+
'project' => [
61+
'id' => '1234',
62+
'name' => 'My project',
63+
],
64+
'uniqueId' => '23456',
65+
],
66+
$failedEventData->jsonSerialize()
67+
);
68+
self::assertEquals('phase-job-processing-long', $failedEventData::getEventTypeName());
69+
}
70+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Tests\Requests\PostEvent;
6+
7+
use DateTimeImmutable;
8+
use Keboola\NotificationClient\Requests\PostEvent\JobData;
9+
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobSucceededWithWarningEventData;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class PhaseJobSucceededWithWarningEventDataTest extends TestCase
13+
{
14+
public function testJsonSerialize(): void
15+
{
16+
$jobData = new JobData(
17+
'23456',
18+
'http://someUrl',
19+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
20+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
21+
'keboola.orchestrator',
22+
'Orchestrator',
23+
'my-configuration',
24+
'My configuration'
25+
);
26+
$failedEventData = new PhaseJobSucceededWithWarningEventData(
27+
'1234',
28+
'My project',
29+
'Lithium Extractors',
30+
'123',
31+
'someMessage',
32+
$jobData
33+
);
34+
35+
self::assertSame(
36+
[
37+
'errorMessage' => 'someMessage',
38+
'job' => [
39+
'id' => '23456',
40+
'url' => 'http://someUrl',
41+
'component' => [
42+
'id' => 'keboola.orchestrator',
43+
'name' => 'Orchestrator',
44+
],
45+
'tasks' => [],
46+
'startTime' => '2020-01-01T11:11:00+00:00',
47+
'endTime' => '2020-01-01T12:11:00+00:00',
48+
'configuration' => [
49+
'id' => 'my-configuration',
50+
'name' => 'My configuration',
51+
],
52+
],
53+
'phase' => [
54+
'id' => '123',
55+
'name' => 'Lithium Extractors',
56+
],
57+
'project' => [
58+
'id' => '1234',
59+
'name' => 'My project',
60+
],
61+
],
62+
$failedEventData->jsonSerialize()
63+
);
64+
}
65+
}

tests/Requests/SubscriptionTest.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testJsonSerializeStrangeFilters(): void
6565
);
6666
}
6767

68-
public function testPhaseJobSubscription(): void
68+
public function testPhaseJobFailedSubscription(): void
6969
{
7070
$subscriptionRequest = new Subscription(
7171
'phase-job-failed',
@@ -101,4 +101,78 @@ public function testPhaseJobSubscription(): void
101101
$subscriptionRequest->jsonSerialize()
102102
);
103103
}
104+
105+
public function testPhaseJobSucceededWithWarningSubscription(): void
106+
{
107+
$subscriptionRequest = new Subscription(
108+
'phase-job-succeeded-with-warning',
109+
new EmailRecipient('john.doe@example.com'),
110+
[
111+
new Filter('job.component.id', 'my.component'),
112+
new Filter('job.configuration.id', '12345'),
113+
new Filter('phase.id', '123'),
114+
]
115+
);
116+
self::assertSame(
117+
[
118+
'event' => 'phase-job-succeeded-with-warning',
119+
'filters' => [
120+
[
121+
'field' => 'job.component.id',
122+
'value' => 'my.component',
123+
],
124+
[
125+
'field' => 'job.configuration.id',
126+
'value' => '12345',
127+
],
128+
[
129+
'field' => 'phase.id',
130+
'value' => '123',
131+
],
132+
],
133+
'recipient' => [
134+
'channel' => 'email',
135+
'address' => 'john.doe@example.com',
136+
],
137+
],
138+
$subscriptionRequest->jsonSerialize()
139+
);
140+
}
141+
142+
public function testPhaseJobProcessingLongSubscription(): void
143+
{
144+
$subscriptionRequest = new Subscription(
145+
'phase-job-processing-long',
146+
new EmailRecipient('john.doe@example.com'),
147+
[
148+
new Filter('job.component.id', 'my.component'),
149+
new Filter('job.configuration.id', '12345'),
150+
new Filter('phase.id', '123'),
151+
]
152+
);
153+
self::assertSame(
154+
[
155+
'event' => 'phase-job-processing-long',
156+
'filters' => [
157+
[
158+
'field' => 'job.component.id',
159+
'value' => 'my.component',
160+
],
161+
[
162+
'field' => 'job.configuration.id',
163+
'value' => '12345',
164+
],
165+
[
166+
'field' => 'phase.id',
167+
'value' => '123',
168+
],
169+
],
170+
'recipient' => [
171+
'channel' => 'email',
172+
'address' => 'john.doe@example.com',
173+
],
174+
],
175+
$subscriptionRequest->jsonSerialize()
176+
);
177+
}
104178
}

tests/SubscriptionClientFunctionalTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function testCreateInvalidSubscription(): void
4646
self::expectException(ClientException::class);
4747
self::expectExceptionMessage(
4848
'Invalid event type "dummy-event", valid types are: ' .
49-
'"job-failed, job-succeeded-with-warning, job-processing-long, phase-job-failed".'
49+
'"job-failed, job-succeeded-with-warning, job-processing-long, phase-job-failed, ' .
50+
'phase-job-succeeded-with-warning, phase-job-processing-long".'
5051
);
5152
$client->createSubscription(new Subscription(
5253
'dummy-event',

0 commit comments

Comments
 (0)