How to use the @lhci/utils/src/lodash.js.groupBy function in @lhci/utils

To help you get started, we’ve selected a few @lhci/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GoogleChrome / lighthouse-ci / packages / cli / src / assert / assert.js View on Github external
async function runCommand(options) {
  const {budgetsFile, assertions, assertMatrix, preset} = options;
  const areAssertionsSet = Boolean(assertions || assertMatrix || preset);
  if (!areAssertionsSet && !budgetsFile) throw new Error('No assertions to use');
  if (budgetsFile && areAssertionsSet) throw new Error('Cannot use both budgets AND assertions');
  // If we have a budgets file, convert it to our assertions format.
  if (budgetsFile) options = convertBudgetsToAssertions(readBudgets(budgetsFile));

  const lhrs = loadSavedLHRs().map(json => JSON.parse(json));
  const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl));
  const allResults = getAllAssertionResults(options, lhrs);
  const groupedResults = _.groupBy(allResults, result => result.url);

  process.stderr.write(
    `Checking assertions against ${uniqueUrls.size} URL(s), ${lhrs.length} total run(s)\n\n`
  );

  let hasFailure = false;
  for (const results of groupedResults) {
    const url = results[0].url;
    const sortedResults = results.sort((a, b) => {
      const {level: levelA = 'error', auditId: auditIdA = 'unknown'} = a;
      const {level: levelB = 'error', auditId: auditIdB = 'unknown'} = b;
      return levelA.localeCompare(levelB) || auditIdA.localeCompare(auditIdB);
    });

    process.stderr.write(`${sortedResults.length} result(s) for ${log.bold}${url}${log.reset}\n`);
github GoogleChrome / lighthouse-ci / packages / cli / src / upload / upload.js View on Github external
await postStatusToGitHub({
        slug,
        hash,
        state,
        context,
        description,
        targetUrl,
        githubToken,
        githubAppToken,
      });
    }
  } else {
    /** @type {Array} */
    const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
    /** @type {Array>} */
    const lhrsByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs => lhrs.map(lhr => [lhr, lhr]));
    const representativeLhrs = computeRepresentativeRuns(lhrsByUrl);

    if (!representativeLhrs.length) return print('No LHRs for status check, skipping.\n');

    for (const lhr of representativeLhrs) {
      const rawUrl = lhr.finalUrl;
      const urlLabel = getUrlLabelForGithub(rawUrl, options);
      const state = 'success';
      const context = `lhci/url${urlLabel}`;
      const categoriesDescription = Object.values(lhr.categories)
        .map(category => `${category.title}: ${Math.round(category.score * 100)}`)
        .join(', ');
      const description = `${categoriesDescription}`;
      const targetUrl = targetUrlMap.get(rawUrl) || rawUrl;

      await postStatusToGitHub({
github GoogleChrome / lighthouse-ci / packages / cli / src / upload / upload.js View on Github external
async function runGithubStatusCheck(options, targetUrlMap) {
  const hash = getCurrentHash();
  const slug = getGitHubRepoSlug();
  const {githubToken, githubAppToken} = options;

  if (!githubToken && !githubAppToken) return print('No GitHub token set, skipping.\n');
  print('GitHub token found, attempting to set status...\n');
  if (!slug) return print(`No GitHub remote found, skipping.\n`);
  if (!slug.includes('/')) return print(`Invalid repo slug "${slug}", skipping.\n`);
  if (!hash) return print(`Invalid hash "${hash}"\n, skipping.`);

  const assertionResults = loadAssertionResults();
  const groupedResults = _.groupBy(assertionResults, result => result.url).sort(
    (a, b) => a[0].url.length - b[0].url.length
  );

  if (groupedResults.length) {
    for (const group of groupedResults) {
      const rawUrl = group[0].url;
      const urlLabel = getUrlLabelForGithub(rawUrl, options);
      const failedResults = group.filter(result => result.level === 'error');
      const warnResults = group.filter(result => result.level === 'warn');
      const state = failedResults.length ? 'failure' : 'success';
      const context = `lhci/url${urlLabel}`;
      const warningsLabel = warnResults.length ? ` with ${warnResults.length} warning(s)` : '';
      const description = failedResults.length
        ? `Failed ${failedResults.length} assertion(s)`
        : `Passed${warningsLabel}`;
      const targetUrl = targetUrlMap.get(rawUrl) || rawUrl;
github GoogleChrome / lighthouse-ci / packages / cli / src / open / open.js View on Github external
async function runCommand(options) {
  /** @type {Array} */
  const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
  /** @type {Array>} */
  const groupedByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs =>
    lhrs.map(lhr => [lhr, lhr])
  );
  const representativeLhrs = computeRepresentativeRuns(groupedByUrl);

  if (!representativeLhrs.length) {
    process.stdout.write('No available reports to open. ');
  }

  for (const lhr of representativeLhrs) {
    if (options.url && lhr.finalUrl !== options.url) continue;

    process.stdout.write(`Opening median report for ${lhr.finalUrl}...\n`);
    const tmpFile = tmp.fileSync({postfix: '.html'});
    fs.writeFileSync(tmpFile.name, getHTMLReportForLHR(lhr));
    await open(tmpFile.name);
  }
github GoogleChrome / lighthouse-ci / packages / cli / src / upload / upload.js View on Github external
async function runTemporaryPublicStorageTarget(options) {
  /** @type {Array} */
  const lhrs = loadSavedLHRs().map(lhr => JSON.parse(lhr));
  /** @type {Array>} */
  const lhrsByUrl = _.groupBy(lhrs, lhr => lhr.finalUrl).map(lhrs => lhrs.map(lhr => [lhr, lhr]));
  const representativeLhrs = computeRepresentativeRuns(lhrsByUrl);
  const targetUrlMap = new Map();

  for (const lhr of representativeLhrs) {
    const urlAudited = lhr.finalUrl;
    print(`Uploading median LHR of ${urlAudited}...`);

    try {
      const response = await fetch(TEMPORARY_PUBLIC_STORAGE_URL, {
        method: 'POST',
        headers: {'content-type': 'text/html'},
        body: getHTMLReportForLHR(lhr),
      });

      const {success, url} = await response.json();
      if (success && url) {