Skip to content

Commit 0be5213

Browse files
Merge pull request #38 from quickwit-oss/feat_timestamp_field
Issue #4: timestamp field
2 parents dac4ba6 + 1d64540 commit 0be5213

File tree

7 files changed

+523
-52
lines changed

7 files changed

+523
-52
lines changed

pkg/quickwit/quickwit.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"io"
109
"net/http"
@@ -26,6 +25,13 @@ type QuickwitDatasource struct {
2625
dsInfo es.DatasourceInfo
2726
}
2827

28+
type FieldMappings struct {
29+
Name string `json:"name"`
30+
Type string `json:"type"`
31+
OutputFormat *string `json:"output_format,omitempty"`
32+
FieldMappings []FieldMappings `json:"field_mappings,omitempty"`
33+
}
34+
2935
// Creates a Quickwit datasource.
3036
func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
3137
qwlog.Debug("Initializing new data source instance")
@@ -50,19 +56,8 @@ func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instanc
5056
return nil, err
5157
}
5258

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-
}
59+
timeField, toOk := jsonData["timeField"].(string)
60+
timeOutputFormat, tofOk := jsonData["timeOutputFormat"].(string)
6661

6762
logLevelField, ok := jsonData["logLevelField"].(string)
6863
if !ok {
@@ -96,6 +91,13 @@ func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instanc
9691
maxConcurrentShardRequests = 256
9792
}
9893

94+
if !toOk || !tofOk {
95+
timeField, timeOutputFormat, err = GetTimestampFieldInfos(index, settings.URL, httpCli)
96+
if nil != err {
97+
return nil, err
98+
}
99+
}
100+
99101
configuredFields := es.ConfiguredFields{
100102
TimeField: timeField,
101103
TimeOutputFormat: timeOutputFormat,

pkg/quickwit/timestamp_infos.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package quickwit
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
)
10+
11+
type QuickwitMapping struct {
12+
IndexConfig struct {
13+
DocMapping struct {
14+
TimestampField string `json:"timestamp_field"`
15+
FieldMappings []FieldMappings `json:"field_mappings"`
16+
} `json:"doc_mapping"`
17+
} `json:"index_config"`
18+
}
19+
20+
type QuickwitCreationErrorPayload struct {
21+
Message string `json:"message"`
22+
StatusCode int `json:"status"`
23+
}
24+
25+
func NewErrorCreationPayload(statusCode int, message string) error {
26+
var payload QuickwitCreationErrorPayload
27+
payload.Message = message
28+
payload.StatusCode = statusCode
29+
json, err := json.Marshal(payload)
30+
if nil != err {
31+
return err
32+
}
33+
34+
return errors.New(string(json))
35+
}
36+
37+
func FindTimeStampFormat(timestampFieldName string, parentName *string, fieldMappings []FieldMappings) *string {
38+
if nil == fieldMappings {
39+
return nil
40+
}
41+
42+
for _, field := range fieldMappings {
43+
fieldName := field.Name
44+
if nil != parentName {
45+
fieldName = fmt.Sprintf("%s.%s", *parentName, fieldName)
46+
}
47+
48+
if field.Type == "datetime" && fieldName == timestampFieldName && nil != field.OutputFormat {
49+
return field.OutputFormat
50+
} else if field.Type == "object" && nil != field.FieldMappings {
51+
format := FindTimeStampFormat(timestampFieldName, &field.Name, field.FieldMappings)
52+
if nil != format {
53+
return format
54+
}
55+
}
56+
}
57+
58+
return nil
59+
}
60+
61+
func DecodeTimestampFieldInfos(statusCode int, body []byte) (string, string, error) {
62+
var payload QuickwitMapping
63+
err := json.Unmarshal(body, &payload)
64+
65+
if err != nil {
66+
errMsg := fmt.Sprintf("Unmarshalling body error: err = %s, body = %s", err.Error(), (body))
67+
qwlog.Error(errMsg)
68+
return "", "", NewErrorCreationPayload(statusCode, errMsg)
69+
}
70+
71+
timestampFieldName := payload.IndexConfig.DocMapping.TimestampField
72+
timestampFieldFormat := FindTimeStampFormat(timestampFieldName, nil, payload.IndexConfig.DocMapping.FieldMappings)
73+
74+
if nil == timestampFieldFormat {
75+
errMsg := fmt.Sprintf("No format found for field: %s", string(timestampFieldName))
76+
qwlog.Error(errMsg)
77+
return timestampFieldName, "", NewErrorCreationPayload(statusCode, errMsg)
78+
}
79+
80+
qwlog.Info(fmt.Sprintf("Found timestampFieldName = %s, timestampFieldFormat = %s", timestampFieldName, *timestampFieldFormat))
81+
return timestampFieldName, *timestampFieldFormat, nil
82+
}
83+
84+
func GetTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (string, string, error) {
85+
mappingEndpointUrl := qwUrl + "/indexes/" + index
86+
qwlog.Info("Calling quickwit endpoint: " + mappingEndpointUrl)
87+
r, err := cli.Get(mappingEndpointUrl)
88+
if err != nil {
89+
errMsg := fmt.Sprintf("Error when calling url = %s: err = %s", mappingEndpointUrl, err.Error())
90+
qwlog.Error(errMsg)
91+
return "", "", err
92+
}
93+
94+
statusCode := r.StatusCode
95+
96+
if statusCode < 200 || statusCode >= 400 {
97+
errMsg := fmt.Sprintf("Error when calling url = %s", mappingEndpointUrl)
98+
qwlog.Error(errMsg)
99+
return "", "", NewErrorCreationPayload(statusCode, errMsg)
100+
}
101+
102+
defer r.Body.Close()
103+
body, err := io.ReadAll(r.Body)
104+
if err != nil {
105+
errMsg := fmt.Sprintf("Error when calling url = %s: err = %s", mappingEndpointUrl, err.Error())
106+
qwlog.Error(errMsg)
107+
return "", "", NewErrorCreationPayload(statusCode, errMsg)
108+
}
109+
110+
return DecodeTimestampFieldInfos(statusCode, body)
111+
}

0 commit comments

Comments
 (0)