Created
June 1, 2015 18:18
-
-
Save dallasgutauckis/4f774618a84de3ecd931 to your computer and use it in GitHub Desktop.
A simple PHP class and script for building geckoboard-compatible JSON output about the top build crash rates from Crashlytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Crashlytics { | |
private $orgId; | |
private $appId; | |
private $devToken; | |
private $fabricSession; | |
public function __construct( $orgId, $appId, $devToken, $fabricSession ) { | |
$this->orgId = $orgId; | |
$this->appId = $appId; | |
$this->devToken = $devToken; | |
$this->fabricSession = $fabricSession; | |
} | |
function request( $file, $query ) { | |
$c = curl_init( 'https://fabric.io/api/v2/organizations/'.$this->orgId.'/apps/'.$this->appId.'/growth_analytics/'.$file.'?'.http_build_query($query) ); | |
$headers = array( | |
'X-CRASHLYTICS-DEVELOPER-TOKEN:'.$this->devToken, | |
'Cookie: _fabric_session='.$this->fabricSession | |
); | |
curl_setopt( $c, CURLOPT_HTTPHEADER, $headers ); | |
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true ); | |
$response = curl_exec($c); | |
return json_decode( $response ); | |
} | |
function getTopBuilds( $start, $end ) { | |
return $this->request( 'top_builds', array( 'start' => $start, 'end' => $end ) ); | |
} | |
function getBuildInfo( $date, $builds ) { | |
return $this->request( 'builds_info', array( 'date' => $date, 'builds' => implode(',', $builds ) ) ); | |
} | |
} | |
$crashlytics = new Crashlytics( ORG_ID, APP_ID, DEV_TOKEN, FABRIC_SESSION ); | |
$buildStats = $crashlytics->getBuildInfo( strtotime( 'yesterday' ), $crashlytics->getTopBuilds( strtotime( '24 hours ago' ), time() )->builds ); | |
/* | |
{ | |
"item": 23, | |
"min": { | |
"value": 10 | |
}, | |
"max": { | |
"value": 30 | |
} | |
} | |
*/ | |
// First item (current pointer) | |
$build = current( $buildStats->builds ); | |
$buildName = key( $buildStats->builds ); | |
$response = (object) array( | |
'title' => $buildName, | |
'item' => round( $build->stability * 100, 2 ), | |
'min' => (object) array( | |
'value' => 98 | |
), | |
'max' => (object) array( | |
'value' => 100 | |
), | |
); | |
echo json_encode( $response ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment