1
0

More changes because of case-sensitive filesystems -.-

This commit is contained in:
Mike Schwörer 2014-08-04 15:42:24 +02:00
parent 2af3e05fe1
commit 2c5f34c071
10 changed files with 758 additions and 19 deletions

View File

@ -0,0 +1,494 @@
<?php
class ExtendedGitGraph {
const FILE_RAW_DATA = 'protected/data/ext_git_graph_apidata.dat';
const FILE_FINISHED_DATA = 'protected/data/gitgraph.dat';
const API_AUTHORIZE = 'https://github.com/login/oauth/authorize?client_id=%s';
const API_TOKEN = 'https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s';
const API_RATELIMIT = 'https://api.github.com/rate_limit';
const API_REPOSITORIESLIST = 'https://api.github.com/users/%s/repos?page=%d&per_page=100';
private $username;
private $token;
private $tokenHeader;
private $repositories;
private $commits;
private $commitmap = array();
private $startdate = null;
private $enddate = null;
public function __construct($usr_name) {
$this->username = $usr_name;
set_time_limit(300); // 5min
}
public function authenticate($auth_key, $client_id, $client_secret) {
$url = sprintf(self::API_TOKEN, $client_id, $client_secret, $auth_key);
$result = file_get_contents($url);
$result = str_replace('access_token=', '', $result);
$result = str_replace('&scope=&token_type=bearer', '', $result);
setToken($result);
}
public function setToken($token) {
$this->token = $token;
$this->tokenHeader = 'access_token=' . $token . '&token_type=bearer';
}
public function collect() {
ob_implicit_flush(true);
ob_end_flush();
//--------------------------------
$this->listRepositories();
$this->listAllCommits();
$this->save();
//--------------------------------
$this->output_flushed($this->getRemainingRequests() . ' Requests remaining');
}
private function listRepositories() {
$page = 1;
$url = sprintf(self::API_REPOSITORIESLIST . '&' . $this->tokenHeader, $this->username, $page);
$result = $this->getJSON($url);
$repo_list = array();
while (! empty($result)) {
foreach ($result as $result_repo) {
$repo_list[] = $this->parseRepoJSON($result_repo);
$this->output_flushed("Found Repo: " . $result_repo->{'full_name'});
}
//##########
$url = sprintf(self::API_REPOSITORIESLIST . '&' . $this->tokenHeader, $this->username, ++$page);
$result = $this->getJSON($url);
}
$this->repositories = $repo_list;
}
private function getJSON($url) {
$options = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT']));
// $options = array('http' => array('user_agent'=> 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36'));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
return json_decode($response);
}
private function getRemainingRequests() {
$json = $this->getJSON(self::API_RATELIMIT . '?' . $this->tokenHeader);
return $json->{'resources'}->{'core'}->{'remaining'};
}
private function listAllCommits() {
$this->commits = array();
foreach($this->repositories as $repo) {
$this->listCommits($repo);
}
}
private function listCommits($repo) {
$page = 1;
$url = $repo['commits_url'] . '?per_page=100&page=' . $page . '&author=' .$this->username . '&' .$this->tokenHeader;
$result = $this->getJSON($url);
$commit_list = array();
while (! empty($result)) {
foreach ($result as $result_commit) {
$commit_list[] = $this->parseCommitJSON($repo, $result_commit);
}
$this->output_flushed("Found 100 Commits from " . $repo['full_name']);
//##########
$url = $repo['commits_url'] . '?per_page=100&page=' . ++$page . '&author=' .$this->username . '&' .$this->tokenHeader;
$result = $this->getJSON($url);
}
$this->commits = array_merge($this->commits, $commit_list);
}
private function parseRepoJSON($json) {
return
[
'id' => $json->{'id'},
'name' => $json->{'name'},
'full_name' => $json->{'full_name'},
'owner' => $json->{'owner'}->{'login'},
'owner_id' => $json->{'owner'}->{'id'},
'owner_avatar-url' => $json->{'owner'}->{'avatar_url'},
'owner_gravatar-id' => $json->{'owner'}->{'gravatar_id'},
'url' => $json->{'html_url'},
'language' => $json->{'language'},
'url' => $json->{'html_url'},
'creation' => DateTime::createFromFormat(DateTime::ISO8601, $json->{'created_at'}),
'size' => $json->{'size'},
'default_branch' => $json->{'default_branch'},
'commits_url' => str_replace('{/sha}', '', $json->{'commits_url'}),
];
}
private function parseCommitJSON($repo, $json) {
return
[
'sha' => $json->{'sha'},
'author_name' => $json->{'commit'}->{'author'}->{'name'},
'author_mail' => $json->{'commit'}->{'author'}->{'email'},
'author_login' => $json->{'author'}->{'login'},
'author_id' => $json->{'author'}->{'id'},
'sha' => $json->{'sha'},
'message' => $json->{'commit'}->{'message'},
'repository' => $repo,
'date' => DateTime::createFromFormat(DateTime::ISO8601, $json->{'commit'}->{'author'}->{'date'}),
];
}
private function save() {
$this->output_flushed("Start saving data");
$save = serialize(
[
'repositories' => $this->repositories,
'commits' => $this->commits,
]);
file_put_contents(self::FILE_RAW_DATA, $save);
$this->output_flushed('Finished saving data');
}
public function output_flushed($txt)
{
echo '[' . date('H:i.s') . '] ' . $txt . "<br>";
}
public function loadData() {
$data = unserialize(file_get_contents(self::FILE_RAW_DATA));
$this->repositories = $data['repositories'];
$this->commits = $data['commits'];
}
public function generate($year) {
$ymap = $this->generateYearMap($year); // unused on purpose (template.php needs it)
$ymapmax = $this->getMaxCommitCount(); // unused on purpose (template.php needs it)
ob_start();
include('template.php');
$returned = ob_get_contents();
ob_end_clean();
return $returned;
}
public function generateAndSave() {
$result = '';
foreach($this->getYears() as $year) {
$result.= $this->generate($year);
$result.= '<br />';
}
$this->generateCommitMap();
file_put_contents(self::FILE_FINISHED_DATA,
serialize(
[
'creation' => new DateTime(),
'total' => count($this->commits),
'repos' => count($this->repositories),
'streak' => $this->getLongestStreak(),
'streak_start' => $this->getLongestStreakStart(),
'streak_end' => $this->getLongestStreakEnd(),
'max_commits' => $this->getMaxCommits(),
'max_commits_date' => $this->getMaxCommitsDate(),
'avg_commits' => $this->getAvgCommits(),
'start' => $this->startdate,
'end' => $this->enddate,
'content' => $result,
]));
return $result;
}
private function generateCommitMap() {
$this->commitmap = array();
$this->startdate = $this->getStartDate();
$this->enddate = $this->getEndDate();
$date = clone $this->startdate;
while($date < $this->enddate) {
$this->commitmap[$date->format('Y-m-d')] = 0;
$date = $date->modify("+1 day");
}
foreach ($this->commits as $commit) {
if(array_key_exists($commit['date']->format('Y-m-d'), $this->commitmap))
$this->commitmap[$commit['date']->format('Y-m-d')]++;
}
}
private function getStartDate() {
$date = $this->commits[0]['date'];
foreach($this->commits as $commit) {
if ($commit['date'] < $date) {
$date = clone $commit['date'];
}
}
return new DateTime($date->format('Y-m-d'));
}
private function getEndDate() {
$date = $this->commits[0]['date'];
foreach($this->commits as $commit) {
if ($commit['date'] > $date) {
$date = clone $commit['date'];
}
}
return new DateTime($date->format('Y-m-d'));
}
private function getCommitsForDate($d) {
$v = $d->format('Y-m-d');
if (array_key_exists($v, $this->commitmap))
return $this->commitmap[$d->format('Y-m-d')];
else
return 0;
}
private function getLongestStreak() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$streak_curr = 0;
$streak_max = 0;
while ($curr <= $end) {
if ($this->getCommitsForDate($curr) > 0) {
$streak_curr++;
} else {
$streak_max = max($streak_max, $streak_curr);
$streak_curr = 0;
}
$curr = $curr->modify('+1 day');
}
$streak_max = max($streak_max, $streak_curr);
return $streak_max;
}
private function getLongestStreakStart() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$streak_curr_start = clone $curr;
$streak_max_start = null;
$streak_curr = 0;
$streak_max = 0;
while ($curr <= $end) {
if ($this->getCommitsForDate($curr) > 0) {
$streak_curr++;
} else {
if ($streak_curr > $streak_max) {
$streak_max = $streak_curr;
$streak_max_start = clone $streak_curr_start;
}
$streak_curr = 0;
$streak_curr_start = clone $curr;
}
$curr = $curr->modify('+1 day');
}
if ($streak_curr > $streak_max) {
$streak_max_start = clone $streak_curr_start;
}
return $streak_max_start;
}
private function getLongestStreakEnd() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$streak_max_end = null;
$streak_curr = 0;
$streak_max = 0;
while ($curr <= $end) {
if ($this->getCommitsForDate($curr) > 0) {
$streak_curr++;
} else {
if ($streak_curr > $streak_max) {
$streak_max = $streak_curr;
$streak_max_end = clone $curr;
}
$streak_curr = 0;
}
$curr = $curr->modify('+1 day');
}
if ($streak_curr > $streak_max) {
$streak_max_end = clone $curr;
}
return $streak_max_end;
}
private function getMaxCommits() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$max = 0;
while ($curr <= $end) {
$max = max($max, $this->getCommitsForDate($curr));
$curr = $curr->modify('+1 day');
}
return $max;
}
private function getMaxCommitsDate() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$max = 0;
$max_date = null;
while ($curr <= $end) {
$c = $this->getCommitsForDate($curr);
if ($c >= $max) {
$max = $c;
$max_date = clone $curr;
}
$max = max($max, $this->getCommitsForDate($curr));
$curr = $curr->modify('+1 day');
}
return $max_date;
}
private function getAvgCommits() {
/* @var $curr DateTime */
/* @var $end DateTime */
$curr = clone $this->startdate;
$end = clone $this->enddate;
$max = array();
while ($curr <= $end) {
$max[] = $this->getCommitsForDate($curr);
$curr = $curr->modify('+1 day');
}
$sum = array_sum($max);
$count = count($max);
return $sum / $count;
}
public function loadFinishedContent() {
$data = unserialize(file_get_contents(self::FILE_FINISHED_DATA));
return $data['content'];
}
public function loadFinishedData() {
$data = unserialize(file_get_contents(self::FILE_FINISHED_DATA));
return $data;
}
private function getMaxCommitCount() {
$max = 0;
foreach($this->getYears() as $year) {
$max = max($max, max($this->generateYearMap($year)));
}
return $max;
}
private function generateYearMap($year) {
$ymap = array();
$date = new DateTime($year . '-01-01');
while($date->format('Y') == $year) {
$ymap[$date->format('Y-m-d')] = 0;
$date = $date->modify("+1 day");
}
foreach ($this->commits as $commit) {
if(array_key_exists($commit['date']->format('Y-m-d'), $ymap))
$ymap[$commit['date']->format('Y-m-d')]++;
}
return $ymap;
}
public function getYears() {
$years = array();
foreach ($this->commits as $commit) {
if(! in_array($commit['date']->format('Y'), $years))
$years[] = $commit['date']->format('Y');
}
asort($years);
return $years;
}
}

