Skip to content

Commit 59e72cc

Browse files
authored
Update the grafana dashboard templater (#1)
* Add unittests * Add/ Update GitHub actions * Add docstrings * Add dashboard example * Update tests, src, documentation, github actions and add setup.py * Update the documentation and Github actions * Update the tests and setup.py * Update the README.md * Add coverage badge
1 parent c07e300 commit 59e72cc

File tree

14 files changed

+470
-84
lines changed

14 files changed

+470
-84
lines changed

.coveragerc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[run]
2+
branch = True
3+
omit =
4+
*/setup.py
5+
*/dashboard.json.sample
6+
tests/*
7+
source = .
8+
9+
[report]
10+
omit =
11+
*/setup.py
12+
*/dashboard.json.sample
13+
tests/*
14+
fail_under = 80
15+
show_missing = True
16+
skip_covered = False
17+
exclude_lines =
18+
# Have to re-enable the standard pragma
19+
pragma: no cover
20+
21+
# Don't complain if tests don't hit defensive assertion code:
22+
raise AssertionError
23+
raise NotImplementedError
24+
25+
# Don't complain if non-runnable code isn't run:
26+
if __name__ == .__main__.:

.flake8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[flake8]
2+
ignore = E203, E266, E501, W503
3+
max-line-length = 120
4+
max-complexity = 20
5+
select = B,C,D,E,F,W,T4,B902,B950
6+
exclude = venc,.git
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Build and publish
2+
3+
on:
4+
release:
5+
types: [ published ]
6+
7+
jobs:
8+
9+
build-and-publish:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
python-version: ['3.x']
14+
15+
steps:
16+
- name: Checkout the repository and the branch
17+
uses: actions/checkout@v2
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
architecture: x64
24+
cache: 'pip'
25+
26+
- name: Install the requirements
27+
run: pip install -r requirements.txt
28+
29+
- name: Install pypa/build
30+
run: >-
31+
python -m
32+
pip install
33+
build
34+
--user
35+
36+
- name: Build a binary wheel and a source tarball
37+
run: >-
38+
python -m
39+
build
40+
--sdist
41+
--wheel
42+
--outdir dist/
43+
44+
- name: Publish distribution package to PyPI
45+
if: startsWith(github.ref, 'refs/tags')
46+
uses: pypa/gh-action-pypi-publish@master
47+
with:
48+
password: ${{ secrets.PYPI_API_TOKEN }}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: PR checks
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
7+
jobs:
8+
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
python-version: [ '3.x' ]
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v2
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
architecture: x64
23+
cache: 'pip'
24+
25+
- name: Install the requirements
26+
run: pip install -r requirements.txt
27+
28+
- name: Execute the unittests
29+
run: python3 -m unittest discover tests
30+
31+
lint:
32+
runs-on: ubuntu-latest
33+
strategy:
34+
matrix:
35+
python-version: [ '3.x' ]
36+
37+
steps:
38+
- uses: actions/checkout@v2
39+
40+
- name: Set up Python
41+
uses: actions/setup-python@v2
42+
with:
43+
python-version: ${{ matrix.python-version }}
44+
architecture: x64
45+
cache: 'pip'
46+
47+
- name: Install the requirements
48+
run: pip install -r requirements.txt
49+
50+
- name: Execute the linting checks
51+
uses: reviewdog/[email protected]
52+
with:
53+
github_token: ${{ secrets.GITHUB_TOKEN }}
54+
flake8_args: --config=.flake8
55+
56+
coverage:
57+
runs-on: ubuntu-latest
58+
strategy:
59+
matrix:
60+
python-version: [ '3.x' ]
61+
62+
steps:
63+
- uses: actions/checkout@v2
64+
with:
65+
persist-credentials: false
66+
fetch-depth: 0
67+
68+
- name: Set up Python
69+
uses: actions/setup-python@v2
70+
with:
71+
python-version: ${{ matrix.python-version }}
72+
architecture: x64
73+
cache: 'pip'
74+
75+
- name: Install the requirements
76+
run: pip install -r requirements.txt && pip install pytest pytest-cov coverage-badge
77+
78+
- name: Generate the coverage report
79+
run: export PYTHONPATH=$PWD && pytest --junitxml=pytest.xml --cov=. tests/ | tee pytest-coverage.txt
80+
81+
- name: Execute the coverage checks
82+
uses: MishaKav/[email protected]
83+
with:
84+
github_token: ${{ secrets.GITHUB_TOKEN }}
85+
pytest-coverage-path: ./pytest-coverage.txt
86+
junitxml-path: ./pytest.xml
87+
hide-badge: true
88+
create-new-commit: true
89+
90+
- name: "Check if coverage badge file existence"
91+
id: check_files
92+
uses: andstor/file-existence-action@v1
93+
with:
94+
files: "docs/coverage.svg"
95+
96+
- name: Generate coverage badge
97+
if: steps.check_files.outputs.files_exists == 'false'
98+
run: coverage-badge -o docs/coverage.svg -f
99+
100+
- name: Commit files
101+
if: steps.check_files.outputs.files_exists == 'false'
102+
run: |
103+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
104+
git config --local user.name "github-actions[bot]"
105+
git add --force docs/coverage.svg
106+
git commit -m "Add coverage badge"
107+
108+
- name: Push changes
109+
if: steps.check_files.outputs.files_exists == 'false'
110+
uses: ad-m/github-push-action@master
111+
with:
112+
github_token: ${{ secrets.GITHUB_TOKEN }}
113+
branch: ${{ github.head_ref }}
114+
force: true

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
# grafana_dashboard_templater
1+
# Grafana dashboard templater ![Coverage report](docs/coverage.svg)
22

3-
## TODO
4-
- Documentation
5-
- Docstrings
6-
- PYPI support
3+
## Description
74

8-
## Installation & Requirements
5+
The Grafana dashboard templater create a valid Grafana dashboard as dictionary based on a template and injected values.
96

10-
### Programs & tools to install
7+
## Dashboard template folder structure
118

12-
- Jinja2
9+
```
10+
dashboard-templates <- Folder of the dashboard templates
11+
database <- Dashboard type
12+
postgresql <- Dashboard name
13+
v13 <- Dashboard version
14+
dashboard.json.sample <- Dashboard template
15+
```
16+
17+
## Installation
18+
19+
`pip install grafana-dashboard-templater`
20+
21+
## Example
22+
23+
```python
24+
from grafana_dashboard.model import Model
25+
from grafana_dashboard.dashboard import Dashboard
26+
27+
dashboard_model: Model = Model(dashboard_templates_path="./dashboard-templates", dashboard_type="database",
28+
dashboard_name="postgresql", dashboard_version="v13")
29+
30+
dashboard: Dashboard = Dashboard(dashboard_model)
31+
dashboard_json = dashboard.get_dashboard_json(template_values={"app_name": "PostgreSQL", "prometheus_name": "k8s-sonarqube-postgresql"})
32+
```
1333

1434
## Contribution
1535
If you would like to contribute something, have an improvement request, or want to make a change inside the code, please open a pull request.

docs/.placeholder

Whitespace-only changes.

docs/coverage.svg

Lines changed: 21 additions & 0 deletions
Loading

grafana_dashboard/dashboard.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import setuptools
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="grafana-dashboard-templater",
8+
version="0.0.1",
9+
author="Pascal Zimmermann",
10+
author_email="[email protected]",
11+
description="A Grafana dashboard templater",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/ZPascal/grafana_dashboard_templater",
15+
project_urls={
16+
"Bug Tracker": "https://github.com/ZPascal/grafana_dashboard_templater/issues",
17+
},
18+
classifiers=[
19+
"Programming Language :: Python :: 3",
20+
"License :: OSI Approved :: Apache 2.0 License",
21+
"Operating System :: OS Independent",
22+
],
23+
package_dir={"": "src"},
24+
packages=setuptools.find_packages(where="src"),
25+
install_requires=["jinja2"],
26+
python_requires=">=3.6",
27+
)
File renamed without changes.

0 commit comments

Comments
 (0)