From ac7aeb40f49eccfbbdb04754758dc79f37d93567 Mon Sep 17 00:00:00 2001 From: Mikhail Atuchin Date: Wed, 20 Nov 2024 17:29:02 +0700 Subject: [PATCH] Reuse channel/platform serializers --- src/core/serializers.ts | 34 ------------- src/seed_tools/utils/study_json_utils.test.ts | 4 +- src/seed_tools/utils/study_json_utils.ts | 49 +++++++++++-------- src/web/app/study_model.ts | 25 ++++++++-- 4 files changed, 51 insertions(+), 61 deletions(-) delete mode 100644 src/core/serializers.ts diff --git a/src/core/serializers.ts b/src/core/serializers.ts deleted file mode 100644 index 4da2cb09..00000000 --- a/src/core/serializers.ts +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2023 The Brave Authors. All rights reserved. -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this file, -// You can obtain one at https://mozilla.org/MPL/2.0/. -import { Study_Channel, Study_Platform } from '../proto/generated/study'; - -export function getPlatformNameFromString(protoPlatfrom: string): string { - const PREFIX = 'PLATFORM_'; - if (protoPlatfrom.startsWith(PREFIX)) - return protoPlatfrom.substring(PREFIX.length); - return protoPlatfrom; -} - -export function getPlatfromName(protoPlatfrom: Study_Platform): string { - return getPlatformNameFromString(Study_Platform[protoPlatfrom]); -} - -export function getChannelNameFromString( - protoChannel: string, - isBraveSpecific: boolean, -): string { - if (isBraveSpecific) { - if (protoChannel === 'STABLE') return 'RELEASE'; - if (protoChannel === 'CANARY') return 'NIGHTLY'; - } - return protoChannel; -} - -export function getChannelName( - protoChannel: Study_Channel, - isBraveSpecific: boolean, -): string { - return getChannelNameFromString(Study_Channel[protoChannel], isBraveSpecific); -} diff --git a/src/seed_tools/utils/study_json_utils.test.ts b/src/seed_tools/utils/study_json_utils.test.ts index 9c29d313..6e3925e5 100644 --- a/src/seed_tools/utils/study_json_utils.test.ts +++ b/src/seed_tools/utils/study_json_utils.test.ts @@ -112,7 +112,7 @@ describe('stringifyStudies', () => { filter: { start_date: Math.floor(startDate.getTime() / 1000), channel: ['CANARY', 'BETA', 'STABLE'], - platform: ['PLATFORM_LINUX', 'PLATFORM_MAC'], + platform: ['LINUX', 'MAC'], }, }, { ignoreUnknownFields: false }, @@ -127,7 +127,7 @@ describe('stringifyStudies', () => { filter: { start_date: startDate.toISOString(), channel: ['CANARY', 'BETA', 'STABLE'], - platform: ['PLATFORM_LINUX', 'PLATFORM_MAC'], + platform: ['LINUX', 'MAC'], }, }, ]); diff --git a/src/seed_tools/utils/study_json_utils.ts b/src/seed_tools/utils/study_json_utils.ts index 4ddda910..ff77221d 100644 --- a/src/seed_tools/utils/study_json_utils.ts +++ b/src/seed_tools/utils/study_json_utils.ts @@ -86,6 +86,29 @@ export function stringifyStudies(studies: Study[], options?: Options): string { ); } +export function replaceChannels( + channels: string[] | undefined, + isChromium: boolean, +): string[] | undefined { + if (isChromium) return channels; + + return channels?.map((channel) => { + switch (channel) { + case 'CANARY': + return 'NIGHTLY'; + case 'STABLE': + return 'RELEASE'; + } + return channel; + }); +} + +export function replacePlatforms( + platforms: string[] | undefined, +): string[] | undefined { + return platforms?.map((platform) => platform.replace(/^PLATFORM_/, '')); +} + function jsonStudyReplacer( options: Options | undefined, key: string, @@ -96,26 +119,12 @@ function jsonStudyReplacer( case 'end_date': { return new Date(value * 1000).toISOString(); } - case 'channel': - if (options?.isChromium === true) { - return value; - } - return value.map((value: string): string => { - switch (value) { - case 'CANARY': - return 'NIGHTLY'; - case 'STABLE': - return 'RELEASE'; - } - return value; - }); - case 'platform': - if (options?.isChromium === true) { - return value; - } - return value.map((value: string): string => { - return value.replace(/^PLATFORM_/, ''); - }); + case 'channel': { + return replaceChannels(value, options?.isChromium === true); + } + case 'platform': { + return replacePlatforms(value); + } default: return value; } diff --git a/src/web/app/study_model.ts b/src/web/app/study_model.ts index 46b016e6..f7cd84f4 100644 --- a/src/web/app/study_model.ts +++ b/src/web/app/study_model.ts @@ -4,14 +4,21 @@ // You can obtain one at https://mozilla.org/MPL/2.0/. import { SeedType } from '../../core/base_types'; -import { getChannelName, getPlatfromName } from '../../core/serializers'; import { type ProcessedStudy, type StudyFilter, type StudyPriority, } from '../../core/study_processor'; import * as url_utils from '../../core/url_utils'; -import { Study_Filter } from '../../proto/generated/study'; +import { + Study_Channel, + Study_Filter, + Study_Platform, +} from '../../proto/generated/study'; +import { + replaceChannels, + replacePlatforms, +} from '../../seed_tools/utils/study_json_utils'; import { ExperimentModel } from './experiment_model'; export class StudyModel { @@ -55,12 +62,20 @@ export class StudyModel { } platforms(): string[] | undefined { - return this.filter()?.platform?.map((p) => getPlatfromName(p)); + const string_platforms = this.filter()?.platform?.map( + (p) => Study_Platform[p], + ); + return replacePlatforms(string_platforms); } channels(): string[] | undefined { - const isBraveSeed = this.seedType !== SeedType.UPSTREAM; - return this.filter()?.channel?.map((c) => getChannelName(c, isBraveSeed)); + const string_channels = this.filter()?.channel?.map( + (c) => Study_Channel[c], + ); + return replaceChannels( + string_channels, + this.seedType === SeedType.UPSTREAM, + ); } getConfigUrl(): string {