Skip to content

Commit

Permalink
move channel/platform functions to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
atuchin-m committed Nov 20, 2024
1 parent ac7aeb4 commit 6236883
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
20 changes: 20 additions & 0 deletions src/seed_tools/utils/serializers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024 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/.

export function channelToString(channel: string, isChromium: boolean): string {
if (isChromium) return channel;

switch (channel) {
case 'CANARY':
return 'NIGHTLY';
case 'STABLE':
return 'RELEASE';
}
return channel;
}

export function platformToString(platform: string): string {
return platform.replace(/^PLATFORM_/, '');
}
34 changes: 7 additions & 27 deletions src/seed_tools/utils/study_json_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { promises as fs } from 'fs';
import JSON5 from 'json5';
import { Study } from '../../proto/generated/study';
import { channelToString, platformToString } from './serializers';

export interface Options {
isChromium?: boolean;
Expand Down Expand Up @@ -50,7 +51,7 @@ export function parseStudies(
): Study[] {
const jsonStudies = JSON5.parse(
studyArrayString,
jsonStudyReviever.bind(null, options),
jsonStudyReviewer.bind(null, options),
);
if (!Array.isArray(jsonStudies)) {
throw new Error('Root element must be an array');
Expand Down Expand Up @@ -86,29 +87,6 @@ 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,
Expand All @@ -120,17 +98,19 @@ function jsonStudyReplacer(
return new Date(value * 1000).toISOString();
}
case 'channel': {
return replaceChannels(value, options?.isChromium === true);
return value.map((c: string) =>
channelToString(c, options?.isChromium === true),
);
}
case 'platform': {
return replacePlatforms(value);
return value.map((p: string) => platformToString(p));
}
default:
return value;
}
}

function jsonStudyReviever(
function jsonStudyReviewer(
options: Options | undefined,
key: string,
value: any,
Expand Down
19 changes: 7 additions & 12 deletions src/web/app/study_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
Study_Platform,
} from '../../proto/generated/study';
import {
replaceChannels,
replacePlatforms,
} from '../../seed_tools/utils/study_json_utils';
channelToString,
platformToString,
} from '../../seed_tools/utils/serializers';
import { ExperimentModel } from './experiment_model';

export class StudyModel {
Expand Down Expand Up @@ -62,19 +62,14 @@ export class StudyModel {
}

platforms(): string[] | undefined {
const string_platforms = this.filter()?.platform?.map(
(p) => Study_Platform[p],
return this.filter()?.platform?.map((p) =>
platformToString(Study_Platform[p]),
);
return replacePlatforms(string_platforms);
}

channels(): string[] | undefined {
const string_channels = this.filter()?.channel?.map(
(c) => Study_Channel[c],
);
return replaceChannels(
string_channels,
this.seedType === SeedType.UPSTREAM,
return this.filter()?.channel?.map((c) =>
channelToString(Study_Channel[c], this.seedType === SeedType.UPSTREAM),
);
}

Expand Down

0 comments on commit 6236883

Please sign in to comment.