Skip to content

Commit

Permalink
Merge pull request #35 from matks/add-week-view
Browse files Browse the repository at this point in the history
Enable developer week view
  • Loading branch information
matks committed Apr 1, 2022
2 parents a35dcff + 885b694 commit bba8a9e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 38 deletions.
6 changes: 5 additions & 1 deletion src/Controller/ReviewStatsHomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public function viewDeveloper(string $login): Response
);

return $this->render('developer_stats.html.twig',
['stats' => $developerStats, 'login' => $login]
[
'login' => $login,
'dayByDayStats' => $developerStats['dayByDayStats'],
'weekStats' => array_reverse($developerStats['weekStats']),
]
);
}
}
5 changes: 5 additions & 0 deletions src/Helper/DayComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ public static function getPastWeekRanges($numberOfWeeks, DateTime $day)

return array_reverse($result);
}

public static function findWeekNumber(DateTime $date): int
{
return (int) $date->format("W");
}
}
68 changes: 41 additions & 27 deletions src/Helper/ReviewStatsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getTeamStatsGroupedByDay(DateTime $from, DateTime $to): array
* @param int $howManyDays
* @param DateTime $endDate
*
* @return array<int, array<string, mixed>>
* @return array
*/
public function getDeveloperStats(string $login, int $howManyDays, DateTime $endDate): array
{
Expand All @@ -131,37 +131,27 @@ public function getDeveloperStats(string $login, int $howManyDays, DateTime $end

$result = $this->pdo->query($sql)->fetchAll();

$cleanResult = [];
foreach ($result as $item) {
$cleanResult[] = [
'day' => $item['day'],
'PR' => $this->formatPRs($item['PR']),
'total' => $item['total'],
$weekRanges = DayComputer::getPastWeekRanges(12, DayComputer::getSundayBefore($endDate));
$weekRangesTotals = [];
foreach ($weekRanges as $weekRange) {
$weekRangesTotals[] = [
'begin' => new DateTime($weekRange[0]),
'end' => new DateTime($weekRange[1]),
'number' => DayComputer::findWeekNumber(new DateTime($weekRange[0])),
'total' => 0
];
}

return $cleanResult;
}

/**
* @param array<string, array<string, int>> $groupedByLogin
* @param string $login
* @param string $day
* @param int $total
*
* @return array<string, array<string, int>>
*/
private function addOrInsert(array $groupedByLogin, string $login, string $day, int $total): array
{
if (!array_key_exists($login, $groupedByLogin)) {
$groupedByLogin[$login] = [];
}
if (!array_key_exists($day, $groupedByLogin[$login])) {
$groupedByLogin[$login][$day] = [];
$dayByDayStats = [];
foreach ($result as $item) {
$dayByDayStats[] = $this->buildDayStat($item);
$weekRangesTotals = $this->insertIntoWeekStat($item, $weekRangesTotals);
}
$groupedByLogin[$login][$day] = $total;

return $groupedByLogin;
return [
'dayByDayStats' => $dayByDayStats,
'weekStats' => $weekRangesTotals,
];
}

/**
Expand Down Expand Up @@ -226,4 +216,28 @@ private function computeAndInsertTotals(array $groupedByLogin): array

return $copy;
}

private function buildDayStat(array $item): array
{
return [
'day' => $item['day'],
'PR' => $this->formatPRs($item['PR']),
'total' => $item['total'],
];
}

private function insertIntoWeekStat(array $item, array $weekRangesTotals): array
{
$day = new DateTime($item['day']);
foreach ($weekRangesTotals as $i => $weekRangesTotal) {
if ($day >= $weekRangesTotal['begin'] && $day <= $weekRangesTotal['end']) {
$weekRangesTotals[$i]['total'] += (int) $item['total'];
break;
}
}

return $weekRangesTotals;
}


}
42 changes: 34 additions & 8 deletions templates/developer_stats.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,47 @@
{% endblock %}

{% block page_info %}
<div class="jumbotron">
<h2>About this dashboard</h2>
<div>
You can see the same data on your personal GitHub page:
<a href="https://github.com/{{ login }}">https://github.com/{{ login }}</a>
<div class="jumbotron">
<h2>About this dashboard</h2>
<div>
You can see the same data on your personal GitHub page:
<a href="https://github.com/{{ login }}">https://github.com/{{ login }}</a>
</div>
</div>
</div>
{% endblock %}

{% block page_content %}

<div class="light-jumbotron container">
<h2>Week stats</h2>
</div>

<table class="table">
<thead>
<tr>
<th scope="col">Week</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
{% for weekStat in weekStats %}
<tr>
<th scope="row"> {{ weekStat.number }}</th>
<td>{{ weekStat.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<div class="light-jumbotron container">
<h2>Last 90 days</h2>
</div>

<dl class="row">
{% for data in stats %}
{% for data in dayByDayStats %}
<dt class="col-sm-4">On {{ data['day'] }} reviewed {{ data['total'] }} pull requests:</dt>
<dd class="col-sm-8">
{{ data['PR'] | raw }}
{{ data['PR'] | raw }}
</dd>

{% endfor %}
Expand Down
13 changes: 13 additions & 0 deletions tests/Helper/DayComputerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,17 @@ public function testGetMultiplePastWeekRange(): void
DayComputer::getPastWeekRanges(3, new DateTime('2022-01-10 05:00:00'))
);
}

public function testFindWeekNumber(): void
{
$dateTime1 = new DateTime('2022-03-21 05:00:00');
$dateTime2 = new DateTime('2022-03-22 05:00:00');
$dateTime3 = new DateTime('2022-03-27 05:00:00');
$dateTime4 = new DateTime('2022-03-20 05:00:00');

$this->assertEquals(12, DayComputer::findWeekNumber($dateTime1));
$this->assertEquals(12, DayComputer::findWeekNumber($dateTime2));
$this->assertEquals(12, DayComputer::findWeekNumber($dateTime3));
$this->assertEquals(11, DayComputer::findWeekNumber($dateTime4));
}
}
8 changes: 6 additions & 2 deletions tests/Helper/ReviewStatsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public function testGetDeveloperStatsFromMatks(): void

$statsService = new ReviewStatsService($this->getPDO());

$stats = $statsService->getDeveloperStats(
$result = $statsService->getDeveloperStats(
'matks',
10,
new DateTime('2021-12-06')
);

$stats = $result['dayByDayStats'];

$this->assertEquals($stats[0]['day'], '2021-12-05');
$this->assertEquals($stats[0]['total'], 2);
$this->assertEquals($stats[4]['day'], '2021-12-01');
Expand All @@ -56,12 +58,14 @@ public function testGetDeveloperStatsFromPierreRambaud(): void

$statsService = new ReviewStatsService($this->getPDO());

$stats = $statsService->getDeveloperStats(
$result = $statsService->getDeveloperStats(
'PierreRambaud',
10,
new DateTime('2021-12-06')
);

$stats = $result['dayByDayStats'];

$this->assertEquals($stats[0]['day'], '2021-12-02');
$this->assertEquals($stats[0]['total'], 20);
$this->assertEquals($stats[1]['day'], '2021-12-01');
Expand Down

0 comments on commit bba8a9e

Please sign in to comment.