ExperimentManager.tsx 17.3 KB
Newer Older
1
import * as React from 'react';
Lijiaoa's avatar
Lijiaoa committed
2
import { Stack, DetailsList, DefaultButton, Icon, SearchBox, IColumn, IStackTokens } from '@fluentui/react';
3
4
5
6
7
8
9
import { ExperimentsManager } from '../../static/model/experimentsManager';
import { expformatTimestamp, copyAndSort } from '../../static/function';
import { AllExperimentList, SortInfo } from '../../static/interface';
import MessageInfo from '../modals/MessageInfo';
import { compareDate, filterByStatusOrPlatform, getSortedSource } from './expFunction';
import { MAXSCREENCOLUMNWIDHT, MINSCREENCOLUMNWIDHT } from './experimentConst';
import { Hearder } from './Header';
10
import TrialIdColumn from './TrialIdColumn';
11
import FilterBtns from './FilterBtns';
12
13
import { TitleContext } from '../overview/TitleContext';
import { Title } from '../overview/Title';
14
import '../../App.scss';
15
import '../../static/style/common.scss';
16
17
18
19
20
import '../../static/style/nav/nav.scss';
import '../../static/style/experiment/experiment.scss';
import '../../static/style/overview/probar.scss';
import '../../static/style/tableStatus.css';

Lijiaoa's avatar
Lijiaoa committed
21
22
23
24
const expTokens: IStackTokens = {
    childrenGap: 25
};

25
26
27
28
29
30
interface ExpListState {
    columns: IColumn[];
    platform: string[];
    errorMessage: string;
    hideFilter: boolean;
    searchInputVal: string;
31
    selectedStatus: string[];
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    selectedPlatform: string;
    selectedStartDate?: Date;
    selectedEndDate?: Date;
    sortInfo: SortInfo;
    source: AllExperimentList[];
    originExperimentList: AllExperimentList[];
    searchSource: AllExperimentList[];
}

class Experiment extends React.Component<{}, ExpListState> {
    constructor(props) {
        super(props);
        this.state = {
            platform: [],
            columns: this.columns,
            errorMessage: '',
            hideFilter: true,
            searchInputVal: '',
50
            selectedStatus: [],
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
            selectedPlatform: '',
            source: [], // data in table
            originExperimentList: [], // api /experiments-info
            searchSource: [], // search box search result
            sortInfo: { field: '', isDescend: false }
        };
    }

    async componentDidMount(): Promise<void> {
        const EXPERIMENTMANAGER = new ExperimentsManager();
        await EXPERIMENTMANAGER.init();
        const result = EXPERIMENTMANAGER.getExperimentList();
        this.setState(() => ({
            source: result,
            originExperimentList: result,
            searchSource: result,
            platform: EXPERIMENTMANAGER.getPlatformList(),
            errorMessage: EXPERIMENTMANAGER.getExpErrorMessage()
        }));
    }

