MonthlyRequestsGather.php 3.77 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace App\Console\Commands;

use DB;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class MonthlyRequestsGather extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'requests:gather';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends the gathered monthly requests from the redis to the central Database';

    private $values = [];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        # Read in the suma Files
        $sumaFile = config_path('sumas.json');
        $sumaEnFile = config_path('sumasEn.json');
        if (file_exists($sumaFile) && file_exists($sumaEnFile)) {
            $sumas = json_decode(file_get_contents($sumaFile));
            $sumasEn = json_decode(file_get_contents($sumaEnFile));

            $this->gatherLogs($sumas);
            $this->gatherLogs($sumasEn);

            foreach ($this->values as $name => $value) {
                $entry = DB::table('monthlyrequests')->where(['name' => $name])->first();
                $newCount = $value;
                if ($entry === null) {
                    DB::table('monthlyrequests')->insert(['name' => $name, 'count' => $newCount]);
                } else {
                    $newCount = $value + $entry->count;
                    DB::table('monthlyrequests')->where(['name' => $name])->update(['count' => $newCount]);
                }
            }

            $this->disableOverusedEngines($sumaFile, $sumas);
            $this->disableOverusedEngines($sumaEnFile, $sumasEn);

        }
    }

    private function disableOverusedEngines($sumasFile, $sumas)
    {
        $currentValues = DB::table('monthlyrequests')->get();
        foreach ($sumas->sumas as $sumaName => $suma) {
            if (!empty($suma->disabled) && $suma->disabled === true) {
                continue;
            }

            if (empty($suma->{"monthly-requests"})) {
                continue;
            }
            $currentValue = 0;
            foreach ($currentValues as $value) {
                if ($value->name === $sumaName) {
                    $currentValue = $value->count;
                }
            }

            $monthlyRequests = $suma->{"monthly-requests"};

            if (!empty($suma->{"auto-disabled"}) && $suma->{"auto-disabled"} === true) {
                # Maybe this engine should be enabled again
                if ($currentValue < $monthlyRequests) {
                    unset($sumas->sumas->{$sumaName}->{"auto-disabled"});
                    file_put_contents($sumasFile, json_encode($sumas, JSON_PRETTY_PRINT));
                }
            } else if ($currentValue >= $monthlyRequests) {
                $sumas->sumas->{$sumaName}->{"auto-disabled"} = true;
                file_put_contents($sumasFile, json_encode($sumas, JSON_PRETTY_PRINT));
            }
        }
    }

    private function gatherLogs($sumas)
    {
        foreach ($sumas->sumas as $sumaName => $suma) {
            if (!empty($suma->{"monthly-requests"})) {
                $monthlyLimit = $suma->{"monthly-requests"};
                $currentValue = Redis::get('monthlyRequests:' . $sumaName);
                Redis::del('monthlyRequests:' . $sumaName);
                if (empty($currentValue)) {
                    $currentValue = 0;
                }
                if (empty($this->values[$sumaName]) && $currentValue > 0) {
                    $this->values[$sumaName] = intval($currentValue);
                }
            }

        }
    }
}