Skip to content

Commit 25fbce3

Browse files
committed
feat: support cluster endpoint
1 parent decc2cf commit 25fbce3

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

src/Objects/Metric.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typesense\Objects;
6+
7+
class Metric extends TypesenseObject
8+
{
9+
public string $system_cpu1_active_percentage;
10+
11+
public ?string $system_cpu2_active_percentage = null;
12+
13+
public ?string $system_cpu3_active_percentage = null;
14+
15+
public ?string $system_cpu4_active_percentage = null;
16+
17+
public ?string $system_cpu5_active_percentage = null;
18+
19+
public ?string $system_cpu6_active_percentage = null;
20+
21+
public ?string $system_cpu7_active_percentage = null;
22+
23+
public ?string $system_cpu8_active_percentage = null;
24+
25+
public ?string $system_cpu9_active_percentage = null;
26+
27+
public ?string $system_cpu10_active_percentage = null;
28+
29+
public ?string $system_cpu11_active_percentage = null;
30+
31+
public ?string $system_cpu12_active_percentage = null;
32+
33+
public ?string $system_cpu13_active_percentage = null;
34+
35+
public ?string $system_cpu14_active_percentage = null;
36+
37+
public ?string $system_cpu15_active_percentage = null;
38+
39+
public ?string $system_cpu16_active_percentage = null;
40+
41+
public ?string $system_cpu17_active_percentage = null;
42+
43+
public ?string $system_cpu18_active_percentage = null;
44+
45+
public ?string $system_cpu19_active_percentage = null;
46+
47+
public ?string $system_cpu20_active_percentage = null;
48+
49+
public ?string $system_cpu21_active_percentage = null;
50+
51+
public ?string $system_cpu22_active_percentage = null;
52+
53+
public ?string $system_cpu23_active_percentage = null;
54+
55+
public ?string $system_cpu24_active_percentage = null;
56+
57+
public string $system_cpu_active_percentage;
58+
59+
public string $system_disk_total_bytes;
60+
61+
public string $system_disk_used_bytes;
62+
63+
public string $system_memory_total_bytes;
64+
65+
public string $system_memory_used_bytes;
66+
67+
public string $system_network_received_bytes;
68+
69+
public string $system_network_sent_bytes;
70+
71+
public string $typesense_memory_active_bytes;
72+
73+
public string $typesense_memory_allocated_bytes;
74+
75+
public string $typesense_memory_fragmentation_ratio;
76+
77+
public string $typesense_memory_mapped_bytes;
78+
79+
public string $typesense_memory_metadata_bytes;
80+
81+
public string $typesense_memory_resident_bytes;
82+
83+
public string $typesense_memory_retained_bytes;
84+
}

src/Objects/Stat.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typesense\Objects;
6+
7+
use stdClass;
8+
9+
class Stat extends TypesenseObject
10+
{
11+
public int $delete_latency_ms;
12+
13+
public int $delete_requests_per_second;
14+
15+
public int $import_latency_ms;
16+
17+
public int $import_requests_per_second;
18+
19+
public stdClass $latency_ms;
20+
21+
public int $overloaded_requests_per_second;
22+
23+
public int $pending_write_batches;
24+
25+
public stdClass $requests_per_second;
26+
27+
public int $search_latency_ms;
28+
29+
public int $search_requests_per_second;
30+
31+
public float $total_requests_per_second;
32+
33+
public int $write_latency_ms;
34+
35+
public int $write_requests_per_second;
36+
}

src/Requests/Cluster.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typesense\Requests;
6+
7+
use Typesense\Objects\Metric;
8+
use Typesense\Objects\Stat;
9+
10+
class Cluster extends Request
11+
{
12+
/**
13+
* Creates a point-in-time snapshot of a Typesense node's state and data in the specified directory.
14+
*
15+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#create-snapshot-for-backups
16+
*/
17+
public function snapshot(string $path): bool
18+
{
19+
$path = sprintf('/operations/snapshot?snapshot_path=%s', $path);
20+
21+
$data = $this->send('POST', $path);
22+
23+
return $data->success;
24+
}
25+
26+
/**
27+
* Compacting the on-disk database.
28+
*
29+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#compacting-the-on-disk-database
30+
*/
31+
public function compact(): bool
32+
{
33+
$data = $this->send('POST', '/operations/db/compact');
34+
35+
return $data->success;
36+
}
37+
38+
/**
39+
* Triggers a follower node to initiate the raft voting process, which triggers leader re-election.
40+
*
41+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#re-elect-leader
42+
*/
43+
public function reElectLeader(): bool
44+
{
45+
$data = $this->send('POST', '/operations/vote');
46+
47+
return $data->success;
48+
}
49+
50+
/**
51+
* Enable logging of requests that take over a defined threshold of time.
52+
*
53+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#toggle-slow-request-log
54+
*/
55+
public function updateSlowRequestLog(int $ms): bool
56+
{
57+
$data = $this->send('POST', '/config', [
58+
'log-slow-requests-time-ms' => $ms,
59+
]);
60+
61+
return $data->success;
62+
}
63+
64+
/**
65+
* Get current RAM, CPU, Disk & Network usage metrics.
66+
*
67+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#cluster-metrics
68+
*/
69+
public function metrics(): Metric
70+
{
71+
$data = $this->send('GET', '/metrics.json');
72+
73+
return Metric::from($data);
74+
}
75+
76+
/**
77+
* Get stats about API endpoints.
78+
*
79+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#api-stats
80+
*/
81+
public function stats(): Stat
82+
{
83+
$data = $this->send('GET', '/stats.json');
84+
85+
return Stat::from($data);
86+
}
87+
88+
/**
89+
* Get health information about a Typesense node.
90+
*
91+
* @see https://typesense.org/docs/latest/api/cluster-operations.html#health
92+
*/
93+
public function health(): bool
94+
{
95+
$data = $this->send('GET', '/health');
96+
97+
return $data->ok;
98+
}
99+
}

tests/Unit/ClusterTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Typesense\Tests\Unit;
6+
7+
test('it can create a snapshot', function () {
8+
$path = sprintf('/tmp/%s', $this->slug());
9+
10+
$success = $this->typesense->cluster->snapshot(
11+
$path,
12+
);
13+
14+
expect($success)->toBeTrue();
15+
});
16+
17+
test('it can compact the database', function () {
18+
$success = $this->typesense->cluster->compact();
19+
20+
expect($success)->toBeTrue();
21+
});
22+
23+
test('it can update slow request log', function () {
24+
$success = $this->typesense->cluster->updateSlowRequestLog(100);
25+
26+
expect($success)->toBeTrue();
27+
});
28+
29+
test('it can get metrics', function () {
30+
$metric = $this->typesense->cluster->metrics();
31+
32+
expect($metric->typesense_memory_retained_bytes)->toBeGreaterThanOrEqual(0);
33+
});
34+
35+
test('it can get stats', function () {
36+
$stats = $this->typesense->cluster->stats();
37+
38+
expect($stats->delete_latency_ms)->toBeGreaterThanOrEqual(0);
39+
});
40+
41+
test('it can get health', function () {
42+
$health = $this->typesense->cluster->health();
43+
44+
expect($health)->toBeTrue();
45+
});

0 commit comments

Comments
 (0)