4
4
"bytes"
5
5
"context"
6
6
"encoding/json"
7
- "errors"
8
7
"fmt"
9
8
"io"
10
9
"net/http"
@@ -26,6 +25,69 @@ type QuickwitDatasource struct {
26
25
dsInfo es.DatasourceInfo
27
26
}
28
27
28
+ type QuickwitMapping struct {
29
+ IndexConfig struct {
30
+ DocMapping struct {
31
+ TimestampField string `json:"timestamp_field"`
32
+ FieldMappings []struct {
33
+ Name string `json:"name"`
34
+ InputFormats []string `json:"input_formats"`
35
+ } `json:"field_mappings"`
36
+ } `json:"doc_mapping"`
37
+ } `json:"index_config"`
38
+ }
39
+
40
+ func getTimestampFieldInfos (index string , qwUrl string , cli * http.Client ) (string , string , error ) {
41
+ mappingEndpointUrl := qwUrl + "/indexes/" + index
42
+ qwlog .Info ("Calling quickwit endpoint: " + mappingEndpointUrl )
43
+ r , err := cli .Get (mappingEndpointUrl )
44
+ if err != nil {
45
+ errMsg := fmt .Sprintf ("Error when calling url = %s: err = %s" , mappingEndpointUrl , err .Error ())
46
+ qwlog .Error (errMsg )
47
+ return "" , "" , err
48
+ }
49
+
50
+ statusCode := r .StatusCode
51
+ if statusCode < 200 || statusCode >= 400 {
52
+ errMsg := fmt .Sprintf ("Error when calling url = %s: statusCode = %d" , mappingEndpointUrl , statusCode )
53
+ qwlog .Error (errMsg )
54
+ return "" , "" , fmt .Errorf (errMsg )
55
+ }
56
+
57
+ defer r .Body .Close ()
58
+ body , err := io .ReadAll (r .Body )
59
+ if err != nil {
60
+ errMsg := fmt .Sprintf ("Error when calling url = %s: err = %s" , mappingEndpointUrl , err .Error ())
61
+ qwlog .Error (errMsg )
62
+ return "" , "" , err
63
+ }
64
+
65
+ var payload QuickwitMapping
66
+ err = json .Unmarshal (body , & payload )
67
+ if err != nil {
68
+ errMsg := fmt .Sprintf ("Unmarshalling body error: %s" , string (body ))
69
+ qwlog .Error (errMsg )
70
+ return "" , "" , fmt .Errorf (errMsg )
71
+ }
72
+
73
+ timestampFieldName := payload .IndexConfig .DocMapping .TimestampField
74
+ timestampFieldFormat := "undef"
75
+ for _ , field := range payload .IndexConfig .DocMapping .FieldMappings {
76
+ if field .Name == timestampFieldName && len (field .InputFormats ) > 0 {
77
+ timestampFieldFormat = field .InputFormats [0 ]
78
+ break
79
+ }
80
+ }
81
+
82
+ if timestampFieldFormat == "undef" {
83
+ errMsg := fmt .Sprintf ("No format found for field: %s" , string (timestampFieldName ))
84
+ return timestampFieldName , "" , fmt .Errorf (errMsg )
85
+ }
86
+
87
+ qwlog .Info (fmt .Sprintf ("Found timestampFieldName = %s, timestampFieldFormat = %s" , timestampFieldName , timestampFieldFormat ))
88
+ return timestampFieldName , timestampFieldFormat , nil
89
+ }
90
+
29
91
// Creates a Quickwit datasource.
30
92
func NewQuickwitDatasource (settings backend.DataSourceInstanceSettings ) (instancemgmt.Instance , error ) {
31
93
qwlog .Debug ("Initializing new data source instance" )
@@ -50,19 +112,8 @@ func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instanc
50
112
return nil , err
51
113
}
52
114
53
- timeField , ok := jsonData ["timeField" ].(string )
54
- if ! ok {
55
- return nil , errors .New ("timeField cannot be cast to string" )
56
- }
57
-
58
- if timeField == "" {
59
- return nil , errors .New ("a time field name is required" )
60
- }
61
-
62
- timeOutputFormat , ok := jsonData ["timeOutputFormat" ].(string )
63
- if ! ok {
64
- return nil , errors .New ("timeOutputFormat cannot be cast to string" )
65
- }
115
+ timeField , toerr := jsonData ["timeField" ].(string )
116
+ timeOutputFormat , toferr := jsonData ["timeOutputFormat" ].(string )
66
117
67
118
logLevelField , ok := jsonData ["logLevelField" ].(string )
68
119
if ! ok {
@@ -96,6 +147,13 @@ func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instanc
96
147
maxConcurrentShardRequests = 256
97
148
}
98
149
150
+ if ! toerr || ! toferr {
151
+ timeField , timeOutputFormat , err = getTimestampFieldInfos (index , settings .URL , httpCli )
152
+ if nil != err {
153
+ return nil , err
154
+ }
155
+ }
156
+
99
157
configuredFields := es.ConfiguredFields {
100
158
TimeField : timeField ,
101
159
TimeOutputFormat : timeOutputFormat ,
0 commit comments