Skip to content

Commit

Permalink
Fix granularity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedimar Turatti committed Jan 3, 2024
1 parent 309db26 commit 73d0f2e
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 20 deletions.
21 changes: 21 additions & 0 deletions Dockerfile.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# syntax=docker/dockerfile:1

# Node build
ARG BASE_IMAGE=alpine:3.18.3
ARG JS_IMAGE=node:16-alpine3.18
ARG JS_PLATFORM=linux/amd64
ARG GO_IMAGE=golang:1.16

FROM ${GO_IMAGE} as go-builder-1

WORKDIR /tmp/grafana

COPY go.* ./
COPY Magefile.go .
COPY pkg pkg
COPY src src

RUN go mod tidy

RUN git clone https://github.com/magefile/mage
RUN cd mage && go run bootstrap.go && go install
24 changes: 24 additions & 0 deletions Dockerfile.node
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# syntax=docker/dockerfile:1

# Node build
ARG BASE_IMAGE=alpine:3.18.3
ARG JS_IMAGE=node:16-alpine3.18
ARG JS_PLATFORM=linux/amd64
ARG GO_IMAGE=golang:1.16

ARG GO_SRC=go-builder
ARG JS_SRC=js-builder

FROM --platform=${JS_PLATFORM} ${JS_IMAGE} as js-builder-1

WORKDIR /tmp/grafana

COPY package.json yarn.lock ./
RUN yarn install --immutable

COPY tsconfig.json .prettierrc.js ./
COPY img img
COPY src src
COPY README.md LICENSE CHANGELOG.md .

ENV NODE_ENV production
2 changes: 1 addition & 1 deletion Magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Build build for all archs
func Build() {
fmt.Println("Build all archs!")
fmt.Println("Building plugin")
build.BuildAll()
}

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ build:
@yarn build
@mage -v

run: build
build-with-docker:
@docker compose run --remove-orphans go-builder
@docker compose run --remove-orphans js-builder

run:
@docker compose up grafana

package_node:
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Youbora data source for Grafana
<img src="img/logo.png" width="64" style="float:right"/>

<img src="src/img/logo.png" width="64" style="float:right"/>

The Youbora data source plugin allows you to query and visualize Youbora data from within Grafana.

Expand All @@ -25,15 +26,13 @@ Follow [these instructions](https://grafana.com/docs/grafana/latest/datasources/

You will need an API key from Youbora. You can generate one at your instance web interface or ask for help.


## Querying

<img src="img/screenshot-edit1.png" />
<img src="src/img/screenshot-edit1.png" />

## Visualizing

<img src="img/screenshot-dashboard1.png" />

<img src="src/img/screenshot-dashboard1.png" />

## Learn more

Expand Down
28 changes: 24 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.1'
services:

grafana:
image: grafana/grafana:8.5.1
image: grafana/grafana:9.3.8
restart: always
ports:
- 3000:3000
Expand All @@ -13,10 +13,30 @@ services:
GF_LOG_LEVEL: "debug"
volumes:
- grafana-data:/var/lib/grafana
- ./:/var/lib/grafana/plugins/youbora-datasource
- ./:/var/lib/grafana/plugins/globocom-youbora-datasource

js-builder:
build:
context: .
dockerfile: Dockerfile.node
restart: always
volumes:
- ./dist/:/tmp/grafana/dist/
entrypoint: /bin/sh -c
command: yarn build

go-builder:
build:
context: .
dockerfile: Dockerfile.golang
restart: always
volumes:
- ./dist/:/tmp/grafana/dist/
entrypoint: /bin/sh -c
command: mage build:linux

console:
image: grafana/grafana:8.5.1
image: grafana/grafana:9.3.8
restart: always
ports:
- 3000:3000
Expand All @@ -25,7 +45,7 @@ services:
environment:
GF_DEFAULT_APP_MODE: "development"
entrypoint: /bin/sh -c
command: /bin/sh
command: /bin/bash

volumes:
grafana-data:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nedimar-youbora-datasource",
"name": "globocom-youbora-datasource",
"version": "1.0.5",
"description": "NPAW's Youbora data source plugin",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
// from Grafana to create different instances of SampleDatasource (per datasource
// ID). When datasource configuration changed Dispose method will be called and
// new datasource instance created using NewSampleDatasource factory.
if err := datasource.Manage("nedimar-youbora-datasource", plugin.NewYouboraDataSource, datasource.ManageOpts{}); err != nil {
if err := datasource.Manage("globocom-youbora-datasource", plugin.NewYouboraDataSource, datasource.ManageOpts{}); err != nil {
log.DefaultLogger.Error(err.Error())
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func ParseQuery(dq backend.DataQuery, qm *QueryModel) error {

// setup granularity.
qm.Granularity = "minute"
if dq.Interval.Minutes() > 120 {
if dq.Interval.Minutes() > 2 {
qm.Granularity = "hour"
}
if dq.Interval.Hours() > 48 {
if dq.Interval.Hours() > 2 {
qm.Granularity = "day"
}

Expand Down
15 changes: 11 additions & 4 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,27 @@ func (d *YouboraDataSource) doRequest(ctx context.Context, qm *QueryModel) (body
url := buildQuery(d, qm)

rsp, err := d.httpclient.Get(url)
log.DefaultLogger.Debug("DEBUG URL", "url", url)

if err != nil {
log.DefaultLogger.Error("failed executing GET.", "error", err)

return body, err
}
defer rsp.Body.Close()

if rsp.StatusCode > 399 {
return nil, fmt.Errorf("invalid HTTP response %v", rsp.Status)
}

if body, err = io.ReadAll(rsp.Body); err != nil {
return nil, err
}

if rsp.StatusCode > 399 {
var parsed map[string]interface{}
err := json.Unmarshal(body, &parsed)
if err != nil {
return nil, fmt.Errorf("invalid HTTP response %v (invalid response json)", rsp.Status)
}
return nil, fmt.Errorf("invalid API request: %v", parsed)
}

return body, err
}
2 changes: 1 addition & 1 deletion src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/grafana/grafana/master/docs/sources/developers/plugins/plugin.schema.json",
"type": "datasource",
"name": "Youbora",
"id": "nedimar-youbora-datasource",
"id": "globocom-youbora-datasource",
"metrics": true,
"backend": true,
"alerting": true,
Expand Down

0 comments on commit 73d0f2e

Please sign in to comment.