Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

feat(frontend): new app selector #712

Merged
merged 17 commits into from
May 30, 2023
Prev Previous commit
Next Next commit
write a very simple parser for query
  • Loading branch information
eh-am committed May 25, 2023
commit 77d4ea672c434f0cf6d8bbbb1c929c6159e16ab0
54 changes: 54 additions & 0 deletions public/app/overrides/models/query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { brandQuery } from '@webapp/models/query';
import { parse } from './query';

const cases: Array<
[string, { profileId: string; tags?: Record<string, string> }]
> = [
['{}', { profileId: '' }],
[
'process_cpu:cpu:nanoseconds:cpu:nanoseconds{}',
{ profileId: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds' },
],
[
'process_cpu:cpu:nanoseconds:cpu:nanoseconds{tag="mytag"}',
{
profileId: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds',
tags: {
tag: 'mytag',
},
},
],
[
'process_cpu:cpu:nanoseconds:cpu:nanoseconds{tag="mytag", tag2="mytag2"}',
{
profileId: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds',
tags: {
tag: 'mytag',
tag2: 'mytag2',
},
},
],
[
'process_cpu:cpu:nanoseconds:cpu:nanoseconds{tag="mytag",tag2="mytag2"}',
{
profileId: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds',
tags: {
tag: 'mytag',
tag2: 'mytag2',
},
},
],
[
'process_cpu:cpu:nanoseconds:cpu:nanoseconds{tag="my.ta/g_"}',
{
profileId: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds',
tags: {
tag: 'my.ta/g_',
},
},
],
];

test.each(cases)('parse(%s) should be %s', (query, expected) => {
expect(parse(brandQuery(query))).toEqual(expected);
});
39 changes: 39 additions & 0 deletions public/app/overrides/models/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Query } from '@webapp/models/query';

// ParseQuery parses a string of $app_name<{<$tag_matchers>}> form.
// It assumes the query is well formed
export function parse(query: Query): {
profileId: string;
tags?: Record<string, string>;
} {
const regex = /(.+){(.*)}/;
const match = query.match(regex);

if (!match) {
// TODO: return a Nothing() ?
return { profileId: '' };
}

const [_original, head, tail] = match;
const tags = parseTags(tail);

if (!Object.keys(tags).length) {
return { profileId: head };
}
return { profileId: head, tags };
}

function parseTags(s: string) {
const pattern = /(\w+)="([^"]+)/g;

let match;
const matches: Record<string, string> = {};

while ((match = pattern.exec(s)) !== null) {
const key = match[1];
const value = match[2];
matches[key] = value;
}

return matches;
}