|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Helpers; |
|
5
|
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Models\SolrLog; |
|
7
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use SilverStripe\ORM\ValidationException; |
|
11
|
|
|
|
|
12
|
|
|
class SolrLogger |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Client |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $client; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* SolrLogger constructor. |
|
21
|
|
|
*/ |
|
22
|
28 |
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
28 |
|
$config = SolrCoreService::config()->get('config'); |
|
25
|
28 |
|
$endpoint = $config['endpoint']; |
|
26
|
28 |
|
$hostConfig = array_values($endpoint); |
|
27
|
28 |
|
$this->client = new Client( |
|
28
|
|
|
[ |
|
29
|
28 |
|
'base_uri' => $hostConfig[0]['host'] . ':' . $hostConfig[0]['port'] |
|
30
|
|
|
] |
|
31
|
|
|
); |
|
32
|
28 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $type |
|
36
|
|
|
* @throws GuzzleException |
|
37
|
|
|
* @throws ValidationException |
|
38
|
|
|
*/ |
|
39
|
26 |
|
public function saveSolrLog($type = 'Query'): void |
|
40
|
|
|
{ |
|
41
|
26 |
|
$response = $this->client->request('GET', 'solr/admin/info/logging', [ |
|
42
|
26 |
|
'query' => [ |
|
43
|
|
|
'since' => 0, |
|
44
|
|
|
'wt' => 'json' |
|
45
|
|
|
] |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
26 |
|
$arrayResponse = json_decode($response->getBody(), true); |
|
49
|
|
|
|
|
50
|
26 |
|
foreach ($arrayResponse['history']['docs'] as $error) { |
|
51
|
|
|
$filter = [ |
|
52
|
26 |
|
'Timestamp' => $error['time'], |
|
53
|
26 |
|
'Level' => $error['level'], |
|
54
|
26 |
|
'Index' => $error['core'], |
|
55
|
|
|
]; |
|
56
|
26 |
|
if (!SolrLog::get()->filter($filter)->exists()) { |
|
57
|
26 |
|
SolrLog::create([ |
|
58
|
26 |
|
'Timestamp' => $error['time'], |
|
59
|
26 |
|
'Message' => $error['message'], |
|
60
|
26 |
|
'Index' => $error['core'], |
|
61
|
26 |
|
'Type' => $type, |
|
62
|
26 |
|
'Level' => $error['level'], |
|
63
|
26 |
|
])->write(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
26 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return Client |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function getClient(): Client |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->client; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Client $client |
|
78
|
|
|
* @return SolrLogger |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function setClient(Client $client): self |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->client = $client; |
|
83
|
|
|
|
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|