Skip to content

Commit 7c81e05

Browse files
jeyemweyHerrLevin
authored andcommittedDec 9, 2019
First try of a usage board, missing many features:
* X-Axis should recognize dates * Y-Axis should have 0 * Queries should have higher performance * Queries should be tested
1 parent 00ba61a commit 7c81e05

12 files changed

+224
-1
lines changed
 

‎app/Http/Controllers/FrontendStatusController.php

+12
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,16 @@ public function getStatus($id) {
102102
$StatusResponse = StatusBackend::getStatus($id);
103103
return view('status', ['status' => $StatusResponse]);
104104
}
105+
106+
public function usageboard(Request $request) {
107+
if($request->input('auth_key') != sha1("This is bad security, but it's an okay prototype until we have a better way of working with user roles.")) {
108+
abort(401); // Unauthorized
109+
}
110+
111+
$statusesByDay = StatusController::usageByDay();
112+
$userRegistrationsByDay = UserController::registerByDay();
113+
$hafasTripsByDay = TransportController::usageByDay();
114+
115+
return view('usageboard', ['statusesByDay' => $statusesByDay, 'userRegistrationsByDay' => $userRegistrationsByDay, 'hafasTripsByDay' => $hafasTripsByDay]);
116+
}
105117
}

‎app/Http/Controllers/StatusController.php

+9
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ public function exportCSV(Request $request) {
176176
public function writeLine($array): String {
177177
return vsprintf("\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\"%s\"\t\n", $array);
178178
}
179+
180+
public static function usageByDay() {
181+
$q = DB::table('statuses')
182+
->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as occurs'))
183+
->groupBy('date')
184+
->orderBy('date', 'DESC')
185+
->get(14);
186+
return $q;
187+
}
179188
}

‎app/Http/Controllers/TransportController.php

+19
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,23 @@ public static function SetHome($user, $ibnr) {
428428
}
429429
return $station->name;
430430
}
431+
432+
public static function usageByDay() {
433+
434+
$hafas = DB::table('hafas_trips')
435+
->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as hafas_trips'))
436+
->groupBy('date')
437+
->orderBy('date', 'DESC')
438+
->get(14)
439+
->map(function($line) {
440+
$query = DB::select('SELECT COUNT(*) AS c FROM poly_lines WHERE DATE(created_at) = ?', [$line->date]);
441+
$line->polylines = $query[0]->c;
442+
443+
return $line;
444+
});
445+
446+
447+
448+
return $hafas;
449+
}
431450
}

‎app/Http/Controllers/UserController.php

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Support\Facades\File;
1616
use Illuminate\Support\Facades\Storage;
1717
use Illuminate\Support\Facades\Hash;
18+
use Illuminate\Support\Facades\DB;
1819
use Jenssegers\Agent\Agent;
1920
use Intervention\Image\ImageManagerStatic as Image;
2021

@@ -223,4 +224,12 @@ public static function getLeaderboard() {
223224
return ['users' => $users, 'friends' => $friends, 'kilometers' => $kilometers];
224225
}
225226

227+
public static function registerByDay() {
228+
$q = DB::table('users')
229+
->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as occurs'))
230+
->groupBy('date')
231+
->orderBy('date', 'DESC')
232+
->get(14);
233+
return $q;
234+
}
226235
}

‎artisan

100755100644
File mode changed.

‎package-lock.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
"awesomplete": "^1.1.5",
2929
"bootstrap": "^4.3.1",
3030
"bootstrap-cookie-alert": "^1.1.1",
31+
"chart.js": "^2.9.3",
3132
"croppie": "^2.6.4",
3233
"font-awesome": "^4.7.0",
3334
"leaflet": "^1.5.1",
34-
"mdbootstrap": "^4.8.9"
35+
"mdbootstrap": "^4.8.9",
36+
"momentjs": "^2.0.0"
3537
}
3638
}

‎public/js/app.js

+43
Original file line numberDiff line numberDiff line change
@@ -57580,6 +57580,8 @@ window.addEventListener("load", function () {
5758057580
__webpack_require__(/*! ./components/alert */ "./resources/js/components/alert.js");
5758157581

5758257582
__webpack_require__(/*! ./components/pwa_fix */ "./resources/js/components/pwa_fix.js");
57583+
57584+
__webpack_require__(/*! ./components/usageBoard */ "./resources/js/components/usageBoard.js");
5758357585
});
5758457586

5758557587
/***/ }),
@@ -58166,6 +58168,47 @@ if (document.getElementById("timepicker-reveal")) {
5816658168

5816758169
/***/ }),
5816858170

