Skip to content

Commit dc1b72b

Browse files
authored
Merge pull request #59 from DiffyWebsite/retrieve-screenshot-status
Retrieve screenshot status
2 parents a9b2596 + 01c0930 commit dc1b72b

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ You can use `--wait` key to wait for the screenshot to be completed.
5050

5151
As a result, you will get an ID of the screenshot.
5252

53+
#### Get screenshot status
54+
55+
```shell script
56+
diffy screenshot:get-status SCREENSHOT_ID
57+
```
58+
59+
Prints `1` when the set of screenshots is completed, and nothing while it is still being captured.
60+
61+
Add `--format=json` to get the capture state and progress instead:
62+
63+
```shell script
64+
diffy screenshot:get-status SCREENSHOT_ID --format=json
65+
```
66+
67+
```json
68+
{"id":1421873,"state":3,"completed":true,"name":null,"environment":"Production","executionTime":"2min 6s","status":{"results":84,"items":84,"queue":0,"estimate":"under 1 minute","totalTime":126,"jobs":0,"inProgress":0}}
69+
```
70+
71+
`state` is `-1` stopped, `0` not started, `1` in progress, `2`/`3`/`4` completed. Note that the plain
72+
output `1` means completed, while `state` of `1` means the capture is still in progress.
73+
5374
#### Compare screenshots (creating diff)
5475

5576
If you know your screenshots IDs the easiest would be:

src/Commands/ScreenshotCommand.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,57 @@ protected function createScreenshotInternal($projectId, $environment, array $opt
108108
return $screenshotId;
109109
}
110110

111+
/**
112+
* Get screenshot status
113+
*
114+
* @command screenshot:get-status
115+
*
116+
* @param int $screenshotId
117+
*
118+
* @option format Format of the output (supported: json). Omit it to print 1 when the screenshot set
119+
* is completed and nothing when it is not.
120+
*
121+
* @return mixed
122+
*
123+
* @throws \Exception
124+
*
125+
* @usage screenshot:get-status 12345 Get screenshot status.
126+
* @usage screenshot:get-status 12345 --format=json Get screenshot state and capture progress as JSON.
127+
*/
128+
public function getScreenshotStatus(int $screenshotId, array $options = ['format' => ''])
129+
{
130+
$format = $options['format'] ?? '';
131+
132+
if ($format && $format !== 'json') {
133+
$this->io()->error('Format value is not supported. Provide the following value: json.');
134+
return;
135+
}
136+
137+
$apiKey = Config::getConfig()['key'];
138+
Diffy::setApiKey($apiKey);
139+
$screenshot = Screenshot::retrieve($screenshotId);
140+
141+
if ($format === 'json') {
142+
$this->io()->write(json_encode([
143+
'id' => $screenshotId,
144+
'state' => $screenshot->data['state'] ?? null,
145+
'completed' => $screenshot->isCompleted(),
146+
'name' => $screenshot->data['name'] ?? null,
147+
'environment' => $screenshot->data['environment'] ?? null,
148+
'executionTime' => $screenshot->data['executionTime'] ?? null,
149+
'status' => $screenshot->data['status'] ?? null,
150+
]));
151+
152+
// Successful exit.
153+
return new ResultData();
154+
}
155+
156+
$this->io()->write($screenshot->isCompleted());
157+
158+
// Successful exit.
159+
return new ResultData();
160+
}
161+
111162
/**
112163
* Get the list of screenshots
113164
*

0 commit comments

Comments
 (0)