Completed
Pull Request — master (#447)
by Alexandru
01:38
created

DatabaseStore::getForGraph()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics\Stores;
4
5
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
10
class DatabaseStore implements StatisticsStore
11
{
12
    /**
13
     * The model that will interact with the database.
14
     *
15
     * @var string
16
     */
17
    public static $model = \BeyondCode\LaravelWebSockets\Models\WebSocketsStatisticsEntry::class;
18
19
    /**
20
     * Store a new record in the database and return
21
     * the created instance.
22
     *
23
     * @param  array  $data
24
     * @return mixed
25
     */
26
    public static function store(array $data)
27
    {
28
        return static::$model::create($data);
0 ignored issues
show
Bug introduced by
The method create cannot be called on static::$model (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
29
    }
30
31
    /**
32
     * Delete records older than the given moment,
33
     * for a specific app id (if given), returning
34
     * the amount of deleted records.
35
     *
36
     * @param  \Carbon\Carbon  $moment
37
     * @param  string|int|null  $appId
38
     * @return int
39
     */
40
    public static function delete(Carbon $moment, $appId = null): int
41
    {
42
        return static::$model::where('created_at', '<', $moment->toDateTimeString())
0 ignored issues
show
Bug introduced by
The method where cannot be called on static::$model (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
43
            ->when(! is_null($appId), function ($query) use ($appId) {
44
                return $query->whereAppId($appId);
45
            })
46
            ->delete();
47
    }
48
49
    /**
50
     * Get the query result as eloquent collection.
51
     *
52
     * @param  callable  $processQuery
53
     * @return \Illuminate\Support\Collection
54
     */
55
    public function getRawRecords(callable $processQuery = null)
56
    {
57
        return static::$model::query()
0 ignored issues
show
Bug introduced by
The method query cannot be called on static::$model (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
            ->when(! is_null($processQuery), function ($query) use ($processQuery) {
59
                return call_user_func($processQuery, $query);
60
            }, function ($query) {
61
                return $query->latest()->limit(120);
62
            })->get();
63
    }
64
65
    /**
66
     * Get the results for a specific query.
67
     *
68
     * @param  callable  $processQuery
69
     * @param  callable  $processCollection
70
     * @return array
71
     */
72
    public function getRecords(callable $processQuery = null, callable $processCollection = null): array
73
    {
74
        return $this->getRawRecords($processQuery)
75
            ->when(! is_null($processCollection), function ($collection) use ($processCollection) {
76
                return call_user_func($processCollection, $collection);
77
            })
78
            ->map(function (Model $statistic) {
79
                return $this->statisticToArray($statistic);
80
            })
81
            ->toArray();
82
    }
83
84
    /**
85
     * Get the results for a specific query into a
86
     * format that is easily to read for graphs.
87
     *
88
     * @param  callable  $processQuery
89
     * @param  callable  $processCollection
90
     * @return array
91
     */
92
    public function getForGraph(callable $processQuery = null, callable $processCollection = null): array
93
    {
94
        $statistics = collect(
95
            $this->getRecords($processQuery, $processCollection)
96
        );
97
98
        return $this->statisticsToGraph($statistics);
99
    }
100
101
    /**
102
     * Turn the statistic model to an array.
103
     *
104
     * @param  \Illuminate\Database\Eloquent\Model  $statistic
105
     * @return array
106
     */
107
    protected function statisticToArray(Model $statistic): array
108
    {
109
        return [
110
            'timestamp' => (string) $statistic->created_at,
111
            'peak_connections_count' => $statistic->peak_connections_count,
112
            'websocket_messages_count' => $statistic->websocket_messages_count,
113
            'api_messages_count' => $statistic->api_messages_count,
114
        ];
115
    }
116
117
    /**
118
     * Turn the statistics collection to an array used for graph.
119
     *
120
     * @param  \Illuminate\Support\Collection  $statistics
121
     * @return array
122
     */
123
    protected function statisticsToGraph(Collection $statistics): array
124
    {
125
        return [
126
            'peak_connections' => [
127
                'x' => $statistics->pluck('timestamp')->toArray(),
128
                'y' => $statistics->pluck('peak_connections_count')->toArray(),
129
            ],
130
            'websocket_messages_count' => [
131
                'x' => $statistics->pluck('timestamp')->toArray(),
132
                'y' => $statistics->pluck('websocket_messages_count')->toArray(),
133
            ],
134
            'api_messages_count' => [
135
                'x' => $statistics->pluck('timestamp')->toArray(),
136
                'y' => $statistics->pluck('api_messages_count')->toArray(),
137
            ],
138
        ];
139
    }
140
}
141