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

DatabaseStore::getRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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
9
class DatabaseStore implements StatisticsStore
10
{
11
    /**
12
     * The model that will interact with the database.
13
     *
14
     * @var string
15
     */
16
    protected static $model = \BeyondCode\LaravelWebSockets\Models\WebSocketsStatisticsEntry::class;
17
18
    /**
19
     * Store a new record in the database and return
20
     * the created instance.
21
     *
22
     * @param  array  $data
23
     * @return mixed
24
     */
25
    public static function store(array $data)
26
    {
27
        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...
28
    }
29
30
    /**
31
     * Delete records older than the given moment,
32
     * for a specific app id (if given), returning
33
     * the amount of deleted records.
34
     *
35
     * @param  \Carbon\Carbon  $moment
36
     * @param  string|int|null  $appId
37
     * @return int
38
     */
39
    public static function delete(Carbon $moment, $appId = null): int
40
    {
41
        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...
42
            ->when(! is_null($appId), function ($query) use ($appId) {
43
                return $query->whereAppId($appId);
44
            })
45
            ->delete();
46
    }
47
48
    /**
49
     * Get the query result as eloquent collection.
50
     *
51
     * @param  callable  $processQuery
52
     * @return \Illuminate\Support\Collection
53
     */
54
    public function getRawRecords(callable $processQuery = null)
55
    {
56
        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...
57
            ->when(! is_null($processQuery), function ($query) use ($processQuery) {
58
                return call_user_func($processQuery, $query);
59
            }, function ($query) {
60
                return $query->latest()->limit(120);
61
            })->get();
62
    }
63
64
    /**
65
     * Get the results for a specific query.
66
     *
67
     * @param  callable  $processQuery
68
     * @param  callable  $processCollection
69
     * @return array
70
     */
71
    public function getRecords(callable $processQuery = null, callable $processCollection = null): array
72
    {
73
        return $this->getRawRecords($processQuery)
74
            ->when(! is_null($processCollection), function ($collection) use ($processCollection) {
75
                return call_user_func($processCollection, $collection);
76
            })
77
            ->map(function (Model $statistic) {
78
                return [
79
                    'timestamp' => (string) $statistic->created_at,
80
                    'peak_connections_count' => $statistic->peak_connections_count,
81
                    'websocket_messages_count' => $statistic->websocket_messages_count,
82
                    'api_messages_count' => $statistic->api_messages_count,
83
                ];
84
            })
85
            ->toArray();
86
    }
87
88
    /**
89
     * Get the results for a specific query into a
90
     * format that is easily to read for graphs.
91
     *
92
     * @param  callable  $processQuery
93
     * @return array
94
     */
95
    public function getForGraph(callable $processQuery = null): array
96
    {
97
        $statistics = collect(
98
            $this->getRecords($processQuery)
99
        );
100
101
        return [
102
            'peak_connections' => [
103
                'x' => $statistics->pluck('timestamp')->toArray(),
104
                'y' => $statistics->pluck('peak_connections_count')->toArray(),
105
            ],
106
            'websocket_messages_count' => [
107
                'x' => $statistics->pluck('timestamp')->toArray(),
108
                'y' => $statistics->pluck('websocket_messages_count')->toArray(),
109
            ],
110
            'api_messages_count' => [
111
                'x' => $statistics->pluck('timestamp')->toArray(),
112
                'y' => $statistics->pluck('api_messages_count')->toArray(),
113
            ],
114
        ];
115
    }
116
}
117