Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 98 additions & 42 deletions scripts/stats.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,126 @@
import { Compat } from "compute-baseline/browser-compat-data";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import yargs from "yargs";
import { features } from "../index.js";
import { features, groups } from "../index.js";
import { isOrdinaryFeatureData } from "../type-guards.js";
import { caniuseToWebFeaturesId } from "./caniuse.js";
import { compatFeaturesToCumulativeDaysShipped } from "./unmapped-compat-keys.js";

yargs(process.argv.slice(2))
interface Result {
featureCount: number;
groupCount: number;
compatKeys: number;
unmappedCompatKeys: number;
unmappedCompatKeysNormal: number;
unmappedCompatKeysDiscourageable: number;
unmappedCompatKeysCumulativeShippingDays: number;
unmappedCompatKeysCumulativeShippingDaysNormal: number;
unmappedCompatKeysCumulativeShippingDaysDiscourageable: number;
caniuseIds: number;
unmappedCaniuseIds: number;
change?: Change;
}

type ResultKey = keyof Result;
type ChangeKey = `${ResultKey}Change`;
type Change = Record<ChangeKey, number>;

const argv = yargs(process.argv.slice(2))
.scriptName("stats")
.usage("$0", "Generate statistics").argv;
.option("previous", {
alias: "p",
type: "string",
description: "Path to a JSON file",
coerce: (filePath) => {
const raw = readFileSync(filePath, "utf-8");
return JSON.parse(raw);
},
})
.usage("$0", "Generate statistics")
.parseSync();

