Skip to content

Commit

Permalink
Move functions to a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
atuchin-m committed Nov 7, 2024
1 parent 8a072a5 commit d90c11b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 68 deletions.
66 changes: 1 addition & 65 deletions src/seed_tools/commands/split_seed_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

import { Command } from '@commander-js/extra-typings';
import { promises as fs } from 'fs';
import DefaultMap from '../../base/containers/default_map';
import { type Study } from '../../proto/generated/study';
import { VariationsSeed } from '../../proto/generated/variations_seed';
import * as study_json_utils from '../utils/study_json_utils';
import { parseLegacySeedJson } from '../utils/legacy_json_to_seed';

export default function createCommand() {
return new Command('split_seed_json')
Expand All @@ -18,26 +15,6 @@ export default function createCommand() {
.action(main);
}

export function parseLegacySeedJson(seedContent: string): {
parsedSeed: VariationsSeed;
studiesMap: DefaultMap<string, Study[]>;
} {
const seedJson = preprocessSeedJson(JSON.parse(seedContent));

// Parse the seed as protobuf json representation. The parse will fail if any
// unknown fields or values are present in the json.
const parsedSeed = VariationsSeed.fromJson(seedJson, {
ignoreUnknownFields: false,
});

const studiesMap = new DefaultMap<string, Study[]>(() => []);
for (const study of parsedSeed.study) {
studiesMap.get(study.name).push(study);
}

return { parsedSeed, studiesMap };
}

async function main(seedPath: string, outputDir: string) {
const seedContent = await fs.readFile(seedPath, 'utf8');
const { studiesMap } = parseLegacySeedJson(seedContent);
Expand All @@ -59,44 +36,3 @@ async function main(seedPath: string, outputDir: string) {
await study_json_utils.writeStudyFile(studyArray, studyFile);
}
}

function preprocessSeedJson(json: any): any {
json.study = json.studies;
delete json.studies;

for (const study of json.study) {
if (study.experiments !== undefined) {
study.experiment = study.experiments;
delete study.experiments;
}
for (const experiment of study.experiment) {
if (experiment.parameters !== undefined) {
experiment.param = experiment.parameters;
delete experiment.parameters;
}
}
if (study.filter !== undefined) {
if (study.filter.channel !== undefined) {
study.filter.channel = study.filter.channel.map((channel: string) => {
switch (channel) {
case 'NIGHTLY':
return 'CANARY';
case 'RELEASE':
return 'STABLE';
default:
return channel;
}
});
}
if (study.filter.platform !== undefined) {
study.filter.platform = study.filter.platform.map(
(platform: string) => {
return 'PLATFORM_' + platform;
},
);
}
}
}

return json;
}
69 changes: 69 additions & 0 deletions src/seed_tools/utils/legacy_json_to_seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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/.

import DefaultMap from '../../base/containers/default_map';
import { type Study } from '../../proto/generated/study';
import { VariationsSeed } from '../../proto/generated/variations_seed';

export function parseLegacySeedJson(seedContent: string): {
parsedSeed: VariationsSeed;
studiesMap: DefaultMap<string, Study[]>;
} {
const seedJson = preprocessSeedJson(JSON.parse(seedContent));

// Parse the seed as protobuf json representation. The parse will fail if any
// unknown fields or values are present in the json.
const parsedSeed = VariationsSeed.fromJson(seedJson, {
ignoreUnknownFields: false,
});

const studiesMap = new DefaultMap<string, Study[]>(() => []);
for (const study of parsedSeed.study) {
studiesMap.get(study.name).push(study);
}

return { parsedSeed, studiesMap };
}

function preprocessSeedJson(json: any): any {
json.study = json.studies;
delete json.studies;

for (const study of json.study) {
if (study.experiments !== undefined) {
study.experiment = study.experiments;
delete study.experiments;
}
for (const experiment of study.experiment) {
if (experiment.parameters !== undefined) {
experiment.param = experiment.parameters;
delete experiment.parameters;
}
}
if (study.filter !== undefined) {
if (study.filter.channel !== undefined) {
study.filter.channel = study.filter.channel.map((channel: string) => {
switch (channel) {
case 'NIGHTLY':
return 'CANARY';
case 'RELEASE':
return 'STABLE';
default:
return channel;
}
});
}
if (study.filter.platform !== undefined) {
study.filter.platform = study.filter.platform.map(
(platform: string) => {
return 'PLATFORM_' + platform;
},
);
}
}
}

return json;
}
6 changes: 3 additions & 3 deletions src/seed_tools/utils/studies_to_seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
type Study,
} from '../../proto/generated/study';
import { VariationsSeed } from '../../proto/generated/variations_seed';
import { parseLegacySeedJson } from '../commands/split_seed_json';
import { parseLegacySeedJson } from './legacy_json_to_seed';
import diffStrings from '../utils/diff_strings';
import * as file_utils from '../utils/file_utils';
import * as seed_validation from '../utils/seed_validation';
Expand Down Expand Up @@ -86,8 +86,8 @@ async function readStudiesAtRevision(
filesWithContent.push({ path: file, content });
}
return await readStudies(filesWithContent, false);
} catch (e) {
console.log(`Failed to read studies ${revision}, use seed.json fallback`);
} catch (error) {
console.log(`Failed to read studies ${revision}, use seed.json fallback:`, error);
const seedContent = execSync(`git show "${revision}":seed/seed.json`, {
encoding: 'utf8',
});
Expand Down

0 comments on commit d90c11b

Please sign in to comment.