blob: 1c5b8629b06f73f9d14a0aa28cad78fb6d0c6436 [file] [log] [blame]
/**
* @license
* Copyright (C) 2020 The Android Open Source Project
*
* 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 {BehaviorSubject, Observable} from 'rxjs';
import {
Action,
Category,
CheckResult,
CheckRun,
ChecksApiConfig,
LinkIcon,
RunStatus,
} from '../../api/checks';
import {distinctUntilChanged, map} from 'rxjs/operators';
import {PatchSetNumber} from '../../types/common';
// This is a convenience type for working with results, because when working
// with a bunch of results you will typically also want to know about the run
// properties. So you can just combine them with {...run, ...result}.
export type RunResult = CheckRun & CheckResult;
interface ChecksProviderState {
pluginName: string;
loading: boolean;
config?: ChecksApiConfig;
runs: CheckRun[];
actions: Action[];
}
interface ChecksState {
patchsetNumber?: PatchSetNumber;
providerNameToState: {
[name: string]: ChecksProviderState;
};
}
const initialState: ChecksState = {
providerNameToState: {},
};
const privateState$ = new BehaviorSubject(initialState);
// Re-exporting as Observable so that you can only subscribe, but not emit.
export const checksState$: Observable<ChecksState> = privateState$;
export const checksPatchsetNumber$ = checksState$.pipe(
map(state => state.patchsetNumber),
distinctUntilChanged()
);
export const checksProviderState$ = checksState$.pipe(
map(state => state.providerNameToState),
distinctUntilChanged()
);
export const aPluginHasRegistered$ = checksProviderState$.pipe(
map(state => Object.keys(state).length > 0),
distinctUntilChanged()
);
export const someProvidersAreLoading$ = checksProviderState$.pipe(
map(state => {
return Object.values(state).some(providerState => providerState.loading);
}),
distinctUntilChanged()
);
export const allActions$ = checksProviderState$.pipe(
map(state => {
return Object.values(state).reduce(
(allActions: Action[], providerState: ChecksProviderState) => [
...allActions,
...providerState.actions,
],
[]
);
})
);
export const allRuns$ = checksProviderState$.pipe(
map(state => {
return Object.values(state).reduce(
(allRuns: CheckRun[], providerState: ChecksProviderState) => [
...allRuns,
...providerState.runs,
],
[]
);
})
);
export const checkToPluginMap$ = checksProviderState$.pipe(
map(state => {
const map = new Map<string, string>();
for (const [pluginName, providerState] of Object.entries(state)) {
for (const run of providerState.runs) {
map.set(run.checkName, pluginName);
}
}
return map;
})
);
export const allResults$ = checksProviderState$.pipe(
map(state => {
return Object.values(state)
.reduce(
(allResults: CheckResult[], providerState: ChecksProviderState) => [
...allResults,
...providerState.runs.reduce(
(results: CheckResult[], run: CheckRun) =>
results.concat(run.results ?? []),
[]
),
],
[]
)
.filter(r => r !== undefined);
})
);
// Must only be used by the checks service or whatever is in control of this
// model.
export function updateStateSetProvider(
pluginName: string,
config?: ChecksApiConfig
) {
const nextState = {...privateState$.getValue()};
nextState.providerNameToState = {...nextState.providerNameToState};
nextState.providerNameToState[pluginName] = {
pluginName,
loading: false,
config,
runs: [],
actions: [],
};
privateState$.next(nextState);
}
// TODO(brohlfs): Remove all fake runs by end of January. They are just making
// it easier to develop the UI and always see all the different types/states of
// runs and results.
export const fakeRun0: CheckRun = {
checkName: 'FAKE Error Finder',
results: [
{
category: Category.ERROR,
summary: 'I would like to point out this error: 1 is not equal to 2!',
links: [
{primary: true, url: 'https://www.google.com', icon: LinkIcon.EXTERNAL},
],
tags: [{name: 'OBSOLETE'}, {name: 'E2E'}],
},
{
category: Category.ERROR,
summary: 'Running the mighty test has failed by crashing.',
links: [
{primary: true, url: 'https://www.google.com', icon: LinkIcon.EXTERNAL},
],
},
],
status: RunStatus.COMPLETED,
};
export const fakeRun1: CheckRun = {
checkName: 'FAKE Super Check',
labelName: 'Verified',
results: [
{
category: Category.WARNING,
summary: 'We think that you could improve this.',
message: `There is a lot to be said. A lot. I say, a lot.\n
So please keep reading.`,
tags: [{name: 'INTERRUPTED'}, {name: 'WINDOWS'}],
},
],
status: RunStatus.RUNNING,
};
export const fakeRun2: CheckRun = {
checkName: 'FAKE Mega Analysis',
results: [
{
category: Category.INFO,
summary: 'This is looking a bit too large.',
message: 'We are still looking into how large exactly. Stay tuned.',
tags: [{name: 'FLAKY'}, {name: 'MAC-OS'}],
},
],
status: RunStatus.COMPLETED,
};
export const fakeRun3: CheckRun = {
checkName: 'FAKE Critical Observations',
status: RunStatus.RUNNABLE,
};
export const fakeRun4: CheckRun = {
checkName: 'FAKE TODO Elimination',
status: RunStatus.COMPLETED,
};
export function updateStateSetLoading(pluginName: string) {
const nextState = {...privateState$.getValue()};
nextState.providerNameToState = {...nextState.providerNameToState};
nextState.providerNameToState[pluginName] = {
...nextState.providerNameToState[pluginName],
loading: true,
};
privateState$.next(nextState);
}
export function updateStateSetResults(
pluginName: string,
runs: CheckRun[],
actions: Action[] = []
) {
const nextState = {...privateState$.getValue()};
nextState.providerNameToState = {...nextState.providerNameToState};
nextState.providerNameToState[pluginName] = {
...nextState.providerNameToState[pluginName],
loading: false,
runs: [...runs],
actions: [...actions],
};
privateState$.next(nextState);
}
export function updateStateSetPatchset(patchsetNumber?: PatchSetNumber) {
const nextState = {...privateState$.getValue()};
nextState.patchsetNumber = patchsetNumber;
privateState$.next(nextState);
}