<commit_before>'use strict';

const deprecatedVerify = require('./deprecatedVerify').verify;
const verify = require('./verify').verify;
const tap = require('./tap');
const junit = require('./junit');
const json = require('./json');
const log = require('intel').getLogger('sitespeedio.plugin.budget');

module.exports = {
  open(context, options) {
    this.options = options;
    this.storageManager = context.storageManager;
    this.result = context.budget;
    this.budgetTypes = [
      'browsertime.pageSummary',
      'webpagetest.pageSummary',
      'pagexray.pageSummary',
      'coach.pageSummary',
      'axe.pageSummary'
    ];
  },
  processMessage(message) {
    // if there's no configured budget do nothing
    if (!this.options.budget) {
      return;
    }

    const budget = this.options.budget.config;

    if (this.budgetTypes.indexOf(message.type) > -1) {
      // If it doesn't have the new structure of a budget file
      // use the old verdion
      if (!budget.budget) {
        deprecatedVerify(message, this.result, budget);
      } else {
        verify(message, this.result, budget);
      }
    } else {
      switch (message.type) {
        case 'budget.addMessageType': {
          if (!message.data.type) {
            log.error('Received add message for budget without message type');
          } else {
            this.budgetTypes.push(message.data.type);
          }
          break;
        }

        case 'sitespeedio.render': {
          if (this.options.budget) {
            if (this.options.budget.output === 'json') {
              json.writeJson(this.result, this.storageManager.getBaseDir());
            } else if (this.options.budget.output === 'tap') {
              tap.writeTap(this.result, this.storageManager.getBaseDir());
            } else if (this.options.budget.output === 'junit') {
              junit.writeJunit(this.result, this.storageManager.getBaseDir());
            } else {
              let failing = 0;
              let working = 0;
              for (const url of Object.keys(this.result.failing)) {
                for (const result of this.result.failing[url]) {
                  log.error(
                    'Failing budget %s for %s with value %s %s limit %s',
                    result.metric,
                    url,
                    result.friendlyValue || result.value,
                    result.limitType,
                    result.friendlyLimit || result.limit
                  );
                  failing = failing + 1;
                }
              }
              for (const url of Object.keys(this.result.working)) {
                working = working + this.result.working[url].length;
              }
              log.info(
                'Budget: %d working and %d failing tests',
                working,
                failing
              );
            }
          }
        }
      }
    }
  }
};<commit_msg>Send the performance budget message in the queue for easier use (#3085)<commit_after>'use strict';

const deprecatedVerify = require('./deprecatedVerify').verify;
const verify = require('./verify').verify;
const tap = require('./tap');
const junit = require('./junit');
const json = require('./json');
const log = require('intel').getLogger('sitespeedio.plugin.budget');

module.exports = {
  open(context, options) {
    this.options = options;
    this.storageManager = context.storageManager;
    this.result = context.budget;
    this.make = context.messageMaker('budget').make;
    this.budgetTypes = [
      'browsertime.pageSummary',
      'webpagetest.pageSummary',
      'pagexray.pageSummary',
      'coach.pageSummary',
      'axe.pageSummary'
    ];
  },
  processMessage(message, queue) {
    // if there's no configured budget do nothing
    if (!this.options.budget) {
      return;
    }

    const budget = this.options.budget.config;

    if (this.budgetTypes.indexOf(message.type) > -1) {
      // If it doesn't have the new structure of a budget file
      // use the old verdion
      if (!budget.budget) {
        deprecatedVerify(message, this.result, budget);
      } else {
        verify(message, this.result, budget);
      }
    } else {
      switch (message.type) {
        case 'budget.addMessageType': {
          if (!message.data.type) {
            log.error('Received add message for budget without message type');
          } else {
            this.budgetTypes.push(message.data.type);
          }
          break;
        }

        case 'sitespeedio.render': {
          if (this.options.budget) {
            if (this.options.budget.output === 'json') {
              json.writeJson(this.result, this.storageManager.getBaseDir());
            } else if (this.options.budget.output === 'tap') {
              tap.writeTap(this.result, this.storageManager.getBaseDir());
            } else if (this.options.budget.output === 'junit') {
              junit.writeJunit(this.result, this.storageManager.getBaseDir());
            } else {
              let failing = 0;
              let working = 0;
              for (const url of Object.keys(this.result.failing)) {
                for (const result of this.result.failing[url]) {
                  log.error(
                    'Failing budget %s for %s with value %s %s limit %s',
                    result.metric,
                    url,
                    result.friendlyValue || result.value,
                    result.limitType,
                    result.friendlyLimit || result.limit
                  );
                  failing = failing + 1;
                }
              }

              queue.postMessage(this.make('budget.result', this.result));

              for (const url of Object.keys(this.result.working)) {
                working = working + this.result.working[url].length;
              }
              log.info(
                'Budget: %d working and %d failing tests',
                working,
                failing
              );
            }
          }
        }
      }
    }
  }
};
{'fraction_non_alphanumeric': 0.12239152037098378, 'fraction_numerical': 0.001987413050679033, 'mean_word_length': 1.699597675458203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2331995', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1628, 'n_words': 360}