Skip to content

Commit

Permalink
Issue #4: timestamp is given by the describe endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 9, 2023
1 parent 28ebe43 commit cd95e27
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 40 deletions.
18 changes: 0 additions & 18 deletions src/configuration/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,6 @@ export const QuickwitDetails = ({ value, onChange }: DetailsProps) => {
width={40}
/>
</InlineField>
<InlineField label="Timestamp field" labelWidth={26} tooltip="Timestamp field of your index. Required.">
<Input
id="quickwit_index_timestamp_field"
value={value.jsonData.timeField}
onChange={(event) => onChange({ ...value, jsonData: {...value.jsonData, timeField: event.currentTarget.value}})}
placeholder="timestamp"
width={40}
/>
</InlineField>
<InlineField label="Timestamp output format" labelWidth={26} tooltip="If you don't remember the output format, check the datasource and the error message will give you a hint.">
<Input
id="quickwit_index_timestamp_field_output_format"
value={value.jsonData.timeOutputFormat}
onChange={(event) => onChange({ ...value, jsonData: {...value.jsonData, timeOutputFormat: event.currentTarget.value}})}
placeholder="unix_timestamp_millisecs"
width={40}
/>
</InlineField>
<InlineField label="Message field name" labelWidth={26} tooltip="Field used to display a log line in the Explore view">
<Input
id="quickwit_log_message_field"
Expand Down
2 changes: 0 additions & 2 deletions src/configuration/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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
33 changes: 17 additions & 16 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class QuickwitDataSource
{
index: string;
timeField: string;
timeOutputFormat: string;
logMessageField?: string;
logLevelField?: string;
queryBuilder: ElasticQueryBuilder;
Expand All @@ -72,13 +71,20 @@ export class QuickwitDataSource
super(instanceSettings);
const settingsData = instanceSettings.jsonData || ({} as QuickwitOptions);
this.index = settingsData.index || '';
this.timeField = settingsData.timeField || '';
this.timeOutputFormat = settingsData.timeOutputFormat || '';
this.logMessageField = settingsData.logMessageField || '';
this.logLevelField = settingsData.logLevelField || '';
this.timeField = ''
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField,
});
from(this.getResource('indexes/' + this.index + '/describe')).pipe(
map(config => config.timestamp_field_name)
).subscribe(result => {
this.timeField = result;
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField,
});
});
this.logMessageField = settingsData.logMessageField || '';
this.logLevelField = settingsData.logLevelField || '';
this.dataLinks = settingsData.dataLinks || [];
this.languageProvider = new ElasticsearchLanguageProvider(this);
}
Expand All @@ -104,12 +110,7 @@ export class QuickwitDataSource
message: 'Cannot save datasource, `index` is required',
};
}
if (this.timeField === '' ) {
return {
status: 'error',
message: 'Cannot save datasource, `timeField` is required',
};
}

return lastValueFrom(
from(this.getResource('indexes/' + this.index)).pipe(
mergeMap((indexMetadata) => {
Expand Down Expand Up @@ -145,16 +146,16 @@ export class QuickwitDataSource
}
let fields = getAllFields(indexMetadata.index_config.doc_mapping.field_mappings);
let timestampField = fields.find((field) => field.json_path === this.timeField);

// Should never happen.
if (timestampField === undefined) {
return `No field named '${this.timeField}' found in the doc mapping. This should never happen.`;
}
if (timestampField.field_mapping.output_format !== this.timeOutputFormat) {
return `Timestamp output format is declared as '${timestampField.field_mapping.output_format}' in the doc mapping, not '${this.timeOutputFormat}'.`;
}

let timeOutputFormat = timestampField.field_mapping.output_format || 'unknown';
const supportedTimestampOutputFormats = ['unix_timestamp_secs', 'unix_timestamp_millis', 'unix_timestamp_micros', 'unix_timestamp_nanos', 'iso8601', 'rfc3339'];
if (!supportedTimestampOutputFormats.includes(this.timeOutputFormat)) {
return `Timestamp output format '${this.timeOutputFormat} is not yet supported.`;
if (!supportedTimestampOutputFormats.includes(timeOutputFormat)) {
return `Timestamp output format '${timeOutputFormat} is not yet supported.`;
}
return;
}
Expand Down
2 changes: 0 additions & 2 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export function createElasticDatasource(
access: 'proxy',
url: '',
jsonData: {
timeField: '',
timeOutputFormat: '',
index: '',
...jsonData,
},
Expand Down
2 changes: 0 additions & 2 deletions src/quickwit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ 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 cd95e27

Please sign in to comment.