searchFunction.ts 6.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { mergeStyleSets } from '@fluentui/react';
import { trialJobStatus } from '../../../static/const';
import { EXPERIMENT } from '../../../static/datamodel';
import { TableObj, SearchItems } from '../../../static/interface';

const classNames = mergeStyleSets({
    menu: {
        textAlign: 'center',
        maxWidth: 600,
        selectors: {
            '.ms-ContextualMenu-item': {
                height: 'auto'
            }
        }
    },
    item: {
        display: 'inline-block',
        width: 40,
        height: 40,
        lineHeight: 40,
        textAlign: 'center',
        verticalAlign: 'middle',
        marginBottom: 8,
        cursor: 'pointer',
        selectors: {
            '&:hover': {
                backgroundColor: '#eaeaea'
            }
        }
    },
    categoriesList: {
        margin: 0,
        padding: 0,
        listStyleType: 'none'
    },
    button: {
        width: '40%',
        margin: '2%'
    }
});

function getDropdownOptions(parameter): any {
    if (parameter === 'StatusNNI') {
        return trialJobStatus.map(item => ({
            key: item,
            text: item
        }));
    } else {
        return EXPERIMENT.searchSpace[parameter]._value.map(item => ({
            key: item.toString(),
            text: item.toString()
        }));
    }
}

// change origin data according to parameter type, string -> number
const convertParametersValue = (searchItems: SearchItems[], relation: Map<string, string>): SearchItems[] => {
    const choice: any[] = [];
Lijiaoa's avatar
Lijiaoa committed
59
60
    const copySearchItems = JSON.parse(JSON.stringify(searchItems));
    copySearchItems.forEach(item => {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        if (relation.get(item.name) === 'number') {
            if (item.isChoice === true) {
                item.choice.forEach(ele => {
                    choice.push(JSON.parse(ele));
                });
                item.choice = choice;
            } else {
                item.value1 = JSON.parse(item.value1);
                if (item.value2 !== '') {
                    item.value2 = JSON.parse(item.value2);
                }
            }
        }
    });

Lijiaoa's avatar
Lijiaoa committed
76
    return copySearchItems;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
};
// relation: trial parameter -> type {conv_size -> number}
const getTrialsBySearchFilters = (
    arr: TableObj[],
    searchItems: SearchItems[],
    relation: Map<string, string>
): TableObj[] => {
    const que = convertParametersValue(searchItems, relation);
    // start to filter data by ['Trial id', 'Trial No.', 'Status'] [...parameters]...
    que.forEach(element => {
        if (element.name === 'Trial id') {
            arr = arr.filter(trial => trial.id.toUpperCase().includes(element.value1.toUpperCase()));
        } else if (element.name === 'Trial No.') {
            arr = arr.filter(trial => trial.sequenceId.toString() === element.value1);
        } else if (element.name === 'StatusNNI') {
            arr = searchChoiceFilter(arr, element, 'status');
        } else {
            const parameter = `space/${element.name}`;

            if (element.isChoice === true) {
                arr = searchChoiceFilter(arr, element, element.name);
            } else {
                if (element.operator === '=') {
                    arr = arr.filter(trial => trial[parameter] === element.value1);
                } else if (element.operator === '>') {
                    arr = arr.filter(trial => trial[parameter] > element.value1);
                } else if (element.operator === '<') {
                    arr = arr.filter(trial => trial[parameter] < element.value1);
                } else if (element.operator === 'between') {
                    arr = arr.filter(trial => trial[parameter] > element.value1 && trial[parameter] < element.value2);
                } else {
                    // operator is '≠'
                    arr = arr.filter(trial => trial[parameter] !== element.value1);
                }
            }
        }
    });

    return arr;
};

// isChoice = true: status and trial parameters
function findTrials(arr: TableObj[], choice: string[], filed: string): TableObj[] {
    const newResult: TableObj[] = [];
    const parameter = filed === 'status' ? 'status' : `space/${filed}`;
    arr.forEach(trial => {
        choice.forEach(item => {
            if (trial[parameter] === item) {
                newResult.push(trial);
            }
        });
    });

    return newResult;
}

function searchChoiceFilter(arr: TableObj[], element: SearchItems, field: string): TableObj[] {
    if (element.operator === '=') {
        return findTrials(arr, element.choice, field);
    } else {
        let choice;
        if (field === 'status') {
            choice = trialJobStatus.filter(index => !new Set(element.choice).has(index));
        } else {
            choice = EXPERIMENT.searchSpace[field]._value.filter(index => !new Set(element.choice).has(index));
        }
        return findTrials(arr, choice, field);
    }
}

// click Apply btn: set searchBox value now
function getSearchInputValueBySearchList(searchFilter): string {
    let str = ''; // store search input value

    searchFilter.forEach(item => {
        const filterName = item.name === 'StatusNNI' ? 'Status' : item.name;

        if (item.isChoice === false) {
            // id, No, !choice parameter
            if (item.name === 'Trial id' || item.name === 'Trial No.') {
                str = str + `${item.name}:${item.value1}; `;
            } else {
                // !choice parameter
                if (['=', '', '>', '<'].includes(item.operator)) {
                    str = str + `${filterName}${item.operator === '=' ? ':' : item.operator}${item.value1}; `;
                } else {
                    // between
                    str = str + `${filterName}:[${item.value1},${item.value2}]; `;
                }
            }
        } else {
            // status, choice parameter
            str = str + `${filterName}${item.operator === '=' ? ':' : ''}[${[...item.choice]}]; `;
        }
    });

    return str;
}

/***
 * from experiment search space
* "conv_size": {
        "_type": "choice", // is choice type
        "_value": [
            2,
            3,
            5,
            7
        ]
    },
 */
function isChoiceType(parameterName): boolean {
    // 判断是 [choice, status] 还是普通的类型
    let flag = false; // normal type

    if (parameterName === 'StatusNNI') {
        flag = true;
    }

    if (parameterName in EXPERIMENT.searchSpace) {
        flag = EXPERIMENT.searchSpace[parameterName]._type === 'choice' ? true : false;
    }

    return flag;
}

export { classNames, getDropdownOptions, getTrialsBySearchFilters, getSearchInputValueBySearchList, isChoiceType };