View File

@ -0,0 +1,34 @@
<?php
include 'extendedGitGraph.php';
$v = new ExtendedGitGraph('Mikescher');
//$v->authenticate('7e26c5f1621349c14a7d');
//$v->setToken('7b3f6443cdd4b2f92d75c4c8aa83cfda6c7ca3ce');
//$v->collect();
$v->loadData();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" language="JavaScript">
<?php include 'script.js'; ?>
</script>
</head>
<body>
<?php
//echo $v->generateAndSave();
echo $v->loadFinished();
?>
</body>
</html>

View File

@ -0,0 +1,25 @@
jQuery(document).ready(function ($) {
$('.svg-tip').fadeOut(0);
$('.svg-tip').css({opacity: 1});
$("rect").mouseenter(
function (event) {
$('.svg-tip').stop(true, true);
$('.svg-tip').fadeIn(400);
$('.svg-tip strong').html($(event.target).attr('hvr_header'));
$('.svg-tip span').html($(event.target).attr('hvr_content'));
$('.svg-tip').css({left: $(event.target).position().left - $('.svg-tip').outerWidth() /2 - 2.5 + 9});
$('.svg-tip').css({top: $(event.target).position().top - $('.svg-tip').outerHeight() - 10});
}
);
$("rect").mouseleave(
function () {
$('.svg-tip').stop(true, true);
$('.svg-tip').fadeOut(400);
}
);
});

