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

hackathon: add backend graphql query support #457

Draft
wants to merge 43 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4bb151f
Add backend query code path
polomani Sep 25, 2024
8cd96ab
duplicate the data modeling tab
bkuzma Sep 25, 2024
04315a1
add backend (does not work)
polomani Sep 25, 2024
5298907
Update datasource.ts
polomani Sep 25, 2024
2f814e1
add a post processing field
bkuzma Sep 25, 2024
be09cbb
seems to work (not auth)
polomani Sep 25, 2024
8c6db27
extend query object
polomani Sep 25, 2024
ddd8ff9
Merge remote-tracking branch 'origin/hackathon-data-modeling' into ha…
polomani Sep 25, 2024
c54dc17
rename the tab
polomani Sep 25, 2024
4bf09cb
do no filter out
polomani Sep 25, 2024
2180bb9
enable alerting
polomani Sep 25, 2024
0cad6a6
commit default query
polomani Sep 25, 2024
65db1ea
support client creds
polomani Sep 25, 2024
425c447
support client creds
polomani Sep 25, 2024
375d940
add token pass thru
polomani Sep 27, 2024
147668f
__from & __to interpolation
polomani Oct 25, 2024
edd3336
Merge branch 'master' into hackathon-add-backend-query-path
polomani Jan 7, 2025
d2d229e
disable old tests, add e2e
polomani Jan 10, 2025
882d891
fixup dev dep
polomani Jan 10, 2025
12547b5
Update yarn.lock
polomani Jan 10, 2025
5b34343
Update yarn.lock
polomani Jan 10, 2025
80a5a17
correct lock file
polomani Jan 10, 2025
f4699e9
Merge branch 'master' into hackathon-add-backend-query-path
polomani Jan 13, 2025
896f5f2
enable e2e tests in CI
polomani Jan 13, 2025
502158b
make backend tests pass
polomani Jan 14, 2025
e5f2094
add outputs
polomani Jan 14, 2025
9c5569d
Update buind_n_test.yml
polomani Jan 14, 2025
70943a4
Update buind_n_test.yml
polomani Jan 14, 2025
67b529f
fix the pod name
polomani Jan 14, 2025
9ef0191
add playwright connection env vars
polomani Jan 14, 2025
afdbc63
adjust the version matrix
polomani Jan 14, 2025
75cdd0a
enable successful auth test
polomani Jan 15, 2025
17a17f1
pass the secret into e2e
polomani Jan 15, 2025
8000d3c
try to restore tests
polomani Jan 15, 2025
77da412
revive more tests
polomani Jan 15, 2025
07d10d6
revive error path tests
polomani Jan 15, 2025
1748964
fix some warnings/deprecated interfaces
polomani Jan 15, 2025
20dfb7f
Fix backend test
polomani Jan 16, 2025
d14bc3f
fixup
polomani Jan 16, 2025
8f93273
env vars
polomani Jan 16, 2025
254aa6b
Update buind_n_test.yml
polomani Jan 16, 2025
65b3217
Merge branch 'master' into hackathon-add-backend-query-path
polomani Jan 20, 2025
59ab14d
add provisioning
polomani Jan 29, 2025
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
67 changes: 64 additions & 3 deletions .config/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,75 @@ ARG grafana_image=grafana-enterprise

FROM grafana/${grafana_image}:${grafana_version}

ARG anonymous_auth_enabled=true
ARG development=true
ARG TARGETARCH

ARG GO_VERSION=1.21.6
ARG GO_ARCH=${TARGETARCH:-amd64}

ENV DEV "${development}"

# Make it as simple as possible to access the grafana instance for development purposes
# Do NOT enable these settings in a public facing / production grafana instance
ENV GF_AUTH_ANONYMOUS_ORG_ROLE "Admin"
ENV GF_AUTH_ANONYMOUS_ENABLED "true"
ENV GF_AUTH_ANONYMOUS_ENABLED "${anonymous_auth_enabled}"
ENV GF_AUTH_BASIC_ENABLED "false"
# Set development mode so plugins can be loaded without the need to sign
ENV GF_DEFAULT_APP_MODE "development"

# Inject livereload script into grafana index.html

LABEL maintainer="Grafana Labs <[email protected]>"

ENV GF_PATHS_HOME="/usr/share/grafana"
WORKDIR $GF_PATHS_HOME

USER root
RUN sed -i 's/<\/body><\/html>/<script src=\"http:\/\/localhost:35729\/livereload.js\"><\/script><\/body><\/html>/g' /usr/share/grafana/public/views/index.html