    render(): React.ReactNode {
        const {
            platform,
            hideFilter,
            selectedStatus,
            source,
            selectedPlatform,
            selectedStartDate,
            selectedEndDate,
            errorMessage
        } = this.state;
        return (
            <Stack className='nni' style={{ minHeight: window.innerHeight }}>
                <Hearder />
                {errorMessage !== undefined ? (
                    <div className='warning'>
                        <MessageInfo info={errorMessage} typeInfo='error' />
                    </div>
                ) : null}
                <Stack className='contentBox expBackground'>
92
93
                    {/* 56px: navBarHeight; 48: marginTop & Bottom */}
                    <Stack className='content' styles={{ root: { minHeight: window.innerHeight - 104 } }}>
94
                        <Stack className='experimentList'>
95
96
97
                            <TitleContext.Provider value={{ text: 'All experiments', icon: 'CustomList' }}>
                                <Title />
                            </TitleContext.Provider>
98
99
100
101
                            <Stack className='box' horizontal>
                                <div className='search'>
                                    <SearchBox
                                        className='search-input'
102
                                        placeholder='Search the experiment by name or ID'
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
                                        onEscape={this.setOriginSource.bind(this)}
                                        onClear={this.setOriginSource.bind(this)}
                                        onChange={this.searchNameAndId.bind(this)}
                                    />
                                </div>
                                <div className='filter'>
                                    <DefaultButton
                                        onClick={this.clickFilter.bind(this)}
                                        className={`${!hideFilter ? 'filter-button-open' : null}`}
                                    >
                                        <Icon iconName='Equalizer' />
                                        <span className='margin'>Filter</span>
                                    </DefaultButton>
                                </div>
                            </Stack>
Lijiaoa's avatar
Lijiaoa committed
118
119
120
121
122
                            <Stack
                                className={`${hideFilter ? 'hidden' : ''} filter-condition`}
                                horizontal
                                tokens={expTokens}
                            >
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
                                <FilterBtns
                                    platform={platform}
                                    selectedStatus={selectedStatus}
                                    selectedPlatform={selectedPlatform}
                                    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                                    selectedStartDate={selectedStartDate!}
                                    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                                    selectedEndDate={selectedEndDate!}
                                    selectStatus={this.selectStatus.bind(this)}
                                    selectPlatform={this.selectPlatform.bind(this)}
                                    getSelectedData={this.getSelectedData.bind(this)}
                                    setSearchSource={this.setSearchSource.bind(this)}
                                />
                            </Stack>
                            <DetailsList
                                columns={this.columns}
                                items={source}
                                setKey='set'
                                compact={true}
                                selectionMode={0} // close selector function
                                className='table'
                            />
                        </Stack>
                    </Stack>
                </Stack>
            </Stack>
        );
    }

    private onColumnClick = (_ev: React.MouseEvent<HTMLElement>, getColumn: IColumn): void => {
        const { columns, source } = this.state;
        const newColumns: IColumn[] = columns.slice();
        const currColumn: IColumn = newColumns.filter(item => getColumn.key === item.key)[0];
        newColumns.forEach((newCol: IColumn) => {
            if (newCol === currColumn) {
                currColumn.isSortedDescending = !currColumn.isSortedDescending;
                currColumn.isSorted = true;
            } else {
                newCol.isSorted = false;
                newCol.isSortedDescending = true;
            }
        });
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const newItems = copyAndSort(source, currColumn.fieldName!, currColumn.isSortedDescending);
        this.setState(() => ({
            columns: newColumns,
            source: newItems,
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            sortInfo: { field: currColumn.fieldName!, isDescend: currColumn.isSortedDescending }
        }));
    };

