Skip to content

Commit

Permalink
Add a second chart - the rate in signatures per hour.
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Jan 13, 2019
1 parent 3bebc19 commit 0f770ad
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
44 changes: 43 additions & 1 deletion app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use App\Charts\SimpleOverview;
use App\Petition;
use Carbon\Carbon;

class ReportController extends Controller
{
Expand All @@ -25,7 +26,8 @@ public function simpleOverview(int $petitionNumber = null)

$chart = new SimpleOverview;

// TODO: format the time without seconds.
// The time is formatted without seconds and rounded to
// the nearest five minutes.

$chart->labels(
$allOverviewCounts->pluck(['count_time_five_minute'])
Expand All @@ -44,13 +46,53 @@ public function simpleOverview(int $petitionNumber = null)
]
]
]);

// Now calculate the derivative.

$previous = null;
$derivative = [];

$allOverviewCounts->each(function ($item) use (& $previous, & $derivative) {
$fiveMinutes = Carbon::parse($item->count_time)->roundMinute(5);

if ($previous === null) {
$previous = $item;
$previous->time = $fiveMinutes;
return;
}

$hours = ($fiveMinutes->diffInMinutes($previous->time)) / 60;
if ($hours == 0) {
// Sometimes two times will be the same.
// Skip that iteration to avoid a divide by zero.
return;
}
$signatures = $item->count - $previous->count;
$signaturesPerHour = round($signatures / $hours, 2);

$derivative[$fiveMinutes->format('Y-m-d H:i')] = $signaturesPerHour;

$previous = $item;
$previous->time = $fiveMinutes;
});

$chart2 = new SimpleOverview;

$chart2->labels(array_keys($derivative));

$chart2->dataset(
$petitionData->getAction(),
'line',
array_values($derivative)
);
}
}

$petitionList = Petition::get();

return view('charts.simple-overview', [
'chart' => $chart ?? null,
'chart2' => $chart2 ?? null,
'petitionList' => $petitionList,
'petition' => $petition ?? null,
]);
Expand Down
2 changes: 1 addition & 1 deletion app/Petition.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getJobFetchCount(Carbon $fromTime = null, Carbon $toTime = null)
public function getJobFetchRange(
Carbon $fromTime = null,
Carbon $toTime = null,
int $maxPoints = 350
int $maxPoints = 1000
) {
if ($fromTime == null) {
$fromTime = $this->getJobFetchMinTime();
Expand Down
25 changes: 22 additions & 3 deletions resources/views/charts/simple-overview.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,34 @@
<hr />

@if(!empty($chart))

<script src="https://unpkg.com/vue"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js" charset="utf-8"></script>

<h2>Total Signatures</h2>

<div id="app">
{!! $chart->container() !!}
</div>
<script src="https://unpkg.com/vue"></script>
<script>
var app = new Vue({
el: '#app',
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js" charset="utf-8"></script>
{!! $chart->script() !!}

<h2>Signatures per Hour</h2>

<div id="app2">
{!! $chart2->container() !!}
</div>
<script>
var app2 = new Vue({
el: '#app2',
});
</script>
{!! $chart2->script() !!}

@elseif(!empty($petition))
<p class="alert alert-info">First sample will be gathered shortly.</p>
@endif
Expand Down Expand Up @@ -63,12 +80,14 @@
{!! Markdown::convertToHtml($petition->getPetitionData()->getBackground()) !!}
</td>
</tr>
@if($petition->getPetitionData()->getAdditionalDetails())
<tr>
<th scope="row">Additional Details</td>
<td>
{!! Markdown::convertToHtml($petition->getPetitionData()->getAdditionalDetails()) !!}
</td>
</tr>
@endif
<tr>
<th scope="row">Petition Home Page</td>
<td><a href="{{ $petition->getPetitionData()->getHtmlUrl() }}" rel="external">
Expand All @@ -91,7 +110,7 @@
@if($petition->fetchJobs()->count())
{{ $petition->fetchJobs()->count() }}
since
{{ $petition->fetchJobs()->oldest()->first()->created_at }}
{{ $petition->fetchJobs()->oldest()->first()->created_at->format('Y-m-d') }}
@else
No results yet
@endif
Expand Down

0 comments on commit 0f770ad

Please sign in to comment.