Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ than one metric in a single path.
match ``servers.ix02ehssvc04v.cpu.total.user``, while ``servers.*.*.*.*``
will.


Examples
^^^^^^^^

Expand Down Expand Up @@ -571,7 +571,7 @@ mode to use:
plus the value of the current line.

.. _param-bgcolor:

bgcolor
```````

Expand Down Expand Up @@ -647,7 +647,7 @@ drawNullAsZero
Converts any None (null) values in the displayed metrics to zero at render
time.

.. _param-fgcolor:
.. _param-fgcolor:

fgcolor
```````
Expand Down Expand Up @@ -981,7 +981,7 @@ max
maxDataPoints
`````````````

Set the maximum numbers of datapoints returned when using json content.
Set the maximum numbers of datapoints returned when using json content.

If the number of datapoints in a selected range exceeds the maxDataPoints
value then the datapoints over the whole period are consolidated.
Expand Down Expand Up @@ -1125,8 +1125,7 @@ template

*Default: default*

Used to specify a template from ``graphTemplates.conf`` to use for default
colors and graph styles.
Used to specify a template to use for default colors and graph styles.

Example::

Expand Down Expand Up @@ -1155,7 +1154,7 @@ Example::
&title=Apache Busy Threads, All Servers, Past 24h

.. _param-tz:

tz
``

Expand Down Expand Up @@ -1270,7 +1269,7 @@ the X-axis. See `datetime.date.strftime()
format specification details.

.. _param-yAxisSide:

yAxisSide
`````````

Expand All @@ -1280,7 +1279,7 @@ Sets the side of the graph on which to render the Y-axis. Accepts values of
``left`` or ``right``.

.. _param-yDivisors:

yDivisors
`````````

Expand Down Expand Up @@ -1377,7 +1376,7 @@ yMinRight
In dual Y-axis mode, sets the lower bound of the right Y-Axis (see: `yMin`_).

.. _param-yStep:

yStep
`````

Expand Down
26 changes: 26 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ Extra sections
If ``True`` (default), full tracebacks are returned in the HTTP
response in case of application errors.

*templates*

To define color/styling, of graphs, like so:
* values under the 'default' template key serve to override
the built-in defaults (see glyph.py).
* Other template keys provide further styling which will be used
via the http parameter ``template=<template_key>``.
The defaults are used for unspecified values.

Example::

templates:
default:
background: 'white'
foreground: 'black'
minorLine: 'grey'
majorLine: 'rose'
linecolors: 'blue,green,red,purple,brown,yellow,aqua,grey,magenta,pink,gold,rose'
fontname: 'Sans'
fontsize: 10
fontbold: 'false'
fontitalic: 'false'
my_template:
background: 'pink'


Custom location
---------------

Expand Down
3 changes: 3 additions & 0 deletions graphite_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def configure(app):
else:
Sentry(app, dsn=config['sentry_dsn'])

if 'templates' in config:
app.config['templates'] = config['templates']

app.wsgi_app = TrailingSlash(CORS(app.wsgi_app,
config.get('allowed_origins')))
if config.get('render_errors', True):
Expand Down
8 changes: 7 additions & 1 deletion graphite_api/render/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'darkgrey': (111, 111, 111),
}

# This gets overridden by graphTemplates.conf
# This gets overridden by your graph templates
defaultGraphOptions = dict(
background='white',
foreground='black',
Expand Down Expand Up @@ -995,7 +995,13 @@ def encodeHeader(self, text):
self.ctx.restore()

def loadTemplate(self, template):
from ..app import app
conf = app.config.get('templates', {})

opts = defaults = defaultGraphOptions
defaults.update(conf.get('default', {}))
if template != 'default':
opts.update(conf.get(template, {}))

self.defaultBackground = opts.get('background', defaults['background'])
self.defaultForeground = opts.get('foreground', defaults['foreground'])
Expand Down
2 changes: 2 additions & 0 deletions graphite_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def keys(self):
keys.update(request.form.keys())
keys.update(request.args.keys())
return keys


RequestParams = RequestParams()


Expand Down
32 changes: 32 additions & 0 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import time

from graphite_api.app import app
from graphite_api._vendor import whisper

from . import TestCase, WHISPER_DIR
Expand Down Expand Up @@ -451,6 +452,37 @@ def test_render_validation(self):
})
self.assertEqual(response.status_code, 200)

def test_graph_templates(self):
def get_metadata(svg):
prefix = 'metadata = '
metadata = {}
for line in svg.split('\n'):
line = line.strip()
if line.startswith(prefix):
metadata = json.loads(line[len(prefix):])
return metadata

whisper.create(self.db, [(1, 60)])
app.config['templates'] = {
'default': {'linecolors': 'white,blue,red'},
'my_template': {'linecolors': 'blue,red,white'}
}
response = self.app.get(self.url, query_string={'target': 'test',
'format': 'svg'})
svg = response.data.decode('utf-8')
metadata = get_metadata(svg)
self.assertEqual(metadata['series'][0]['name'], 'test')
self.assertEqual(metadata['series'][0]['color'], 'white')

response = self.app.get(self.url,
query_string={'target': 'test',
'format': 'svg',
'template': 'my_template'})
svg = response.data.decode('utf-8')
metadata = get_metadata(svg)
self.assertEqual(metadata['series'][0]['name'], 'test')
self.assertEqual(metadata['series'][0]['color'], 'blue')

def test_raw_data(self):
whisper.create(self.db, [(1, 60)])

Expand Down