-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathFindRuleStatsController.php
More file actions
130 lines (101 loc) · 3.5 KB
/
FindRuleStatsController.php
File metadata and controls
130 lines (101 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace App\Controller\Stats;
use App\Enum\FindRuleQuery;
use App\Utils\Arrays;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Controller;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
/**
* @see \App\Livewire\FindRuleComponent
*/
final class FindRuleStatsController extends Controller
{
public function __invoke(): View
{
$searchLogFilePath = storage_path('logs/search.json');
$searchRecords = $this->loadFileToJsonItems($searchLogFilePath);
$queriesToCount = $this->filterQueriesToCount($searchRecords);
$rulesToCount = $this->filterRulesToCount($searchRecords);
$sets = $this->getArrayFlattenKey($searchRecords, 'set');
$setsToCount = Arrays::groupToCount($sets, 4);
$setsToCount = array_slice($setsToCount, 0, 10);
// day by day stats
$timestamps = $this->getArrayFlattenKey($searchRecords, 'timestamp');
$dates = [];
foreach ($timestamps as $timestamp) {
$dates[] = Carbon::make($timestamp)->format('Y-m-d');
}
$datesToCount = Arrays::groupToCount($dates);
ksort($datesToCount);
return view('stats.find_rule_stats', [
'queriesToCount' => $queriesToCount,
'setsToCount' => $setsToCount,
// counts
'rulesToCount' => $rulesToCount,
'datesToCount' => $datesToCount,
]);
}
/**
* @param mixed[][] $items
* @return mixed[]
*/
private function getArrayFlattenKey(array $items, string $keyName): array
{
$items = array_map(fn (array $item) => $item[$keyName], $items);
// remove empty ones
return array_filter($items);
}
/**
* @return mixed[]
*/
private function loadFileToJsonItems(string $filePath): array
{
$fileContents = FileSystem::read($filePath);
$fileLines = explode(PHP_EOL, $fileContents);
$items = [];
foreach ($fileLines as $fileLine) {
// skip empty
if (trim($fileLine) === '') {
continue;
}
$items[] = Json::decode($fileLine, true);
}
return $items;
}
/**
* @param mixed[] $searchRecords
* @return array<string, int>
*/
private function filterQueriesToCount(array $searchRecords): array
{
$queries = $this->getArrayFlattenKey($searchRecords, 'query');
// remove prepared queries
$queries = array_diff($queries, FindRuleQuery::EXAMPLES);
// lowercase to standardize similar searches
$queries = array_map('strtolower', $queries);
$queriesToCount = Arrays::groupToCount($queries, 1);
// remove super short queries
return array_filter($queriesToCount, function (string $query): bool {
// skip rector rules
if (str_ends_with($query, 'rector')) {
return false;
}
return strlen($query) > 6;
}, ARRAY_FILTER_USE_KEY);
}
/**
* @param mixed[] $searchRecords
* @return array<string, int>
*/
private function filterRulesToCount(array $searchRecords): array
{
$rules = $this->getArrayFlattenKey($searchRecords, 'query');
// keep only items with "Rector" suffix
$rules = array_filter($rules, fn (string $rule): bool => str_ends_with($rule, 'Rector'));
$rulesToCount = Arrays::groupToCount($rules, 6);
return array_slice($rulesToCount, 0, 10);
}
}