<commit_before>/**
 * @fileoverview Handle WebPageTest API
 * @author <NAME>
 */
'use strict'

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var progress    = require('progress'),
    debug       = require('debug'),
    WebPageTest = require('webpagetest'),
    chalk       = require('chalk'),
    log         = require('./../logging')

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
debug = debug('perflint:wpt')

var wpt = {
  translateResults: function(config, results) {
    debug('Converting WebPageTest results to common format')
    var average = config.average,
        view = config.view,
        filtered = results[average][view]

    if (!filtered) {
      if (!results[config.average]) {
        log.error('Invalid \'average\' specified in config')
      }
      if (!results[config.average][config.view]) {
        log.error('Invalid \'view\' specified in config')
      }
      return 1
    }

    return {
      URL: results.url,
      summary: results.summary,
      loadTime: filtered.loadTime ? filtered.loadTime : '',
      TTFB: filtered.TTFB ? filtered.TTFB : '',
      bytesOut: filtered.bytesOut ? filtered.bytesOut : '',
      bytesOutDoc: filtered.bytesOutDoc ? filtered.bytesOutDoc : '',
      bytesIn: filtered.bytesIn ? filtered.bytesIn : '',
      bytesInDoc: filtered.bytesInDoc ? filtered.bytesInDoc : '',
      connections: filtered.connections ? filtered.connections : '',
      requestsFull: filtered.requestsFull ? filtered.requestsFull : '',
      requestsDoc: filtered.requestsDoc ? filtered.requestsDoc : '',
      requestsHTML: filtered.breakdown && filtered.breakdown.html ? filtered.breakdown.html : '',
      requestsJS: filtered.breakdown && filtered.breakdown.js ? filtered.breakdown.js : '',
      requestsCSS: filtered.breakdown && filtered.breakdown.css ? filtered.breakdown.css : '',
      requestsImage: filtered.breakdown && filtered.breakdown.image ? filtered.breakdown.image : '',
      requestsFlash: filtered.breakdown && filtered.breakdown.flash ? filtered.breakdown.flash : '',
      requestsFont: filtered.breakdown && filtered.breakdown.font ? filtered.breakdown.font : '',
      requestsOther: filtered.breakdown && filtered.breakdown.other ? filtered.breakdown.other : '',
      responses_200: filtered.responses_200 ? filtered.responses_200 : '',
      responses_404: filtered.responses_404 ? filtered.responses_404 : '',
      responses_other: filtered.responses_other ? filtered.responses_other : '',
      result: filtered.result ? filtered.result : '',
      render: filtered.render ? filtered.render : '',
      fullyLoaded: filtered.fullyLoaded ? filtered.fullyLoaded : '',
      cached: filtered.cached ? filtered.cached : '',
      docTime: filtered.docTime ? filtered.docTime : '',
      domTime: filtered.domTime ? filtered.domTime : '',
      score_cache: filtered.score_cache ? filtered.score_cache : '',
      score_cdn: filtered.score_cdn ? filtered.score_cdn : '',
      score_gzip: filtered.score_gzip ? filtered.score_gzip : '',
      score_cookies: filtered.score_cookies ? filtered.score_cookies : '',
      score_keepAlive: filtered['score_keep-alive'],
      score_minify: filtered.score_minify ? filtered.score_minify : '',
      score_combine: filtered.score_combine ? filtered.score_combine : '',
      score_compress: filtered.score_compress ? filtered.score_compress : '',
      score_etags: filtered.score_etags ? filtered.score_etags : '',
      score_progressive_jpeg: filtered.score_progressive_jpeg ? filtered.score_progressive_jpeg : '',
      gzip_total: filtered.gzip_total ? filtered.gzip_total : '',
      gzip_savings: filtered.gzip_savings ? filtered.gzip_savings : '',
      minify_total: filtered.minify_total ? filtered.minify_total : '',
      minify_savings: filtered.minify_savings ? filtered.minify_savings : '',
      image_total: filtered.image_total ? filtered.image_total : '',
      image_savings: filtered.image_savings ? filtered.image_savings : '',
      optimization_checked: filtered.optimization_checked ? filtered.optimization_checked : '',
      aft: filtered.aft ? filtered.aft : '',
      domElements: filtered.domElements ? filtered.domElements : '',
      pageSpeedVersion: filtered.pageSpeedVersion ? filtered.pageSpeedVersion : '',
      titleTime: filtered.titleTime ? filtered.titleTime : '',
      loadEventStart: filtered.loadEventStart ? filtered.loadEventStart : '',
      loadEventEnd: filtered.loadEventEnd ? filtered.loadEventEnd : '',
      domContentLoadedEventStart: filtered.domContentLoadedEventStart ? filtered.domContentLoadedEventStart : '',
      domContentLoadedEventEnd: filtered.domContentLoadedEventEnd ? filtered.domContentLoadedEventEnd : '',
      lastVisualChange: filtered.lastVisualChange ? filtered.lastVisualChange : '',
      SpeedIndex: filtered.SpeedIndex ? filtered.SpeedIndex : '',
      visualComplete: filtered.visualComplete ? filtered.visualComplete : '',
      firstPaint: filtered.firstPaint ? filtered.firstPaint : '',
      docCPUms: filtered.docCPUms ? filtered.docCPUms : '',
      fullyLoadedCPUms: filtered.fullyLoadedCPUms ? filtered.fullyLoadedCPUms : '',
      docCPUpct: filtered.docCPUpct ? filtered.docCPUpct : '',
      fullyLoadedCPUpct: filtered.fullyLoadedCPUpct ? filtered.fullyLoadedCPUpct : '',
      browser_process_count: filtered.browser_process_count ? filtered.browser_process_count : '',
      browser_main_memory_kb: filtered.browser_main_memory_kb ? filtered.browser_main_memory_kb : '',
      browser_other_private_memory_kb: filtered.browser_other_private_memory_kb ? filtered.browser_other_private_memory_kb : '',
      browser_working_set_kb: filtered.browser_working_set_kb ? filtered.browser_working_set_kb : '',
      effectiveBps: filtered.effectiveBps ? filtered.effectiveBps : '',
      effectiveBpsDoc: filtered.effectiveBpsDoc ? filtered.effectiveBpsDoc : ''
    }
  },

  getResults: function(config, cb) {
    var url = config.URL || config.test,
        test = config.test,
        key = config.key,
        timeout = config.timeout,
        requestTest = new WebPageTest(config.server, key),
        bar = new progress(chalk.bold.blue('[:bar] :elapsed Timeout at ' + timeout + 's'), { total: timeout * 10, width: 40, clear: true }),
        timer = setInterval(function () {
          bar.tick()
          if (bar.complete) {
            clearInterval(timer)
          }
        }, 100)

    debug('Get WebPageTest results for: ' + url)

    if (test) {
      requestTest.getTestResults(test, function (err, data) {
        if (err || data.statusCode !== 200) {
          log.error('Error: ' + data.statusText)
          bar.tick(timeout * 10)
          cb(err, null)
          return 1
        }
        bar.tick(timeout * 10)
        debug('Test Summary: ' + data.data.summary)

        if (config.info) {
          wpt.printInfo(config, data.data)
        }

        var parsed = wpt.translateResults(config, data.data)
        cb(null, parsed)
        return 0
      })
    } else if (url) {
      requestTest.runTest(url,
        {
          pollResults: 5,
          timeout: timeout,
          breakDown: true,
          domains: true,
          requests: false,
          pageSpeed: true,
          firstViewOnly: (config.view === 'firstView'),
          location: config.location,
          connectivity: config.connectivity,
          private: config.private
        },
        function (err, data) {
        if (err) {
          bar.tick(timeout * 10)

          if (err.error && err.error.code === 'TIMEOUT') {
            log.error('Error: Test timed out.')
          } else {
            var isWptServer = config.server !== 'www.webpagetest.org' ? 'Please check server is a valid WebPageTest server' : ''<commit_msg>Fix: Label not being set on test<commit_after>/**
 * @fileoverview Handle WebPageTest API
 * @author <NAME>
 */
'use strict'

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var progress    = require('progress'),
    debug       = require('debug'),
    WebPageTest = require('webpagetest'),
    chalk       = require('chalk'),
    log         = require('./../logging')

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
debug = debug('perflint:wpt')

var wpt = {
  translateResults: function(config, results) {
    debug('Converting WebPageTest results to common format')
    var average = config.average,
        view = config.view,
        filtered = results[average][view]

    if (!filtered) {
      if (!results[config.average]) {
        log.error('Invalid \'average\' specified in config')
      }
      if (!results[config.average][config.view]) {
        log.error('Invalid \'view\' specified in config')
      }
      return 1
    }

    return {
      URL: results.url,
      summary: results.summary,
      loadTime: filtered.loadTime ? filtered.loadTime : '',
      TTFB: filtered.TTFB ? filtered.TTFB : '',
      bytesOut: filtered.bytesOut ? filtered.bytesOut : '',
      bytesOutDoc: filtered.bytesOutDoc ? filtered.bytesOutDoc : '',
      bytesIn: filtered.bytesIn ? filtered.bytesIn : '',
      bytesInDoc: filtered.bytesInDoc ? filtered.bytesInDoc : '',
      connections: filtered.connections ? filtered.connections : '',
      requestsFull: filtered.requestsFull ? filtered.requestsFull : '',
      requestsDoc: filtered.requestsDoc ? filtered.requestsDoc : '',
      requestsHTML: filtered.breakdown && filtered.breakdown.html ? filtered.breakdown.html : '',
      requestsJS: filtered.breakdown && filtered.breakdown.js ? filtered.breakdown.js : '',
      requestsCSS: filtered.breakdown && filtered.breakdown.css ? filtered.breakdown.css : '',
      requestsImage: filtered.breakdown && filtered.breakdown.image ? filtered.breakdown.image : '',
      requestsFlash: filtered.breakdown && filtered.breakdown.flash ? filtered.breakdown.flash : '',
      requestsFont: filtered.breakdown && filtered.breakdown.font ? filtered.breakdown.font : '',
      requestsOther: filtered.breakdown && filtered.breakdown.other ? filtered.breakdown.other : '',
      responses_200: filtered.responses_200 ? filtered.responses_200 : '',
      responses_404: filtered.responses_404 ? filtered.responses_404 : '',
      responses_other: filtered.responses_other ? filtered.responses_other : '',
      result: filtered.result ? filtered.result : '',
      render: filtered.render ? filtered.render : '',
      fullyLoaded: filtered.fullyLoaded ? filtered.fullyLoaded : '',
      cached: filtered.cached ? filtered.cached : '',
      docTime: filtered.docTime ? filtered.docTime : '',
      domTime: filtered.domTime ? filtered.domTime : '',
      score_cache: filtered.score_cache ? filtered.score_cache : '',
      score_cdn: filtered.score_cdn ? filtered.score_cdn : '',
      score_gzip: filtered.score_gzip ? filtered.score_gzip : '',
      score_cookies: filtered.score_cookies ? filtered.score_cookies : '',
      score_keepAlive: filtered['score_keep-alive'],
      score_minify: filtered.score_minify ? filtered.score_minify : '',
      score_combine: filtered.score_combine ? filtered.score_combine : '',
      score_compress: filtered.score_compress ? filtered.score_compress : '',
      score_etags: filtered.score_etags ? filtered.score_etags : '',
      score_progressive_jpeg: filtered.score_progressive_jpeg ? filtered.score_progressive_jpeg : '',
      gzip_total: filtered.gzip_total ? filtered.gzip_total : '',
      gzip_savings: filtered.gzip_savings ? filtered.gzip_savings : '',
      minify_total: filtered.minify_total ? filtered.minify_total : '',
      minify_savings: filtered.minify_savings ? filtered.minify_savings : '',
      image_total: filtered.image_total ? filtered.image_total : '',
      image_savings: filtered.image_savings ? filtered.image_savings : '',
      optimization_checked: filtered.optimization_checked ? filtered.optimization_checked : '',
      aft: filtered.aft ? filtered.aft : '',
      domElements: filtered.domElements ? filtered.domElements : '',
      pageSpeedVersion: filtered.pageSpeedVersion ? filtered.pageSpeedVersion : '',
      titleTime: filtered.titleTime ? filtered.titleTime : '',
      loadEventStart: filtered.loadEventStart ? filtered.loadEventStart : '',
      loadEventEnd: filtered.loadEventEnd ? filtered.loadEventEnd : '',
      domContentLoadedEventStart: filtered.domContentLoadedEventStart ? filtered.domContentLoadedEventStart : '',
      domContentLoadedEventEnd: filtered.domContentLoadedEventEnd ? filtered.domContentLoadedEventEnd : '',
      lastVisualChange: filtered.lastVisualChange ? filtered.lastVisualChange : '',
      SpeedIndex: filtered.SpeedIndex ? filtered.SpeedIndex : '',
      visualComplete: filtered.visualComplete ? filtered.visualComplete : '',
      firstPaint: filtered.firstPaint ? filtered.firstPaint : '',
      docCPUms: filtered.docCPUms ? filtered.docCPUms : '',
      fullyLoadedCPUms: filtered.fullyLoadedCPUms ? filtered.fullyLoadedCPUms : '',
      docCPUpct: filtered.docCPUpct ? filtered.docCPUpct : '',
      fullyLoadedCPUpct: filtered.fullyLoadedCPUpct ? filtered.fullyLoadedCPUpct : '',
      browser_process_count: filtered.browser_process_count ? filtered.browser_process_count : '',
      browser_main_memory_kb: filtered.browser_main_memory_kb ? filtered.browser_main_memory_kb : '',
      browser_other_private_memory_kb: filtered.browser_other_private_memory_kb ? filtered.browser_other_private_memory_kb : '',
      browser_working_set_kb: filtered.browser_working_set_kb ? filtered.browser_working_set_kb : '',
      effectiveBps: filtered.effectiveBps ? filtered.effectiveBps : '',
      effectiveBpsDoc: filtered.effectiveBpsDoc ? filtered.effectiveBpsDoc : ''
    }
  },

  getResults: function(config, cb) {
    var url = config.URL || config.test,
        test = config.test,
        key = config.key,
        timeout = config.timeout,
        requestTest = new WebPageTest(config.server, key),
        bar = new progress(chalk.bold.blue('[:bar] :elapsed Timeout at ' + timeout + 's'), { total: timeout * 10, width: 40, clear: true }),
        timer = setInterval(function () {
          bar.tick()
          if (bar.complete) {
            clearInterval(timer)
          }
        }, 100)

    debug('Get WebPageTest results for: ' + url)

    if (test) {
      requestTest.getTestResults(test, function (err, data) {
        if (err || data.statusCode !== 200) {
          log.error('Error: ' + data.statusText)
          bar.tick(timeout * 10)
          cb(err, null)
          return 1
        }
        bar.tick(timeout * 10)
        debug('Test Summary: ' + data.data.summary)

        if (config.info) {
          wpt.printInfo(config, data.data)
        }

        var parsed = wpt.translateResults(config, data.data)
        cb(null, parsed)
        return 0
      })
    } else if (url) {
      requestTest.runTest(url,
        {
          pollResults: 5,
          timeout: timeout,
          breakDown: true,
          domains: true,
          requests: false,
          pageSpeed: true,
          firstViewOnly: (config.view === 'firstView'),
          location: config.location,
          connectivity: config.connectivity,
          private: config.private,
          label: config.label
        },
        function (err, data) {
        if (err) {
          bar.tick(timeout * 10)

          if (err.error && err.error.code === 'TIMEOUT') {
            log.error('Error: Test timed out.')
          } else {
            var isWptServer = config.server !== 'www.webpagetest.org' ? 'Please check server is a valid WebPageTest server' : ''
{'fraction_non_alphanumeric': 0.15382697581650462, 'fraction_numerical': 0.004736973323360758, 'mean_word_length': 2.653233151183971, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1652259', 'n_tokens_mistral': 4410, 'n_tokens_neox': 4269, 'n_words': 798}