Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
hrl20 committed Jan 3, 2025
1 parent 13a7154 commit 2ca72eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
23 changes: 6 additions & 17 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plugin

import (
"context"
"sync"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
Expand All @@ -23,7 +22,12 @@ var (
// NewDatasource creates a new `SQLDatasource`.
// It uses the provided settings argument to call the ds.Driver to connect to the SQL server
func (ds *SQLDatasourceWithDebug) NewDatasource(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
return ds.SQLDatasource.NewDatasource(ctx, settings)
newSqlDs, err := ds.SQLDatasource.NewDatasource(ctx, settings)
if err != nil {
return nil, err
}
ds.SQLDatasource = newSqlDs.(*sqlds.SQLDatasource)
return ds, nil
}

// SQLDatasourceWithDebug
Expand Down Expand Up @@ -53,21 +57,6 @@ func (d *SQLDatasourceWithDebug) Dispose() {
// The QueryDataResponse contains a map of RefID to the response for each query, and each response
// contains Frames ([]*Frame).
func (d *SQLDatasourceWithDebug) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
var wg = sync.WaitGroup{}

wg.Add(len(req.Queries))

// Execute each query and store the results by query RefID
for _, q := range req.Queries {
go func(query backend.DataQuery) {
// log query
backend.Logger.Info("Going to query", "query", query)
wg.Done()
}(q)
}

wg.Wait()

response, err := d.SQLDatasource.QueryData(ctx, req)

return response, err
Expand Down
11 changes: 9 additions & 2 deletions pkg/plugin/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@ package plugin

import (
"context"
"encoding/json"
"testing"

"github.com/grafana/grafana-plugin-sdk-go/backend"
)

func TestQueryData(t *testing.T) {
ds := NewDatasource(&DuckDBDriver{})
_, err := ds.NewDatasource(context.Background(), backend.DataSourceInstanceSettings{})
_, err := ds.NewDatasource(context.Background(), backend.DataSourceInstanceSettings{
JSONData: []byte(`{"path":""}`),
})

resp, err := ds.QueryData(
context.Background(),
&backend.QueryDataRequest{
PluginContext: backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
},
Queries: []backend.DataQuery{
{RefID: "A"},
{RefID: "A", JSON: json.RawMessage(`{"rawSql": "from duckdb_settings();"}`)},
},
},
)
if err != nil {
t.Error(err)
}

backend.Logger.Info("Response", "resp", resp)
if len(resp.Responses) != 1 {
t.Fatal("QueryData must return a response")
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/plugin/duckdb_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ import (
type DuckDBDriver struct {
}

var allowedSettings = []string{"access_mode", "checkpoint_threshold", "debug_checkpoint_abort", "debug_force_external", "debug_force_no_cross_product", "debug_asof_iejoin", "prefer_range_joins", "debug_window_mode", "default_collation", "default_order", "default_null_order", "disabled_filesystems", "disabled_optimizers", "enable_external_access", "enable_fsst_vectors", "allow_unsigned_extensions", "custom_extension_repository", "autoinstall_extension_repository", "autoinstall_known_extensions", "autoload_known_extensions", "enable_object_cache", "enable_http_metadata_cache", "enable_profiling", "enable_progress_bar", "enable_progress_bar_print", "explain_output", "extension_directory", "external_threads", "file_search_path", "force_compression", "force_bitpacking_mode", "home_directory", "log_query_path", "lock_configuration", "immediate_transaction_mode", "integer_division", "max_expression_depth", "max_memory", "memory_limit", "null_order", "ordered_aggregate_threshold", "password", "perfect_ht_threshold", "pivot_filter_threshold", "pivot_limit", "preserve_identifier_case", "preserve_insertion_order", "profiler_history_size", "profile_output", "profiling_mode", "profiling_output", "progress_bar_time", "schema", "search_path", "temp_directory", "threads", "username", "arrow_large_buffer_size", "user", "wal_autocheckpoint", "worker_threads", "allocator_flush_threshold", "duckdb_api", "custom_user_agent", "motherduck_saas_mode", "motherduck_database_uuid", "motherduck_use_tls", "motherduck_background_catalog_refresh_long_poll_timeout", "motherduck_background_catalog_refresh_inactivity_timeout", "motherduck_lease_timeout", "motherduck_database_name", "motherduck_port", "motherduck_log_level", "pandas_analyze_sample", "motherduck_host", "motherduck_background_catalog_refresh", "binary_as_string", "motherduck_token", "Calendar", "TimeZone"}

// parse config from settings.JSONData
func parseConfig(settings backend.DataSourceInstanceSettings) (map[string]string, error) {

config := make(map[string]string)
err := json.Unmarshal(settings.JSONData, &config)
if err != nil {
Expand All @@ -38,7 +35,6 @@ func parseConfig(settings backend.DataSourceInstanceSettings) (map[string]string
}

func (d *DuckDBDriver) Connect(ctx context.Context, settings backend.DataSourceInstanceSettings, msg json.RawMessage) (*sql.DB, error) {

config, err := models.LoadPluginSettings(settings)
if err != nil {
return nil, err
Expand All @@ -47,6 +43,7 @@ func (d *DuckDBDriver) Connect(ctx context.Context, settings backend.DataSourceI
if config.Secrets.MotherDuckToken != "" {
os.Setenv("motherduck_token", config.Secrets.MotherDuckToken)
}

// // join config as url parmaeters
// config, err := parseConfig(settings)

Expand Down Expand Up @@ -76,15 +73,16 @@ func (d *DuckDBDriver) Connect(ctx context.Context, settings backend.DataSourceI
// Join all parts with '&' to form the final query string
// queryString := strings.Join(parts, "&")
// dbString := strings.Join([]string{dbPath, queryString}, "?")

connector, err := duckdb.NewConnector(config.Path, func(execer driver.ExecerContext) error {
// read env variable GF_PATHS_HOME
homePath := os.Getenv("GF_PATHS_HOME")

bootQueries := []string{
"INSTALL 'motherduck'",
"LOAD 'motherduck'",
}

// read env variable GF_PATHS_HOME
homePath := os.Getenv("GF_PATHS_HOME")
if homePath != "" {
bootQueries = append(bootQueries, "SET home_directory='"+homePath+"'")
}
Expand Down

0 comments on commit 2ca72eb

Please sign in to comment.