Fix tabbing
This commit is contained in:
@@ -1,87 +1,87 @@
|
||||
import {
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_DATA,
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_DATA,
|
||||
} from "@blade-and-brawn/calculator";
|
||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/calculator";
|
||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||
|
||||
for (const activity of Object.values(Activity)) {
|
||||
const DATASET_MAX_LEVEL = 5;
|
||||
const NEW_LEVEL_COUNT = 2;
|
||||
const RATIO_CLAMP = 1.4;
|
||||
const DATASET_MAX_LEVEL = 5;
|
||||
const NEW_LEVEL_COUNT = 2;
|
||||
const RATIO_CLAMP = 1.4;
|
||||
|
||||
function expDecayModel([A, B, C]: number[]) {
|
||||
return (i: number) => A! * Math.exp(-B! * i) + C!;
|
||||
}
|
||||
|
||||
for (const gender of Object.values(Gender)) {
|
||||
const standardsByGender = DEFAULT_STANDARDS_DATA[activity].standards.filter(
|
||||
(s) => s["metrics"].gender === gender,
|
||||
);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
for (const age of ages) {
|
||||
const standards = standardsByGender.filter(
|
||||
(s) => s.metrics.age === age,
|
||||
);
|
||||
|
||||
const data = {
|
||||
x: [] as number[],
|
||||
y: [] as number[],
|
||||
};
|
||||
|
||||
const isIncreasing =
|
||||
standards[0]!.levels["2"]! > standards[0]!.levels["1"]!;
|
||||
|
||||
for (const i of range(DATASET_MAX_LEVEL).slice(0, -1)) {
|
||||
const level = i + 1;
|
||||
const progressionRatios: number[] = [];
|
||||
for (const standard of standards) {
|
||||
const curr = standard.levels[level]!;
|
||||
const next = standard.levels[level]!;
|
||||
progressionRatios.push(
|
||||
isIncreasing ? next / curr : curr / next,
|
||||
);
|
||||
}
|
||||
|
||||
const sum = progressionRatios.reduce((p, c) => p + c, 0);
|
||||
const progressionRatioAvg = sum / progressionRatios.length;
|
||||
|
||||
data.x.push(i);
|
||||
data.y.push(progressionRatioAvg);
|
||||
}
|
||||
|
||||
const fittedParams = LM(data, expDecayModel, {
|
||||
initialValues: [0.4, 0.5, 1.1],
|
||||
minValues: [0.0, 0.0, 1.02],
|
||||
maxValues: [1.0, 2.0, 1.25],
|
||||
maxIterations: 200,
|
||||
});
|
||||
const getDecayRatio = (i: number) =>
|
||||
clamp(
|
||||
expDecayModel(fittedParams.parameterValues)(i),
|
||||
1,
|
||||
RATIO_CLAMP,
|
||||
);
|
||||
|
||||
// UPDATE THE DATA
|
||||
for (const standard of standards) {
|
||||
const newLevels: Levels = {};
|
||||
|
||||
let prev = standard.levels["1"]!;
|
||||
for (const i of range(NEW_LEVEL_COUNT)) {
|
||||
const level = i + 1;
|
||||
const ratio = getDecayRatio(level - NEW_LEVEL_COUNT);
|
||||
prev = isIncreasing ? prev / ratio : prev * ratio;
|
||||
newLevels[level] = Math.round(prev);
|
||||
}
|
||||
|
||||
const oldLevels = standard.levels as Levels;
|
||||
for (const i of range(DATASET_MAX_LEVEL)) {
|
||||
const level = i + 1;
|
||||
newLevels[level + NEW_LEVEL_COUNT] = oldLevels[level]!;
|
||||
}
|
||||
standard.levels = newLevels;
|
||||
}
|
||||
function expDecayModel([A, B, C]: number[]) {
|
||||
return (i: number) => A! * Math.exp(-B! * i) + C!;
|
||||
}
|
||||
|
||||
for (const gender of Object.values(Gender)) {
|
||||
const standardsByGender = DEFAULT_STANDARDS_DATA[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||
for (const age of ages) {
|
||||
const standards = standardsByGender.filter(
|
||||
(s) => s.metrics.age === age,
|
||||
);
|
||||
|
||||
const data = {
|
||||
x: [] as number[],
|
||||
y: [] as number[],
|
||||
};
|
||||
|
||||
const isIncreasing =
|
||||
standards[0]!.levels["2"]! > standards[0]!.levels["1"]!;
|
||||
|
||||
for (const i of range(DATASET_MAX_LEVEL).slice(0, -1)) {
|
||||
const level = i + 1;
|
||||
const progressionRatios: number[] = [];
|
||||
for (const standard of standards) {
|
||||
const curr = standard.levels[level]!;
|
||||
const next = standard.levels[level]!;
|
||||
progressionRatios.push(
|
||||
isIncreasing ? next / curr : curr / next,
|
||||
);
|
||||
}
|
||||
|
||||
const sum = progressionRatios.reduce((p, c) => p + c, 0);
|
||||
const progressionRatioAvg = sum / progressionRatios.length;
|
||||
|
||||
data.x.push(i);
|
||||
data.y.push(progressionRatioAvg);
|
||||
}
|
||||
|
||||
const fittedParams = LM(data, expDecayModel, {
|
||||
initialValues: [0.4, 0.5, 1.1],
|
||||
minValues: [0.0, 0.0, 1.02],
|
||||
maxValues: [1.0, 2.0, 1.25],
|
||||
maxIterations: 200,
|
||||
});
|
||||
const getDecayRatio = (i: number) =>
|
||||
clamp(
|
||||
expDecayModel(fittedParams.parameterValues)(i),
|
||||
1,
|
||||
RATIO_CLAMP,
|
||||
);
|
||||
|
||||
// UPDATE THE DATA
|
||||
for (const standard of standards) {
|
||||
const newLevels: Levels = {};
|
||||
|
||||
let prev = standard.levels["1"]!;
|
||||
for (const i of range(NEW_LEVEL_COUNT)) {
|
||||
const level = i + 1;
|
||||
const ratio = getDecayRatio(level - NEW_LEVEL_COUNT);
|
||||
prev = isIncreasing ? prev / ratio : prev * ratio;
|
||||
newLevels[level] = Math.round(prev);
|
||||
}
|
||||
|
||||
const oldLevels = standard.levels as Levels;
|
||||
for (const i of range(DATASET_MAX_LEVEL)) {
|
||||
const level = i + 1;
|
||||
newLevels[level + NEW_LEVEL_COUNT] = oldLevels[level]!;
|
||||
}
|
||||
standard.levels = newLevels;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,79 @@
|
||||
import { PrintfulService, SyncService, WebflowService } from "@blade-and-brawn/commerce";
|
||||
import {
|
||||
PrintfulService,
|
||||
SyncService,
|
||||
WebflowService,
|
||||
} from "@blade-and-brawn/commerce";
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
type Validation = {
|
||||
isValid: boolean;
|
||||
message: string;
|
||||
isValid: boolean;
|
||||
message: string;
|
||||
};
|
||||
|
||||
const validateEquality = (wValue: any, pValue: any): Validation => {
|
||||
const validation = {
|
||||
isValid: true,
|
||||
message: "Valid",
|
||||
};
|
||||
const validation = {
|
||||
isValid: true,
|
||||
message: "Valid",
|
||||
};
|
||||
|
||||
if (wValue !== pValue) {
|
||||
validation.isValid = false;
|
||||
validation.message = `${wValue} !== ${pValue}`;
|
||||
}
|
||||
if (wValue !== pValue) {
|
||||
validation.isValid = false;
|
||||
validation.message = `${wValue} !== ${pValue}`;
|
||||
}
|
||||
|
||||
return validation;
|
||||
return validation;
|
||||
};
|
||||
|
||||
const wProducts = await WebflowService.Products.getAll();
|
||||
|
||||
let invalidCount = 0;
|
||||
for (const wProduct of wProducts) {
|
||||
for (const sku of wProduct.skus ?? []) {
|
||||
console.log("Webflow: " + sku.id, sku.fieldData.name);
|
||||
for (const sku of wProduct.skus ?? []) {
|
||||
console.log("Webflow: " + sku.id, sku.fieldData.name);
|
||||
|
||||
const pVariant = await PrintfulService.Products.Variants.get(
|
||||
`@${sku.id}`,
|
||||
);
|
||||
if (pVariant) {
|
||||
console.log("Printful: " + pVariant.id, pVariant.name);
|
||||
const validations = [
|
||||
// validateEquality(sku.fieldData.name, pVariant.name),
|
||||
validateEquality(
|
||||
sku.fieldData["sku-values"]?.["color"],
|
||||
SyncService.findColorInProductName(pVariant.name),
|
||||
),
|
||||
validateEquality(
|
||||
sku.fieldData["sku-values"]?.["size"],
|
||||
pVariant.size,
|
||||
),
|
||||
validateEquality(
|
||||
sku.fieldData.price.value,
|
||||
Math.floor(+pVariant.retail_price * 100),
|
||||
),
|
||||
];
|
||||
const pVariant = await PrintfulService.Products.Variants.get(
|
||||
`@${sku.id}`,
|
||||
);
|
||||
if (pVariant) {
|
||||
console.log("Printful: " + pVariant.id, pVariant.name);
|
||||
const validations = [
|
||||
// validateEquality(sku.fieldData.name, pVariant.name),
|
||||
validateEquality(
|
||||
sku.fieldData["sku-values"]?.["color"],
|
||||
SyncService.findColorInProductName(pVariant.name),
|
||||
),
|
||||
validateEquality(
|
||||
sku.fieldData["sku-values"]?.["size"],
|
||||
pVariant.size,
|
||||
),
|
||||
validateEquality(
|
||||
sku.fieldData.price.value,
|
||||
Math.floor(+pVariant.retail_price * 100),
|
||||
),
|
||||
];
|
||||
|
||||
// if (!validateEquality(sku.fieldData.name, pVariant.name).isValid) {
|
||||
// sku.fieldData.name = pVariant.name;
|
||||
// await WebflowService.Products.Skus.update(wProduct.product.id, sku.id, sku);
|
||||
// }
|
||||
// if (!validateEquality(sku.fieldData.name, pVariant.name).isValid) {
|
||||
// sku.fieldData.name = pVariant.name;
|
||||
// await WebflowService.Products.Skus.update(wProduct.product.id, sku.id, sku);
|
||||
// }
|
||||
|
||||
const errors = validations.filter((v) => !v.isValid);
|
||||
if (errors.length) {
|
||||
++invalidCount;
|
||||
console.log("INVALID: ");
|
||||
console.log(errors.map((e) => e.message).join(", "));
|
||||
} else {
|
||||
console.log("VALID");
|
||||
}
|
||||
} else {
|
||||
++invalidCount;
|
||||
console.log("INVALID");
|
||||
console.log("Not found");
|
||||
const errors = validations.filter((v) => !v.isValid);
|
||||
if (errors.length) {
|
||||
++invalidCount;
|
||||
console.log("INVALID: ");
|
||||
console.log(errors.map((e) => e.message).join(", "));
|
||||
} else {
|
||||
console.log("VALID");
|
||||
}
|
||||
} else {
|
||||
++invalidCount;
|
||||
console.log("INVALID");
|
||||
console.log("Not found");
|
||||
}
|
||||
console.log();
|
||||
}
|
||||
console.log();
|
||||
}
|
||||
await sleep(3000);
|
||||
await sleep(3000);
|
||||
}
|
||||
|
||||
console.log("INVALID COUNT: " + invalidCount);
|
||||
|
||||
Reference in New Issue
Block a user