From 38f3132b66eca79325ee2ace8b2c85bb779d33bc Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 7 May 2019 08:21:32 -0700 Subject: [PATCH] Fix the export metrics format Summary: Fix the format of the export. Due to unknown reason the changes done in the D14933499 was [reverted back](https://our.intern.facebook.com/intern/diff/D14933499/?src_number=80619316) on the same diff. Thus this diff fixes the format of the metrics export Reviewed By: passy Differential Revision: D15239653 fbshipit-source-id: b9416f89e1b0022000d6201eb08ca1f456d5ed2e --- src/index.js | 1 + src/plugin.js | 6 +++--- src/plugins/network/index.js | 13 +++++++++---- src/utils/exportMetrics.js | 33 ++++++++++++++++----------------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index ba3099b39..38e8edcdc 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ export { callClient, } from './plugin.js'; export type {PluginClient, Props} from './plugin.js'; +export type {MetricType} from './utils/exportMetrics.js'; export {default as Client} from './Client.js'; export {clipboard} from 'electron'; export * from './fb-stubs/constants.js'; diff --git a/src/plugin.js b/src/plugin.js index ddca78837..e1bcbf7c0 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -10,7 +10,7 @@ import type {App} from './App.js'; import type {Logger} from './fb-interfaces/Logger.js'; import type Client from './Client.js'; import type {Store, MiddlewareAPI} from './reducers/index.js'; - +import type {MetricType} from './utils/exportMetrics.js'; import React from 'react'; import type {Node} from 'react'; import BaseDevice from './devices/BaseDevice.js'; @@ -83,9 +83,9 @@ export class FlipperBasePlugin< method: string, data: Object, ) => $Shape; - static exportMetrics: ?( + static metricsReducer: ?( persistedState: PersistedState, - ) => Promise>; + ) => Promise; static exportPersistedState: ?( callClient: (string, ?Object) => Promise, persistedState: ?PersistedState, diff --git a/src/plugins/network/index.js b/src/plugins/network/index.js index 5ebce2925..dfe0462a9 100644 --- a/src/plugins/network/index.js +++ b/src/plugins/network/index.js @@ -5,7 +5,12 @@ * @format */ -import type {TableHighlightedRows, TableRows, TableBodyRow} from 'flipper'; +import type { + TableHighlightedRows, + TableRows, + TableBodyRow, + MetricType, +} from 'flipper'; import {padStart} from 'lodash'; import { @@ -107,9 +112,9 @@ export default class extends FlipperPlugin { responses: {}, }; - static exportMetrics = ( + static metricsReducer = ( persistedState: PersistedState, - ): Promise> => { + ): Promise => { const failures = Object.keys(persistedState.responses).reduce(function( previous, key, @@ -117,7 +122,7 @@ export default class extends FlipperPlugin { return previous + (persistedState.responses[key].status >= 400); }, 0); - return Promise.resolve(new Map([['NUMBER_NETWORK_FAILURES', failures]])); + return Promise.resolve({NUMBER_NETWORK_FAILURES: failures}); }; static persistedStateReducer = ( diff --git a/src/utils/exportMetrics.js b/src/utils/exportMetrics.js index 0997676c5..db65741f8 100644 --- a/src/utils/exportMetrics.js +++ b/src/utils/exportMetrics.js @@ -4,19 +4,19 @@ * LICENSE file in the root directory of this source tree. * @format */ -import type {MiddlewareAPI} from '../reducers'; -import {serialize} from './serialization.js'; import type {FlipperPlugin, FlipperDevicePlugin} from 'flipper'; +import {serialize} from './serialization'; +import type {MiddlewareAPI} from '../reducers'; -type MetricType = Map>; - -export type ExportMetricType = Map; +export type MetricType = {[metricName: string]: number}; +type MetricPluginType = {[pluginID: string]: MetricType}; +export type ExportMetricType = {[clientID: string]: MetricPluginType}; export default async function exportMetrics( store: MiddlewareAPI, ): Promise { const state = store.getState(); - let metrics: ExportMetricType = new Map(); + const metrics: ExportMetricType = {}; for (let key in state.pluginStates) { const pluginStateData = state.pluginStates[key]; const arr = key.split('#'); @@ -32,20 +32,19 @@ export default async function exportMetrics( state.plugins.devicePlugins.forEach((val, key) => { pluginsMap.set(key, val); }); - const exportMetrics1: ?( + const metricsReducer: ?( persistedState: any, - ) => Promise> = pluginsMap.get(pluginName) - ?.exportMetrics; - if (pluginsMap.has(pluginName) && exportMetrics1) { - const metricMap = await exportMetrics1(pluginStateData); - const pluginMap = new Map([[pluginName, metricMap]]); - if (!metrics.get(clientID)) { - metrics.set(clientID, pluginMap); + ) => Promise = pluginsMap.get(pluginName)?.metricsReducer; + if (pluginsMap.has(pluginName) && metricsReducer) { + const metricsObject = await metricsReducer(pluginStateData); + const pluginObject = {}; + pluginObject[pluginName] = metricsObject; + if (!metrics[clientID]) { + metrics[clientID] = pluginObject; continue; } - const prevMetricMap = metrics.get(clientID); - // $FlowFixMe: prevMetricMap cannot be null, because clientID is added only when the pluingMetricMap is available - metrics.set(clientID, new Map([...prevMetricMap, ...pluginMap])); + const mergedMetrics = {...metrics[clientID], ...pluginObject}; + metrics[clientID] = mergedMetrics; } } return Promise.resolve(serialize(metrics));