Skip to content

Commit

Permalink
Some caching updates; show percentage of votes linked to a constituency.
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Mar 22, 2019
1 parent 3610540 commit 2722722
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 29 deletions.
23 changes: 19 additions & 4 deletions app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public function simpleOverview($petitionNumber = null)

$petitionData = $petition->getPetitionData();

$allOverviewCounts = $petition->getJobFetchRange();

$chartData = $petition->getChartData();

if ($allOverviewCounts !== null) {
if ($chartData !== null) {
// Construct the chart.
// Couldn't be easier. Package here:
// https://github.com/ConsoleTVs/Charts
Expand Down Expand Up @@ -64,8 +62,22 @@ public function simpleOverview($petitionNumber = null)
'color' => ['#aa6666'],
'backgroundColor' => ['#ffbbbb'],
]);

}
} else {
$petition = null;
}

if (!empty($petition) && $petition->fetchJobs()->count()) {
$totalCount = $petition->fetchJobs()->latest()->first()->count;
$constituencyCount = $petition
->fetchJobs()
->latest()
->first()
->constituencySignatures()
->sum('count');
} else {
$totalCount = 0;
$constituencyCount = 0;
}

$petitionList = Petition::get();
Expand All @@ -75,6 +87,9 @@ public function simpleOverview($petitionNumber = null)
'chart2' => $chart2 ?? null,
'petitionList' => $petitionList,
'petition' => $petition ?? null,
'petitionData' => $petition ? $petition->getPetitionData() : null,
'totalCount' => $totalCount,
'constituencyCount' => $constituencyCount,
]);
}
}
12 changes: 6 additions & 6 deletions app/Petition.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ public function getCacheKey()
*/
public function getChartData(bool $withCache = true)
{
$allOverviewCounts = $this->getJobFetchRange();

if ($allOverviewCounts === null) {
return;
}

$cacheKey = $this->getCacheKey();

if ($withCache && ($data = Cache::get($cacheKey, null)) !== null) {
return $data;
}

$allOverviewCounts = $this->getJobFetchRange();

if ($allOverviewCounts === null) {
return;
}

$petitionData = $this->getPetitionData();

$data = collect();
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"license": "MIT",
"require": {
"php": "^7.1.3",
"barryvdh/laravel-debugbar": "^3.2",
"consoletvs/charts": "6.*",
"fideloper/proxy": "^4.0",
"graham-campbell/markdown": "^10.2",
"kylekatarnls/laravel-carbon-2": "^1.0.0",
"laravel/framework": "5.7.*",
"laravel/tinker": "^1.0",
"nesbot/carbon": "2.5.1 as 1.34.0",
"kylekatarnls/laravel-carbon-2": "^1.0.0"
"nesbot/carbon": "2.5.1 as 1.34.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
Expand Down
131 changes: 130 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
|
*/

'debug' => env('APP_DEBUG', false),
'debug' => env('APP_DEBUG', false)
&& in_array(
$_SERVER['REMOTE_ADDR'] ?? 'nowhere',
explode(',', env('DEBUG_IP_LIST', ''))
),

/*
|--------------------------------------------------------------------------
Expand Down
31 changes: 16 additions & 15 deletions resources/views/charts/simple-overview.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,48 +62,49 @@
<table class="table">
<tr>
<th scope="row">Action</th>
<td>{{ $petition->getPetitionData()->getAction() }}</td>
<td>{{ $petitionData->getAction() }}</td>
</tr>
<tr>
<th scope="row">Total Signatures</th>
<td>
@if($petition->fetchJobs()->count())
{{ number_format($petition->fetchJobs()->latest()->first()->count) }}
({{ number_format($petition->fetchJobs()->latest()->first()->constituencySignatures()->sum('count')) }}
for UK constituencies)
@if($totalCount > 0)
{{ number_format($totalCount) }}
({{ number_format($constituencyCount) }}
for UK constituencies;
{{ number_format(($constituencyCount / $totalCount) * 100) }}%)
@else
No results yet
@endif
</td>
</tr>
<tr>
<th scope="row">State</th>
<td>{{ $petition->getPetitionData()->getState() }}</td>
<td>{{ $petitionData->getState() }}</td>
</tr>
<tr>
<th scope="row">Background</th>
<td>
{!! Markdown::convertToHtml($petition->getPetitionData()->getBackground()) !!}
{!! Markdown::convertToHtml($petitionData->getBackground()) !!}
</td>
</tr>
@if($petition->getPetitionData()->getAdditionalDetails())
@if($petitionData->getAdditionalDetails())
<tr>
<th scope="row">Additional Details</th>
<td>
{!! Markdown::convertToHtml($petition->getPetitionData()->getAdditionalDetails()) !!}
{!! Markdown::convertToHtml($petitionData->getAdditionalDetails()) !!}
</td>
</tr>
@endif
<tr>
<th scope="row">Petition Home Page</th>
<td><a href="{{ $petition->getPetitionData()->getHtmlUrl() }}" rel="external">
{{ $petition->getPetitionData()->getHtmlUrl() }}
<td><a href="{{ $petitionData->getHtmlUrl() }}" rel="external">
{{ $petitionData->getHtmlUrl() }}
</a></td>
</tr>
<tr>
<th scope="row">Source JSON Feed</th>
<td><a href="{{ $petition->getPetitionData()->getJsonUrl() }}" rel="external">
{{ $petition->getPetitionData()->getJsonUrl() }}
<td><a href="{{ $petitionData->getJsonUrl() }}" rel="external">
{{ $petitionData->getJsonUrl() }}
</a></td>
</tr>
<tr>
Expand All @@ -113,8 +114,8 @@
<tr>
<th scope="row">Sample Count</th>
<td>
@if($petition->fetchJobs()->count())
{{ $petition->fetchJobs()->count() }}
@if($sampleCount = $petition->fetchJobs()->count())
{{ $sampleCount }}
since
{{ $petition->fetchJobs()->oldest()->first()->created_at->format('Y-m-d') }}
@else
Expand Down

0 comments on commit 2722722

Please sign in to comment.