    private columns: IColumn[] = [
        {
            name: 'Name',
            key: 'experimentName',
            fieldName: 'experimentName', // required!
            minWidth: MINSCREENCOLUMNWIDHT,
            maxWidth: MAXSCREENCOLUMNWIDHT,
            isResizable: true,
            data: 'number',
            onColumnClick: this.onColumnClick,
185
            onRender: (item: any): React.ReactNode => <div>{item.experimentName}</div>
186
187
188
189
190
191
192
193
194
195
196
        },
        {
            name: 'ID',
            key: 'id',
            fieldName: 'id',
            minWidth: MINSCREENCOLUMNWIDHT,
            maxWidth: MAXSCREENCOLUMNWIDHT,
            isResizable: true,
            className: 'tableHead leftTitle',
            data: 'string',
            onColumnClick: this.onColumnClick,
197
            onRender: (item: any): React.ReactNode => <TrialIdColumn item={item} />
198
199
200
201
202
203
204
205
206
        },
        {
            name: 'Status',
            key: 'status',
            fieldName: 'status',
            minWidth: MINSCREENCOLUMNWIDHT,
            maxWidth: MAXSCREENCOLUMNWIDHT,
            isResizable: true,
            onColumnClick: this.onColumnClick,
207
            onRender: (item: any): React.ReactNode => <div className={`${item.status} commonStyle`}>{item.status}</div>
208
209
210
211
212
213
214
215
216
217
218
        },
        {
            name: 'Port',
            key: 'port',
            fieldName: 'port',
            minWidth: MINSCREENCOLUMNWIDHT - 15,
            maxWidth: MAXSCREENCOLUMNWIDHT - 30,
            isResizable: true,
            data: 'number',
            onColumnClick: this.onColumnClick,
            onRender: (item: any): React.ReactNode => (
219
220
                <div className={item.status === 'STOPPED' ? 'gray-port' : ''}>
                    {item.port !== undefined ? item.port : '--'}
221
222
223
224
225
226
227
228
229
230
231
232
                </div>
            )
        },
        {
            name: 'Platform',
            key: 'platform',
            fieldName: 'platform',
            minWidth: MINSCREENCOLUMNWIDHT - 15,
            maxWidth: MAXSCREENCOLUMNWIDHT - 30,
            isResizable: true,
            data: 'string',
            onColumnClick: this.onColumnClick,
233
            onRender: (item: any): React.ReactNode => <div className='commonStyle'>{item.platform}</div>
234
235
236
237
238
239
240
241
242
243
        },
        {
            name: 'Start time',
            key: 'startTime',
            fieldName: 'startTime',
            minWidth: MINSCREENCOLUMNWIDHT + 15,
            maxWidth: MAXSCREENCOLUMNWIDHT + 30,
            isResizable: true,
            data: 'number',
            onColumnClick: this.onColumnClick,
244
            onRender: (item: any): React.ReactNode => <div>{expformatTimestamp(item.startTime)}</div>
245
246
247
248
249
250
251
252
253
254
        },
        {
            name: 'End time',
            key: 'endTime',
            fieldName: 'endTime',
            minWidth: MINSCREENCOLUMNWIDHT + 15,
            maxWidth: MAXSCREENCOLUMNWIDHT + 30,
            isResizable: true,
            data: 'number',
            onColumnClick: this.onColumnClick,
255
            onRender: (item: any): React.ReactNode => <div>{expformatTimestamp(item.endTime)}</div>
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        }
    ];

    private clickFilter(_e: any): void {
        const { hideFilter } = this.state;
        if (!hideFilter === true) {
            this.setSearchSource();
        }
        this.setState(() => ({ hideFilter: !hideFilter }));
    }

    private setOriginSource(): void {
        let { originExperimentList } = this.state;
        const { sortInfo } = this.state;
        if (originExperimentList !== undefined) {
            originExperimentList = this.commonSelectString(originExperimentList, '');
            const sortedData = getSortedSource(originExperimentList, sortInfo);
            this.setState(() => ({
                source: sortedData
            }));
        }
    }

    private searchNameAndId(_event, newValue): void {
        const { originExperimentList, sortInfo } = this.state;
        if (newValue !== undefined) {
            if (newValue === '') {
                this.setOriginSource();
            } else {
285
                const searchInput = newValue.trim();
286
287
                let result = originExperimentList.filter(
                    item =>
Lijiaoa's avatar
Lijiaoa committed
288
289
                        (item.experimentName !== null &&
                            item.experimentName.toLowerCase().includes(searchInput.toLowerCase())) ||
290
                        item.id.toLowerCase().includes(searchInput.toLowerCase())
291
292
293
294
295
296
297
298
299
                );
                result = this.commonSelectString(result, '');
                const sortedResult = getSortedSource(result, sortInfo);
                this.setState(() => ({
                    source: sortedResult,
                    searchSource: sortedResult
                }));
            }
            this.setState(() => ({
300
                searchInputVal: newValue.trim()
301
302
303
304
305
306
307
308
309
310
311
312
313
314
            }));
        }
    }

    /***
     * status, platform
     * param
     * data: searchSource
     * field: no care selected filed
     */
    private commonSelectString = (data: AllExperimentList[], field: string): AllExperimentList[] => {
        const { selectedStatus, selectedPlatform, selectedStartDate, selectedEndDate } = this.state;

        if (field === 'status') {
315
            data = filterByStatusOrPlatform(selectedPlatform, 'platform', data);
316
317
        }
        if (field === 'platform') {
318
            data = filterByStatusOrPlatform(selectedStatus, 'status', data);
319
320
321
        }

        if (field === '') {
322
323
324
325
326
327
            data = Array.from(
                new Set([
                    ...filterByStatusOrPlatform(selectedPlatform, 'platform', data),
                    ...filterByStatusOrPlatform(selectedStatus, 'status', data)
                ])
            );
328
329
        }

330
331
332
333
334
        data = data.filter(
            item =>
                (selectedStartDate === undefined || compareDate(new Date(item.startTime), selectedStartDate)) &&
                (selectedEndDate === undefined || compareDate(new Date(item.endTime), selectedEndDate))
        );
335
336
337
338
339
340
341

        return data;
    };

