Skip to content

Commit

Permalink
Checkpoint on the appurify integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeenan committed Aug 14, 2013
1 parent 3eb1d3a commit df954ee
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 40 deletions.
1 change: 1 addition & 0 deletions www/devtools.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$eventList = array();

if(extension_loaded('newrelic')) {
newrelic_add_custom_tracer('GetCachedDevToolsProgress');
newrelic_add_custom_tracer('GetDevToolsProgress');
newrelic_add_custom_tracer('GetTimeline');
newrelic_add_custom_tracer('GetDevToolsRequests');
Expand Down
124 changes: 86 additions & 38 deletions www/lib/appurify.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,32 @@ function __construct($key, $secret) {
* Get a list of the available devices
*/
public function GetDevices() {
$devices = array();
$list = $this->Get('https://live.appurify.com/resource/devices/list/');
if ($list !== false && is_array($list)) {
foreach($list as $device) {
$name = "{$device['brand']} {$device['name']} {$device['os_name']} {$device['os_version']}";
$devices[$device['device_type_id']] = $name;
$devices = null;
$this->Lock();
$ttl = 120;
if (is_file("./tmp/appurify_{$this->key}.devices")) {
$cache = json_decode(file_get_contents("./tmp/appurify_{$this->key}.devices"), true);
$now = time();
if ($cache &&
is_array($cache) &&
array_key_exists('devices', $cache) &&
array_key_exists('time', $cache) &&
$now >= $cache['time'] &&
$now - $cache['time'] < $ttl / 2)
$devices = $cache['devices'];
}
if (!isset($devices)) {
$devices = array();
$list = $this->Get('https://live.appurify.com/resource/devices/list/');
if ($list !== false && is_array($list)) {
foreach($list as $device) {
$name = "{$device['brand']} {$device['name']} {$device['os_name']} {$device['os_version']}";
$devices[$device['device_type_id']] = $name;
}
}
file_put_contents("./tmp/appurify_{$this->key}.devices", json_encode(array('devices' => $devices, 'time' => time())));
}
$this->Unlock();
return $devices;
}

Expand Down Expand Up @@ -106,23 +124,24 @@ public function CheckTestRun(&$test, &$run, $index, $testPath) {
is_array($status) &&
array_key_exists('status', $status)) {
$run['status'] = $status['status'];
if ($status['status'] == 'complete') {
$run['completed'] = true;
if ($status['status'] == 'complete')
$this->GetFile('https://live.appurify.com/resource/tests/result/', $file, array('run_id' => $run['id']));
}
$ret = true;
}
} else {
$run['completed'] = true;
}
if (is_file($file)) {
$ret = true;
if ($this->ProcessResult($test, $run, $index, $testPath))
$run['completed'] = true;
else
unlink($file);
}
if (is_file($file))
$this->ProcessResult($test, $run, $index, $testPath);
}
return $ret;
}

protected function ProcessResult(&$test, &$run, $index, $testPath) {
$ok = false;
$zipfile = "$testPath/{$index}_appurify.zip";
$zip = new ZipArchive;
if ($zip->open($zipfile) === TRUE) {
Expand All @@ -134,8 +153,9 @@ protected function ProcessResult(&$test, &$run, $index, $testPath) {
$zip->close();
}
if (isset($tempdir) && is_dir($tempdir)) {
if (is_file("$tempdir/appurify_results/video.mov"))
rename("$tempdir/appurify_results/video.mov", "$testPath/{$index}_video.mov");
$ok = true;
$this->ProcessScreenShot($test, $tempdir, $testPath, $index);
$this->ProcessVideo($test, $tempdir, $testPath, $index);
$devtools = array();
$files = glob("$tempdir/appurify_results/WSData*");
if (isset($files) && is_array($files) && count($files)) {
Expand All @@ -153,33 +173,61 @@ protected function ProcessResult(&$test, &$run, $index, $testPath) {
}
delTree($tempdir);
}
return $ok;
}

protected function ProcessScreenShot(&$test, $tempdir, $testPath, $index) {
if (is_file("$tempdir/appurify_results/Run 1/Screenshot_Tabs.png.png"))
rename("$tempdir/appurify_results/Run 1/Screenshot_Tabs.png.png", "$testPath/{$index}_screen.png");
elseif (is_file("$tempdir/appurify_results/Run 1/Screenshot_Tabs.png"))
rename("$tempdir/appurify_results/Run 1/Screenshot_Tabs.png", "$testPath/{$index}_screen.png");
elseif (is_file("$tempdir/appurify_results/Run 1/Screenshot_Timeline.png.png"))
rename("$tempdir/appurify_results/Run 1/Screenshot_Timeline.png.png", "$testPath/{$index}_screen.png");
elseif (is_file("$tempdir/appurify_results/Run 1/Screenshot_Timeline.png"))
rename("$tempdir/appurify_results/Run 1/Screenshot_Timeline.png", "$testPath/{$index}_screen.png");
if (is_file("$testPath/{$index}_screen.png")) {
$img = imagecreatefrompng("$testPath/{$index}_screen.png");
if ($img) {
imageinterlace($img, 1);
$quality = 75;
if (array_key_exists('iq', $test) && $test['iq'] >= 30 && $test['iq'] < 100)
$quality = $test['iq'];
imagejpeg($img, "$testPath/{$index}_screen.jpg", $quality);
imagedestroy($img);
}
$keep_png = false;
if (array_key_exists('pngss', $test) && $test['pngss'])
$keep_png = true;
if (!$keep_png)
unlink("$testPath/{$index}_screen.png");
}
}

protected function ProcessVideo(&$test, $tempdir, $testPath, $index) {
if (is_file("$tempdir/appurify_results/video.mov"))
rename("$tempdir/appurify_results/video.mov", "$testPath/{$index}_video.mov");
}

protected function ProcessDevTools($file, $outfile) {
$f = fopen($file, 'r');
if ($f) {
$buffer = '';
do {
$line = fgets($f);
if ($line === false ||
substr($line, 0, 7) == 'Buffer[' ||
substr($line, 0, 6) == 'Frame[') {
$pos = strpos($buffer, '{');
if ($pos !== false && $pos < 10) {
$buffer = substr($buffer, $pos);
$event = json_decode($buffer, true);
if (isset($event) &&
is_array($event) &&
array_key_exists('method', $event)) {
fwrite($outfile, json_encode($event));
fwrite($outfile, ',');
}
$len = filesize($file);
if ($len > 2) {
$f = fopen($file, 'r');
if ($f) {
fseek($f, 1);
$remaining = $len - 2;
while ($remaining > 0) {
$size = min($remaining, 4096);
$buff = fread($f, $size);
if ($buff !== false) {
fwrite($outfile, $buff);
$remaining -= $size;
} else {
$remaining = 0;
}
$buffer = '';
} elseif ($line !== false)
$buffer .= trim(substr($line, 59), "\r\n");
} while ($line !== false);
fclose($f);
}
fwrite($outfile, ',');
fclose($f);
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions www/running.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,18 @@
</div>
<script type="text/javascript">
var testId = "<?php echo $id; ?>";
var pollingInterval = 5000;
var pollingIntervalTesting = 1000;
<?php
$polling_interval_testing = 1000;
$polling_interval = 15000;
if (array_key_exists('testinfo', $test) &&
array_key_exists('loc_type', $test['testinfo']) &&
$test['testinfo']['loc_type'] == 'Appurify') {
$polling_interval = 30000;
$polling_interval_testing = 15000;
}
echo "var pollingInterval = $polling_interval;\n";
echo "var pollingIntervalTesting = $polling_interval_testing;\n";
?>
var waitingImage = '<?php echo "{$GLOBALS['cdnPath']}/images/status_waiting.png"; ?>';
var testingImage = '<?php echo "{$GLOBALS['cdnPath']}/images/status_testing.png"; ?>';
var lastStatusCode = 0;
Expand Down

0 comments on commit df954ee

Please sign in to comment.