Skip to content

Commit 18b6d7e

Browse files
lroggenroshangautam
authored andcommitted
[feature] new sortable columns
- adds sorting ability on last run and average runtime columns resolves #151
1 parent 89e60b9 commit 18b6d7e

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

resources/views/tasks/index.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<thead>
2323
<tr>
2424
<th>{!! Html::columnSort('Description', 'description') !!}</th>
25-
<th>Average Runtime</th>
26-
<th>Last Run</th>
25+
<th>{!! Html::columnSort('Average Runtime', 'average_runtime') !!}</th>
26+
<th>{!! Html::columnSort('Last Run', 'last_ran_at') !!}</th>
2727
<th>Next Run</th>
2828
<th class="uk-text-center">Execute</th>
2929
</tr>
@@ -85,4 +85,4 @@
8585
</span>
8686
</div>
8787
{{$tasks->links('totem::partials.pagination')}}
88-
@stop
88+
@stop

src/Http/Controllers/TasksController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function index()
3838
->builder()
3939
->sortableBy([
4040
'description',
41+
'last_ran_at',
42+
'average_runtime',
4143
], ['description'=>'asc'])
4244
->when(request('q'), function ($query) {
4345
$query->where('description', 'LIKE', '%'.request('q').'%');

src/Repositories/EloquentTaskRepository.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Studio\Totem\Repositories;
44

55
use Studio\Totem\Task;
6+
use Studio\Totem\Result;
67
use Studio\Totem\Events\Created;
78
use Studio\Totem\Events\Deleted;
89
use Studio\Totem\Events\Updated;
@@ -14,6 +15,7 @@
1415
use Studio\Totem\Events\Deactivated;
1516
use Illuminate\Support\Facades\Cache;
1617
use Illuminate\Support\Facades\Artisan;
18+
use Illuminate\Database\Eloquent\Builder;
1719
use Studio\Totem\Contracts\TaskInterface;
1820

1921
class EloquentTaskRepository implements TaskInterface
@@ -23,9 +25,19 @@ class EloquentTaskRepository implements TaskInterface
2325
*
2426
* @return Task
2527
*/
26-
public function builder()
28+
public function builder() : Builder
2729
{
28-
return new Task;
30+
$result = new Result;
31+
32+
return (new Task)->select('tasks.*')
33+
->selectSub(
34+
$result->getLastRun(),
35+
'last_ran_at'
36+
)
37+
->selectSub(
38+
$result->getAverageRunTime(),
39+
'average_runtime'
40+
);
2941
}
3042

3143
/**

src/Result.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Studio\Totem;
44

5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Database\Query\Builder;
7+
58
class Result extends TotemModel
69
{
710
protected $table = 'task_results';
@@ -19,4 +22,26 @@ public function task()
1922
{
2023
return $this->belongsTo(Task::class);
2124
}
25+
26+
/**
27+
* @return Builder
28+
*/
29+
public function getLastRun() : Builder
30+
{
31+
return $this->select('ran_at')
32+
->whereColumn('task_id', 'tasks.id')
33+
->latest()
34+
->limit(1)
35+
->getQuery();
36+
}
37+
38+
/**
39+
* @return Builder
40+
*/
41+
public function getAverageRunTime() : Builder
42+
{
43+
return $this->select(DB::raw('avg(duration)'))
44+
->whereColumn('task_id', 'tasks.id')
45+
->getQuery();
46+
}
2247
}

0 commit comments

Comments
 (0)