-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathIndexedSearchEnable.php
More file actions
218 lines (187 loc) · 6.48 KB
/
IndexedSearchEnable.php
File metadata and controls
218 lines (187 loc) · 6.48 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace ProcessMaker\Console\Commands;
use App;
use Illuminate\Console\Command;
use Log;
use ProcessMaker\Managers\IndexManager;
use ProcessMaker\Models\Setting;
use ProcessMaker\Traits\SupportsNonInteraction;
use Storage;
class IndexedSearchEnable extends Command
{
use SupportsNonInteraction;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '
indexed-search:enable
{--driver= : Choose a search driver (either Elasticsearch or SQLite)}
{--url= : If Elasticsearch, the URL to the server (including protocol and port number)}
{--prefix= : The prefix of your search indices (needed if using one Elasticache instance for multiple ProcessMaker 4 instances)}
';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Enable faster searches';
private $manager;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->manager = App::make(IndexManager::class);
$this->setCommandDescription();
parent::__construct();
}
private function setCommandDescription()
{
$items = $this->manager->list()->pluck('name')->implode(', ');
return $this->description = "{$this->description} of {$items}";
}
private function addToIndex($items)
{
try {
$items->searchable();
} catch (\PDOException $e) {
Log::error('Encountered database lock when indexing records, trying again');
sleep(1);
$this->addToIndex($items);
}
}
private function indexRecords()
{
foreach ($this->manager->list() as $index) {
$this->line("\nIndexing {$index->name}...");
if ($index->callback && is_callable($index->callback)) {
call_user_func($index->callback);
$this->info("All {$index->name} records have been imported.");
} else {
if ($index->model == 'ProcessMaker\Models\ProcessRequestToken') {
$query = $index->model::whereIn('element_type', ['task', 'userTask']);
} else {
$query = $index->model::query();
}
$bar = $this->output->createProgressBar($query->count());
$bar->start();
$query->chunk(5, function ($items) use (&$bar, &$count) {
$this->addToIndex($items);
foreach ($items as $item) {
$bar->advance();
}
});
$bar->finish();
$this->info("\nAll {$index->name} records have been imported.");
}
}
}
private function setConfig($driver, $url = null, $prefix = null)
{
config(['filesystems.disks.install' => [
'driver' => 'local',
'root' => base_path(),
]]);
if (Storage::disk('install')->exists('.env')) {
$env = Storage::disk('install')->get('.env');
} else {
$env = '';
}
$driver = mb_strtolower($driver);
$prefix = mb_strtolower(preg_replace('/[^A-Za-z0-9]/', '', $prefix));
if (empty($prefix)) {
$prefix = null;
} else {
$prefix .= '_';
}
switch ($driver) {
case 'elasticsearch':
$driver = 'elastic';
$env .= "\n\nSCOUT_DRIVER={$driver}";
$env .= "\nELASTIC_HOST={$url}";
break;
case 'sqlite':
$driver = 'tntsearch';
$env .= "\n\nSCOUT_DRIVER={$driver}";
break;
}
if ($prefix) {
$env .= "\nSCOUT_PREFIX={$prefix}";
}
Storage::disk('install')->put('.env', $env);
config([
'scout.driver' => $driver,
'scout.prefix' => $prefix,
'scout.queue' => false,
'elastic.client.hosts' => [$url],
]);
refresh_artisan_caches();
}
private function retrieveOptions()
{
$driver = mb_strtolower($this->option('driver'));
$url = $this->option('url');
$prefix = $this->option('prefix');
if (in_array($driver, ['elasticsearch', 'sqlite'])) {
if ($driver === 'elasticsearch') {
if (!$url) {
exit($this->error('No URL provided for Elasticsearch.'));
}
}
$this->setConfig($driver, $url, $prefix);
} else {
exit($this->error('Invalid driver specified. Must be one of Elasticsearch or SQLite.'));
}
}
private function promptForInput()
{
$url = null;
$prefix = null;
$driver = $this->choice('Which search driver would you like to use?', ['Elasticsearch', 'SQLite'], 0);
if ($driver === 'Elasticsearch') {
$url = $this->ask('What is the full URL to your Elasticsearch server (including protocol and port number)?');
$prefix = $this->ask(
"What unique prefix would you like to apply to your indices?\n " .
"This is needed if using one Elasticache server for multiple ProcessMaker 4 instances.\n " .
'Leave empty for none'
);
}
$this->setConfig($driver, $url, $prefix);
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): void
{
if ($this->interactive()) {
$confirmed = $this->confirm(
"Running this command will index data from Requests, Tasks, and packages that support indexed search.\n " .
"This may take a very long time and consume system resources on large datasets.\n\n " .
'Are you sure you wish to proceed?'
);
} else {
$confirmed = true;
}
if ($confirmed) {
if ($this->option('driver')) {
$this->retrieveOptions();
} else {
$this->promptForInput();
}
$setting = Setting::updateOrCreate(
['key' => 'indexed-search'],
['config' => ['enabled' => true]]
);
$this->indexRecords();
if ($setting->config['enabled'] === true) {
$this->line("\nIndexed search has been enabled.");
}
}
}
}