Code Monkey home page Code Monkey logo

cachet.php's Introduction

cachet.php

cachet.php is a PHP client library for the Cachet status page (https://cachethq.io/).

This library could be useful for displaying details from your Cachet status page in other systems, such as a monitoring dashboard, a client ticketing system or any other web applications.

If you want to grab cachet.php with Composer, take a look on Packagist: https://packagist.org/packages/divineomega/cachetphp

Quick Start

Before starting, install the cachet.php library via Composer.

Create CachetInstance object

Now, you need to create a CachetInstance object that represents your installation of Cachet. You can do this like so:

require_once 'vendor/autoload.php';

use \DivineOmega\CachetPHP\Factories\CachetInstanceFactory;

// The API token for the demo instance is 9yMHsdioQosnyVK4iCVR.
$cachetInstance = CachetInstanceFactory::create('https://demo.cachethq.io/api/v1/', '9yMHsdioQosnyVK4iCVR');

Retrieving Cachet elements

Retrieving data from the various elements of your Cachet instance is easy. Just call the appropriate getter method on your $cachetInstance object. The Cachet install will be contacted and an array of request appropriate objects be returned.

$components = $cachetInstance->getAllComponents();         // Components
$incidents = $cachetInstance->getAllIncidents();           // Incidents
$incidentUpdates = $incidents[0]->getAllIncidentUpdates(); // Incident Updates (Cachet 2.4.0 or above required)
$metrics = $cachetInstance->getAllMetrics();               // Metrics
$metricPoints = $metrics[0]->getAllMetricPoints();         // Metric Points
$subscribers = $cachetInstance->getAllSubscribers();       // Subscribers

To retrieve a single Cachet element by its ID, you can use code similar to the following method.

// Get incident by id
$incident = $cachetInstance->getIncidentById($incidentId);

Sorting Cachet elements

If you wish to sort your results, you can use the following syntax. This works for components, incidents, metrics, metric points and subscribers.

// Get components sorted by name ascending
$components = $cachetInstance->getAllComponents('name', 'asc');

Reading from Cachet element objects

Reading data from retrieved Cachet element objects is easy. Just access their public member variables.

Here's an example of outputting the id, name, description and status of a Cachet component.

// Get components
$components = $cachetInstance->getAllComponents();

// Display components
foreach ($components as $component) {
    echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
    echo "<br/>";
}

See the official Cachet documentation for information on the variables available for each type of Cachet element.

Creating Cachet elements

You can create a Cachet element, such as a component or incident very easily using the cachet.php library. This involves creating an associative array of the details for the Cachet elements and then passing this to the relevant creation method.

The following example shows you how to make a simple test component.

$componentDetails = ['name' => 'Test Component '.rand(1, 99999), 'status' => 1];

$component = $cachetInstance->createComponent($componentDetails);

echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;

The more comprehensive example below shows how to create an incident and add an incident update to it. Please note that Cachet 2.4.0 or above required if you wish to use incident updates.

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999)];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

echo $incidentUpdate->id.' - '.$incidentUpdate->incident_id.' - '.$incidentUpdate->status.' - '.$incidentUpdate->message;

Updating Cachet elements

The cachet.php allows you to make changes to a Cachet element and saving those changes back to your Cachet install. This is done by directly changing the Cachet element's public member variables, and then calling the object's save() method.

The following example shows how to change the name and status of a component, then save the changes.

// Get components
$components = $cachetInstance->getAllComponents();

// Change component details
$component[0]->name = 'My awesome component';
$component[0]->status = 1;
$component[0]->save();

Updating Component Status via Incident and Incident Updates

If you create an incident with a component_id, you can also include a component_status to change the component's status at the same time as creating the incident. See the example below.

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1,
    'component_id' => 1, 'component_status' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

When creating incident updates, you can also specify a component_status to change the component's status when the incident update is created. See the example below.

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999), 'component_status' => 2];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

You can also change a component status via an incident or incident update by changing the component_status on the related object and saving, as shown below in the exmaples below. Note that this will only work if the incident was created with an assoicated component_id.

$incident->component_status = 2;
$incident->save();
$incidentUpdate->component_status = 3;
$incidentUpdate->save();

