search-multiple.effects.ts 8.67 KB
Newer Older
1
import { Injectable } from '@angular/core';
Tifenn Guillas's avatar
Tifenn Guillas committed
2

3
4
5
6
7
8
import { Action, Store } from '@ngrx/store';
import { Actions, Effect, ofType } from '@ngrx/effects';
import * as fromRouter from '@ngrx/router-store';
import { forkJoin, Observable, of } from 'rxjs';
import { catchError, map, switchMap, tap, withLatestFrom } from 'rxjs/operators';
import { ToastrService } from 'ngx-toastr';
Tifenn Guillas's avatar
Tifenn Guillas committed
9

10
import * as fromSearchMultiple from './search-multiple.reducer';
Tifenn Guillas's avatar
Tifenn Guillas committed
11
import * as searchMultipleActions from './search-multiple.action';
12
import { SearchMultipleService } from './search-multiple.service';
Tifenn Guillas's avatar
Tifenn Guillas committed
13
import * as fromMetamodel from '../../metamodel/reducers';
14
import * as datasetActions from '../../metamodel/action/dataset.action';
15
import * as attributeActions from '../../metamodel/action/attribute.action';
16
import { Dataset } from '../../metamodel/model';
Tifenn Guillas's avatar
Tifenn Guillas committed
17
import * as fromConeSearch from '../../shared/cone-search/store/cone-search.reducer';
18
19
20
import * as coneSearchActions from '../../shared/cone-search/store/cone-search.action';
import { ConeSearch } from '../../shared/cone-search/store/model';
import { Pagination, PaginationOrder } from '../../shared/datatable/model';
Tifenn Guillas's avatar
Tifenn Guillas committed
21
22
23
24
25
26
import * as utils from '../../shared/utils';

@Injectable()
export class SearchMultipleEffects {
    constructor(
        private actions$: Actions,
Tifenn Guillas's avatar
Tifenn Guillas committed
27
        private searchMultipleService: SearchMultipleService,
Tifenn Guillas's avatar
Tifenn Guillas committed
28
29
30
        private toastr: ToastrService,
        private store$: Store<{
            router: fromRouter.RouterReducerState<utils.RouterStateUrl>,
Tifenn Guillas's avatar
Tifenn Guillas committed
31
            searchMultiple: fromSearchMultiple.State,
Tifenn Guillas's avatar
Tifenn Guillas committed
32
33
34
35
36
            metamodel: fromMetamodel.State,
            coneSearch: fromConeSearch.State
        }>
    ) { }

Tifenn Guillas's avatar
Tifenn Guillas committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    @Effect()
    initSearchByUrlAction$ = this.actions$.pipe(
        ofType(searchMultipleActions.INIT_SEARCH_BY_URL),
        withLatestFrom(this.store$),
        switchMap(([action, state]) => {
            if (state.searchMultiple.pristine) {
                const actions: Action[] = [];
                if (state.router.state.queryParams.cs) {
                    const params = state.router.state.queryParams.cs.split(':');
                    const coneSearch: ConeSearch = {
                        ra: parseFloat(params[0]),
                        dec: parseFloat(params[1]),
                        radius: parseInt(params[2], 10)
                    };
51
                    actions.push(new coneSearchActions.AddConeSearchFromUrlAction(coneSearch));
Tifenn Guillas's avatar
Tifenn Guillas committed
52
53
54
                    actions.push(new searchMultipleActions.PositionCheckedAction());
                }
                if (state.router.state.queryParams.d) {
55
56
                    actions.push(new searchMultipleActions.InitSelectedDatasetsAction(state.router.state.queryParams.d.split(';')));
                    actions.push(new searchMultipleActions.DatasetsCheckedAction());
Tifenn Guillas's avatar
Tifenn Guillas committed
57
58
59
60
61
62
63
                }
                return actions;
            } else {
                return of({ type: '[No Action] ' + searchMultipleActions.INIT_SEARCH_BY_URL });
            }
        })
    );
Tifenn Guillas's avatar
Tifenn Guillas committed
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    // @Effect()
    // loadDatasetSearchMetaSuccessAction$ = this.actions$.pipe(
    //     ofType(datasetActions.LOAD_DATASET_SEARCH_META_SUCCESS),
    //     withLatestFrom(this.store$),
    //     switchMap(([action, state]) => {
    //         if (state.searchMultiple.selectedDatasets.length === 0) {
    //             const loadDatasetSearchMetaSuccessAction = action as datasetActions.LoadDatasetSearchMetaSuccessAction;
    //             const datasetWithConeSearchList: Dataset[] = loadDatasetSearchMetaSuccessAction.payload[1]
    //                 .filter(d => d.config.cone_search && d.config.cone_search.enabled === true);
    //             const selectedDatasets: string[] = datasetWithConeSearchList.map(d => d.name);
    //             return of(new searchMultipleActions.InitSelectedDatasetsAction(selectedDatasets));
    //         } else {
    //             return of({ type: '[No Action] ' + searchMultipleActions.INIT_SELECTED_DATASETS });
    //         }
    //     })
    // );
Tifenn Guillas's avatar
Tifenn Guillas committed
81

82
83
84
85
86
    @Effect()
    retrieveDatasetsCountAction$ = this.actions$.pipe(
        ofType(searchMultipleActions.RETRIEVE_DATASETS_COUNT),
        withLatestFrom(this.store$),
        switchMap(([action, state]) => {
87
            const datasetList: Dataset[] = state.metamodel.dataset.datasetList;
88
            let requests: Observable<any>[] = [];
Tifenn Guillas's avatar
Tifenn Guillas committed
89
            state.searchMultiple.selectedDatasets.forEach(dname => {
90
91
92
93
94
95
                const datasetConfig = datasetList.find(d => d.name === dname).config;
                if (datasetConfig !== null && 'cone_search' in datasetConfig && datasetConfig.cone_search.enabled) {
                    let query: string = dname + '?a=count';
                    query += '&cs=' + state.coneSearch.coneSearch.ra + ':' + state.coneSearch.coneSearch.dec + ':' + state.coneSearch.coneSearch.radius;
                    requests.push(this.searchMultipleService.getCount(dname, query));
                }
96
97
98
            });
            return forkJoin(requests);
        }),
99
100
        withLatestFrom(this.store$),
        switchMap(([data, state]) => {
101
            let counts: { dname: string, count: number }[] = [];
Tifenn Guillas's avatar
Tifenn Guillas committed
102
            data.forEach(d => {
103
104
105
                counts.push(d);
            });
            const datasetList: Dataset[] = state.metamodel.dataset.datasetList;
Tifenn Guillas's avatar
Tifenn Guillas committed
106
            state.searchMultiple.selectedDatasets.forEach(dname => {
107
108
109
110
                const datasetConfig = datasetList.find(d => d.name === dname).config;
                if (datasetConfig === null || !('cone_search' in datasetConfig) || !datasetConfig.cone_search.enabled) {
                    counts.push({ dname: dname, count: 0});
                }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            });
            return of(new searchMultipleActions.RetrieveDatasetsCountSuccessAction(counts));
        }),
        catchError(_ => {
            return of(new searchMultipleActions.RetrieveDatasetsCountFailAction());
        })
    );

