Hosting Handler

getStatus()

Signature

public function getStatus(ProductHosting $productHosting): ServerHostingStatusResponse

Description

This method gets executed via ajax request to display the status badge of the hosting which consists of the badge type (success, warning, danger etc...) and the badge text.

Tipp: You can pass additional data as third parameter, e.g. the current statistics for real time usage which can be used in the frontend via javascript event as explained below.

Example

public function getStatus(ProductHosting $productHosting): ServerHostingStatusResponse {
	// Make the API call to retrieve the VPS information
	$response = $this->getClient($productHosting->host)->sendHostCall("{$productHosting->custom_fields['proxmoxVirtualization']}/{$productHosting->custom_fields['proxmoxVmId']}/status/current")->data;

	// Handle the response to display the badge accordingly
	if ($response->status === 'stopped') {
		$status = "Offline";
		$badge = "danger";
	}

	if ($response->status === 'running') {
		$status = "Online";
		$badge = "success";
	}

	return new ServerHostingStatusResponse(
		$status ?? "Unknown", // The badge text
		$badge ?? "danger",   // The badge variant (see https://getbootstrap.com/docs/4.0/components/badge/#contextual-variations)
		[                     // Additional statistics of the VPS which are displayed in the frontend (optional)
			"stats" => [
				"uptime" => $response->uptime,
				"ram" => [
					"max" => $response->maxmem,
					"current" => $response->mem,
					"usage" => round($response->mem / $response->maxmem * 100, 2),
				],
				"disk" => [
					"max" => $response->maxdisk,
					"current" => $response->disk,
				],
				"cpu" => [
					"cores" => $response->cpus,
					"usage" => round($response->cpu * 100, 2),
				],
		]
	]);
}
Was this helpful?