Skip to content

Commit

Permalink
Issue #4: find timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 9, 2023
1 parent cd95e27 commit 9674bda
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
16 changes: 2 additions & 14 deletions pkg/quickwit/quickwit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -50,19 +49,8 @@ func NewQuickwitDatasource(settings backend.DataSourceInstanceSettings) (instanc
return nil, err
}

timeField, ok := jsonData["timeField"].(string)
if !ok {
return nil, errors.New("timeField cannot be cast to string")
}

if timeField == "" {
return nil, errors.New("a time field name is required")
}

timeOutputFormat, ok := jsonData["timeOutputFormat"].(string)
if !ok {
return nil, errors.New("timeOutputFormat cannot be cast to string")
}
timeField, _ := jsonData["timeField"].(string)
timeOutputFormat, _ := jsonData["timeOutputFormat"].(string)

logLevelField, ok := jsonData["logLevelField"].(string)
if !ok {
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { QuickwitOptions } from 'quickwit';

export function createDefaultConfigOptions(): DataSourceSettings<QuickwitOptions> {
return createDatasourceSettings<QuickwitOptions>({
timeField: 'timestamp',
timeOutputFormat: 'unix_timestamp_millisecs',
logMessageField: 'test.message',
logLevelField: 'test.level',
index: 'test',
Expand Down
22 changes: 16 additions & 6 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class QuickwitDataSource
{
index: string;
timeField: string;
timeOutputFormat: string;
logMessageField?: string;
logLevelField?: string;
queryBuilder: ElasticQueryBuilder;
Expand All @@ -72,17 +73,28 @@ export class QuickwitDataSource
const settingsData = instanceSettings.jsonData || ({} as QuickwitOptions);
this.index = settingsData.index || '';
this.timeField = ''
this.timeOutputFormat = ''
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField,
});
from(this.getResource('indexes/' + this.index + '/describe')).pipe(
map(config => config.timestamp_field_name)
from(this.getResource('indexes/' + this.index)).pipe(
map((indexMetadata) => {
let fields = getAllFields(indexMetadata.index_config.doc_mapping.field_mappings);
let timestampField = fields.find((field) => field.json_path === this.timeField);

return {
'field': indexMetadata.index_config.doc_mapping.timestamp_field,
'format': timestampField !== undefined ? timestampField.field_mapping.output_format || '' : ''
}
})
).subscribe(result => {
this.timeField = result;
this.timeField = result.field;
this.timeOutputFormat = result.format;
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField,
});
});

this.logMessageField = settingsData.logMessageField || '';
this.logLevelField = settingsData.logLevelField || '';
this.dataLinks = settingsData.dataLinks || [];
Expand Down Expand Up @@ -141,9 +153,7 @@ export class QuickwitDataSource
if (this.timeField === '') {
return `Time field must not be empty`;
}
if (indexMetadata.index_config.doc_mapping.timestamp_field !== this.timeField) {
return `No timestamp field named '${this.timeField}' found`;
}

let fields = getAllFields(indexMetadata.index_config.doc_mapping.field_mappings);
let timestampField = fields.find((field) => field.json_path === this.timeField);

Expand Down
2 changes: 2 additions & 0 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function createElasticDatasource(
access: 'proxy',
url: '',
jsonData: {
timeField: '',
timeOutputFormat: '',
index: '',
...jsonData,
},
Expand Down
2 changes: 2 additions & 0 deletions src/quickwit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { DataSourceJsonData } from "@grafana/data";
import { DataLinkConfig } from "types";

export interface QuickwitOptions extends DataSourceJsonData {
timeField: string;
timeOutputFormat: string;
interval?: Interval;
logMessageField?: string;
logLevelField?: string;
Expand Down

0 comments on commit 9674bda

Please sign in to comment.