    @Effect({ dispatch: false })
    retrieveDatasetsCountFailAction$ = this.actions$.pipe(
        ofType(searchMultipleActions.RETRIEVE_DATASETS_COUNT_FAIL),
        tap(_ => this.toastr.error('Loading Failed!', 'The data count of datasets loading failed'))
    );

125
126
    @Effect()
    loadAttributeListSuccessAction$ = this.actions$.pipe(
127
        ofType(attributeActions.LOAD_ATTRIBUTE_LIST_SUCCESS),
128
129
130
        withLatestFrom(this.store$),
        switchMap(([action, state]) => {
            if (state.router.state.url.includes('/search-multiple/')) {
131
                const loadAttributeListSuccessAction = action as attributeActions.LoadAttributeListSuccessAction;
132
133
134
135
136
                const dname: string = loadAttributeListSuccessAction.payload.datasetName;
                const sortedCol: number = loadAttributeListSuccessAction.payload.attributeList.find(a => a.search_flag === 'ID').id;
                const pagination: Pagination = { dname: dname, nbItems: 10, page: 1, sortedCol: sortedCol, order: PaginationOrder.a };
                return of(new searchMultipleActions.RetrieveDataAction(pagination));
            } else {
137
                return of({ type: '[No Action] ' + attributeActions.LOAD_ATTRIBUTE_LIST_SUCCESS });
138
139
140
141
            }
        })
    );

142
143
144
145
146
147
    @Effect()
    retrieveDataAction$ = this.actions$.pipe(
        ofType(searchMultipleActions.RETRIEVE_DATA),
        withLatestFrom(this.store$),
        switchMap(([action, state]) => {
            const retrieveDataAction = action as searchMultipleActions.RetrieveDataAction;
148
            const dname: string = retrieveDataAction.payload.dname;
149
            const outputList: number[] = state.metamodel.attribute.entities[dname].attributeList
150
151
152
153
154
155
                .filter(attribute => attribute.selected && attribute.id_output_category)
                .sort((a, b) => a.output_display - b.output_display)
                .map(attribute => attribute.id);
            let query = dname;
            query +='?a=' + outputList.join(';');
            query += '&cs=' + state.coneSearch.coneSearch.ra + ':' + state.coneSearch.coneSearch.dec + ':' + state.coneSearch.coneSearch.radius;
156
157
            query += '&p=' + retrieveDataAction.payload.nbItems + ':' + retrieveDataAction.payload.page;
            query += '&o=' + retrieveDataAction.payload.sortedCol + ':' + retrieveDataAction.payload.order;
158
            return this.searchMultipleService.retrieveData(dname, query).pipe(
159
160
                map((data: { datasetName: string, data: any[] }) => new searchMultipleActions.RetrieveDataSuccessAction(data)),
                // TODO: Pass dname
161
                catchError(() => of(new searchMultipleActions.RetrieveDataFailAction('')))
162
163
164
            );
        })
    );
Tifenn Guillas's avatar
Tifenn Guillas committed
165

166
167
168
169
170
    @Effect({ dispatch: false })
    retrieveDataFailAction$ = this.actions$.pipe(
        ofType(searchMultipleActions.RETRIEVE_DATA_FAIL),
        tap(_ => this.toastr.error('Loading Failed!', 'The search data loading failed'))
    );
Tifenn Guillas's avatar
Tifenn Guillas committed
171
}