# Installing supervisor and inotify-tools
RUN if [ "${development}" = "true" ]; then \
if grep -i -q alpine /etc/issue; then \
apk add supervisor inotify-tools git; \
elif grep -i -q ubuntu /etc/issue; then \
DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -y supervisor inotify-tools git && \
rm -rf /var/lib/apt/lists/*; \
else \
echo 'ERROR: Unsupported base image' && /bin/false; \
fi \
fi

COPY supervisord/supervisord.conf /etc/supervisor.d/supervisord.ini
COPY supervisord/supervisord.conf /etc/supervisor/conf.d/supervisord.conf


# Installing Go
RUN if [ "${development}" = "true" ]; then \
curl -O -L https://golang.org/dl/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \
rm -rf /usr/local/go && \
tar -C /usr/local -xzf go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \
echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bashrc && \
rm -f go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \
fi

# Installing delve for debugging
RUN if [ "${development}" = "true" ]; then \
/usr/local/go/bin/go install github.com/go-delve/delve/cmd/dlv@latest; \
fi

# Installing mage for plugin (re)building
RUN if [ "${development}" = "true" ]; then \
git clone https://github.com/magefile/mage; \
cd mage; \
export PATH=$PATH:/usr/local/go/bin; \
go run bootstrap.go; \
fi

# Inject livereload script into grafana index.html
RUN sed -i 's|</body>|<script src="http://localhost:35729/livereload.js"></script></body>|g' /usr/share/grafana/public/views/index.html


COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
18 changes: 18 additions & 0 deletions .config/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

if [ "${DEV}" = "false" ]; then
echo "Starting test mode"
exec /run.sh
fi

echo "Starting development mode"

if grep -i -q alpine /etc/issue; then
exec /usr/bin/supervisord -c /etc/supervisord.conf
elif grep -i -q ubuntu /etc/issue; then
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
else
echo 'ERROR: Unsupported base image'
exit 1
fi

47 changes: 47 additions & 0 deletions .config/supervisord/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[supervisord]
nodaemon=true
user=root

[program:grafana]
user=root
directory=/var/lib/grafana
command=bash -c 'while [ ! -f /root/cognitedata-datasource/dist/gpx_dm_grafana_datasource* ]; do sleep 1; done; /run.sh'
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
killasgroup=true
stopasgroup=true
autostart=true

[program:delve]
user=root
command=/bin/bash -c 'pid=""; while [ -z "$pid" ]; do pid=$(pgrep -f gpx_dm_grafana_datasource); done; /root/go/bin/dlv attach --api-version=2 --headless --continue --accept-multiclient --listen=:2345 $pid'
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
killasgroup=false
stopasgroup=false
autostart=true
autorestart=true

[program:build-watcher]
user=root
command=/bin/bash -c 'while inotifywait -e modify,create,delete -r /var/lib/grafana/plugins/cognitedata-datasource; do echo "Change detected, restarting delve...";supervisorctl restart delve; done'
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
killasgroup=true
stopasgroup=true
autostart=true

[program:mage-watcher]
user=root
environment=PATH="/usr/local/go/bin:/root/go/bin:%(ENV_PATH)s"
directory=/root/cognitedata-datasource
command=/bin/bash -c 'git config --global --add safe.directory /root/cognitedata-datasource && mage -v watch'
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
killasgroup=true
stopasgroup=true
autostart=true
180 changes: 180 additions & 0 deletions .github/workflows/buind_n_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@ on:
pull_request:
branches: [ master ]

env:
COGNITE_HOST: westeurope-1.cognitedata.com
CLIENT_ID: fecf978a-25c9-45d8-8b80-f29f411929c2
TOKEN_URL: https://login.microsoftonline.com/a10bc71f-eb26-4e94-a961-58b5311eb4d0/oauth2/v2.0/token
COGNITE_PROJECT: grafana-plugin-test

jobs:
build-and-test:
runs-on: ubuntu-latest
outputs:
plugin-id: ${{ steps.metadata.outputs.plugin-id }}
plugin-version: ${{ steps.metadata.outputs.plugin-version }}
has-e2e: ${{ steps.check-for-e2e.outputs.has-e2e }}
has-backend: ${{ steps.check-for-backend.outputs.has-backend }}
steps:
- uses: actions/checkout@v4

Expand All @@ -29,3 +40,172 @@ jobs:
- name: Build frontend
run: yarn build

- name: Check for backend
id: check-for-backend
run: |
if [ -f "Magefile.go" ]
then
echo "has-backend=true" >> $GITHUB_OUTPUT
fi

- name: Setup Go environment
if: steps.check-for-backend.outputs.has-backend == 'true'
uses: actions/setup-go@v5
with:
go-version: '1.22'

- name: Test backend
env:
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
if: steps.check-for-backend.outputs.has-backend == 'true'
uses: magefile/mage-action@v3
with:
version: latest
args: coverage

- name: Build backend
if: steps.check-for-backend.outputs.has-backend == 'true'
uses: magefile/mage-action@v3
with:
version: latest
args: buildAll

- name: Check for E2E
id: check-for-e2e
run: |
if [ -f "playwright.config.ts" ]
then
echo "has-e2e=true" >> $GITHUB_OUTPUT
fi

- name: Get plugin metadata
id: metadata
run: |
sudo apt-get install jq

export GRAFANA_PLUGIN_ID=$(cat dist/plugin.json | jq -r .id)
export GRAFANA_PLUGIN_VERSION=$(cat dist/plugin.json | jq -r .info.version)
export GRAFANA_PLUGIN_ARTIFACT=${GRAFANA_PLUGIN_ID}-${GRAFANA_PLUGIN_VERSION}.zip

echo "plugin-id=${GRAFANA_PLUGIN_ID}" >> $GITHUB_OUTPUT
echo "plugin-version=${GRAFANA_PLUGIN_VERSION}" >> $GITHUB_OUTPUT
echo "archive=${GRAFANA_PLUGIN_ARTIFACT}" >> $GITHUB_OUTPUT

- name: Package plugin
id: package-plugin
run: |
mv dist ${{ steps.metadata.outputs.plugin-id }}
zip ${{ steps.metadata.outputs.archive }} ${{ steps.metadata.outputs.plugin-id }} -r

- name: Check plugin.json
run: |
docker run --pull=always \
-v $PWD/${{ steps.metadata.outputs.archive }}:/archive.zip \
grafana/plugin-validator-cli -analyzer=metadatavalid /archive.zip

- name: Archive Build
uses: actions/upload-artifact@v4
with:
name: ${{ steps.metadata.outputs.plugin-id }}-${{ steps.metadata.outputs.plugin-version }}
path: ${{ steps.metadata.outputs.plugin-id }}
retention-days: 5

resolve-versions:
name: Resolve e2e images
runs-on: ubuntu-latest
timeout-minutes: 3
needs: build-and-test
if: ${{ needs.build-and-test.outputs.has-e2e == 'true' }}
outputs:
matrix: ${{ steps.resolve-versions.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Resolve Grafana E2E versions
id: resolve-versions
uses: grafana/plugin-actions/e2e-version@main
with:
skip-grafana-dev-image: true
limit: 5

playwright-tests:
env:
GRAFANA_URL: http://localhost:2999
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}

needs: [resolve-versions, build-and-test]
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
GRAFANA_IMAGE: ${{fromJson(needs.resolve-versions.outputs.matrix)}}
name: e2e test ${{ matrix.GRAFANA_IMAGE.name }}@${{ matrix.GRAFANA_IMAGE.VERSION }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Download plugin
uses: actions/download-artifact@v4
with:
path: dist
name: ${{ needs.build-and-test.outputs.plugin-id }}-${{ needs.build-and-test.outputs.plugin-version }}

- name: Execute permissions on binary
if: needs.build-and-test.outputs.has-backend == 'true'
run: |
chmod +x ./dist/gpx_*

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Install dev dependencies
run: yarn install --immutable --prefer-offline

- name: Start Grafana
run: |
docker compose pull
ANONYMOUS_AUTH_ENABLED=false DEVELOPMENT=false GRAFANA_VERSION=${{ matrix.GRAFANA_IMAGE.VERSION }} GRAFANA_IMAGE=${{ matrix.GRAFANA_IMAGE.NAME }} docker compose up -d

- name: Wait for grafana server
uses: grafana/plugin-actions/wait-for-grafana@main
with:
url: ${{ env.GRAFANA_URL }}/login

- name: Install Playwright Browsers
run: npm exec playwright install chromium --with-deps

- name: Run Playwright tests
id: run-tests
run: yarn e2e

- name: Docker logs
if: ${{ always() && steps.run-tests.outcome == 'failure' }}
run: |
docker logs cognitedata-datasource >& grafana-server.log

- name: Stop grafana docker
run: docker compose down

- name: Upload server log
uses: actions/upload-artifact@v4
if: ${{ always() && steps.run-tests.outcome == 'failure' }}
with:
name: ${{ matrix.GRAFANA_IMAGE.NAME }}-v${{ matrix.GRAFANA_IMAGE.VERSION }}-${{github.run_id}}-server-log
path: grafana-server.log
retention-days: 5

- name: Get plugin metadata
id: metadata
run: |
sudo apt-get install jq

export GRAFANA_PLUGIN_ID=$(cat dist/plugin.json | jq -r .id)
export GRAFANA_PLUGIN_VERSION=$(cat dist/plugin.json | jq -r .info.version)
export GRAFANA_PLUGIN_ARTIFACT=${GRAFANA_PLUGIN_ID}-${GRAFANA_PLUGIN_VERSION}.zip

echo "plugin-id=${GRAFANA_PLUGIN_ID}" >> $GITHUB_OUTPUT
echo "plugin-version=${GRAFANA_PLUGIN_VERSION}" >> $GITHUB_OUTPUT
echo "archive=${GRAFANA_PLUGIN_ARTIFACT}" >> $GITHUB_OUTPUT
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ yarn-error.log*
.idea
.ionide/
.eslintcache

# e2e test directories
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
/playwright/.auth/

.env
.envrc
9 changes: 8 additions & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ cd /path/to/grafana/data/plugins
ln -s /path/to/cognite-grafana-datasource cognitedata-datasource
```

## Building

## Building backend
Build backend for Linux and run docker compose:
```bash
mage -v build:linuxARM64 && docker compose up
```

## Building frontend

`yarn` followed by `yarn build` should work on systems with a shell.

Expand Down
12 changes: 12 additions & 0 deletions Magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build mage
// +build mage

package main

import (
// mage:import
build "github.com/grafana/grafana-plugin-sdk-go/build"
)

// Default configures the default target.
var Default = build.BuildAll
Loading