From aff02b2ca12b94e1a63dfcf9b8a516f5554e8c11 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 29 Jun 2021 08:02:53 -0700 Subject: [PATCH] Change internal storage to number instead of Date Summary: per title, for reasons see next diff :) Reviewed By: passy Differential Revision: D29327501 fbshipit-source-id: 548d943e90769af478232d3031d916fb399a067a --- .../plugins/public/crash_reporter/Crashes.tsx | 4 +++- .../__tests__/testCrashReporterPlugin.node.tsx | 16 +++++++++------- .../crash_reporter/android-crash-utils.tsx | 6 +++--- desktop/plugins/public/crash_reporter/index.tsx | 8 ++++---- .../public/crash_reporter/ios-crash-utils.tsx | 8 +++++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/desktop/plugins/public/crash_reporter/Crashes.tsx b/desktop/plugins/public/crash_reporter/Crashes.tsx index 66670b671..861f3ef8c 100644 --- a/desktop/plugins/public/crash_reporter/Crashes.tsx +++ b/desktop/plugins/public/crash_reporter/Crashes.tsx @@ -35,7 +35,9 @@ export function Crashes() { items={crashes.map((crash) => ({ id: crash.notificationID, title: crash.reason ?? crash.name, - description: `${crash.date.toLocaleString()} - ${crash.name}`, + description: `${new Date(crash.date).toLocaleString()} - ${ + crash.name + }`, }))} selection={selectedCrashId} onSelect={(id) => { diff --git a/desktop/plugins/public/crash_reporter/__tests__/testCrashReporterPlugin.node.tsx b/desktop/plugins/public/crash_reporter/__tests__/testCrashReporterPlugin.node.tsx index b7eb2a089..fd146ffc4 100644 --- a/desktop/plugins/public/crash_reporter/__tests__/testCrashReporterPlugin.node.tsx +++ b/desktop/plugins/public/crash_reporter/__tests__/testCrashReporterPlugin.node.tsx @@ -8,7 +8,7 @@ */ import {BaseDevice} from 'flipper'; -import {Crash} from '../index'; +import {Crash, CrashLog} from '../index'; import {TestUtils} from 'flipper-plugin'; import {getPluginKey} from 'flipper'; import * as CrashReporterPlugin from '../index'; @@ -24,13 +24,13 @@ function getCrash( callstack: string, name: string, reason: string, -): Crash { +): Crash & CrashLog { return { notificationID: id.toString(), callstack: callstack, reason: reason, name: name, - date: new Date(), + date: new Date().getTime(), }; } @@ -40,7 +40,7 @@ function assertCrash(crash: Crash, expectedCrash: Crash) { expect(callstack).toEqual(expectedCrash.callstack); expect(reason).toEqual(expectedCrash.reason); expect(name).toEqual(expectedCrash.name); - expect(date.toDateString()).toEqual(expectedCrash.date.toDateString()); + expect(Math.abs(date - expectedCrash.date)).toBeLessThan(1000); } test('test the parsing of the date and crash info for the log which matches the predefined regex', () => { @@ -50,7 +50,7 @@ test('test the parsing of the date and crash info for the log which matches the expect(crash.callstack).toEqual(log); expect(crash.reason).toEqual('SIGSEGV'); expect(crash.name).toEqual('SIGSEGV'); - expect(crash.date).toEqual(new Date('2019-03-21 12:07:00.861')); + expect(crash.date).toEqual(new Date('2019-03-21 12:07:00.861').getTime()); }); test('test the parsing of the reason for crash when log matches the crash regex, but there is no mention of date', () => { @@ -95,7 +95,7 @@ test('test the parsing of the Android crash log for the proper android crash for 'java.lang.IndexOutOfBoundsException: Index: 190, Size: 0', ); expect(crash.name).toEqual('FATAL EXCEPTION: main'); - expect(crash.date).toEqual(date); + expect(crash.date).toEqual(date.getTime()); }); test('test the parsing of the Android crash log for the unknown crash format and no date', () => { const log = 'Blaa Blaa Blaa'; @@ -147,7 +147,9 @@ test('test helper setdefaultPersistedState function', () => { const crash = getCrash(0, 'callstack', 'crash0', 'crash0'); const plugin = TestUtils.startDevicePlugin(CrashReporterPlugin); plugin.instance.reportCrash(crash); - expect(plugin.exportState()).toEqual({crashes: [crash]}); + expect(plugin.exportState()).toEqual({ + crashes: [crash], + }); }); test('test getNewPersistedStateFromCrashLog for non-empty defaultPersistedState and defined pluginState', () => { const crash = getCrash(0, 'callstack', 'crash0', 'crash0'); diff --git a/desktop/plugins/public/crash_reporter/android-crash-utils.tsx b/desktop/plugins/public/crash_reporter/android-crash-utils.tsx index 18ef07acd..ac6e4805d 100644 --- a/desktop/plugins/public/crash_reporter/android-crash-utils.tsx +++ b/desktop/plugins/public/crash_reporter/android-crash-utils.tsx @@ -9,7 +9,7 @@ import type {DeviceLogEntry, DevicePluginClient} from 'flipper-plugin'; import {UNKNOWN_CRASH_REASON} from './crash-utils'; -import type {Crash, CrashLog} from './index'; +import type {CrashLog} from './index'; export function parseAndroidCrash(content: string, logDate?: Date) { const regForName = /.*\n/; @@ -35,7 +35,7 @@ export function parseAndroidCrash(content: string, logDate?: Date) { callstack: content, name: name, reason: reason, - date: logDate, + date: logDate?.getTime(), }; return crash; } @@ -53,7 +53,7 @@ export function shouldParseAndroidLog( export function startAndroidCrashWatcher( client: DevicePluginClient, - reportCrash: (payload: CrashLog | Crash) => void, + reportCrash: (payload: CrashLog) => void, ) { const referenceDate = new Date(); let androidLog: string = ''; diff --git a/desktop/plugins/public/crash_reporter/index.tsx b/desktop/plugins/public/crash_reporter/index.tsx index d4bf6e954..cc94eac6a 100644 --- a/desktop/plugins/public/crash_reporter/index.tsx +++ b/desktop/plugins/public/crash_reporter/index.tsx @@ -18,14 +18,14 @@ export type Crash = { callstack?: string; reason: string; name: string; - date: Date; + date: number; }; export type CrashLog = { callstack: string; reason: string; name: string; - date?: Date | null; + date?: number; }; export function devicePlugin(client: DevicePluginClient) { @@ -39,7 +39,7 @@ export function devicePlugin(client: DevicePluginClient) { selectedCrash.set(crashId as string); }); - function reportCrash(payload: CrashLog | Crash) { + function reportCrash(payload: CrashLog) { notificationID++; const crash = { @@ -47,7 +47,7 @@ export function devicePlugin(client: DevicePluginClient) { callstack: payload.callstack, name: payload.name, reason: payload.reason, - date: payload.date || new Date(), + date: payload.date ?? Date.now(), }; crashes.update((draft) => { diff --git a/desktop/plugins/public/crash_reporter/ios-crash-utils.tsx b/desktop/plugins/public/crash_reporter/ios-crash-utils.tsx index 097f243a8..322556ebb 100644 --- a/desktop/plugins/public/crash_reporter/ios-crash-utils.tsx +++ b/desktop/plugins/public/crash_reporter/ios-crash-utils.tsx @@ -7,7 +7,7 @@ * @format */ -import type {Crash, CrashLog} from './index'; +import type {CrashLog} from './index'; import fs from 'fs'; import os from 'os'; import path from 'path'; @@ -29,7 +29,9 @@ export function parseIosCrash(content: string) { const tmp1 = dateRegex2.exec(dateString); const extractedDateString: string | null = tmp1 && tmp1[0].length ? tmp1[0] : null; - const date = extractedDateString ? new Date(extractedDateString) : new Date(); + const date = extractedDateString + ? new Date(extractedDateString).getTime() + : Date.now(); const crash: CrashLog = { callstack: content, @@ -64,7 +66,7 @@ export function parsePath(content: string): string | null { export function addFileWatcherForiOSCrashLogs( serial: string, - reportCrash: (payload: CrashLog | Crash) => void, + reportCrash: (payload: CrashLog) => void, ) { const dir = path.join(os.homedir(), 'Library', 'Logs', 'DiagnosticReports'); if (!fs.existsSync(dir)) {