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

Commit 1f0a153

Browse files
committed
Charting
1 parent cdf229b commit 1f0a153

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

resources/views/dashboard.blade.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
<title>WebSockets Dashboard</title>
55
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"
66
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
7-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/epoch/0.8.4/css/epoch.min.css" />
87
<script
98
src="https://code.jquery.com/jquery-3.3.1.min.js"
109
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
1110
crossorigin="anonymous"></script>
1211
<script src="https://js.pusher.com/4.3/pusher.min.js"></script>
1312
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
14-
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
15-
<script src="https://cdnjs.cloudflare.com/ajax/libs/epoch/0.8.4/js/epoch.min.js"></script>
13+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
1614
</head>
1715

1816
<body>
1917
<div class="container" id="app">
2018
<div class="card col-xs-12 mt-4">
2119
<div class="card-header">
2220
<form id="connect" class="form-inline" role="form">
23-
<label class="my-1 mr-2" for="app">app:</label>
21+
<label class="my-1 mr-2" for="app">App:</label>
2422
<select class="form-control form-control-sm mr-2" name="app" id="app" v-model="app">
2523
<option v-for="app in apps" :value="app">@{{ app.name }}</option>
2624
</select>
@@ -36,8 +34,9 @@
3634
<div id="status"></div>
3735
</div>
3836
<div class="card-body">
39-
<div v-if="connected" id="statisticsChart" style="width: 100%; height: 250px;">
40-
37+
<div v-if="connected && app.statisticsEnabled">
38+
<h4>Peak Connections</h4>
39+
<div id="statisticsChart" style="width: 100%; height: 250px;"></div>
4140
</div>
4241
<div v-if="connected">
4342
<h4>Event Creator</h4>
@@ -148,16 +147,12 @@
148147
149148
loadChart() {
150149
$.getJSON('/{{ request()->path() }}/api/'+this.app.id+'/statistics', (data) => {
151-
this.chart = $('#statisticsChart').epoch({
152-
type: 'time.line',
153-
axes: ['left', 'right', 'bottom'],
154-
data: [
155-
{
156-
label: "Peak Connections",
157-
values: data.peak_connections,
158-
}
159-
]
160-
});
150+
this.chart = Plotly.plot('statisticsChart', [{
151+
x: data.peak_connections.x,
152+
y: data.peak_connections.y,
153+
mode: 'lines',
154+
line: {color: '#80CAF6'}
155+
}]);
161156
});
162157
},
163158
@@ -183,10 +178,12 @@
183178
subscribeToStatistics() {
184179
this.pusher.subscribe('{{ \BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger::LOG_CHANNEL_PREFIX }}statistics')
185180
.bind('statistics-updated', (data) => {
186-
this.chart.push([{
187-
time: data.time,
188-
y: data.peak_connection_count
189-
}]);
181+
var update = {
182+
x: [[data.time]],
183+
y: [[data.peak_connection_count]]
184+
};
185+
186+
Plotly.extendTraces('statisticsChart', update, [0]);
190187
});
191188
},
192189

src/Dashboard/Http/Controllers/DashboardApiController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ public function getStatistics($appId)
1111

1212
$peakConnections = $statistics->map(function ($statistic) {
1313
return [
14-
'time' => $statistic->created_at->timestamp,
15-
'y' => $statistic->peak_connection_count
14+
'timestamp' => (string)$statistic->created_at,
15+
'count' => $statistic->peak_connection_count
1616
];
17-
})->reverse()->values();
17+
})->reverse();
1818

1919
return [
20-
'peak_connections' => $peakConnections
20+
'peak_connections' => [
21+
'x' => $peakConnections->pluck('timestamp'),
22+
'y' => $peakConnections->pluck('count'),
23+
]
2124
];
2225
}
2326
}

src/Statistics/Events/StatisticsUpdated.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class StatisticsUpdated implements ShouldBroadcast
1111
{
1212
use SerializesModels;
1313

14-
protected $statisticModel;
14+
public $statisticModel;
1515

1616
public function __construct($statisticModel)
1717
{
@@ -21,11 +21,11 @@ public function __construct($statisticModel)
2121
public function broadcastWith()
2222
{
2323
return [
24-
'time' => $this->statisticModel->created_at->timestamp,
25-
'app_id' => $this->statisticModel->appId,
26-
'peak_connection_count' => $this->statisticModel->peakConnectionCount,
27-
'websocket_message_count' => $this->statisticModel->webSocketMessageCount,
28-
'api_message_count' => $this->statisticModel->apiMessageCount,
24+
'time' => (string)$this->statisticModel->created_at,
25+
'app_id' => $this->statisticModel->app_id,
26+
'peak_connection_count' => $this->statisticModel->peak_connection_count,
27+
'websocket_message_count' => $this->statisticModel->websocket_message_count,
28+
'api_message_count' => $this->statisticModel->api_message_count,
2929
];
3030
}
3131

0 commit comments

Comments
 (0)