View File

@ -0,0 +1,90 @@
.git_list {
display: inline-block;
width: 715px;
height: 115px;
overflow: visible;
}
.git_list text.caption {
font-size: 10px;
fill: #666;
}
.git_list text.caption_month {
font-size: 8px;
fill: #BBB;
}
.git_list text.caption_day {
font-size: 8px;
fill: #BBB;
}
.svg-tip:after {
box-sizing: border-box;
position: absolute;
left: 50%;
height: 5px;
width: 5px;
bottom: -10px;
margin: 0px 0px 0px -5px;
content: " ";
border: 5px solid transparent;
border-top-color: rgba(0, 0, 0, 0.8);
-moz-border-top-colors: none;
-moz-border-right-colors: none;
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
border-image: none;
}
.svg-tip {
padding: 5px;
background: none repeat scroll 0% 0% rgba(0, 0, 0, 0.8);
color: #BBB;
font-size: 12px;
position: absolute;
z-index: 99999;
text-align: center;
border-radius: 3px;
box-sizing: border-box;
opacity: 0;
}
body {
background-color: white;
margin: 40px 0 0;
}
.extGitGraphContainer {
background-color: #ECF0F1;
margin: 10px;
display: inline-block;
border: 1px solid #E3E3E3;
border-radius: 4px;
box-shadow: 0px 0 1px rgba(0, 0, 0, 0.25) inset;
}
.egg_footer {
margin-top: 5px;
text-align: right;
margin-right: 5px;
margin-bottom: 5px;
color: #FFF;
text-shadow: 0px 0px 8px #2C3E50, 0px 0px 8px #2C3E50;
}
.egg_footer > a {
text-decoration: none;
color: inherit;
}
.egg_footer > a:hover {
text-decoration: none;
color: #2C3E50;
text-shadow: 0px 0px 8px #00F;
}

View File

@ -0,0 +1,109 @@
<?php
/* @var $year integer */
/* @var ymap integer[] */
/* @var $ymapmax integer */
?>
<?php
$DIST_X = 13;
$DIST_Y = 13;
$DAY_WIDTH = 11;
$DAY_HEIGHT = 11;
$COLORS = ['#F5F5F5', '#DBDEE0', '#C2C7CB', '#AAB0B7', '#9099A2', '#77828E', '#5E6B79', '#455464', '#2C3E50'];
$MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$DAYS = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
?>
<?php
$date = new DateTime($year . '-01-01');
$monthlist = array_fill(0, 12, [0, 0]);
?>
<div class="extGitGraphContainer">
<svg class="git_list">
<g transform="translate(20, 20) ">
<g transform="translate(0, 0)">
<?php
$now = new DateTime();
$exponent = log(0.98/(count($COLORS)-1), 1/$ymapmax); // (1/max)^n = 0.98 // => 1 commit erreicht immer genau die erste stufe
$week = 0;
while($date->format('Y') == $year) {
if ($date > new DateTime()) {// THE FUTURE, SPONGEBOB
while ($date->format('d') != $date->format('t')) {
if ($date->format('N') == 1 && $date->format('z') > 0) {
$week++;
}
$date = $date->modify("+1 day");
}
$monthlist[$date->format('m') - 1][1] = $week + ($wday / 7);
$date = $date->modify("+1 year"); // Kill
continue;
}
$c_count = $ymap[$date->format('Y-m-d')];
$color_idx = ceil(pow($c_count/$ymapmax, $exponent) * (count($COLORS)-1));
$color = $COLORS[$color_idx];
$wday = ($date->format('N') - 1);
if ($date->format('N') == 1 && $date->format('z') > 0) {
echo '</g>', PHP_EOL;
$week++;
echo '<g transform="translate(' . $week*$DIST_X . ', 0)">', PHP_EOL;
}
if ($date->format('d') == 1) {
$monthlist[$date->format('m') - 1][0] = $week + ($wday / 7);
} else if ($date->format('d') == $date->format('t')) {
$monthlist[$date->format('m') - 1][1] = $week + ($wday / 7);
}
echo '<rect style="fill: ' . $color .
';" y="' . $wday*$DIST_Y .
'" height="' . $DAY_HEIGHT .
'" width="' . $DAY_WIDTH .
'" dbg_tag="' . $date->format('d.m.Y') . ' [' . $year . ' :: '.$week.' :: '.$wday.'] -> ' . $color_idx .
'" hvr_header="' . $c_count . ' commits'.
'" hvr_content="' . ' ' . $date->format('\o\n l jS F Y') .
'"/>', PHP_EOL;
$date = $date->modify("+1 day");
}
?>
</g>
<?php
for($i = 0; $i < 12; $i++) {
if ($monthlist[$i][1]-$monthlist[$i][0] > 0) {
$posx = (($monthlist[$i][0]+$monthlist[$i][1])/2) * $DIST_X;
echo '<text y="-3" x="' . $posx . '" style="text-anchor: middle" class="caption_month">' . $MONTHS[$i] . '</text>';
}
}
for($i = 0; $i < 7; $i++) {
echo '<text y="' . ($i*$DIST_Y + $DAY_HEIGHT/2) . '" x="-6" style="text-anchor: middle" class="caption_day" dominant-baseline="central">' . $DAYS[$i] . '</text>';
}
echo '<text x="-10" y="-5" class="caption">' . $year . '</text>';
?>
</g>
</svg>
<div class="svg-tip n">
<strong>&nbsp;</strong><span>&nbsp;</span>
</div>
<div class="egg_footer">
<a href="/programs/view/ExtendedGitGraph">extendedGitGraph</a>
</div>
</div>