    // status platform startTime endTime
    private selectStatus = (_event: React.FormEvent<HTMLDivElement>, item: any): void => {
        if (item !== undefined) {
342
343
344
345
346
            const { searchSource, sortInfo, selectedStatus } = this.state;
            const newSelectedStatus = item.selected
                ? [...selectedStatus, item.key as string]
                : selectedStatus.filter(key => key !== item.key);
            let result = filterByStatusOrPlatform(newSelectedStatus, 'status', searchSource);
347
            result = this.commonSelectString(result, 'status');
348
349
350
351
            this.setState({
                selectedStatus: newSelectedStatus,
                source: getSortedSource(result, sortInfo)
            });
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        }
    };

    private selectPlatform = (_event: React.FormEvent<HTMLDivElement>, item: any): void => {
        if (item !== undefined) {
            const { searchSource, sortInfo } = this.state;
            let result = filterByStatusOrPlatform(item.key, 'platform', searchSource);
            result = this.commonSelectString(result, 'platform');
            this.setState({ selectedPlatform: item.key, source: getSortedSource(result, sortInfo) });
        }
    };

    private getSelectedData(type: string, date: Date | null | undefined): void {
        if (date !== null && date !== undefined) {
366
367
            const { selectedStatus, selectedPlatform, selectedStartDate, selectedEndDate, searchSource, sortInfo } =
                this.state;
368
            const hasPlatform = selectedPlatform === '' ? false : true;
369
370
371
372
373
374
375

            // filter status, platform
            let result = filterByStatusOrPlatform(selectedStatus, 'status', searchSource);
            if (hasPlatform) {
                result = result.filter(temp => temp.platform === selectedPlatform);
            }

376
            if (type === 'start') {
377
378
379
380
381
                result = result.filter(
                    item =>
                        compareDate(new Date(item.startTime), date) &&
                        (selectedEndDate === undefined || compareDate(new Date(item.endTime), selectedEndDate))
                );
382
383
384
385
386
                this.setState(() => ({
                    source: getSortedSource(result, sortInfo),
                    selectedStartDate: date
                }));
            } else {
387
388
389
390
391
                result = result.filter(
                    item =>
                        compareDate(new Date(item.endTime), date) &&
                        (selectedStartDate === undefined || compareDate(new Date(item.startTime), selectedStartDate))
                );
392
393
394
395
396
397
398
399
400
401
                this.setState(() => ({
                    source: getSortedSource(result, sortInfo),
                    selectedEndDate: date
                }));
            }
        }
    }

    // reset
    private setSearchSource(): void {
402
403
        const { sortInfo, originExperimentList } = this.state;
        let { searchInputVal } = this.state;
Lijiaoa's avatar
Lijiaoa committed
404
        let result = JSON.parse(JSON.stringify(originExperimentList));
405
        searchInputVal = searchInputVal.trim();
Lijiaoa's avatar
Lijiaoa committed
406
407
408
409
410
411
412
413
414
415
        // user input some value to filter trial [name, id] first...
        if (searchInputVal !== '') {
            // reset experiments list to first filter result
            result = originExperimentList.filter(
                item =>
                    item.id.toLowerCase().includes(searchInputVal.toLowerCase()) ||
                    (item.experimentName !== null &&
                        item.experimentName.toLowerCase().includes(searchInputVal.toLowerCase()))
            );
        }
416
417
        this.setState(() => ({
            source: getSortedSource(result, sortInfo),
418
            selectedStatus: [],
419
420
421
422
423
424
425
426
            selectedPlatform: '',
            selectedStartDate: undefined,
            selectedEndDate: undefined
        }));
    }
}

export default Experiment;