export function stats() {
export function stats(previous: Partial<Result>): Result {
const featureCount = Object.values(features).filter(
isOrdinaryFeatureData,
).length;
const groupCount = Object.values(groups).length;

const keys = [];
const doneKeys = Array.from(
new Set(
Object.values(features).flatMap((f) => {
if (isOrdinaryFeatureData(f)) {
return f.compat_features ?? [];
}
return [];
}),
),
const mappedCompatKeys = new Set(
Object.values(features).flatMap((f) => {
if (isOrdinaryFeatureData(f)) {
return f.compat_features ?? [];
}
return [];
}),
);

let compatKeys = 0;
let unmappedCompatKeys = 0;
let unmappedCompatKeysNormal = 0;
let unmappedCompatKeysDiscourageable = 0;
for (const f of new Compat().walk()) {
if (!f.id.startsWith("webextensions")) {
keys.push(f.id);
if (!f.id.startsWith("webextensions.")) {
compatKeys += 1;
unmappedCompatKeys += mappedCompatKeys.has(f.id) ? 0 : 1;
if (f.deprecated || !f.standard_track) {
unmappedCompatKeysNormal += 1;
} else {
unmappedCompatKeysDiscourageable += 1;
}
}
}

const featureSizes = Object.values(features)
.filter(isOrdinaryFeatureData)
.map((feature) => (feature.compat_features ?? []).length)
.sort((a, b) => a - b);
const featuresToDays = compatFeaturesToCumulativeDaysShipped();
const unmappedCompatKeysCumulativeShippingDaysDiscourageable = [
...featuresToDays.values(),
].reduce((prev, curr) => prev + curr, 0);
Comment thread
ddbeck marked this conversation as resolved.
Outdated
const unmappedCompatKeysCumulativeShippingDaysNormal = (() => {
let sum = 0;
for (const [feature, days] of featuresToDays.entries()) {
if (!feature.deprecated && feature.standard_track) {
sum += days;
}
}
return sum;
})();
const unmappedCompatKeysCumulativeShippingDays =
unmappedCompatKeysCumulativeShippingDaysDiscourageable +
unmappedCompatKeysCumulativeShippingDaysNormal;

const caniuseIds = [...caniuseToWebFeaturesId.keys()].length;
const unmappedCaniuseIds = [...caniuseToWebFeaturesId.values()].filter(
(v) => v === null,
).length;

const result = {
features: featureCount,
compatKeys: doneKeys.length,
compatKeysUnmapped: keys.length - doneKeys.length,
compatCoverage: doneKeys.length / keys.length,
compatKeysPerFeatureMean: doneKeys.length / featureCount,
compatKeysPerFeatureMedian: (() => {
const sizes = featureSizes;
const middle = Math.floor(sizes.length / 2);
return sizes.length % 2
? sizes[middle]
: (sizes[middle - 1] + sizes[middle]) / 2;
})(),
compatKeysPerFeatureMode: (() => {
const frequencyMap = new Map<number, number>();
for (const size of featureSizes) {
frequencyMap.set(size, (frequencyMap.get(size) ?? 0) + 1);
}
return [...frequencyMap.entries()]
.sort(([, frequencyA], [, frequencyB]) => frequencyA - frequencyB)
.pop()[0];
})(),
featureCount,
groupCount,
compatKeys,
unmappedCompatKeys,
unmappedCompatKeysNormal,
unmappedCompatKeysDiscourageable,
unmappedCompatKeysCumulativeShippingDays,
unmappedCompatKeysCumulativeShippingDaysNormal,
unmappedCompatKeysCumulativeShippingDaysDiscourageable,
caniuseIds,
unmappedCaniuseIds,
};

if (previous) {
const change = Object.fromEntries(
Object.keys(previous).map((key) => [
`${key}Change`,
result[key] - previous[key],
]),
) as Change;
return { ...result, change };
}
return result;
}

if (import.meta.url.startsWith("file:")) {
if (process.argv[1] === fileURLToPath(import.meta.url)) {
console.log(JSON.stringify(stats(), undefined, 2));
console.log(JSON.stringify(stats(argv.previous), undefined, 2));
}
}
112 changes: 64 additions & 48 deletions scripts/unmapped-compat-keys.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
import { Temporal } from "@js-temporal/polyfill";
import { coreBrowserSet } from "compute-baseline";
import { Compat, Feature } from "compute-baseline/browser-compat-data";
import { fileURLToPath } from "node:url";
import winston from "winston";
import yargs from "yargs";
import { features } from "../index.js";
import { support } from "../packages/compute-baseline/dist/baseline/support.js";
import { isOrdinaryFeatureData } from "../type-guards.js";

const compat = new Compat();
const browsers = coreBrowserSet.map((b) => compat.browser(b));
const defaultLogLevel = "warn";
const today = Temporal.Now.plainDateISO();

const argv = yargs(process.argv.slice(2))
.scriptName("unmapped-compat-keys")
.usage(
"$0",
"Print keys from mdn/browser-compat-data not assigned to a feature",
)
.option("format", {
choices: ["json", "yaml"],
default: "yaml",
describe:
"Choose the output format. JSON has more detail, while YAML is suited to pasting into feature files.",
})
.option("verbose", {
alias: "v",
describe: "Show more information",
type: "count",
default: 0,
defaultDescription: "warn",
})
.parseSync();
const compat = new Compat();

const logger = winston.createLogger({
level: argv.verbose > 0 ? "debug" : "warn",
level: defaultLogLevel,
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple(),
Expand All @@ -52,7 +33,10 @@ const mappedCompatKeys = (() => {
);
})();

const compatFeatures: Map<Feature, number> = (() => {
/**
* Get a map of each compat key to the sum of days that key has been shipping.
*/
export function compatFeaturesToCumulativeDaysShipped(): Map<Feature, number> {
const map = new Map();
for (const f of compat.walk()) {
if (f.id.startsWith("webextensions")) {
Expand All @@ -66,30 +50,6 @@ const compatFeatures: Map<Feature, number> = (() => {
map.set(f, cumulativeDaysShipped(f));
}
return map;
})();

const byAge = [...compatFeatures.entries()].sort(
([, aDays], [, bDays]) => aDays - bDays,
);

if (argv.format === "yaml") {
for (const [f] of byAge) {
console.log(` - ${f.id}`);
}
}

if (argv.format === "json") {
console.log(
JSON.stringify(
byAge.map(([f, days]) => ({
key: f.id,
cumulativeDaysShipped: days,
deprecated: f.deprecated,
})),
undefined,
2,
),
);
}

/**
Expand All @@ -103,6 +63,7 @@ if (argv.format === "json") {
* @return {number} an integer
*/
function cumulativeDaysShipped(feature: Feature) {
const browsers = coreBrowserSet.map((b) => compat.browser(b));
const results = support(feature, browsers);
return [...results.values()]
.filter((r) => r !== undefined)
Expand All @@ -115,3 +76,58 @@ function cumulativeDaysShipped(feature: Feature) {
)
.reduce((prev, curr) => prev + curr, 0);
}

function main() {
const argv = yargs(process.argv.slice(2))
.scriptName("unmapped-compat-keys")
.usage(
"$0",
"Print keys from mdn/browser-compat-data not assigned to a feature",
)
.option("format", {
choices: ["json", "yaml"],
default: "yaml",
describe:
"Choose the output format. JSON has more detail, while YAML is suited to pasting into feature files.",
})
.option("verbose", {
alias: "v",
describe: "Show more information",
type: "count",
default: 0,
defaultDescription: "warn",
})
.parseSync();

logger.transports[0].level = argv.verbose > 0 ? "debug" : "warn";

const byAge = [...compatFeaturesToCumulativeDaysShipped().entries()].sort(
([, aDays], [, bDays]) => aDays - bDays,
);

if (argv.format === "yaml") {
for (const [f] of byAge) {
console.log(` - ${f.id}`);
}
}

if (argv.format === "json") {
console.log(
JSON.stringify(
byAge.map(([f, days]) => ({
key: f.id,
cumulativeDaysShipped: days,
deprecated: f.deprecated,
})),
undefined,
2,
),
);
}
}

if (import.meta.url.startsWith("file:")) {
if (process.argv[1] === fileURLToPath(import.meta.url)) {
main();
}
}