ConvertCountFile.php 3.75 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
34
<?php

namespace App\Jobs;

use Carbon;
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()
    {
35
36
37
38
39
        $result = [
            "insgesamt" => [
                "all" => 0,
            ],
        ];
40
41
        $fh = false;
        $fullRound = false;
42
        $error = false;
43
44
45
46
47
48
49
        try {
            $fh = fopen($this->files["logFile"], "r");
            $currentLogTime = Carbon::now()->hour(0)->minute(0)->second(0)->addMinutes(5);
            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
50
                if (preg_match('/(\d{2}:\d{2}:\d{2}).*?\sinterface=(\S+)/', $line, $matches)) {
51
                    // Create Date Object
Dominik Hebeler's avatar
Dominik Hebeler committed
52
                    $logTime = $matches[1];
53
54
55
56
                    $interface = $matches[2];
                } else {
                    continue;
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
57
58
                $thatTime = \DateTime::createFromFormat('H:i:s', $logTime);
                $thatTime->sub(new \DateInterval("PT" . ($thatTime->format('i') % 5) . "M"));
59

Dominik Hebeler's avatar
Dominik Hebeler committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                if(empty($result["time"][$thatTime->format('H:i')])){
                    $result["time"][$thatTime->format('H:i')] = [
                        "insgesamt" => [
                            "all" => 0,
                        ],
                    ];
                }
                if(empty($result["time"][$thatTime->format('H:i')]["all"])){
                    $result["time"][$thatTime->format('H:i')]["all"] = 1;
                }else{
                    $result["time"][$thatTime->format('H:i')]["all"]++;
                }
                if (!empty($interface)) {
                    if(empty($result["time"][$thatTime->format('H:i')][$interface])){
                        $result["time"][$thatTime->format('H:i')][$interface] = 1;
                    }else{
                        $result["time"][$thatTime->format('H:i')][$interface]++;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
                    }
                }
                // 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]++;
                    }
                }
            }
93
94
        } catch (\ErrorException $e) {
            $error = true;
95
96
97
98
        } finally {
            if ($fh !== false) {
                fclose($fh);
            }
99

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

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