+const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | 'reuse') => {
+ let value = data.searchValue;
+
+ const addRem = (field: string, key: string) => {
+ const v = data[key];
+
+ if (data.hasOwnProperty(key)) {
+ const pattern = v === false
+ ? `${field.replace(':', '\\:\\s*')}\\s*`
+ : `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`;
+ value = value.replace(new RegExp(pattern), '');
+ }
+
+ if (v) {
+ const nv = v === true
+ ? `${field}`
+ : `${field}:${v}`;
+
+ if (mode === 'rebuild') {
+ value = value + ' ' + nv;
+ } else {
+ value = nv + ' ' + value;
+ }
+ }
+ };
+
+ keyMap.forEach(km => addRem(km[0], km[1]));
+
+ return value;
+};
+
+export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => {
+ let value = '';
+
+ const flatData = (data: SearchBarAdvanceFormData) => {
+ const fo = {
+ searchValue: data.searchValue,
+ type: data.type,
+ cluster: data.cluster,
+ projectUuid: data.projectUuid,
+ inTrash: data.inTrash,
+ dateFrom: data.dateFrom,
+ dateTo: data.dateTo,
+ };
+ (data.properties || []).forEach(p => fo[`prop-${p.key}`] = p.value);
+ return fo;
+ };
+
+ const keyMap = [
+ ['type', 'type'],
+ ['cluster', 'cluster'],
+ ['project', 'projectUuid'],
+ ['is:trashed', 'inTrash'],
+ ['from', 'dateFrom'],
+ ['to', 'dateTo']
+ ];
+ _.union(data.properties, prevData ? prevData.properties : [])
+ .forEach(p => keyMap.push([`has:${p.key}`, `prop-${p.key}`]));
+
+ if (prevData) {
+ const obj = getModifiedKeysValues(flatData(data), flatData(prevData));
+ value = buildQueryFromKeyMap({
+ searchValue: data.searchValue,
+ ...obj
+ } as SearchBarAdvanceFormData, keyMap, "reuse");
+ } else {
+ value = buildQueryFromKeyMap(flatData(data), keyMap, "rebuild");
+ }
+
+ value = value.trim();
+ return value;
+};
+
+export interface ParseSearchQuery {
+ hasKeywords: boolean;
+ values: string[];
+ properties: {
+ [key: string]: string[]
+ };
+}
+
+export const parseSearchQuery: (query: string) => ParseSearchQuery = (searchValue: string) => {
+ const keywords = [
+ 'type:',
+ 'cluster:',
+ 'project:',
+ 'is:',
+ 'from:',
+ 'to:',
+ 'has:'
+ ];
+
+ const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + (search.includes(keyword) ? 1 : 0), 0);
+ let keywordsCnt = 0;
+
+ const properties = {};
+
+ keywords.forEach(k => {
+ let p = searchValue.indexOf(k);
+ const key = k.substr(0, k.length - 1);
+
+ while (p >= 0) {
+ const l = searchValue.length;
+ keywordsCnt += 1;
+
+ let v = '';
+ let i = p + k.length;
+ while (i < l && searchValue[i] === ' ') {
+ ++i;
+ }
+ const vp = i;
+ while (i < l && searchValue[i] !== ' ') {
+ v += searchValue[i];
+ ++i;
+ }
+
+ if (hasKeywords(v)) {
+ searchValue = searchValue.substr(0, p) + searchValue.substr(vp);
+ } else {
+ if (v !== '') {
+ if (!properties[key]) {
+ properties[key] = [];
+ }
+ properties[key].push(v);
+ }
+ searchValue = searchValue.substr(0, p) + searchValue.substr(i);
+ }
+ p = searchValue.indexOf(k);
+ }
+ });
+
+ const values = _.uniq(searchValue.split(' ').filter(v => v.length > 0));
+
+ return { hasKeywords: keywordsCnt > 0, values, properties };
+};
+
+const getFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0];
+const getPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value);
+const getProperties = (sq: ParseSearchQuery): PropertyValue[] => {
+ if (sq.properties.has) {
+ return sq.properties.has.map((value: string) => {
+ const v = value.split(':');
+ return {
+ key: v[0],
+ value: v[1]
+ };
+ });
+ }
+ return [];
+};
+
+export const getAdvancedDataFromQuery = (query: string): SearchBarAdvanceFormData => {
+ const sq = parseSearchQuery(query);
+
+ return {
+ searchValue: sq.values.join(' '),
+ type: getFirstProp(sq, 'type') as ResourceKind,
+ cluster: getFirstProp(sq, 'cluster') as ClusterObjectType,
+ projectUuid: getFirstProp(sq, 'project'),
+ inTrash: getPropValue(sq, 'is', 'trashed') !== undefined,
+ dateFrom: getFirstProp(sq, 'from'),
+ dateTo: getFirstProp(sq, 'to'),
+ properties: getProperties(sq),
+ saveQuery: false,
+ queryName: ''
+ };
+};
+