diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 35919a69fb44bab286ecbd8e449ff4f5eb5df631..37fa5b388a53991e214d9f0545ff9c7ad2cbc9e6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -24,6 +24,7 @@ update(144.76.113.134): - scp -P 63824 metager@metager3.de:~/blacklistDomains.txt config/ - scp -P 63824 metager@metager3.de:~/adBlacklistUrl.txt config/ - scp -P 63824 metager@metager3.de:~/adBlacklistDomains.txt config/ + - scp -P 63824 metager@metager3.de:~/UsersSeeder.php database/seeds/ - touch storage/logs/laravel.log - touch storage/logs/worker.log - touch database/metager.sqlite @@ -33,6 +34,7 @@ update(144.76.113.134): - npm install - npm run production - php artisan migrate --force + - php artisan db:seed --force - php artisan requests:gather - if [ -f ~/MetaGer/artisan ]; then php ~/MetaGer/artisan down;fi - cd ~/ @@ -65,6 +67,7 @@ update(metager2): - scp -P 63824 metager@metager3.de:~/sumasEn.json config/ - scp -P 63824 metager@metager3.de:~/blacklistUrl.txt config/ - scp -P 63824 metager@metager3.de:~/blacklistDomains.txt config/ + - scp -P 63824 metager@metager3.de:~/UsersSeeder.php database/seeds/ - touch storage/logs/laravel.log - touch storage/logs/worker.log - touch database/metager.sqlite @@ -74,6 +77,7 @@ update(metager2): - npm install - npm run production - php artisan migrate --force + - php artisan db:seed --force - php artisan requests:gather - if [ -f ~/MetaGer/artisan ]; then php ~/MetaGer/artisan down;fi - cd ~/ diff --git a/app/Http/Controllers/AdminInterface.php b/app/Http/Controllers/AdminInterface.php index 389c5fe1454e61089edecd370fc9b8f041879669..7336e2b095689a0aa8cc71fc83e984d84083df97 100644 --- a/app/Http/Controllers/AdminInterface.php +++ b/app/Http/Controllers/AdminInterface.php @@ -94,7 +94,155 @@ class AdminInterface extends Controller if (!is_int($days) || $days <= 0) { $days = 28; } + $logs = $this->getStats($days); + $oldLogs = []; + $rekordTag = 0; + $minCount = 0; + $rekordTagDate = ""; + $size = 0; + $count = 0; + + $now = Carbon::now()->subMinutes(Carbon::now()->minute % 5)->format('H:i'); + if ($now === "00:00") { + $now = "00:05"; + } + + foreach ($logs as $key => $stats) { + if ($key === 0) { + // Log for today + $logToday = empty($stats->insgesamt->{$interface}) ? 0 : $stats->insgesamt->{$interface}; + continue; + } + $insgesamt = empty($stats->insgesamt->{$interface}) ? 0 : $stats->insgesamt->{$interface}; + $sameTime = empty($stats->time->{$now}->{$interface}) ? 0 : $stats->time->{$now}->{$interface}; + + if ($insgesamt > $rekordTag) { + $rekordTag = $insgesamt; + $rekordTagSameTime = $sameTime; + $rekordTagDate = Carbon::now()->subDays($key)->format('d.m.Y'); + } + if ($minCount === 0 || $insgesamt < $minCount) { + $minCount = $insgesamt; + } + $oldLogs[$key]['sameTime'] = number_format(floatval($sameTime), 0, ",", "."); + $oldLogs[$key]['insgesamt'] = number_format(floatval($insgesamt), 0, ",", "."); + # Nun noch den median: + $count += $insgesamt; + $size++; + if ($size > 0) { + $oldLogs[$key]['median'] = number_format(floatval(round($count / $size)), 0, ",", "."); + } + + } + + if ($request->input('out', 'web') === "web") { + return view('admin.count') + ->with('title', 'Suchanfragen - MetaGer') + ->with('today', number_format(floatval($logToday), 0, ",", ".")) + ->with('oldLogs', $oldLogs) + ->with('minCount', $minCount) + ->with('rekordCount', number_format(floatval($rekordTag), 0, ",", ".")) + ->with('rekordTagSameTime', number_format(floatval($rekordTagSameTime), 0, ",", ".")) + ->with('rekordDate', $rekordTagDate) + ->with('days', $days); + } else { + $result = ""; + foreach ($oldLogs as $key => $value) { + $resultTmp = '"' . date("D, d M y", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $key, date("Y"))) . '",'; + $resultTmp .= '"' . $value['sameTime'] . '",'; + $resultTmp .= '"' . $value['insgesamt'] . '",'; + $resultTmp .= '"' . $value['median'] . '"' . "\r\n"; + $result = $resultTmp . $result; + } + return response($result, 200) + ->header('Content-Type', 'text/csv') + ->header('Content-Disposition', 'attachment; filename="count.csv"'); + + } + + } + + public function countGraphToday() + { + $stats = $this->getStats(0)[0]; + + $hourly = []; + $previous = 0; + $max = 0; + foreach ($stats->time as $time => $timeStats) { + $hour = intval(substr($time, 0, strpos($time, ":"))); + if (empty($hourly[$hour])) { + $hourly[$hour] = 0; + } + $hourly[$hour] += $timeStats->all - $previous; + $previous = $timeStats->all; + if ($hourly[$hour] > $max) { + $max = $hourly[$hour]; + } + } + $result = [ + "insgesamt" => $stats->insgesamt->all, + "max" => $max, + "hourly" => $hourly, + ]; + + return response() + ->view('admin.countGraphToday', ["data" => $result], 200) + ->header('Content-Type', "image/svg+xml"); + + } + + public function engineStats() + { + $result = []; + $result["loadavg"] = sys_getloadavg(); + + // Memory Data + $data = explode("\n", trim(file_get_contents("/proc/meminfo"))); + $meminfo = []; + foreach ($data as $line) { + list($key, $val) = explode(":", $line); + $meminfo[$key] = trim($val); + } + $conversions = [ + "KB", + "MB", + "GB", + "TB", + ]; + + $memAvailable = $meminfo["MemAvailable"]; + $memAvailable = intval(explode(" ", $memAvailable)[0]); + $counter = 0; + while ($memAvailable > 1000) { + $memAvailable /= 1000.0; + $counter++; + } + $memAvailable = round($memAvailable); + $memAvailable .= " " . $conversions[$counter]; + + $result["memoryAvailable"] = $memAvailable; + + $resultCount = 0; + $file = "/var/log/metager/mg3.log"; + if (file_exists($file)) { + $fh = fopen($file, "r"); + try { + while (fgets($fh) !== false) { + $resultCount++; + } + } finally { + fclose($fh); + } + } + + $result["resultCount"] = number_format($resultCount, 0, ",", "."); + return response()->json($result); + } + + private function getStats($days) + { $maxDate = Carbon::createFromFormat('d.m.Y', "28.06.2016"); $selectedDate = Carbon::now()->subDays($days); if ($maxDate > $selectedDate) { @@ -164,78 +312,20 @@ class AdminInterface extends Controller } while ($removedOne === true); } - $oldLogs = []; - $rekordTag = 0; - $minCount = 0; - $rekordTagDate = ""; - $size = 0; - $count = 0; - - $now = Carbon::now()->subMinutes(Carbon::now()->minute % 5)->format('H:i'); - if ($now === "00:00") { - $now = "00:05"; - } + $result = []; foreach ($neededLogs as $key => $value) { $countFile = $value["countFile"]; if (file_exists($countFile)) { - $stats = json_decode(file_get_contents($countFile)); - if ($key === 0) { - // Log for today - $logToday = empty($stats->insgesamt->{$interface}) ? 0 : $stats->insgesamt->{$interface}; - if (\file_exists($today["countFile"])) { - unlink($today["countFile"]); - } - continue; - } - $insgesamt = empty($stats->insgesamt->{$interface}) ? 0 : $stats->insgesamt->{$interface}; - $sameTime = empty($stats->time->{$now}->{$interface}) ? 0 : $stats->time->{$now}->{$interface}; - - if ($insgesamt > $rekordTag) { - $rekordTag = $insgesamt; - $rekordTagSameTime = $sameTime; - $rekordTagDate = Carbon::now()->subDays($key)->format('d.m.Y'); - } - if ($minCount === 0 || $insgesamt < $minCount) { - $minCount = $insgesamt; - } - $oldLogs[$key]['sameTime'] = number_format(floatval($sameTime), 0, ",", "."); - $oldLogs[$key]['insgesamt'] = number_format(floatval($insgesamt), 0, ",", "."); - # Nun noch den median: - $count += $insgesamt; - $size++; - if ($size > 0) { - $oldLogs[$key]['median'] = number_format(floatval(round($count / $size)), 0, ",", "."); - } - + $result[$key] = json_decode(file_get_contents($countFile)); } } - if ($request->input('out', 'web') === "web") { - return view('admin.count') - ->with('title', 'Suchanfragen - MetaGer') - ->with('today', number_format(floatval($logToday), 0, ",", ".")) - ->with('oldLogs', $oldLogs) - ->with('minCount', $minCount) - ->with('rekordCount', number_format(floatval($rekordTag), 0, ",", ".")) - ->with('rekordTagSameTime', number_format(floatval($rekordTagSameTime), 0, ",", ".")) - ->with('rekordDate', $rekordTagDate) - ->with('days', $days); - } else { - $result = ""; - foreach ($oldLogs as $key => $value) { - $resultTmp = '"' . date("D, d M y", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $key, date("Y"))) . '",'; - $resultTmp .= '"' . $value['sameTime'] . '",'; - $resultTmp .= '"' . $value['insgesamt'] . '",'; - $resultTmp .= '"' . $value['median'] . '"' . "\r\n"; - $result = $resultTmp . $result; - } - return response($result, 200) - ->header('Content-Type', 'text/csv') - ->header('Content-Disposition', 'attachment; filename="count.csv"'); - + if (\file_exists($today["countFile"])) { + unlink($today["countFile"]); } + return $result; } public function check() diff --git a/app/Http/Middleware/RefererCheck.php b/app/Http/Middleware/RefererCheck.php deleted file mode 100644 index c4c6d2b865d63779644b25b5ef23555c4c400f3f..0000000000000000000000000000000000000000 --- a/app/Http/Middleware/RefererCheck.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php - -namespace App\Http\Middleware; - -use Closure; - -class RefererCheck -{ - /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed - */ - public function handle($request, Closure $next) - { - $refererCorrect = env('referer_check'); - $referer = $request->server('HTTP_REFERER'); - if ($refererCorrect !== $referer && "https://metager.de/admin/count" !== $referer) { - abort(401, 'Unauthorized'); - } else { - return $next($request); - } - } -} diff --git a/build.sh b/build.sh index e0d0a682c31d4d7418f34ff376726afc26246998..2f3ec74aaf0dd06ec631c16e1b3c5371d1ad9d67 100644 --- a/build.sh +++ b/build.sh @@ -15,6 +15,7 @@ scp -P 63824 metager@metager3.de:~/blacklistUrl.txt config/ scp -P 63824 metager@metager3.de:~/blacklistDomains.txt config/ scp -P 63824 metager@metager3.de:~/adBlacklistUrl.txt config/ scp -P 63824 metager@metager3.de:~/adBlacklistDomains.txt config/ +scp -P 63824 metager@metager3.de:~/UsersSeeder.php database/seeds/ touch storage/logs/laravel.log touch storage/logs/worker.log touch database/metager.sqlite @@ -23,4 +24,12 @@ chmod -R 777 storage chmod -R 777 bootstrap/cache npm install npm run production -php artisan migrate --force \ No newline at end of file +php artisan migrate --force +php artisan db:seed --force +php artisan requests:gather +if [ -f ~/MetaGer/artisan ]; then php ~/MetaGer/artisan down;fi +cd ~/ +while [ -d ~/MetaGer ]; do rm -rf ~/MetaGer;done +mv MetaGer_neu MetaGer +sudo pkill --signal SIGHUP supervisord +php ~/MetaGer/artisan up \ No newline at end of file diff --git a/database/migrations/2019_05_28_153442_create_users_table.php b/database/migrations/2019_05_28_153442_create_users_table.php new file mode 100644 index 0000000000000000000000000000000000000000..a91e1d3c4baed140741e1eb20305b2f13f6ee468 --- /dev/null +++ b/database/migrations/2019_05_28_153442_create_users_table.php @@ -0,0 +1,36 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +class CreateUsersTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('users', function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index e119db624aa8f894d74ef57283c4947e734015b0..379c31513d6f0877a70614fe9d504f5fd559642f 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder */ public function run() { - // $this->call(UsersTableSeeder::class); + $this->call(UsersSeeder::class); } } diff --git a/database/seeds/UsersSeeder.php b/database/seeds/UsersSeeder.php new file mode 100644 index 0000000000000000000000000000000000000000..2acbf13573deb625165c45f8475c756197a0e146 --- /dev/null +++ b/database/seeds/UsersSeeder.php @@ -0,0 +1,16 @@ +<?php + +use Illuminate\Database\Seeder; + +class UsersSeeder extends Seeder +{ + /** + * Run the database seeds. + * + * @return void + */ + public function run() + { + + } +} diff --git a/resources/views/admin/countGraphToday.blade.php b/resources/views/admin/countGraphToday.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..a2e20fc0d92a7ccecc95a23227e7d57ef1950456 --- /dev/null +++ b/resources/views/admin/countGraphToday.blade.php @@ -0,0 +1,41 @@ +<?xml version='1.0' encoding='utf-8'?> +@php ($width = 400) +@php ($height = 100) +@php ($padding = 3) +@php ($paddingArrow = 4) +@php ($startX = 20) +@php ($startY = $height-20) +@php ($entryCount = sizeof($data["hourly"])) +@php ($drawWidth = $width - $padding - $paddingArrow - $startX) +@php ($widthPerElement = $drawWidth / (sizeof($data["hourly"]) * 2)) +@php ($heightPerSearch = ($startY - $padding - $paddingArrow) / $data["max"]) +<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="auto" viewBox="0 0 400 100"> + <!-- Y-Achse --> + <path d="m{{$startX}} {{$startY}} L{{$startX}} {{$padding}} L{{$startX-1}} {{$padding}} L{{$startX}} {{$padding-1}} L{{$startX+1}} {{$padding}} L{{$startX}} {{$padding}}z" fill="#333" stroke="#333" stroke-width=".7" /> + <text x="{{ $startX + 4}}" y="{{ $padding + $paddingArrow - 2 }}" style="font-size: 6px;" fill="rgba(0,0,0,.5)"> + Y(Abfragen) + </text> + <!-- Skalierung Y-Achse --> + @for($i = 0; $i < 10; $i++) + <line x1="{{$startX-1}}" y1="{{ ($padding+$paddingArrow) + (($startY-($padding+$paddingArrow)) * ($i/10.0)) }}" x2="{{$startX+1}}" y2="{{ ($padding+$paddingArrow) + (($startY-($padding+$paddingArrow)) * ($i/10.0)) }}" fill="#333" stroke="#333" stroke-width=".7" /> + <line x1="{{$startX}}" y1="{{ ($padding+$paddingArrow) + (($startY-($padding+$paddingArrow)) * ($i/10.0)) }}" x2="{{$width-$padding-$paddingArrow}}" y2="{{ ($padding+$paddingArrow) + (($startY-($padding+$paddingArrow)) * ($i/10.0)) }}" fill="#777" stroke="#777" stroke-width=".2" /> + <text x="{{$startX - 15}}" y="{{ ($padding+$paddingArrow) + (($startY-($padding+$paddingArrow)) * ($i/10.0)) + 2 }}" style="font-size: 6px;"> + @php ($value = round($data["max"] - $i * ($data["max"] / 10.0))) + @if($value >= 1000) + @php($value = round($value / 100.0) / 10.0 . "k" ) + @endif + {{ $value }} + </text> + @endfor + @for($i = 0; $i < $entryCount; $i++) + <rect x="{{ $startX + ($widthPerElement * $i * 2) + $widthPerElement}}" y="{{ $startY - ($heightPerSearch * $data["hourly"][$i]) }}" width="{{ $widthPerElement }}" height="{{ ($heightPerSearch * $data["hourly"][$i]) }}" style="fill:rgb(255,127,0); stroke-width:0;" /> + <text x="{{ $startX + ($widthPerElement * 1.4) + (($width - $padding - $paddingArrow - $startX - ($widthPerElement * 2)) * ($i/($entryCount-1)))}}" y="{{$startY + 6}}" style="font-size: 6px;" > + {{ number_format($i, 0, ",", ".") }} + </text> + @endfor + <!-- X-Achse --> + <path d="m{{$startX}} {{$startY}} L{{$width-$padding}} {{$startY}} L{{$width-$padding}} {{$startY-1}} L{{$width-($padding-1)}} {{$startY}} L{{$width-$padding}} {{$startY+1}} L{{$width-$padding}} {{$startY}}z" fill="#333" stroke="#333" stroke-width=".5" /> + <text x="{{ $width - 4}}" y="{{ $startY + 3}}" style="font-size: 6px;" fill="rgba(0,0,0,.5)" writing-mode="tb"> + X(h) + </text> +</svg> diff --git a/routes/web.php b/routes/web.php index 64e68bd8a21069b9dee306d48ab12f5b0da70e92..1aceef359f6dcabafdfffeba8127be7069906f6e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -167,9 +167,11 @@ Route::group( ->with('request', $this->input('request', 'GET')); }); - Route::group(['middleware' => ['referer.check'], 'prefix' => 'admin'], function () { + Route::group(['middleware' => ['auth.basic'], 'prefix' => 'admin'], function () { Route::get('/', 'AdminInterface@index'); Route::match(['get', 'post'], 'count', 'AdminInterface@count'); + Route::get('count/graphtoday.svg', 'AdminInterface@countGraphToday'); + Route::get('engine/stats.json', 'AdminInterface@engineStats'); Route::get('check', 'AdminInterface@check'); Route::get('engines', 'AdminInterface@engines'); });