Class Graphite_GraphBuilder
Graphite graph query string generator.
DSL and ini file driven API to assist in generating Graphite graph query strings.
Example:
- <?php
- /**
- * Example usage of the DSL API to construct a graph of memory usage.
- */
- require_once dirname(__FILE__) . '/../src/autoload.php';
- ->title('Memory')
- ->vtitle('Mbytes')
- ->width(800)
- ->height(600)
- ->bgcolor('white')
- ->fgcolor('black')
- ->from('-2days')
- ->area('stacked')
- 'cactistyle' => true,
- 'color' => '00c000',
- 'alias' => 'Free',
- 'scale' => '0.00000095367',
- ))
- 'cactistyle' => true,
- 'color' => 'c00000',
- 'alias' => 'Used',
- 'scale' => '0.00000095367',
- ))
- ;
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <head>
- <body>
- <img src="http://graphite.example.com/render?<?php echo $g; ?>">
- </body>
- </html>
- Bryan Davis <bd808@bd808.com>
Copyright: | 2012 Bryan Davis and contributors. All Rights Reserved. |
Link: | http://readthedocs.org/docs/graphite/en/latest/url-api.html |
Link: | http://bd808.com/graphite-graph-php/ |
Link: | http://graphite.wikidot.com/ |
License: | Simplified BSD License |
Member Variables
protected array |
$meta
= array()
Configuration metadata. Metadata is arbitrary configuration data that may be used in creating a graph but is not directly graph settings or rendered targets. Examples include ini-file prefix configurations and abstract series. The top level array is keyed on category. Each category is expected to be an array of name => data pairs. The format of the data is category specific. |
protected array |
$prefixStack
Series prefix stack. |
protected array |
$settings
Graph settings. |
protected array |
$targets
Configuration for each target that will be rendered or retrieved. |
Method Summary
public static void |
builder(
[ $settings
= null] )
Builder factory. |
public static string |
qsEncode(
$str
)
Query string specific uri encoding. |
public Graphite_GraphBuilder |
__construct(
[ $settings
= null] )
Constructor. |
public string |
build(
[ $format
= null] )
Generate a graphite graph description query string. |
public Graphite_Graph_Series |
buildSeries(
$name
)
Add a data series to the graph using the Graphite_Graph_Series fluent builder DSL. |
public string |
currentPrefix(
)
Get the current target prefix. |
public Graphite_GraphBuilder |
endPrefix(
)
End prefix block. |
public Graphite_GraphBuilder |
forecast(
$name
, $opts
)
Add forecast, confidence bands, aberrations and base series using the Holt-Winters Confidence Band prediction model. |
public mixed |
getMeta(
$type
, $name
, [ $default
= array()] )
Get metadata by type and name. |
public Graphite_GraphBuilder |
ini(
$file
, [ $vars
= null] )
Load a graph description file. |
public Graphite_GraphBuilder |
line(
$opts
)
Draws a straight line on the graph. |
public string |
lookupPrefix(
$name
)
Lookup a prefix in this graph's metadata. |
public Graphite_GraphBuilder |
metric(
$name
, [ $opts
= array()] )
Add a data series to the graph. |
public Graphite_GraphBuilder |
prefix(
$prefix
)
Set a prefix to add to subsequent series. |
public void |
qs(
[ $format
= null] )
Alias for build(). |
public Graphite_GraphBuilder |
reset(
[ $settings
= null] )
Reset builder to empty state. |
protected string |
resolvePrefix(
$meta
)
Resolve a prefix. |
public Graphite_GraphBuilder |
series(
$name
, [ $opts
= array()] )
Add a data series to the graph. |
public void |
storeMeta(
$type
, $name
, $data
)
Add new metadata. |
public void |
url(
[ $format
= null] )
Alias for build(). |
public mixed |
__call(
$name
, $args
)
Handle attempts to call non-existant methods. |
public mixed |
__get(
$name
)
Handle attempts to read from non-existant members. |
public void |
__set(
$name
, $val
)
Handle attempts to write to non-existant members. |
public string |
__toString(
)
Convert to string. |
Methods
builder
Builder factory.
Parameters:
Name | Type | Description |
---|---|---|
$settings |
array | Default settings for graph |
qsEncode
Query string specific uri encoding.
Per RFC-3986: URI producing applications should percent-encode data octets that correspond to characters in the reserved set unless these characters are specifically allowed by the URI scheme to represent data in that component.
Php's builtin urlencode function is a general purpose encoder. This means that it takes the most conservative approach to encoding. It percent-encodes all octets that are not in the "unreserved" set (ALPHA / DIGIT / "-" / "." / "_" / "~"). Actually it goes further than this and encodes the tilde as well for no apparent reason other than potential binary compatibility with the output of early non-conforming user-agents.
Within the "query" section of a URI there is a broader set of valid characters allowed without percent-encoding:
- query = *( pchar / "/" / "?" )
- pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
- unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
- pct-encoded = "%" HEXDIG HEXDIG
- sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
What's the point? I could claim that it reduces the size of the encoded string, but my real reason is that it makes the Graphite query strings more readable for debugging.
Parameters:
Name | Type | Description |
---|---|---|
$str |
string | String to encode for embedding in the query component of a URI. |
__construct
Constructor.
Parameters:
Name | Type | Description |
---|---|---|
$settings |
array | Default settings for graph |
build
Generate a graphite graph description query string.
Parameters:
Name | Type | Description |
---|---|---|
$format |
string | Format to export data in (null for graph) |
Exceptions:
Type | Description |
---|---|
Graphite_ConfigurationException |
If required data is missing |
buildSeries
Add a data series to the graph using the Graphite_Graph_Series fluent builder DSL.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Name of data series to graph |
currentPrefix
Get the current target prefix.
endPrefix
End prefix block.
forecast
Add forecast, confidence bands, aberrations and base series using the Holt-Winters Confidence Band prediction model.
Parameters:
Name | Type | Description |
---|---|---|
$opts |
array | Line options |
$name |
Exceptions:
Type | Description |
---|---|
Graphite_ConfigurationException |
If required options are missing |
getMeta
Get metadata by type and name.
Parameters:
Name | Type | Description |
---|---|---|
$type |
string | Metadata type |
$name |
string | Configuration name |
$default |
mixed | Data to return if no matching metadata is found |
ini
Load a graph description file.
Parameters:
Name | Type | Description |
---|---|---|
$file |
string | Path to file |
$vars |
array | Variables to substitute in the ini file |
line
Draws a straight line on the graph.
Parameters:
Name | Type | Description |
---|---|---|
$opts |
array | Line options |
Exceptions:
Type | Description |
---|---|
Graphite_ConfigurationException |
If required options are missing |
lookupPrefix
Lookup a prefix in this graph's metadata.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Prefix name |
metric
Add a data series to the graph.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Name of data series to graph |
$opts |
array | Series options |
prefix
Set a prefix to add to subsequent series.
Parameters:
Name | Type | Description |
---|---|---|
$prefix |
string | Prefix to add |
qs
Alias for build().
Parameters:
Name | Type | Description |
---|---|---|
$format |
reset
Reset builder to empty state.
Parameters:
Name | Type | Description |
---|---|---|
$settings |
array | Default settings for graph |
resolvePrefix
Resolve a prefix.
Parameters:
Name | Type | Description |
---|---|---|
$meta |
mixed | Scalar prefix value or array of prefix configuration |
series
Add a data series to the graph.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Name of data series to graph |
$opts |
array | Series options |
storeMeta
Add new metadata.
Parameters:
Name | Type | Description |
---|---|---|
$type |
string | Metadata type |
$name |
string | Configuration name |
$data |
mixed | Metadata |
url
Alias for build().
Parameters:
Name | Type | Description |
---|---|---|
$format |
__call
Handle attempts to call non-existant methods.
Looks for $name in settings and gets/sets value if found. If the setting isn't valid an E_USER_NOTICE warning will be raised.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Method name |
$args |
array | Invocation arguments |
__get
Handle attempts to read from non-existant members.
Looks for $name in settings and returns value if found. If the setting isn't valid an E_USER_NOTICE warning will be raised.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Setting name |
__set
Handle attempts to write to non-existant members.
Sets setting $name=$val if $name is a valid setting. If the setting isn't valid an E_USER_NOTICE warning will be raised.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | Member name |
$val |
mixed | Value to set |
__toString
Convert to string.
Located in /src/Graphite/GraphBuilder.php [line 26]