Skip to content

Commit

Permalink
[Dataset Quality] Utility function to extract index name from backing… (
Browse files Browse the repository at this point in the history
#183054)

closes #183052

## 📓 Summary


Created a utility function to extract index name from a given backing
index string.
  • Loading branch information
mohamedhamed-ahmed committed May 9, 2024
1 parent 3230010 commit 4c0089a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
dataStreamPartsToIndexName,
streamPartsToIndexPattern,
indexNameToDataStreamParts,
extractIndexNameFromBackingIndex,
} from './dataset_name';

describe('dataset_name', () => {
Expand Down Expand Up @@ -52,4 +53,30 @@ describe('dataset_name', () => {
});
});
});

describe('extractIndexNameFromBackingIndex', () => {
it('returns the correct index name if backing index provieded', () => {
expect(
extractIndexNameFromBackingIndex(
'.ds-logs-apm.app.adservice-default-2024.04.29-000001',
'logs'
)
).toEqual('logs-apm.app.adservice-default');
});

it('returns the correct index name if index name is passed', () => {
expect(extractIndexNameFromBackingIndex('logs-nginx.access-default', 'logs')).toEqual(
'logs-nginx.access-default'
);
});

it('handles different types', () => {
expect(
extractIndexNameFromBackingIndex(
'.ds-metrics-apm.app.adservice-default-2024.04.29-000001',
'metrics'
)
).toEqual('metrics-apm.app.adservice-default');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ export const indexNameToDataStreamParts = (dataStreamName: string) => {
namespace,
};
};

export const extractIndexNameFromBackingIndex = (
indexString: string,
type: DataStreamType
): string => {
const pattern: RegExp = new RegExp(
`(?:\\.ds-)?(${type}-(?:[^-.]+(?:\\.[^.]+)+)-[^-]+)-\\d{4}\\.\\d{2}\\.\\d{2}-\\d{6}`
);

const match = indexString.match(pattern);

return match ? match[1] : indexString;
};

0 comments on commit 4c0089a

Please sign in to comment.