Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

[2.x] Custom statistics drivers #473

Merged
merged 2 commits into from
Aug 18, 2020
Merged

Conversation

rennokki
Copy link
Collaborator

Currently, you can use only the database (through the model) for the statistics storage. This can be quite unpleasant as MySQL is not so good as a time-series database when it comes to huge data ingestion. You might want to save them somewhere else, like Elasticsearch or InfluxDB.

To fix this, you may now define your own statistics driver that can store/delete data with any kind of database, leaving this customization to the user implementation. You can ingest the record and use it to build the statistic payload, or you can statically create new records or delete records.

For example, this is how the database driver looks:

class DatabaseDriver implements StatisticsDriver
{
    /**
     * The model that controls the database table.
     *
     * @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null
     */
    protected $record;

    /**
     * Initialize the driver.
     *
     * @param  \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry|null  $record
     * @return void
     */
    public function __construct($record = null)
    {
        $this->record = $record;
    }

    /**
     * Get the app ID for the stats.
     *
     * @return mixed
     */
    public function getAppId()
    {
        return $this->record->app_id;
    }

    /**
     * Get the time value. Should be Y-m-d H:i:s.
     *
     * @return string
     */
    public function getTime(): string
    {
        return Carbon::parse($this->record->created_at)->toDateTimeString();
    }

    /**
     * Get the peak connection count for the time.
     *
     * @return int
     */
    public function getPeakConnectionCount(): int
    {
        return $this->record->peak_connection_count ?? 0;
    }

    /**
     * Get the websocket messages count for the time.
     *
     * @return int
     */
    public function getWebsocketMessageCount(): int
    {
        return $this->record->websocket_message_count ?? 0;
    }

    /**
     * Get the API message count for the time.
     *
     * @return int
     */
    public function getApiMessageCount(): int
    {
        return $this->record->api_message_count ?? 0;
    }

    /**
     * Create a new statistic in the store.
     *
     * @param  array  $data
     * @return \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
     */
    public static function create(array $data): StatisticsDriver
    {
        $class = config('websockets.statistics.database.model');

        return new static($class::create($data));
    }

    /**
     * Delete statistics from the store,
     * optionally by app id, returning
     * the number of  deleted records.
     *
     * @param  mixed  $appId
     * @return int
     */
    public static function delete($appId = null): int
    {
        $cutOffDate = Carbon::now()->subDay(
            config('websockets.statistics.delete_statistics_older_than_days')
        )->format('Y-m-d H:i:s');

        $class = config('websockets.statistics.database.model');

        return $class::where('created_at', '<', $cutOffDate)
            ->when($appId, function ($query) use ($appId) {
                return $query->whereAppId($appId);
            })
            ->delete();
    }
}

@rennokki rennokki merged commit a2fc559 into 2.x Aug 18, 2020
@rennokki rennokki deleted the refactor/custom-statistics-drivers branch August 18, 2020 18:39
@rennokki rennokki mentioned this pull request Aug 18, 2020
14 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant