All files / src/assess-distribution index.ts

0% Statements 0/77
0% Branches 0/41
0% Functions 0/10
0% Lines 0/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { randomBytes } from "crypto";
 
import type { FeatureKey, AttributeKey, Context, DatafileContent } from "@featurevisor/types";
import { createFeaturevisor } from "@featurevisor/sdk";
 
import { Dependencies } from "../dependencies";
import { buildRuntimeDatafiles } from "../builder/buildRuntimeDatafiles";
import { prettyPercentage, prettyNumber } from "../utils";
import { Plugin } from "../cli";
import { getProjectSetExecutions, printSetHeader } from "../sets";
import { CLI_COLOR_CYAN, CLI_FORMAT_BOLD, CLI_FORMAT_GREEN, colorize } from "../tester/cliFormat";
 
const UUID_LENGTHS = [4, 2, 2, 2, 6];
 
function generateUUID(): string {
  return UUID_LENGTHS.map((len) => randomBytes(len).toString("hex")).join("-");
}
 
type EvaluationsCount = Record<string, number>;
 
function printCounts(evaluations: EvaluationsCount, n: number, sort = true) {
  let entries = Object.entries(evaluations);
 
  if (sort) {
    entries = entries.sort((a, b) => {
      if (a[1] > b[1]) {
        return -1;
      }
 
      if (a[1] < b[1]) {
        return 1;
      }
 
      return 0;
    });
  }
 
  const longestValueLength = Object.keys(evaluations).reduce(
    (acc, curr) => (curr.length > acc ? curr.length : acc),
    0,
  );
 
  const highestCount = Object.values(evaluations).reduce(
    (acc, curr) => (curr > acc ? curr : acc),
    0,
  );
  const prettyHighestCountLength = prettyNumber(highestCount).length;
 
  for (const [value, count] of entries) {
    console.log(
      `  ${colorize("•", CLI_COLOR_CYAN)} ${value}:`.padEnd(longestValueLength + 14, " "),
      `\t${prettyNumber(count).padStart(prettyHighestCountLength, " ")}`,
      `\t${prettyPercentage(count, n).padStart(7, " ")}`,
    );
  }
}
 
function createContext(providedContext: Context, populateUuid?: AttributeKey[]): Context {
  const context = { ...providedContext };
 
  if (populateUuid) {
    for (const key of populateUuid) {
      context[key] = generateUUID();
    }
  }
 
  return context;
}
 
export interface AssessDistributionOptions {
  environment?: string;
  feature: FeatureKey;
  context: Context;
  n: number;
  inflate?: number;
 
  populateUuid?: AttributeKey[];
  verbose?: boolean;
  target?: string | string[];
}
 
async function assessDistributionWithDatafile(
  datafileContent: DatafileContent,
  options: AssessDistributionOptions,
  target?: string,
) {
  console.log("");
  console.log(CLI_FORMAT_BOLD, "Assess Featurevisor distribution");
  console.log(`  ${colorize("Feature", CLI_COLOR_CYAN)}: ${options.feature}`);
  console.log(`  ${colorize("Environment", CLI_COLOR_CYAN)}: ${options.environment || false}`);
  if (target) console.log(`  ${colorize("Target", CLI_COLOR_CYAN)}: ${target}`);
  console.log(`  ${colorize("Iterations", CLI_COLOR_CYAN)}: ${prettyNumber(options.n)}`);
 
  /**
   * Initialize SDK
   */
  const f = createFeaturevisor({
    datafile: datafileContent as DatafileContent,
    logLevel: "warn",
  });
  console.log("");
  console.log(CLI_FORMAT_GREEN, "SDK initialized");
 
  /**
   * Evaluations
   */
  let hasVariations = false;
  const feature = f.getFeature(options.feature);
 
  if (feature && feature.variations) {
    hasVariations = true;
  }
 
  const flagEvaluations: EvaluationsCount = {
    enabled: 0,
    disabled: 0,
  };
 
  const variationEvaluations: EvaluationsCount = {};
 
  console.log("");
  console.log(`Evaluating ${prettyNumber(options.n)} times...`);
 
  for (let i = 0; i < options.n; i++) {
    const context = createContext(options.context, options.populateUuid);
    if (options.verbose) {
      console.log(`[${i + 1}/${options.n}] Evaluating against context: ${JSON.stringify(context)}`);
    }
 
    // flag
    const flagEvaluation = f.isEnabled(options.feature, context);
 
    if (flagEvaluation) {
      flagEvaluations.enabled++;
    } else {
      flagEvaluations.disabled++;
    }
 
    // variation
    if (hasVariations) {
      const variationEvaluation = f.getVariation(options.feature, context) as string;
 
      if (!variationEvaluations[variationEvaluation]) {
        variationEvaluations[variationEvaluation] = 0;
      }
 
      variationEvaluations[variationEvaluation]++;
    }
  }
 
  /**
   * Print results
   */
 
  console.log("");
  console.log(CLI_FORMAT_BOLD, "Flag evaluations");
  console.log("");
  printCounts(flagEvaluations, options.n);
 
  if (hasVariations) {
    console.log("");
    console.log(CLI_FORMAT_BOLD, "Variation evaluations");
    console.log("");
    printCounts(variationEvaluations, options.n);
  }
}
 
export async function assessDistribution(deps: Dependencies, options: AssessDistributionOptions) {
  const datafileBuildStart = Date.now();
  const datafiles = await buildRuntimeDatafiles(deps, {
    environment: options.environment || false,
    target: options.target,
    revision: "include-all-features",
    inflate: options.inflate,
  });
  const datafileBuildDuration = Date.now() - datafileBuildStart;
 
  console.log("");
  console.log(`Building ${datafiles.length} datafile${datafiles.length === 1 ? "" : "s"}...`);
  console.log(`  ${colorize("Build duration", CLI_COLOR_CYAN)}: ${datafileBuildDuration}ms`);
 
  for (const entry of datafiles) {
    await assessDistributionWithDatafile(entry.datafile, options, entry.target);
  }
}
 
export const assessDistributionPlugin: Plugin = {
  command: "assess-distribution",
  handler: async ({ rootDirectoryPath, projectConfig, datasource, parsed }) => {
    const executions = await getProjectSetExecutions(projectConfig, datasource, parsed.set);
 
    for (const execution of executions) {
      printSetHeader(projectConfig, execution.set);
 
      await assessDistribution(
        {
          rootDirectoryPath,
          projectConfig: execution.projectConfig,
          datasource: execution.datasource,
          options: parsed,
        },
        {
          environment: parsed.environment,
          feature: parsed.feature,
          n: parseInt(parsed.n, 10) || 1,
          context: parsed.context ? JSON.parse(parsed.context) : {},
          populateUuid: Array.isArray(parsed.populateUuid)
            ? parsed.populateUuid
            : [parsed.populateUuid as string].filter(Boolean),
          verbose: parsed.verbose,
          target: parsed.target,
        },
      );
    }
  },
  examples: [
    {
      command:
        "assess-distribution --environment=production --feature=my_feature --context='{}' --populateUuid=userId -n=100",
      description: "test traffic distribution a feature against provided context",
    },
  ],
};