Deleting Cachet elements

To delete a Cachet element from your Cachet install, you simply need to call the delete() method on the appropriate Cachet element object.

For example, to delete an incident you could do the following.

// Get incidents
$incidents = $cachetInstance->getAllIncidents();

// Delete the first one
$incidents[0]->delete();

Contact

If you've found a bug or a new feature, please report it as a GitHub issue.

For other queries, I'm available on Twitter (Jordan Hall).

cachet.php's People

Contributors

divineomega avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cachet.php's Issues

Object 'delete' method

The following code should work as you'd expect.

$components = ComponentFactory::getAll($cachetInstance);
$component = $components[0];
$component->delete();

Use Guzzle

You're duplicating a lot of code for cURL.

Change license to Lesser GPL3

The next version (v0.2) should be released with a more open license.

We'll release v0.2 with the Lesser GPL3, instead of the regular GPL3.

Subscribe on specify component - not working

$subscriberDetails = ['email' => '[email protected]', 'verify' => 1 , 'components' => [5,7]];
$subscriber = $cachetInstance->createSubscriber($subscriberDetails);

echo $subscriber->id.' - '.$subscriber->email;

I run this code, but the new Subscriber isn't being subscribed to the supplied component but is Subscribed to all updates.

Factory 'create' methods

The factories for all Cachet elements should have a 'create' method that accepts an associative array as a parameter and then performs to appropriate POST request.

Appropriate 'createComponent' style methods should be added to the CachetInstance object that calls these new factory methods.

Ordering always goes descending

This might be a regression, not certain.

When running the following two snippets, they return seem to both return the same (descending) results.

// Get components sorted by name ascending
$components = $cachetInstance->getAllComponents('name', 'asc');
// Display components sorted by name ascending
echo "\n";
echo '*** Components (sorted by name ascending) ***';
echo "\n";
foreach ($components as $component) {
    echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
    echo "\n";
}
// Get components sorted by name descending
$components = $cachetInstance->getAllComponents('name', 'desc');
// Display components sorted by name descending
echo "\n";
echo '*** Components (sorted by name descending) ***';
echo "\n";
foreach ($components as $component) {
    echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
    echo "\n";
}

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Not able to update 'Incidents' in v0.2

use \DivineOmega\CachetPHP\Factories\CachetInstanceFactory;

$cachetInstance = CachetInstanceFactory::create($host, $token);

$incidents = $cachetInstance->getAllIncidents();
$incident = $incidents[0];
$incident->status = 1;
$incident->save();

Receiving the following error -

PHP Fatal error:  Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `PUT http://status1.test.in/api/v1/incidents/2?name=Downtime%20Incoming%20Calls&message=%23%20Issue%20Observed%0D%0A%0D%0A%23%20Issue%20Fixed%20at%2012%3A15PM&status=1&visible=1&component_id=9` resulted in a `400 Bad Request` response:
{"errors":[{"id":"390e83d4-a2db-440f-b93d-2f0b4bb754cd","status":400,"title":"Bad Request","detail":"The request cannot  (truncated...)
' in /home/test/commonix/extlib/cachet-status-board-php-client/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
Stack trace:
#0 /home/test/commonix/extlib/cachet-status-board-php-client/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home/test/commonix/extlib/cachet-status-board-php-client/vendor/guzzlehttp/promises/src/Promise.php(201): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /hom in /home/test/commonix/extlib/cachet-status-board-php-client/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 107

What is the fix for this?

Use factories and object classes for all Cachet elements

Factories and object classes should be created for the following Cachet elements and existing code some be re-factored to use them.

  • CachetInstance to hold details of the Cachet instance you are currently using.
  • Component
  • Incident
  • Metric
  • MetricPoint
  • Subscriber

Object 'save' method

The following code should work as you'd expect.

$components = $cachetInstance->getAllComponents();
$component = $components[0];
$component->name = 'Jeff The Awesome Component';
$component->save();

Reduce need to use multiple factories

On dev branch, currently we retrieve components like this:

$components = ComponentFactory::getAll($cachetInstance);

We should allow this more intuitive syntax:

$components = $cachetInstance->getAllComponents();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.