Skip to content

Commit 9fc4a0f

Browse files
feat: Add datasource caching functionality (#30)
* feat: Add datasource caching functionality * docs: Add coverage badge and documentation --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 6d2c4da commit 9fc4a0f

File tree

8 files changed

+594
-12
lines changed

8 files changed

+594
-12
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ The main feature that is implemented inside this library:
1010

1111
In general my focus inside this project is to implement and deliver old and new features from the Grafana API, to document all features and functionality clear and to increase the overall test coverage of the project.
1212

13-
## Features on the roadmap
14-
- [ ] [Query and resource caching API](https://grafana.com/docs/grafana/latest/developers/http_api/query_and_resource_caching/) Planned for CW 26/27
15-
1613
## Currently, supported features
1714

1815
### Dashboard
@@ -63,6 +60,13 @@ In general my focus inside this project is to implement and deliver old and new
6360
- Add datasource permissions
6461
- Delete datasource permissions
6562

63+
### Datasource query and resource caching
64+
- Get datasource cache by uid
65+
- Enable datasource cache by uid
66+
- Disable datasource cache by uid
67+
- Update datasource cache by uid
68+
- Clean all datasource caches
69+
6670
### Legacy Alerting
6771
- Get alerts
6872
- Get alerts by dashboard ids

docs/content/grafana_api/datasource.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
* [get\_datasource\_permissions](#datasource.Datasource.get_datasource_permissions)
1919
* [add\_datasource\_permissions](#datasource.Datasource.add_datasource_permissions)
2020
* [delete\_datasource\_permissions](#datasource.Datasource.delete_datasource_permissions)
21+
* [DatasourceQueryResourceCaching](#datasource.DatasourceQueryResourceCaching)
22+
* [get\_datasource\_cache](#datasource.DatasourceQueryResourceCaching.get_datasource_cache)
23+
* [enable\_datasource\_cache](#datasource.DatasourceQueryResourceCaching.enable_datasource_cache)
24+
* [disable\_datasource\_cache](#datasource.DatasourceQueryResourceCaching.disable_datasource_cache)
25+
* [clean\_datasource\_cache](#datasource.DatasourceQueryResourceCaching.clean_datasource_cache)
26+
* [update\_datasource\_cache](#datasource.DatasourceQueryResourceCaching.update_datasource_cache)
2127

2228
<a id="datasource"></a>
2329

@@ -504,3 +510,171 @@ The method includes a functionality to delete datasource permission specified by
504510

505511
None
506512

513+
<a id="datasource.DatasourceQueryResourceCaching"></a>
514+
515+
## DatasourceQueryResourceCaching Objects
516+
517+
```python
518+
class DatasourceQueryResourceCaching()
519+
```
520+
521+
The class includes all necessary methods to access the Grafana datasource query and resource caching API endpoints. It's required that the API token got the corresponding datasource access rights. Please check the used methods docstring for the necessary access rights. The functionality is a Grafana ENTERPRISE feature
522+
523+
HINT: Note Grafana Enterprise API need required permissions if fine-grained access control is enabled
524+
525+
**Arguments**:
526+
527+
- `grafana_api_model` _APIModel_ - Inject a Grafana API model object that includes all necessary values and information
528+
529+
530+
**Attributes**:
531+
532+
- `grafana_api_model` _APIModel_ - This is where we store the grafana_api_model
533+
534+
<a id="datasource.DatasourceQueryResourceCaching.get_datasource_cache"></a>
535+
536+
#### get\_datasource\_cache
537+
538+
```python
539+
def get_datasource_cache(uid: str) -> dict
540+
```
541+
542+
The method includes a functionality to get the datasource cache config specified by the datasource uid
543+
544+
**Arguments**:
545+
546+
- `uid` _str_ - Specify the uid of the datasource
547+
548+
Required Permissions:
549+
- `Action` - datasources.caching:write
550+
- `Scope` - datasources:*
551+
552+
553+
**Raises**:
554+
555+
- `ValueError` - Missed specifying a necessary value
556+
- `Exception` - Unspecified error by executing the API call
557+
558+
559+
**Returns**:
560+
561+
- `api_call` _dict_ - Returns a datasource
562+
563+
<a id="datasource.DatasourceQueryResourceCaching.enable_datasource_cache"></a>
564+
565+
#### enable\_datasource\_cache
566+
567+
```python
568+
def enable_datasource_cache(uid: str) -> dict
569+
```
570+
571+
The method includes a functionality to enable the datasource cache specified by the datasource uid
572+
573+
**Arguments**:
574+
575+
- `uid` _str_ - Specify the uid of the datasource
576+
577+
Required Permissions:
578+
- `Action` - datasources.caching:read
579+
- `Scope` - datasources:*
580+
581+
582+
**Raises**:
583+
584+
- `ValueError` - Missed specifying a necessary value
585+
- `Exception` - Unspecified error by executing the API call
586+
587+
588+
**Returns**:
589+
590+
- `api_call` _dict_ - Returns a datasource
591+
592+
<a id="datasource.DatasourceQueryResourceCaching.disable_datasource_cache"></a>
593+
594+
#### disable\_datasource\_cache
595+
596+
```python
597+
def disable_datasource_cache(uid: str) -> dict
598+
```
599+
600+
The method includes a functionality to disable the datasource cache specified by the datasource uid
601+
602+
**Arguments**:
603+
604+
- `uid` _str_ - Specify the uid of the datasource
605+
606+
Required Permissions:
607+
- `Action` - datasources.caching:write
608+
- `Scope` - datasources:*
609+
610+
611+
**Raises**:
612+
613+
- `ValueError` - Missed specifying a necessary value
614+
- `Exception` - Unspecified error by executing the API call
615+
616+
617+
**Returns**:
618+
619+
- `api_call` _dict_ - Returns a datasource
620+
621+
<a id="datasource.DatasourceQueryResourceCaching.clean_datasource_cache"></a>
622+
623+
#### clean\_datasource\_cache
624+
625+
```python
626+
def clean_datasource_cache(uid: str) -> dict
627+
```
628+
629+
The method includes a functionality to clean the datasource cache of all data sources with caching enabled. The uid of the datasource will only be used to return the configuration for that data source
630+
631+
**Arguments**:
632+
633+
- `uid` _str_ - Specify the uid of the datasource
634+
635+
Required Permissions:
636+
- `Action` - datasources.caching:write
637+
- `Scope` - datasources:*
638+
639+
640+
**Raises**:
641+
642+
- `ValueError` - Missed specifying a necessary value
643+
- `Exception` - Unspecified error by executing the API call
644+
645+
646+
**Returns**:
647+
648+
- `api_call` _dict_ - Returns a datasource
649+
650+
<a id="datasource.DatasourceQueryResourceCaching.update_datasource_cache"></a>
651+
652+
#### update\_datasource\_cache
653+
654+
```python
655+
def update_datasource_cache(uid: str,
656+
datasource_cache: DatasourceCache) -> dict
657+
```
658+
659+
The method includes a functionality to update the datasource cache specified by the datasource uid
660+
661+
**Arguments**:
662+
663+
- `uid` _str_ - Specify the uid of the datasource
664+
- `datasource_cache` _DatasourceCache_ - Specif the datasource cache object
665+
666+
Required Permissions:
667+
- `Action` - datasources.caching:write
668+
- `Scope` - datasources:*
669+
670+
671+
**Raises**:
672+
673+
- `ValueError` - Missed specifying a necessary value
674+
- `Exception` - Unspecified error by executing the API call
675+
676+
677+
**Returns**:
678+
679+
- `api_call` _dict_ - Returns a datasource
680+

docs/content/grafana_api/model.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* [GlobalUser](#model.GlobalUser)
3636
* [RolePermission](#model.RolePermission)
3737
* [CustomRole](#model.CustomRole)
38+
* [DatasourceCache](#model.DatasourceCache)
3839

3940
<a id="model"></a>
4041

@@ -683,3 +684,23 @@ The class includes all necessary variables to generate a custom role object
683684
- `hidden` _bool_ - Specify whether the role is hidden or not. If set to True, then the role does not show in the role picker. It will not be listed by API endpoints unless explicitly specified (default False)
684685
- `permissions` _list_ - Specify the optional permissions of the role as a list of the RolePermission objects (default None)
685686

687+
<a id="model.DatasourceCache"></a>
688+
689+
## DatasourceCache Objects
690+
691+
```python
692+
@dataclass
693+
class DatasourceCache()
694+
```
695+
696+
The class includes all necessary variables to generate a datasource cache object
697+
698+
**Arguments**:
699+
700+
- `datasource_id` _int_ - Specify the datasource id
701+
- `datasource_uid` _str_ - Specify the datasource uid
702+
- `enabled` _bool_ - Specify if caching should be enabled for the datasource
703+
- `use_default_ttl` _bool_ - Specify if the configured default TTL (Time-To-Live) should be used for both query and resource caching, instead of the user-specified values
704+
- `ttl_queries_ms` _int_ - Specify the TTL to use for query caching, in milliseconds
705+
- `ttl_resources_ms` _int_ - Specify the TTL to use for resource caching, in milliseconds
706+

docs/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)