simplified pagination, cleaned up front controller

Adding total actions count to location data has made it possible to
simplify backend data queries, so the custom pagination was optimized
and location repositories have been cleaned up.

the front end controller has been cleaned up as well, which has resulted
in the appropriate template being polished up as well.
This commit is contained in:
ro 2024-02-10 11:48:09 -06:00
parent fdaf90b89f
commit 2bcb887d19
4 changed files with 36 additions and 74 deletions

View file

@ -17,33 +17,24 @@ class FrontIndexController extends Controller
public function start() public function start()
{ {
$list = $this->locationRepository->getRecent();
$latest_date = $list[0]->updated_at->format('Y M d');
return view('front.index', [ return view('front.index', [
'count' => count($list), 'count' => count($this->locationRepository->getActiveLocations()),
'sources' => count(Source::where("active", true)->get()), 'sources' => count(Source::where("active", true)->get()),
'recent' => $list, 'recent' => $this->locationRepository->getRecent(),
'latest_date' => $latest_date, 'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'),
'title' => "The Bad Space" 'title' => "The Bad Space"
]); ]);
} }
public function indexSearch(Request $request) public function indexSearch(Request $request)
{ {
// this grabs the search results from the db
$results = $this->locationRepository->search($request);
//this gets recent updates to display under search results
$list = $this->locationRepository->getRecent();
$latest_date = $list[0]->updated_at->format('Y M d');
return view('front.index', [ return view('front.index', [
'count' => count($list), 'count' => count($this->locationRepository->getActiveLocations()),
'sources' => count(Source::where("active", true)->get()), 'sources' => count(Source::where("active", true)->get()),
'recent' => $list, 'recent' => $this->locationRepository->getRecent(),
'results' => $results, 'results' => $this->locationRepository->search($request),
'recent' => $list,
'terms' => $request->index_search, 'terms' => $request->index_search,
'latest_date' => $latest_date, 'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'),
'title' => "Search Results", 'title' => "Search Results",
]); ]);
} }

View file

@ -129,6 +129,15 @@ class LocationController extends Controller
} }
} }
//TODO: maintenance script to set locations to inactive if they haven't been updated
// over 90 days
//$diff=date_diff($location->updated_at, new DateTime());
//$days = $diff->format("%R%a days")
//$interval = $location->updated_at->diff(new DateTime());
//$days = $interval->format("%a");
//get all locations and sort which are present in unified or not //get all locations and sort which are present in unified or not
/* /*
$sorted = []; $sorted = [];

View file

@ -38,54 +38,25 @@ class LocationRepository
return $this->model::where("uuid", $uuid)->first(); return $this->model::where("uuid", $uuid)->first();
} }
public function getActiveLocations()
{
return $this->model::where("active", true)->where('actions_count', '>=', 2)->get();
}
public function getRecent() public function getRecent()
{ {
$locations = $this->model::where("active", true)->orderByDesc('updated_at')->get(); return $locations = $this->model::where("active", true)->where('actions_count', '>=', 2)
$list = []; ->orderByDesc('updated_at')->limit(10)->get();
foreach ($locations as $location) {
if (($location->block_count + $location->silence_count) >= 2) {
array_push($list, $location);
}
}
return $list;
} }
public function getPage($pageNum) public function getPage($pageNum)
{ {
$locations = $this->model::where("active", true)->get();
$active = [];
foreach ($locations as $location) {
if (($location->block_count + $location->silence_count) >= 2) {
array_push($active, $location);
}
}
$pages = [];
if (count($active) != 0) {
if (count($active) < $this->limit) {
$this->limit = count($active) - 1;
}
$range = $pageNum * $this->limit - $this->limit; $range = $pageNum * $this->limit - $this->limit;
$active = $this->model::where("active", true)->where('actions_count', '>=', 2)->get();
if ($range != 0) { $locations = $this->model::where("active", true)->where('actions_count', '>=', 2)
$range = $range + 1; ->limit($this->limit)->offset($range)->orderBy('id', 'asc')->get();
}
for ($i = 0; $i <= $this->limit; ++$i) {
if (isset($active[$i + $range])) {
array_push($pages, $active[$i + $range]);
} else {
// chill out
}
}
}
$pageCount = ceil(count($active) / $this->limit); $pageCount = ceil(count($active) / $this->limit);
if ($range != 0) {
$range = $range + 1;
}
$next = $pageNum + 1; $next = $pageNum + 1;
if ($next > $pageCount) { if ($next > $pageCount) {
$next = 1; $next = 1;
@ -97,6 +68,6 @@ class LocationRepository
$prev = $pageCount; $prev = $pageCount;
} }
return $result = [$pages, $pageCount, $prev, $next]; return $result = [$locations, $pageCount, $prev, $next];
} }
} }

View file

@ -17,12 +17,8 @@
<article> <article>
<h2>Found {{count($results)}} results for {{$terms}}</h2> <h2>Found {{count($results)}} results for {{$terms}}</h2>
@foreach($results as $item) @foreach($results as $item)
@php
$action = $item->block_count + $item->silence_count;
$rating = ($action / $sources)*100;
@endphp
<a class="list-link" role="listitem" href="/location/{{$item->uuid}}"> <a class="list-link" role="listitem" href="/location/{{$item->uuid}}">
<span class="item-rating">{{$rating}}%</span> <span class="item-rating">{{($item->actions_count / $sources)*100}}%</span>
<label class="item-name">{{$item->name}}</label> <label class="item-name">{{$item->name}}</label>
<div class="item-silence"> <div class="item-silence">
<img class="item-icon" src="/assets/images/global/status-silence.svg" title="silenced" /> <img class="item-icon" src="/assets/images/global/status-silence.svg" title="silenced" />
@ -40,24 +36,20 @@
<section class="index-meta"> <section class="index-meta">
<article> <article>
<h2>Recent Updates</h2> <h2>Recent Updates</h2>
@for($i = 0; $i < 10; $i++) @foreach($recent as $item)
@php <a class="list-link" role="listitem" href="/location/{{$item->uuid}}">
$action = $recent[$i]->block_count + $recent[$i]->silence_count; <span class="item-rating">{{($item->actions_count / $sources)*100}}%</span>
$rating = ($action / $sources)*100; <label class="item-name">{{$item->name}}</label>
@endphp
<a class="list-link" role="listitem" href="/location/{{$recent[$i]->uuid}}">
<span class="item-rating">{{$rating}}%</span>
<label class="item-name">{{$recent[$i]->name}}</label>
<div class="item-silence"> <div class="item-silence">
<img class="item-icon" src="/assets/images/global/status-silence.svg" title="silenced" /> <img class="item-icon" src="/assets/images/global/status-silence.svg" title="silenced" />
{{$recent[$i]->silence_count}} {{$item->silence_count}}
</div> </div>
<div class="item-block"> <div class="item-block">
<img class="item-icon" src="/assets/images/global/status-suspend.svg" title="suspended" /> <img class="item-icon" src="/assets/images/global/status-suspend.svg" title="suspended" />
{{$recent[$i]->block_count}} {{$item->block_count}}
</div> </div>
</a> </a>
@endfor @endforeach
<h2>Info</h2> <h2>Info</h2>
<div class="index-meta"> <div class="index-meta">
<label>Locations Tracked</label> <label>Locations Tracked</label>
@ -67,7 +59,6 @@
<label>Latest Update</label> <label>Latest Update</label>
<label>{{$latest_date}}</label> <label>{{$latest_date}}</label>
</div> </div>
</article> </article>
</section> </section>
@endsection @endsection