Skip to content

Commit

Permalink
Merge pull request #37 from socialblue/develop
Browse files Browse the repository at this point in the history
Adds logged query time
  • Loading branch information
mbroersen committed Mar 8, 2020
2 parents 1c3eca0 + 358b395 commit 176ce42
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/app.js": "/app.js?id=e0e03c01ce2a282ed513",
"/app.js": "/app.js?id=641f16a57a4c82b06165",
"/app.css": "/app.css?id=a45caf0b385ad40f02c2"
}
26 changes: 26 additions & 0 deletions resources/assets/js/components/query-statistics.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
<p class="title">{{routes}}</p>
</div>
</div>
<div class="level-item has-text-centered">
<div>
<p class="heading">Total Query time</p>
<p class="title">{{queryTime | round}} ms</p>
</div>
</div>
<div class="level-item has-text-centered">
<div>
<p class="heading">AVG Query time</p>
<p class="title">{{queryTime / queries | round}} ms</p>
</div>
</div>
</nav>
</template>

Expand All @@ -24,11 +36,25 @@
return 0;
}
},
routes: {
type: Number,
default() {
return 0;
}
},
queryTime: {
type: Number,
default() {
return 0;
}
}
},
filters: {
round(val) {
return Math.round(val, 2);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion resources/assets/js/view/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div>
<page-header />

<query-statistics :queries="amountOfQueries" :routes="amountOfRoutes"></query-statistics>
<query-statistics :queries="amountOfQueries" :routes="amountOfRoutes" :query-time="totalQueryTime"></query-statistics>
<nav class="panel is-primary">

<div class="panel-heading">
Expand Down Expand Up @@ -84,6 +84,16 @@
return this.cachedKeys;
},
totalQueryTime() {
return Object.values(this.cachedKeys).flat().reduce((total, time, index) => {
if (index === 1) {
total = total.queryTime;
}
return total + time.queryTime;
});
},
amountOfQueries() {
return Object.values(this.cachedKeys).flat().length;
},
Expand Down
58 changes: 39 additions & 19 deletions src/DataListener/QueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,13 @@ public static function listen(QueryExecuted $query) {
$time = time();
$referer = request()->headers->get('referer');

if (empty($url)) {
$url = '';
}

$data = Cache::get(config('laravel-query-adviser.cache.key'), []);
if (!is_array($data)) {
$data = [];
}

if (!isset($data[$time])) {
$data[$time] = [];
}
$data = self::getFromCache($time);

$possibleTraces = self::formatPossibleTraces(self::getPossibleTraces());

$data = self::formatData($query, $data, $time, $possibleTraces, $url, $referer);

if (count($data) > config('laravel-query-adviser.cache.max_entries')) {
array_shift($data);
}

Cache::put(config('laravel-query-adviser.cache.key'), $data, config('laravel-query-adviser.cache.ttl'));
self::putToCache(
self::formatData($query, $data, $time, $possibleTraces, $url, $referer)
);
}

/**
Expand Down Expand Up @@ -92,6 +77,10 @@ protected static function formatPossibleTraces($possibleTraces)
*/
protected static function formatData(QueryExecuted $query, array $data, int $time, $possibleTraces, string $url, ?string $referer): array
{
if (empty($url)) {
$url = '';
}

$key = count($data[$time]);

$data[$time][$key] = [
Expand All @@ -107,4 +96,35 @@ protected static function formatData(QueryExecuted $query, array $data, int $tim
];
return $data;
}

/**
* @param array $data
* @return array
*/
protected static function putToCache(array $data): array
{
if (count($data) > config('laravel-query-adviser.cache.max_entries')) {
array_shift($data);
}

Cache::put(config('laravel-query-adviser.cache.key'), $data, config('laravel-query-adviser.cache.ttl'));
return $data;
}

/**
* @param int $time
* @return array|mixed
*/
protected static function getFromCache(int $time)
{
$data = Cache::get(config('laravel-query-adviser.cache.key'), []);
if (!is_array($data)) {
$data = [];
}

if (!isset($data[$time])) {
$data[$time] = [];
}
return $data;
}
}

0 comments on commit 176ce42

Please sign in to comment.