From 2a34125413dba36e035d127871d108434c1d528e Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Wed, 14 Aug 2019 04:36:51 -0700 Subject: [PATCH] Migrate metrics Summary: Had to mess with the tagged union we had, to distinguish based on a string rather a bool which is a bit rubbish. Reviewed By: danielbuechele Differential Revision: D16764797 fbshipit-source-id: c33536f17b0705ca40abef8448802f9961c4c114 --- src/chrome/ShareSheet.tsx | 6 ++-- src/fb-stubs/user.tsx | 31 ++++++++++---------- src/utils/{metrics.js => metrics.tsx} | 42 +++++++++++++-------------- 3 files changed, 40 insertions(+), 39 deletions(-) rename src/utils/{metrics.js => metrics.tsx} (81%) diff --git a/src/chrome/ShareSheet.tsx b/src/chrome/ShareSheet.tsx index a4b8d91ba..f96a3b5a7 100644 --- a/src/chrome/ShareSheet.tsx +++ b/src/chrome/ShareSheet.tsx @@ -20,7 +20,7 @@ import React, {Component} from 'react'; import {setExportStatusComponent, unsetShare} from '../reducers/application'; import {Logger} from '../fb-interfaces/Logger.js'; import {Idler} from '../utils/Idler'; -import {shareFlipperData} from '../fb-stubs/user'; +import {shareFlipperData, DataExportResult} from '../fb-stubs/user'; import {exportStore, EXPORT_FLIPPER_TRACE_EVENT} from '../utils/exportData'; import PropTypes from 'prop-types'; import {clipboard} from 'electron'; @@ -139,8 +139,8 @@ export default class ShareSheet extends Component { ); this.setState({errorArray, result}); - if (result.flipperUrl) { - clipboard.writeText(String(result.flipperUrl)); + if ((result as DataExportResult).flipperUrl) { + clipboard.writeText(String((result as DataExportResult).flipperUrl)); new Notification('Sharable Flipper trace created', { body: 'URL copied to clipboard', requireInteraction: true, diff --git a/src/fb-stubs/user.tsx b/src/fb-stubs/user.tsx index 623b3f4a2..6c5a869be 100644 --- a/src/fb-stubs/user.tsx +++ b/src/fb-stubs/user.tsx @@ -17,23 +17,24 @@ export function logoutUser(): Promise { return Promise.reject(); } +export type DataExportResult = { + id: string; + os: 'string'; + deviceType: string; + plugins: string[]; + fileUrl: string; + flipperUrl: string; +}; + +export type DataExportError = { + error: string; + error_class: string; + stacktrace: string; +}; + export async function shareFlipperData( trace: string, -): Promise< - | { - id: string; - os: 'string'; - deviceType: string; - plugins: string[]; - fileUrl: string; - flipperUrl: string; - } - | { - error: string; - error_class: string; - stacktrace: string; - } -> { +): Promise { new Notification('Feature not implemented'); return Promise.reject(); } diff --git a/src/utils/metrics.js b/src/utils/metrics.tsx similarity index 81% rename from src/utils/metrics.js rename to src/utils/metrics.tsx index e8d6d81f4..10df18a25 100644 --- a/src/utils/metrics.js +++ b/src/utils/metrics.tsx @@ -5,8 +5,13 @@ * @format */ -import {getInstance} from '../fb-stubs/Logger.tsx'; -import {CancelledPromiseError} from './errors.tsx'; +import {getInstance} from '../fb-stubs/Logger'; +import {CancelledPromiseError} from './errors'; + +type Result = + | {kind: 'success'} + | {kind: 'cancelled'} + | {kind: 'failure'; supportedOperation: boolean; error: any}; export class UnsupportedError extends Error { constructor(message: string) { @@ -27,17 +32,17 @@ export function reportPlatformFailures( ): Promise { return promise.then( fulfilledValue => { - logPlatformSuccessRate(name, {isSuccess: true}); + logPlatformSuccessRate(name, {kind: 'success'}); return fulfilledValue; }, rejectionReason => { if (rejectionReason instanceof CancelledPromiseError) { logPlatformSuccessRate(name, { - isCancelled: true, + kind: 'cancelled', }); } else { logPlatformSuccessRate(name, { - isSuccess: false, + kind: 'failure', supportedOperation: !(rejectionReason instanceof UnsupportedError), error: rejectionReason, }); @@ -61,17 +66,17 @@ export function reportPluginFailures( ): Promise { return promise.then( fulfilledValue => { - logPluginSuccessRate(name, plugin, {isSuccess: true}); + logPluginSuccessRate(name, plugin, {kind: 'success'}); return fulfilledValue; }, rejectionReason => { if (rejectionReason instanceof CancelledPromiseError) { - logPluginSuccessRate(name, plugin, { - isCancelled: true, + logPlatformSuccessRate(name, { + kind: 'cancelled', }); } else { - logPluginSuccessRate(name, plugin, { - isSuccess: false, + logPlatformSuccessRate(name, { + kind: 'failure', supportedOperation: !(rejectionReason instanceof UnsupportedError), error: rejectionReason, }); @@ -91,11 +96,11 @@ export function tryCatchReportPlatformFailures( ): T { try { const result = closure(); - logPlatformSuccessRate(name, {isSuccess: true}); + logPlatformSuccessRate(name, {kind: 'success'}); return result; } catch (e) { logPlatformSuccessRate(name, { - isSuccess: false, + kind: 'failure', supportedOperation: !(e instanceof UnsupportedError), error: e, }); @@ -103,15 +108,10 @@ export function tryCatchReportPlatformFailures( } } -type Result = - | {isSuccess: true} - | {isCancelled: true} - | {isSuccess: false, supportedOperation: boolean, error: any}; - function logPlatformSuccessRate(name: string, result: Result) { - if (result.isSuccess) { + if (result.kind === 'success') { getInstance().track('success-rate', name, {value: 1}); - } else if (result.isCancelled) { + } else if (result.kind === 'cancelled') { getInstance().track('operation-cancelled', name); } else { getInstance().track('success-rate', name, { @@ -123,9 +123,9 @@ function logPlatformSuccessRate(name: string, result: Result) { } function logPluginSuccessRate(name: string, plugin: string, result: Result) { - if (result.isSuccess) { + if (result.kind === 'success') { getInstance().track('success-rate', name, {value: 1}, plugin); - } else if (result.isCancelled) { + } else if (result.kind === 'cancelled') { getInstance().track('operation-cancelled', name, undefined, plugin); } else { getInstance().track(