ConvertCountFile.php 3.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;

class ConvertCountFile implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $files;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($files)
    {
        $this->files = $files;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
34
35
36
37
38
        $result = [
            "insgesamt" => [
                "all" => 0,
            ],
        ];
39
40
        $fh = false;
        $fullRound = false;
41
        $error = false;
42
43
44
45
46
47
        try {
            $fh = fopen($this->files["logFile"], "r");
            while ($fh !== false && ($line = fgets($fh)) !== false) {
                $logTime = [];
                $interface = "";
                // i.e. [Wed Apr 17 00:00:01] ref=https://metager.de/ time=0.51 serv=web interface=de
48
                if (preg_match('/(\d{2}):(\d{2}):\d{2}.*?\sinterface=(\S+)/', $line, $matches)) {
49
                    // Create Date Object
Dominik Hebeler's avatar
Dominik Hebeler committed
50
                    $logTime = $matches[1];
51
52
53
54
55
56
57
                    $minutes = intval($matches[2]);
                    $minutes = $minutes - ($minutes % 5);
                    if($minutes < 10){
                        $minutes = "0" . $minutes;
                    }
                    $logTime .= ":" . $minutes;
                    $interface = $matches[3];
58
59
60
61
                } else {
                    continue;
                }

62
63
                if (empty($result["time"][$logTime])) {
                    $result["time"][$logTime] = [
Dominik Hebeler's avatar
Dominik Hebeler committed
64
65
66
67
68
                        "insgesamt" => [
                            "all" => 0,
                        ],
                    ];
                }
69
70
                if (empty($result["time"][$logTime]["all"])) {
                    $result["time"][$logTime]["all"] = 1;
Dominik Hebeler's avatar
Dominik Hebeler committed
71
                } else {
72
                    $result["time"][$logTime]["all"]++;
Dominik Hebeler's avatar
Dominik Hebeler committed
73
74
                }
                if (!empty($interface)) {
75
76
                    if (empty($result["time"][$logTime][$interface])) {
                        $result["time"][$logTime][$interface] = 1;
Dominik Hebeler's avatar
Dominik Hebeler committed
77
                    } else {
78
                        $result["time"][$logTime][$interface]++;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                    }
                }
                // Update the total statistics
                if (empty($result["insgesamt"]["all"])) {
                    $result["insgesamt"]["all"] = 1;
                } else {
                    $result["insgesamt"]["all"]++;
                }
                if (!empty($interface)) {
                    if (empty($result["insgesamt"][$interface])) {
                        $result["insgesamt"][$interface] = 1;
                    } else {
                        $result["insgesamt"][$interface]++;
                    }
                }
            }
95
96
        } catch (\ErrorException $e) {
            $error = true;
97
98
99
100
        } finally {
            if ($fh !== false) {
                fclose($fh);
            }
101

102
103
104
105
            $oldUmask = umask(0);
            // Write the result to a File
            if (!file_exists($this->files["countPath"])) {
                mkdir($this->files["countPath"], 0777, true);
106
            }
107
108
            file_put_contents($this->files["countFile"], json_encode($result, JSON_PRETTY_PRINT));
            umask($oldUmask);
109
110
111
112
113

            Redis::del(md5($this->files["countFile"]));
        }
    }
}