Skip to content

Commit b64d797

Browse files
authored
feat(core): Automate creation of Time column (#27)
* feat(core): Automate creation of Time column * chore(doc): Update README
1 parent ecd60c6 commit b64d797

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

README.md

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Splunk Data Source Plugin for Grafana is a Grafana (data source) plugin that
1010
allows you to pull Splunk data into your Grafana dashboards. Or, in other words,
1111
it is a Grafana plugin that allows you to query Splunk directly from Grafana.
1212

13+
<img width="50%" alt="graphing-splunk-results-in-grafana" src="https://user-images.githubusercontent.com/603610/170813937-2d7f03c6-d0d7-49b8-83a7-3c1b186fd0f7.png"> <img width="50%" alt="splunk-results-in-logs-panels-in-grafana" src="https://user-images.githubusercontent.com/603610/170837177-863c407b-e115-4e2c-b08a-e85927e07a6b.png">
14+
1315

1416
### Installation
1517

@@ -45,23 +47,6 @@ by an administrator from Grafana's UI `Configuration --> Datasources --> Add dat
4547
| Token | Basic auth token |
4648

4749

48-
### Graphing Splunk Results in Grafana
49-
50-
For Grafana to be able to graph the data pulled from your Splunk instance, it
51-
requieres a `Time` field, which should be formatted as `%Y-%m-%dT%H:%M:%S.000Z`.
52-
An easy way to achieve this is including the following command to your
53-
Splunk queries: `eval _time=strftime(_time, "%Y-%m-%dT%H:%M:%S.000Z") | rename _time AS Time`.
54-
55-
<img width="100%" alt="graphing-splunk-results-in-grafana" src="https://user-images.githubusercontent.com/603610/170813937-2d7f03c6-d0d7-49b8-83a7-3c1b186fd0f7.png">
56-
57-
```
58-
index=_internal *
59-
| timechart span=1m count
60-
| eval _time=strftime(_time, "%Y-%m-%dT%H:%M:%S.000Z")
61-
| rename _time AS Time
62-
```
63-
64-
6550
### Getting Started
6651

6752
1. Build the project

src/datasource.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
2020
}
2121

2222
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
23+
const moment = require('moment');
2324
const promises = options.targets.map((query) =>
2425
this.doRequest(query, options).then((response) => {
2526
const frame = new MutableDataFrame({
@@ -39,8 +40,14 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
3940
response.results.forEach((result: any) => {
4041
// console.log(`DEBUG: result=${JSON.stringify(result)}`);
4142
let row: any[] = [];
43+
4244
response.fields.forEach((field: any) => {
43-
row.push(result[field]);
45+
if (field === 'Time') {
46+
let time = moment(result['_time']).format('YYYY-MM-DDTHH:mm:ssZ');
47+
row.push(time);
48+
} else {
49+
row.push(result[field]);
50+
}
4451
});
4552
frame.appendRow(row);
4653
});
@@ -172,6 +179,18 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
172179
offset = offset + count;
173180
}
174181

182+
if (fields.includes('_time')) {
183+
fields.push('Time');
184+
}
185+
186+
const index = fields.indexOf('_raw', 0);
187+
if (index > -1) {
188+
fields.splice(index, 1);
189+
fields = fields.reverse();
190+
fields.push('_raw');
191+
fields = fields.reverse();
192+
}
193+
175194
return { fields: fields, results: results };
176195
}
177196

0 commit comments

Comments
 (0)