-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.php
executable file
·75 lines (66 loc) · 2.09 KB
/
statistics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
if ( count ( $argv ) != 4 ) {
echo "Usage: statistics.php <repo> <statsd host> <github api key>\n";
die;
}
list( ,$repo, $host, $apiKey ) = $argv;
$credentials = base64_encode( "$apiKey:x-oauth-basic" );
$opts = [
'http'=> [
'user_agent' => 'php',
'header' => "Authorization: Basic $credentials\n"
]
];
$ctx = stream_context_create( $opts );
$response = file_get_contents( "https://api.github.com/repos/$repo/pulls?state=open&per_page=1000", null, $ctx );
$pullRequests = json_decode( $response, true );
if ( !$pullRequests ) {
echo "Error getting JSON\n";
die;
}
echo count( $pullRequests ) . " open pull requests\n";
/**
* @param array $pullRequests
* @return float
*/
function getAverageAge( array $pullRequests )
{
$numRequests = count( $pullRequests );
$totalTime = 0;
foreach ( $pullRequests as $pr ) {
$time = DateTime::createFromFormat( 'Y-m-d\TH:i:s\Z', $pr['created_at'] );
$interval = ( new DateTime )->getTimestamp() - $time->getTimestamp();
$totalTime += $interval;
}
return round( $totalTime / $numRequests / 60 / 60, 1 );
}
/**
* @param array $pullRequests
* @param $ctx
* @return float
*/
function getReviewability( array $pullRequests, $ctx )
{
$totalPrs = count( $pullRequests );
$totalLines = 0;
foreach ( $pullRequests as $pr ) {
$pr = json_decode( file_get_contents( $pr['url'], null, $ctx ), true );
$totalLines += $pr['additions'] + $pr['deletions'];
}
$reviewability = floatval( $totalLines ) / $totalPrs;
return $reviewability;
}
$connection = new \Domnikl\Statsd\Connection\UdpSocket( $host, 8125 );
$statsClient = new \Domnikl\Statsd\Client( $connection, "" ) ;
{
$average = getAverageAge( $pullRequests );
echo 'Average open time: ' . $average . " hours\n";
$statsClient->gauge( 'github.prs.duration', $average );
}
{
$average = getReviewability( $pullRequests, $ctx );
echo "Average reviewometer: ", $average, "\n";
$statsClient->gauge( 'github.prs.reviewability', $average );
}