58171+
/***/ "./resources/js/components/usageBoard.js":
58172+
/*!***********************************************!*\
58173+
!*** ./resources/js/components/usageBoard.js ***!
58174+
\***********************************************/
58175+
/*! no static exports found */
58176+
/***/ (function(module, exports) {
58177+
58178+
var timeFormat = "YYYY-MM-DD";
58179+
var colors = ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)', 'rgb(153, 102, 255)', 'rgb(201, 203, 207)'];
58180+
var colorindex = 0;
58181+
Array.from(document.getElementsByClassName("date-canvas")).forEach(function (canvas) {
58182+
var data = Array.from(JSON.parse(canvas.dataset["json"])).sort(function (a, b) {
58183+
return a.date > b.date;
58184+
});
58185+
var config = {
58186+
type: "line",
58187+
data: {
58188+
labels: data.map(function (data) {
58189+
return data.date;
58190+
}),
58191+
datasets: canvas.dataset.keys.split(',').map(function (key, index) {
58192+
var color = colors[colorindex++ % colors.length];
58193+
console.log(colors);
58194+
return {
58195+
label: canvas.dataset.title.split(',')[index],
58196+
data: data.map(function (date) {
58197+
return date[key];
58198+
}),
58199+
borderColor: color,
58200+
backgroundColor: "rgba(0,0,0,0)" // transparent
58201+
58202+
};
58203+
})
58204+
}
58205+
};
58206+
var ctx = canvas.getContext("2d");
58207+
window.myLine = new Chart(ctx, config);
58208+
});
58209+
58210+
/***/ }),
58211+
5816958212
/***/ "./resources/sass/app.scss":
5817058213
/*!*********************************!*\
5817158214
!*** ./resources/sass/app.scss ***!

‎resources/js/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ window.addEventListener("load", () => {
2020
require("./components/settings");
2121
require("./components/alert");
2222
require("./components/pwa_fix");
23+
require("./components/usageBoard");
2324
});

‎resources/js/components/usageBoard.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
let timeFormat = "YYYY-MM-DD";
2+
3+
const colors = [
4+
'rgb(255, 99, 132)',
5+
'rgb(255, 159, 64)',
6+
'rgb(255, 205, 86)',
7+
'rgb(75, 192, 192)',
8+
'rgb(54, 162, 235)',
9+
'rgb(153, 102, 255)',
10+
'rgb(201, 203, 207)'
11+
];
12+
13+
let colorindex = 0;
14+
15+
Array.from(document.getElementsByClassName("date-canvas")).forEach((canvas) => {
16+
const data = Array.from(JSON.parse(canvas.dataset["json"])).sort(
17+
(a, b) => a.date > b.date
18+
);
19+
20+
21+
var config = {
22+
type: "line",
23+
data: {
24+
labels: data.map(data => data.date),
25+
datasets: canvas.dataset.keys.split(',').map((key, index) => {
26+
const color = colors[colorindex++ % colors.length];
27+
console.log(colors);
28+
29+
return {
30+
label: canvas.dataset.title.split(',')[index],
31+
data: data.map(date => date[key]),
32+
33+
borderColor: color,
34+
backgroundColor: `rgba(0,0,0,0)` // transparent
35+
};
36+
})
37+
}
38+
};
39+
40+
var ctx = canvas.getContext("2d");
41+
window.myLine = new Chart(ctx, config);
42+
});

‎resources/views/usageboard.blade.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@extends('layouts.app')
2+
3+
@section('title')
4+
Usage
5+
@endsection
6+
7+
@section('content')
8+
<div class="container">
9+
<h4 class="mt-4">Usage board</h4>
10+
11+
<div class="row">
12+
<div class="col">
13+
<canvas
14+
id="statusesByDateCanvas"
15+
class="date-canvas"
16+
data-json='{!! json_encode($statusesByDay) !!}'
17+
data-title="Statuses per Day"
18+
data-keys='occurs'
19+
height="300"
20+
></canvas>
21+
</div>
22+
<div class="col">
23+
<canvas
24+
id="userRegistrationCanvas"
25+
class="date-canvas"
26+
data-json='{!! json_encode($userRegistrationsByDay) !!}'
27+
data-title="User Registrations per Day"
28+
data-keys='occurs'
29+
height="300"
30+
></canvas>
31+
</div>
32+
<div class="col">
33+
<canvas
34+
id="userRegistrationCanvas"
35+
class="date-canvas"
36+
data-json='{!! json_encode($hafasTripsByDay) !!}'
37+
data-title="HafasTrips,Polylines"
38+
data-keys='hafas_trips,polylines'
39+
height="300"
40+
></canvas>
41+
</div>
42+
</div>
43+
</div>
44+
45+
@endsection

‎routes/web.php

+5
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,10 @@
229229
'uses' => 'SocialController@testMastodon',
230230
]);
231231

232+
Route::get('/usage', [
233+
'uses' => 'FrontendStatusController@usageboard',
234+
'as' => 'usage'
235+
]);
236+
232237
});
233238
//Route::get('/trip', 'HafasTripController@getTrip')->defaults('tripID', '1|178890|0|80|13082019')->defaults('lineName', 'ICE 376');

0 commit comments

Comments
 (0)