AdminInterface.php 6.58 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
5
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Redis;
7
use Response;
Dominik Hebeler's avatar
Dominik Hebeler committed
8
9
10

class AdminInterface extends Controller
{
Dominik Hebeler's avatar
Dominik Hebeler committed
11
    public function index(Request $request)
Dominik Hebeler's avatar
Dominik Hebeler committed
12
    {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        $localFetcher = file_get_contents(action("AdminInterface@getFetcherStatus"));
        die(var_dump($localFetcher));
    }

    public function getFetcherStatus()
    {
        // Let's get the stats for this server.
        // First we need to check, which Fetcher could be available by parsing the sumas.xml
        $names = $this->getSearchEngineNames();

        // Now we gonna check which stats we can find
        $stati = array();
        foreach($names as $name){
            $stats = Redis::hgetall($name . ".stats");
            if(sizeof($stats) > 0){
                $fetcherStatus = Redis::get($name);
                $stati[$name]["status"] = $fetcherStatus;
                foreach($stats as $pid => $value){
                    if(strstr($value, ";")){
                        $value = explode(";", $value);
                        $connection = json_decode(base64_decode($value[0]), true);
                        foreach($connection as $key => $val){
                            if(strstr($key, "_time"))
                                $stati[$name]["fetcher"][$pid]["connection"][$key] = $val;
                        }
                        $stati[$name]["fetcher"][$pid]["poptime"] = $value[1];
                    }
Dominik Hebeler's avatar
Dominik Hebeler committed
40
                }
41
42
            }
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
43

44
        // So now we can generate Median Times for every Fetcher
Dominik Hebeler's avatar
Dominik Hebeler committed
45
        $fetcherCount = 0;
46
47
48
        foreach($stati as $engineName => $engineStats){
            $connection = array();
            $poptime = 0;
Dominik Hebeler's avatar
Dominik Hebeler committed
49
            $fetcherCount += sizeof($engineStats["fetcher"]);
50
51
52
53
54
55
56
57
58
            foreach($engineStats["fetcher"] as $pid => $stats){
                foreach($stats["connection"] as $key => $value){
                    if(!isset($connection[$key])){
                        $connection[$key] = $value;
                    }else{
                        $connection[$key] += $value;
                    }
                }
                $poptime += floatval($stats["poptime"]);
59
            }
60
61
62
63
64
65
66
            foreach($connection as $key => $value){
                $connection[$key] /= sizeof($engineStats["fetcher"]);
            }
            $poptime /= sizeof($engineStats["fetcher"]);

            $stati[$engineName]["median-connection"] = $connection;
            $stati[$engineName]["median-poptime"] = $poptime;
Dominik Hebeler's avatar
Dominik Hebeler committed
67
        }
68

Dominik Hebeler's avatar
Dominik Hebeler committed
69
        return view('admin.admin')
70
            ->with('title', 'Fetcher Status')
Dominik Hebeler's avatar
Dominik Hebeler committed
71
72
            ->with('stati', $stati)
            ->with('fetcherCount', $fetcherCount);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        $stati = json_encode($stati);
        $response = Response::make($stati, 200);
        $response->header("Content-Type", "application/json");
        return $response;
    }

    private function getSearchEngineNames(){
        $url = config_path() . "/sumas.xml";
        $xml = simplexml_load_file($url);
        $sumas = $xml->xpath("suma");

        $names = array();
        foreach($sumas as $suma){
            $names[] = $suma["name"]->__toString();
        }
        return $names;
Dominik Hebeler's avatar
Dominik Hebeler committed
89
    }
90
91
92

    public function count()
    {
93
        $logToday = "mg3.log";
Dominik Hebeler's avatar
Dominik Hebeler committed
94
95
96
        if (file_exists("/var/log/metager/" . $logToday)) {
            $logToday = file("/var/log/metager/" . $logToday);
        } else {
97
98
            return redirect('');
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
99
100
101
        $oldLogs       = [];
        $yesterday     = 0;
        $rekordTag     = 0;
102
        $rekordTagDate = "";
Dominik Hebeler's avatar
Dominik Hebeler committed
103
104
105
        $size          = 0;
        $count         = 0;
        for ($i = 1; $i <= 28; $i++) {
Dominik Hebeler's avatar
Dominik Hebeler committed
106
            $logDate = "/var/log/metager/archive/mg3.log.$i";
Dominik Hebeler's avatar
Dominik Hebeler committed
107
108
            if (file_exists($logDate)) {
                $sameTime  = exec("grep -n '" . date('H') . ":" . date('i') . ":' $logDate | tail -1 | cut -f1 -d':'");
109
                $insgesamt = exec("wc -l $logDate | cut -f1 -d' '");
Dominik Hebeler's avatar
Dominik Hebeler committed
110
111
                if ($insgesamt > $rekordTag) {
                    $rekordTag         = $insgesamt;
112
                    $rekordTagSameTime = $sameTime;
Dominik Hebeler's avatar
Dominik Hebeler committed
113
                    $rekordTagDate     = date("d.m.Y", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $i, date("Y")));
114
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
115
116
                $oldLogs[$i]['sameTime']  = number_format(floatval($sameTime), 0, ",", ".");
                $oldLogs[$i]['insgesamt'] = number_format(floatval($insgesamt), 0, ",", ".");
Dominik Hebeler's avatar
Dominik Hebeler committed
117
118
                # Nun noch den median:
                $count += $insgesamt;
119
                $size++;
Dominik Hebeler's avatar
Dominik Hebeler committed
120
121
122
123
                if ($size > 0) {
                    $oldLogs[$i]['median'] = number_format(floatval(round($count / $size)), 0, ",", ".");
                }

124
            }
125
126
127
128
129
130
131
132
        }

        return view('admin.count')
            ->with('title', 'Suchanfragen - MetaGer')
            ->with('today', number_format(floatval(sizeof($logToday)), 0, ",", "."))
            ->with('oldLogs', $oldLogs)
            ->with('rekordCount', number_format(floatval($rekordTag), 0, ",", "."))
            ->with('rekordTagSameTime', number_format(floatval($rekordTagSameTime), 0, ",", "."))
Dominik Hebeler's avatar
Dominik Hebeler committed
133
            ->with('rekordDate', $rekordTagDate);
134
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
135
    public function check()
136
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
137
        $q     = "";
138
        $redis = Redis::connection('redisLogs');
Dominik Hebeler's avatar
Dominik Hebeler committed
139
        if ($redis) {
140
            $q = $redis->lrange("logs.search", -1, -1)[0];
Dominik Hebeler's avatar
Dominik Hebeler committed
141
            $q = substr($q, strpos($q, "search=") + 7);
142
143
144
145
146
        }
        return view('admin.check')
            ->with('title', 'Wer sucht was? - MetaGer')
            ->with('q', $q);
    }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

    public function engines()
    {
        # Wir laden den Inhalt der Log Datei und übergeben ihn an den view
        $file = "/var/log/metager/engine.log";
        if (file_exists($file) && is_readable($file)) {
            $engineStats = file_get_contents($file);
            # Daten vom JSON Format dekodieren
            $engineStats = json_decode($engineStats, true);

            # Eine Sortierung wäre nicht das verkehrteste
            uasort($engineStats["recent"], function ($a, $b) {
                if ($a["requests"] == $b["requests"]) {
                    return 0;
                }

                return ($a["requests"] < $b["requests"]) ? 1 : -1;
            });

            uasort($engineStats["overall"], function ($a, $b) {
                if ($a["requests"] == $b["requests"]) {
                    return 0;
                }

                return ($a["requests"] < $b["requests"]) ? 1 : -1;
            });
            return view('admin.engines')
                ->with('engineStats', $engineStats)
                ->with('title', "Suchmaschinenstatus - MetaGer");
        } else {
            return redirect(url('admin'));
        }
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
180
}