forked from tensorflow/tensorboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_deeplink_provider.ts
289 lines (272 loc) · 8.17 KB
/
dashboard_deeplink_provider.ts
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
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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
285
286
287
288
289
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
import {Injectable} from '@angular/core';
import {Store} from '@ngrx/store';
import {combineLatest, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {DeepLinkProvider} from '../app_routing/deep_link_provider';
import {SerializableQueryParams} from '../app_routing/types';
import {State} from '../app_state';
import {
FeatureFlagMetadata,
FeatureFlagMetadataMap,
FeatureFlagType,
} from '../feature_flag/store/feature_flag_metadata';
import {getOverriddenFeatureFlags} from '../feature_flag/store/feature_flag_selectors';
import {
isPluginType,
isSampledPlugin,
isSingleRunPlugin,
} from '../metrics/data_source/types';
import {CardUniqueInfo} from '../metrics/types';
import {GroupBy, GroupByKey} from '../runs/types';
import * as selectors from '../selectors';
import {
DeserializedState,
PINNED_CARDS_KEY,
RUN_COLOR_GROUP_KEY,
RUN_FILTER_KEY,
SMOOTHING_KEY,
TAG_FILTER_KEY,
} from './dashboard_deeplink_provider_types';
import {featureFlagsToSerializableQueryParams} from './feature_flag_serializer';
const COLOR_GROUP_REGEX_VALUE_PREFIX = 'regex:';
/**
* Provides deeplinking for the core dashboards page.
*/
@Injectable()
export class DashboardDeepLinkProvider extends DeepLinkProvider {
private getMetricsPinnedCards(
store: Store<State>
): Observable<SerializableQueryParams> {
return combineLatest([
store.select(selectors.getPinnedCardsWithMetadata),
store.select(selectors.getUnresolvedImportedPinnedCards),
]).pipe(
map(([pinnedCards, unresolvedImportedPinnedCards]) => {
if (!pinnedCards.length && !unresolvedImportedPinnedCards.length) {
return [];
}
const pinnedCardsToStore = pinnedCards.map(
({plugin, tag, sample, runId}) => {
const info = {plugin, tag} as CardUniqueInfo;
if (isSingleRunPlugin(plugin)) {
info.runId = runId!;
}
if (isSampledPlugin(plugin)) {
info.sample = sample!;
}
return info;
}
);
// Intentionally order unresolved cards last, so that cards pinned by
// the user in this session have priority.
const cardsToStore = [
...pinnedCardsToStore,
...unresolvedImportedPinnedCards,
];
return [{key: 'pinnedCards', value: JSON.stringify(cardsToStore)}];
})
);
}
serializeStateToQueryParams(
store: Store<State>
): Observable<SerializableQueryParams> {
return combineLatest([
this.getMetricsPinnedCards(store),
store.select(selectors.getMetricsTagFilter).pipe(
map((filterText) => {
if (!filterText) {
return [];
}
return [{key: TAG_FILTER_KEY, value: filterText}];
})
),
store.select(getOverriddenFeatureFlags).pipe(
map((featureFlags) => {
return featureFlagsToSerializableQueryParams(
featureFlags,
FeatureFlagMetadataMap as Record<
string,
FeatureFlagMetadata<FeatureFlagType>
>
);
})
),
store.select(selectors.getMetricsSettingOverrides).pipe(
map((settingOverrides) => {
if (Number.isFinite(settingOverrides.scalarSmoothing)) {
return [
{
key: SMOOTHING_KEY,
value: String(settingOverrides.scalarSmoothing),
},
];
}
return [];
})
),
store.select(selectors.getRunUserSetGroupBy).pipe(
map((groupBy) => {
if (!groupBy) return [];
let value: string;
switch (groupBy.key) {
case GroupByKey.EXPERIMENT:
value = 'experiment';
break;
case GroupByKey.RUN:
value = 'run';
break;
case GroupByKey.REGEX:
value = `${COLOR_GROUP_REGEX_VALUE_PREFIX}${groupBy.regexString}`;
break;
default:
throw new RangeError(`Serialization not implemented`);
}
return [{key: RUN_COLOR_GROUP_KEY, value}];
})
),
store.select(selectors.getRunSelectorRegexFilter).pipe(
map((value) => {
if (!value) return [];
return [{key: RUN_FILTER_KEY, value}];
})
),
]).pipe(
map((queryParamList) => {
return queryParamList.flat();
})
);
}
deserializeQueryParams(
queryParams: SerializableQueryParams
): DeserializedState {
let pinnedCards = null;
let smoothing = null;
let tagFilter = null;
let groupBy: GroupBy | null = null;
let runFilter = null;
for (const {key, value} of queryParams) {
switch (key) {
case PINNED_CARDS_KEY:
pinnedCards = extractPinnedCardsFromURLText(value);
break;
case SMOOTHING_KEY:
smoothing = Number(value);
break;
case RUN_COLOR_GROUP_KEY: {
switch (value) {
case 'experiment':
groupBy = {key: GroupByKey.EXPERIMENT};
break;
case 'run':
groupBy = {key: GroupByKey.RUN};
break;
}
if (value.startsWith(COLOR_GROUP_REGEX_VALUE_PREFIX)) {
const regexString = value.slice(
COLOR_GROUP_REGEX_VALUE_PREFIX.length
);
groupBy = {key: GroupByKey.REGEX, regexString};
}
break;
}
case TAG_FILTER_KEY:
tagFilter = value;
break;
case RUN_FILTER_KEY:
runFilter = value;
break;
}
}
return {
metrics: {
pinnedCards: pinnedCards || [],
smoothing,
tagFilter,
},
runs: {
groupBy,
regexFilter: runFilter,
},
};
}
}
function extractPinnedCardsFromURLText(
urlText: string
): CardUniqueInfo[] | null {
// Check that the URL text parses.
let object;
try {
object = JSON.parse(urlText) as unknown;
} catch {
return null;
}
if (!Array.isArray(object)) {
return null;
}
const result = [];
for (const item of object) {
// Validate types.
const isPluginString = typeof item.plugin === 'string';
const isRunString = typeof item.runId === 'string';
const isSampleNumber = typeof item.sample === 'number';
const isTagString = typeof item.tag === 'string';
const isRunTypeValid = isRunString || typeof item.runId === 'undefined';
const isSampleTypeValid =
isSampleNumber || typeof item.sample === 'undefined';
if (
!isPluginString ||
!isTagString ||
!isRunTypeValid ||
!isSampleTypeValid
) {
continue;
}
// Required fields and range errors.
if (!isPluginType(item.plugin)) {
continue;
}
if (!item.tag) {
continue;
}
if (isSingleRunPlugin(item.plugin)) {
// A single run plugin must specify a non-empty run.
if (!item.runId) {
continue;
}
} else {
// A multi run plugin must not specify a run.
if (item.runId) {
continue;
}
}
if (isSampleNumber) {
if (!isSampledPlugin(item.plugin)) {
continue;
}
if (!Number.isInteger(item.sample) || item.sample < 0) {
continue;
}
}
// Assemble result.
const resultItem = {plugin: item.plugin, tag: item.tag} as CardUniqueInfo;
if (isRunString) {
resultItem.runId = item.runId;
}
if (isSampleNumber) {
resultItem.sample = item.sample;
}
result.push(resultItem);
}
return result;
}