Giter Site home page Giter Site logo

grafana-sqldb-datasource's Introduction

grafana-sqldb-datasource

SQL DB database is the datasource of Grafana 3.0, which provides the support of both of MySQL and PostgreSQL as a backend database.

Features

1. Query Editor

Forked from influxDB plugin, this datasource frontend has been implemented. Therefore, you can issue SQL queries in the same manner as in influxDB.

(Defining with query editor)
Query Editor

Each parts (schema, table, column name and data type) refer to information_schema in RDB.

2. Text Editor Mode (support raw SQL)

You can switch to raw query mode by clicking icon.

(Toggling edit mode)
Query Editor

Raw queries are generated refering to inputs of query editor. You can modify them (add JOIN another tables, sub queries in WHERE clause, and so on).

Macros

If you tries to mpdify a raw query or define it by yourself without choosing parts in query editor, you can use these macros.

macro detail
$timeColumn This is replaced to "TIME" from [TIME : TYPE] in query editor.
$timeFilter This is replaced to "$timeColumn < $from AND $timeColumn > $to".
$from This is replaced to "from" word of the time range for panels, and this is casted as "TYPE" from [TIME : TYPE] in query editor.
$to This is replaced to "to" of the time range for panels, and this is casted as "TYPE" from [TIME : TYPE] in query editor.
$unixFrom This is replaced to "from" of the time range for panels, and this is casted as number of unix timestamp.
$unixTo This is replaced to "to" of the time range for panels, and this is casted as number of unix timestamp.
$timeFrom This is replaced to "from" of the time range for panels, and this is casted as timestamp.
$timeTo This is replaced to "to" of the time range for panels, and this is casted as timestamp.

3. Templating

You can create a template variable in Grafana and have that variable filled with values from any SQL Database metric exploration query. Then, You can use this variable in your SQL Database metric queries.

(Defining a template)
Template Editor

(Use a template vartiable in query editor)
Query Editor

4. Annotations

Annotaions is also supported. You can issue SQL queries and add event information above graphes.ใ€€

(Defining an annotation)
Annotaions Editor

(Annotations in a graph)
Annotaions Graph

5. Using timestamp and unixtimestamp as a time-serise column

You can choose a time-serise column from the table definition.

(Choosing the column for time series)
time series

Data types

The supported data types for time-serise are:

category PostgreSQL MySQL
timestamp type timestamp without time zone
timestamp with time zone
timestamp
datetime
number type
(if you use unixtimesamp as a time-serise column)
bigint
integer (int)
float
real
double precision
decimal
numeric
bigint
integer (int)
float
real
double precision
decimal
numeric

Example

Query with variables

SELECT $unixtimeColumn * 1000 AS time_msec,
       avg(cpu_usr)
       FROM myschema.dstat
       WHERE tag ~* '/^$host_t$/' AND
             $timeFilter
GROUP BY $unixtimeColumn
ORDER BY $unixtimeColumn

Actual query in PostgreSQL

timestamp type

SELECT round(extract(epoch from coltime::timestamptz) / 1200) * 1200 * 1000 AS time_msec,
       avg(cpu_usr)
       FROM myschema.dstat
       WHERE tag ~* '/^webserver123$/' AND
coltime > (now() - '7d'::interval)
GROUP BY round(extract(epoch from coltime::timestamptz) / 1200) * 1200
ORDER BY round(extract(epoch from coltime::timestamptz) / 1200) * 1200

number type

SELECT round(coltime / 1200) * 1200 * 1000 AS time_msec,
       avg(cpu_usr)
       FROM myschema.dstat
       WHERE tag ~* '/^webserver123$/' AND
coltime > extract(epoch from (now() - '7d'::interval)::timestamptz)
GROUP BY round(coltime / 1200) * 1200
ORDER BY round(coltime / 1200) * 1200

Actual query in MySQL

timestamp type

SELECT (UNIX_TIMESTAMP(coltime) DIV 1200) * 1200 * 1000 AS time_msec,
       avg(cpu_usr)
       FROM myschema.dstat
       WHERE tag REGEXP '^webserver123$' AND
             coltime > DATE_SUB(CURDATE(), INTERVAL 7 DAY)
             GROUP BY (UNIX_TIMESTAMP(coltime) DIV 1200) * 1200
             ORDER BY (UNIX_TIMESTAMP(coltime) DIV 1200) * 1200;

number type

SELECT (coltime DIV 1200) * 1200 * 1000 AS time_msec,
       avg(cpu_usr)
       FROM myschema.dstat
       WHERE tag REGEXP '^webserver123$' AND
coltime > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 7 DAY))
GROUP BY (coltime DIV 1200) * 1200
ORDER BY (coltime DIV 1200) * 1200

Tested versions of RDBMS

  • PostgreSQL
9.5 9.4 9.3 9.2 9.1
OK OK (not yet) OK (not yet)
  • MySQL
5.7 5.6 5.5
(not yet) (not yet) OK

References

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.