|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import {Injectable} from '@angular/core'; |
| 16 | +import {Actions, createEffect, ofType} from '@ngrx/effects'; |
| 17 | +import {Store} from '@ngrx/store'; |
| 18 | +import {Observable, of, throwError, merge} from 'rxjs'; |
| 19 | +import { |
| 20 | + catchError, |
| 21 | + filter, |
| 22 | + map, |
| 23 | + switchMap, |
| 24 | + withLatestFrom, |
| 25 | + throttleTime, |
| 26 | + combineLatestWith, |
| 27 | +} from 'rxjs/operators'; |
| 28 | + |
| 29 | +import {navigated} from '../../app_routing/actions'; |
| 30 | +import { |
| 31 | + getActiveRoute, |
| 32 | + getExperimentIdsFromRoute, |
| 33 | +} from '../../app_routing/store/app_routing_selectors'; |
| 34 | +import {State} from '../../app_state'; |
| 35 | +import * as coreActions from '../../core/actions'; |
| 36 | +import * as runsActions from '../../runs/actions/runs_actions'; |
| 37 | +import {HttpErrorResponse} from '../../webapp_data_source/tb_http_client'; |
| 38 | + |
| 39 | +import * as hparamsActions from './hparams_actions'; |
| 40 | +import {HparamsDataSource} from './hparams_data_source'; |
| 41 | +import {HparamAndMetricSpec, SessionGroup} from '../types'; |
| 42 | +import {getEnableHparamsInTimeSeries} from '../../feature_flag/store/feature_flag_selectors'; |
| 43 | +import {RouteKind} from '../../app_routing/types'; |
| 44 | + |
| 45 | +/** |
| 46 | + * Effects for fetching the hparams data from the backend. |
| 47 | + */ |
| 48 | +@Injectable() |
| 49 | +export class HparamsEffects { |
| 50 | + constructor( |
| 51 | + private readonly actions$: Actions, |
| 52 | + private readonly store: Store<State>, |
| 53 | + private readonly dataSource: HparamsDataSource |
| 54 | + ) {} |
| 55 | + |
| 56 | + private readonly runTableShown$: Observable<string[]> = this.actions$.pipe( |
| 57 | + ofType(runsActions.runTableShown), |
| 58 | + map(({experimentIds}) => experimentIds) |
| 59 | + ); |
| 60 | + |
| 61 | + private readonly loadHparamsOnNavigationOrReload$: Observable<string[]> = |
| 62 | + this.actions$.pipe( |
| 63 | + ofType(navigated, coreActions.reload, coreActions.manualReload), |
| 64 | + withLatestFrom(this.store.select(getExperimentIdsFromRoute)), |
| 65 | + filter(([, experimentIds]) => Boolean(experimentIds)), |
| 66 | + map(([, experimentIds]) => experimentIds as string[]) |
| 67 | + ); |
| 68 | + |
| 69 | + /** @export */ |
| 70 | + loadHparamsData$ = createEffect(() => { |
| 71 | + return merge( |
| 72 | + this.runTableShown$, |
| 73 | + this.loadHparamsOnNavigationOrReload$ |
| 74 | + ).pipe( |
| 75 | + combineLatestWith( |
| 76 | + this.store.select(getEnableHparamsInTimeSeries), |
| 77 | + this.store.select(getActiveRoute) |
| 78 | + ), |
| 79 | + filter( |
| 80 | + ([, getEnableHparamsInTimeSeries]) => getEnableHparamsInTimeSeries |
| 81 | + ), |
| 82 | + filter( |
| 83 | + ([, , activeRoute]) => |
| 84 | + activeRoute?.routeKind === RouteKind.EXPERIMENT || |
| 85 | + activeRoute?.routeKind === RouteKind.COMPARE_EXPERIMENT |
| 86 | + ), |
| 87 | + throttleTime(10), |
| 88 | + switchMap(([experimentIds]) => |
| 89 | + this.loadHparamsForExperiments(experimentIds) |
| 90 | + ), |
| 91 | + map((resp) => hparamsActions.hparamsFetchSessionGroupsSucceeded(resp)) |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + private loadHparamsForExperiments(experimentIds: string[]): Observable<{ |
| 96 | + hparamsAndMetricsSpecs: HparamAndMetricSpec; |
| 97 | + sessionGroups: SessionGroup[]; |
| 98 | + }> { |
| 99 | + return this.dataSource.fetchExperimentInfo(experimentIds).pipe( |
| 100 | + switchMap((hparamsAndMetricsSpecs) => { |
| 101 | + return this.dataSource |
| 102 | + .fetchSessionGroups(experimentIds, hparamsAndMetricsSpecs) |
| 103 | + .pipe( |
| 104 | + catchError((error) => { |
| 105 | + // HParam plugin return 400 when there are no hparams |
| 106 | + // for an experiment. |
| 107 | + if (error instanceof HttpErrorResponse && error.status === 400) { |
| 108 | + return of([] as SessionGroup[]); |
| 109 | + } |
| 110 | + return throwError(() => error); |
| 111 | + }), |
| 112 | + map((sessionGroups) => ({hparamsAndMetricsSpecs, sessionGroups})) |
| 113 | + ); |
| 114 | + }) |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments