Skip to content

Commit

Permalink
Implemented Neo4J DataTypes, Nullable Columns, Healthcheck tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Kniep <[email protected]>
  • Loading branch information
denniskniep committed Jan 23, 2022
1 parent 72dd79c commit e71e134
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 41 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,27 @@ Grafana is started by docker-compose in development mode therefore no restart of

### Run example queries

Nodes
```
Match(m:Movie) return m
```

Tabledata
```
Match(m:Movie) return m
```

Timeseriesdata
```
with timestamp() - (1*60*60*1000) as Time Return Time, 1 as Test
UNION
with timestamp() - (2*60*60*1000) as Time Return Time, 2 as Test
UNION
with timestamp() - (3*60*60*1000) as Time Return Time, 3 as Test
return datetime() - duration({minutes: 1}) as Time, 99 as Test
UNION ALL
return datetime() - duration({minutes: 2}) as Time, 85 as Test
UNION ALL
return datetime() - duration({minutes: 3}) as Time, 86 as Test
UNION ALL
return datetime() - duration({minutes: 4}) as Time, 100 as Test
UNION ALL
return datetime() - duration({minutes: 5}) as Time, 32 as Test
```

## Signing
Expand Down
9 changes: 2 additions & 7 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ services:
environment:
#- NEO4J_AUTH=none
- NEO4J_AUTH=neo4j/Password123
volumes:
- neo4j-data:/data
- NEO4JLABS_PLUGINS=["apoc"]
ports:
- 7474:7474
- 7687:7687
Expand All @@ -31,8 +30,4 @@ services:
- ./neo4j/initMovieDb.cql:/initMovieDb.cql
- ./neo4j/wait-for.sh:/wait-for.sh
entrypoint: []
command: /bin/sh -c '/wait-for.sh http://neo4j:7474 -- cypher-shell -a neo4j://neo4j:7687 -u neo4j -p Password123 -f /initMovieDb.cql'


volumes:
neo4j-data:
command: /bin/sh -c '/wait-for.sh http://neo4j:7474 -- cypher-shell -a neo4j://neo4j:7687 -u neo4j -p Password123 -f /initMovieDb.cql'
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
environment:
#- NEO4J_AUTH=none
- NEO4J_AUTH=neo4j/Password123
- NEO4JLABS_PLUGINS=["apoc"]
volumes:
- neo4j-data:/data
ports:
Expand Down
62 changes: 48 additions & 14 deletions neo4j-datasource-plugin/pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
"github.com/neo4j/neo4j-go-driver/v4/neo4j/dbtype"
)

// Make sure SampleDatasource implements required interfaces. This is important to do
Expand Down Expand Up @@ -106,7 +107,7 @@ func query(settings neo4JSettings, query neo4JQuery) (backend.DataResponse, erro
}
defer driver.Close()

session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
session := driver.NewSession(neo4j.SessionConfig{DatabaseName: settings.Database, AccessMode: neo4j.AccessModeRead})
defer session.Close()

result, err := session.Run(query.CypherQuery, map[string]interface{}{})
Expand Down Expand Up @@ -145,12 +146,13 @@ func toDataResponse(result neo4j.Result) (backend.DataResponse, error) {
row := 0
for currentRecord != nil {
values := result.Record().Values

vals := make([]interface{}, len(frame.Fields))
for col, v := range values {
f := frame.Fields[row]
f.Extend(1)
conValue := toValue(v)
frame.Set(col, row, conValue)
val := toValue(v)
vals[col] = val
}
frame.AppendRow(vals...)
if result.Next() {
currentRecord = result.Record()
row++
Expand All @@ -169,11 +171,13 @@ func toDataResponse(result neo4j.Result) (backend.DataResponse, error) {
// datasource configuration page which allows users to verify that
// a datasource is working as expected.
func (d *SampleDatasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
log.DefaultLogger.Info("CheckHealth called")
return checkHealth(req.PluginContext.DataSourceInstanceSettings)
}

settings, err := unmarshalDataSourceSettings(req.PluginContext.DataSourceInstanceSettings)
func checkHealth(dataSourceInstanceSettings *backend.DataSourceInstanceSettings) (*backend.CheckHealthResult, error) {
log.DefaultLogger.Info("CheckHealth called")

log.DefaultLogger.Info("Settings:", settings)
settings, err := unmarshalDataSourceSettings(dataSourceInstanceSettings)

if err != nil {
return &backend.CheckHealthResult{
Expand Down Expand Up @@ -215,16 +219,22 @@ func unmarshalDataSourceSettings(dSIset *backend.DataSourceInstanceSettings) (ne
//https://github.com/neo4j/neo4j-go-driver#value-types
func getTypeArray(record *neo4j.Record, idx int) interface{} {
if record == nil {
return []string{}
return []*string{}
}

typ := record.Values[idx]

switch typ.(type) {
case int64:
return []int64{}
return []*int64{}
case float64:
return []*float64{}
case bool:
return []*bool{}
case time.Time, dbtype.Date, dbtype.Time, dbtype.LocalTime, dbtype.LocalDateTime:
return []*time.Time{}
default:
return []string{}
return []*string{}
}
}

Expand All @@ -234,14 +244,38 @@ func toValue(val interface{}) interface{} {
return nil
}
switch t := val.(type) {
case string, int64:
return t
case string:
return &t
case int64:
return &t
case bool:
return &t
case float64:
return &t
case time.Time:
return &t
case dbtype.Date:
val := t.Time()
return &val
case dbtype.Time:
val := t.Time()
return &val
case dbtype.LocalTime:
val := t.Time()
return &val
case dbtype.LocalDateTime:
val := t.Time()
return &val
case dbtype.Duration:
val := t.String()
return &val
default:
r, err := json.Marshal(val)
if err != nil {
log.DefaultLogger.Info("Marshalling failed ", "err", err)
}
return string(r)
val := string(r)
return &val
}
}

Expand Down
Loading

0 comments on commit e71e134

Please sign in to comment.