@@ -6,13 +6,13 @@ import (
66 "fmt"
77 "io"
88 "net/http"
9+ "strings"
910)
1011
11- type QuickwitMapping struct {
12+ type QuickwitIndexMetadata struct {
1213 IndexConfig struct {
1314 DocMapping struct {
14- TimestampField string `json:"timestamp_field"`
15- FieldMappings []FieldMappings `json:"field_mappings"`
15+ TimestampField string `json:"timestamp_field"`
1616 } `json:"doc_mapping"`
1717 } `json:"index_config"`
1818}
@@ -34,25 +34,48 @@ func NewErrorCreationPayload(statusCode int, message string) error {
3434 return errors .New (string (json ))
3535}
3636
37- func DecodeTimestampFieldInfos (statusCode int , body []byte ) (string , error ) {
38- var payload QuickwitMapping
39- err := json .Unmarshal (body , & payload )
37+ // TODO: refactor either by using a timestamp alias suppprted by quickwit
38+ // or by only using the `GetTimestampFieldFromIndexPattern` once the endpoint
39+ // /indexes?index_id_pattern= is supported, which is after the next quickwit release > 0.7.1
40+ func GetTimestampField (index string , qwickwitUrl string , cli * http.Client ) (string , error ) {
41+ if strings .Contains (index , "*" ) || strings .Contains (index , "," ) {
42+ return GetTimestampFieldFromIndexPattern (index , qwickwitUrl , cli )
43+ }
44+ return GetTimestampFieldFromIndex (index , qwickwitUrl , cli )
45+ }
4046
47+ func GetTimestampFieldFromIndex (index string , qwickwitUrl string , cli * http.Client ) (string , error ) {
48+ mappingEndpointUrl := qwickwitUrl + "/indexes/" + index
49+ qwlog .Debug ("Calling quickwit endpoint: " + mappingEndpointUrl )
50+ r , err := cli .Get (mappingEndpointUrl )
4151 if err != nil {
42- errMsg := fmt .Sprintf ("Unmarshalling body error: err = %s, body = %s" , err .Error (), (body ))
52+ errMsg := fmt .Sprintf ("Error when calling url = %s: err = %s" , mappingEndpointUrl , err .Error ())
53+ qwlog .Error (errMsg )
54+ return "" , err
55+ }
56+
57+ statusCode := r .StatusCode
58+
59+ if statusCode < 200 || statusCode >= 400 {
60+ errMsg := fmt .Sprintf ("Error when calling url = %s" , mappingEndpointUrl )
4361 qwlog .Error (errMsg )
4462 return "" , NewErrorCreationPayload (statusCode , errMsg )
4563 }
4664
47- timestampFieldName := payload .IndexConfig .DocMapping .TimestampField
65+ defer r .Body .Close ()
66+ body , err := io .ReadAll (r .Body )
67+ if err != nil {
68+ errMsg := fmt .Sprintf ("Error when calling url = %s: err = %s" , mappingEndpointUrl , err .Error ())
69+ qwlog .Error (errMsg )
70+ return "" , NewErrorCreationPayload (statusCode , errMsg )
71+ }
4872
49- qwlog .Info (fmt .Sprintf ("Found timestampFieldName = %s" , timestampFieldName ))
50- return timestampFieldName , nil
73+ return DecodeTimestampFieldFromIndexConfig (body )
5174}
5275
53- func GetTimestampFieldInfos ( index string , qwUrl string , cli * http.Client ) (string , error ) {
54- mappingEndpointUrl := qwUrl + "/indexes/ " + index
55- qwlog .Info ("Calling quickwit endpoint: " + mappingEndpointUrl )
76+ func GetTimestampFieldFromIndexPattern ( indexPattern string , qwickwitUrl string , cli * http.Client ) (string , error ) {
77+ mappingEndpointUrl := qwickwitUrl + "/indexes?index_id_pattern= " + indexPattern
78+ qwlog .Debug ("Calling quickwit endpoint: " + mappingEndpointUrl )
5679 r , err := cli .Get (mappingEndpointUrl )
5780 if err != nil {
5881 errMsg := fmt .Sprintf ("Error when calling url = %s: err = %s" , mappingEndpointUrl , err .Error ())
@@ -76,5 +99,45 @@ func GetTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
7699 return "" , NewErrorCreationPayload (statusCode , errMsg )
77100 }
78101
79- return DecodeTimestampFieldInfos (statusCode , body )
102+ return DecodeTimestampFieldFromIndexConfigs (body )
103+ }
104+
105+ func DecodeTimestampFieldFromIndexConfigs (body []byte ) (string , error ) {
106+ var payload []QuickwitIndexMetadata
107+ err := json .Unmarshal (body , & payload )
108+ if err != nil {
109+ errMsg := fmt .Sprintf ("Unmarshalling body error: err = %s, body = %s" , err .Error (), (body ))
110+ qwlog .Error (errMsg )
111+ return "" , NewErrorCreationPayload (500 , errMsg )
112+ }
113+
114+ var timestampFieldName string = ""
115+ for _ , indexMetadata := range payload {
116+ if timestampFieldName == "" {
117+ timestampFieldName = indexMetadata .IndexConfig .DocMapping .TimestampField
118+ continue
119+ }
120+
121+ if timestampFieldName != indexMetadata .IndexConfig .DocMapping .TimestampField {
122+ errMsg := fmt .Sprintf ("Index matching the pattern should have the same timestamp fields, two found: %s and %s" , timestampFieldName , indexMetadata .IndexConfig .DocMapping .TimestampField )
123+ qwlog .Error (errMsg )
124+ return "" , NewErrorCreationPayload (400 , errMsg )
125+ }
126+ }
127+
128+ qwlog .Debug (fmt .Sprintf ("Found timestampFieldName = %s" , timestampFieldName ))
129+ return timestampFieldName , nil
130+ }
131+
132+ func DecodeTimestampFieldFromIndexConfig (body []byte ) (string , error ) {
133+ var payload QuickwitIndexMetadata
134+ err := json .Unmarshal (body , & payload )
135+ if err != nil {
136+ errMsg := fmt .Sprintf ("Unmarshalling body error: err = %s, body = %s" , err .Error (), (body ))
137+ qwlog .Error (errMsg )
138+ return "" , NewErrorCreationPayload (500 , errMsg )
139+ }
140+ timestampFieldName := payload .IndexConfig .DocMapping .TimestampField
141+ qwlog .Debug (fmt .Sprintf ("Found timestampFieldName = %s" , timestampFieldName ))
142+ return timestampFieldName , nil
80143}
0 commit comments