View File

@ -34,14 +34,6 @@ return [
],
],
'params' =>
[
'yii.debug' => true,
'yii.traceLevel' => 3,
'yii.handleErrors' => true,
],
'modules' =>
[
'gii' =>

View File

@ -23,13 +23,5 @@ return [
[
'errorAction' => 'msmain/error',
],
],
'params' =>
[
'yii.debug' => false,
'yii.traceLevel' => 3,
'yii.handleErrors' => false,
],
];

View File

@ -33,7 +33,7 @@ return ArrayX::merge(
'application.extensions.*',
'application.components.*',
'application.components.widgets.*',
'application.components.extendedGitGraph.*',
'application.components.extendedgitgraph.*',
'application.components.parsedown.*',
'bootstrap.components.*',
'bootstrap.behaviors.*',
@ -143,6 +143,9 @@ return ArrayX::merge(
// using Yii::app()->params['paramName']
'params' =>
[
'yii.debug' => defined('YII_DEBUG'),
'yii.traceLevel' => 3,
'yii.handleErrors' => defined('YII_DEBUG'),
// this is used in contact page
'adminEmail' => 'kundenservice@mikescher.de',
],

View File

@ -1,6 +1,6 @@
<?php
class LogController extends MsController
class LogController extends MSController
{
public $layout='//layouts/column2';

View File

@ -75,7 +75,7 @@ class MSMainController extends MSController
{
$data = array();
$this->js_scripts[] = file_get_contents('protected/components/extendedGitGraph/script.js');
$this->js_scripts[] = file_get_contents('protected/components/extendedgitgraph/script.js');
if(isset($_POST['SendMailForm']))
{