Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch data fields by name #19

Merged
merged 3 commits into from
Feb 7, 2022
Merged
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ allow_loading_unsigned_plugins = spectraphilic-windrose-panel

Restart Grafana service.

Add a panel to your dashboard with the *Windrose* visualization type. Set up the
query to provide data with `speed` and `direction` fields. The speed can be in
the unit of your choice (can be set in the settings), while the direction is
always in degrees.

## PostgreSQL

Example query for PostgreSQL:
Expand Down Expand Up @@ -74,6 +79,22 @@ Set another query for direction:
This information was provided by @newrushbolt ; for further details see
https://github.com/spectraphilic/grafana-windrose/issues/18#issuecomment-1007648330

## Flux

Example for InfluxDB v2 filled from SignalK.

```
from(bucket: "bucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "environment.wind.speedTrue" or r["_measurement"] == "environment.wind.directionTrue")
|> filter(fn: (r) => r["_field"] == "value")
|> pivot(rowKey:["_time"], columnKey: ["_measurement"], valueColumn: "_value") // put dir and speed values into the same row based on timestamps
|> filter(fn: (r) => exists r["environment.wind.directionTrue"] and exists r["environment.wind.speedTrue"]) // drop rows that don't have both values
|> rename(columns: {"environment.wind.directionTrue": "directionRad", "environment.wind.speedTrue": "speedMps"})
|> map(fn: (r) => ({ r with direction: r.directionRad / 3.14 * 180.0 })) // convert to degrees from radians
|> map(fn: (r) => ({ r with speed: r.speedMps / 0.514 })) // convert to knots from m/s
|> aggregateWindow(every: v.windowPeriod, fn: first, column: "direction", createEmpty: false) // sample the first row for each window (real aggregation would show fake values in gusty winds)
```

# Development

Expand Down
27 changes: 24 additions & 3 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,31 @@ class WindroseCtrl extends MetricsPanelCtrl {
let angles = [];

if (data[0].type == 'table') {
// e.g. PostgreSQL
// e.g. PostgreSQL and InfluxDB

let speedCol = null
let dirCol = null
for (let i in data[0].columns) {
if (data[0].columns[i].text == "speed") {
speedCol = i
}
if (data[0].columns[i].text == "direction") {
dirCol = i
}
}

if (speedCol === null) {
console.warn("no `speed` column in data")
speedCol = 1
}
if (dirCol === null) {
console.warn("no `direction` column in data")
dirCol = 2
}

for (let row of data[0].rows) {
speeds.push(row[1]);
angles.push(row[2]);
speeds.push(row[speedCol]);
angles.push(row[dirCol]);
}
} else if (data[0].datapoints) {
// e.g. ClickHouse
Expand Down