Call exportState function before exporting metrics

Summary:
This diff fetches the additional data by calling the hook added in each plugins when the headless tries to export metrics directly out of the store rather than from the trace.

I added `fetchMetadata` function which will be called while exporting the state as well as exporting the metrics. We do not need to fetch metadata in the case when the metrics are exported from the given trace.

Reviewed By: passy

Differential Revision: D15560129

fbshipit-source-id: 9b14340e565ce17d1825bc2d32520d7b0c2b2219
This commit is contained in:
Pritesh Nandgaonkar
2019-06-03 15:03:25 -07:00
committed by Facebook Github Bot
parent 023135ad74
commit 95fae7d4e8
3 changed files with 76 additions and 41 deletions

View File

@@ -25,6 +25,8 @@ import {readCurrentRevision} from './packageMetadata.js';
import {tryCatchReportPlatformFailures} from './metrics';
import {promisify} from 'util';
import promiseTimeout from './promiseTimeout';
import type {State} from '../reducers';
export const IMPORT_FLIPPER_TRACE_EVENT = 'import-flipper-trace';
export const EXPORT_FLIPPER_TRACE_EVENT = 'export-flipper-trace';
@@ -46,6 +48,22 @@ export function processClients(
return clients.filter(client => client.query.device_id === serial);
}
export function pluginsClassMap(
state: State,
): Map<string, Class<FlipperDevicePlugin<> | FlipperPlugin<>>> {
const pluginsMap: Map<
string,
Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
> = new Map([]);
state.plugins.clientPlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
state.plugins.devicePlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
return pluginsMap;
}
export function processPluginStates(
clients: Array<ClientExport>,
serial: string,
@@ -184,34 +202,17 @@ export const processStore = async (
return null;
};
export async function getStoreExport(
export async function fetchMetadata(
pluginStates: PluginStatesState,
pluginsMap: Map<string, Class<FlipperDevicePlugin<> | FlipperPlugin<>>>,
store: MiddlewareAPI,
): Promise<{exportData: ?ExportType, errorArray: Array<Error>}> {
const state = store.getState();
const {clients} = state.connections;
const {pluginStates} = state;
const {plugins} = state;
const {selectedDevice} = store.getState().connections;
if (!selectedDevice) {
throw new Error('Please select a device before exporting data.');
}
): Promise<{pluginStates: PluginStatesState, errorArray: Array<Error>}> {
const newPluginState = {...pluginStates};
// TODO: T39612653 Make Client mockable. Currently rsocket logic is tightly coupled.
// Not passing the entire state as currently Client is not mockable.
const pluginsMap: Map<
string,
Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
> = new Map([]);
plugins.clientPlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
plugins.devicePlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
const errorArray: Array<Error> = [];
const clients = store.getState().connections.clients;
const selectedDevice = store.getState().connections.selectedDevice;
for (let client of clients) {
if (!client.id.includes(selectedDevice.serial)) {
if (!selectedDevice || !client.id.includes(selectedDevice.serial)) {
continue;
}
for (let plugin of client.plugins) {
@@ -235,6 +236,37 @@ export async function getStoreExport(
}
}
}
return {pluginStates: newPluginState, errorArray};
}
export async function getStoreExport(
store: MiddlewareAPI,
): Promise<{exportData: ?ExportType, errorArray: Array<Error>}> {
const state = store.getState();
const {clients} = state.connections;
const {pluginStates} = state;
const {plugins} = state;
const {selectedDevice} = store.getState().connections;
if (!selectedDevice) {
throw new Error('Please select a device before exporting data.');
}
// TODO: T39612653 Make Client mockable. Currently rsocket logic is tightly coupled.
// Not passing the entire state as currently Client is not mockable.
const pluginsMap: Map<
string,
Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
> = new Map([]);
plugins.clientPlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
plugins.devicePlugins.forEach((val, key) => {
pluginsMap.set(key, val);
});
const metadata = await fetchMetadata(pluginStates, pluginsMap, store);
const {errorArray} = metadata;
const newPluginState = metadata.pluginStates;
const {activeNotifications} = store.getState().notifications;
const {devicePlugins} = store.getState().plugins;