benchmarks/dbmon/ENV.js JAVASCRIPT 212 lines View on github.com → Search inside
1var ENV = ENV || (function() {23  var first = true;4  var counter = 0;5  var data;6  var _base;7  (_base = String.prototype).lpad || (_base.lpad = function(padding, toLength) {8    return padding.repeat((toLength - this.length) / padding.length).concat(this);9  });1011  function formatElapsed(value) {12    var str = parseFloat(value).toFixed(2);13    if (value > 60) {14      minutes = Math.floor(value / 60);15      comps = (value % 60).toFixed(2).split('.');16      seconds = comps[0].lpad('0', 2);17      ms = comps[1];18      str = minutes + ":" + seconds + "." + ms;19    }20    return str;21  }2223  function getElapsedClassName(elapsed) {24    var className = 'Query elapsed';25    if (elapsed >= 10.0) {26      className += ' warn_long';27    }28    else if (elapsed >= 1.0) {29      className += ' warn';30    }31    else {32      className += ' short';33    }34    return className;35  }3637  function countClassName(queries) {38    var countClassName = "label";39    if (queries >= 20) {40      countClassName += " label-important";41    }42    else if (queries >= 10) {43      countClassName += " label-warning";44    }45    else {46      countClassName += " label-success";47    }48    return countClassName;49  }5051  function updateQuery(object) {52    if (!object) {53      object = {};54    }55    var elapsed = Math.random() * 15;56    object.elapsed = elapsed;57    object.formatElapsed = formatElapsed(elapsed);58    object.elapsedClassName = getElapsedClassName(elapsed);59    object.query = "SELECT blah FROM something";60    object.waiting = Math.random() < 0.5;61    if (Math.random() < 0.2) {62      object.query = "<IDLE> in transaction";63    }64    if (Math.random() < 0.1) {65      object.query = "vacuum";66    }67    return object;68  }6970  function cleanQuery(value) {71    if (value) {72      value.formatElapsed = "";73      value.elapsedClassName = "";74      value.query = "";75      value.elapsed = null;76      value.waiting = null;77    } else {78      return {79        query: "***",80        formatElapsed: "",81        elapsedClassName: ""82      };83    }84  }8586  function generateRow(object, keepIdentity, counter) {87    var nbQueries = Math.floor((Math.random() * 10) + 1);88    if (!object) {89      object = {};90    }91    object.lastMutationId = counter;92    object.nbQueries = nbQueries;93    if (!object.lastSample) {94      object.lastSample = {};95    }96    if (!object.lastSample.topFiveQueries) {97      object.lastSample.topFiveQueries = [];98    }99    if (keepIdentity) {100      // for Angular optimization101      if (!object.lastSample.queries) {102        object.lastSample.queries = [];103        for (var l = 0; l < 12; l++) {104          object.lastSample.queries[l] = cleanQuery();105        }106      }107      for (var j in object.lastSample.queries) {108        var value = object.lastSample.queries[j];109        if (j <= nbQueries) {110          updateQuery(value);111        } else {112          cleanQuery(value);113        }114      }115    } else {116      object.lastSample.queries = [];117      for (var j = 0; j < 12; j++) {118        if (j < nbQueries) {119          var value = updateQuery(cleanQuery());120          object.lastSample.queries.push(value);121        } else {122          object.lastSample.queries.push(cleanQuery());123        }124      }125    }126    for (var i = 0; i < 5; i++) {127      var source = object.lastSample.queries[i];128      object.lastSample.topFiveQueries[i] = source;129    }130    object.lastSample.nbQueries = nbQueries;131    object.lastSample.countClassName = countClassName(nbQueries);132    return object;133  }134135  function getData(keepIdentity) {136    var oldData = data;137    if (!keepIdentity) { // reset for each tick when !keepIdentity138      data = [];139      for (var i = 1; i <= ENV.rows; i++) {140        data.push({ dbname: 'cluster' + i, query: "", formatElapsed: "", elapsedClassName: "" });141        data.push({ dbname: 'cluster' + i + ' slave', query: "", formatElapsed: "", elapsedClassName: "" });142      }143    }144    if (!data) { // first init when keepIdentity145      data = [];146      for (var i = 1; i <= ENV.rows; i++) {147        data.push({ dbname: 'cluster' + i });148        data.push({ dbname: 'cluster' + i + ' slave' });149      }150      oldData = data;151    }152    for (var i in data) {153      var row = data[i];154      if (!keepIdentity && oldData && oldData[i]) {155        row.lastSample = oldData[i].lastSample;156      }157      if (!row.lastSample || Math.random() < ENV.mutations()) {158        counter = counter + 1;159        if (!keepIdentity) {160          row.lastSample = null;161        }162        generateRow(row, keepIdentity, counter);163      } else {164        data[i] = oldData[i];165      }166    }167    first = false;168    return {169      toArray: function() {170        return data;171      }172    };173  }174175  var mutationsValue = 0.5;176177  function mutations(value) {178    if (value) {179      mutationsValue = value;180      return mutationsValue;181    } else {182      return mutationsValue;183    }184  }185186  var body = document.querySelector('body');187  var theFirstChild = body.firstChild;188189  var sliderContainer = document.createElement( 'div' );190  sliderContainer.style.cssText = "display: flex";191  var slider = document.createElement('input');192  var text = document.createElement('label');193  text.innerHTML = 'mutations : ' + (mutationsValue * 100).toFixed(0) + '%';194  text.id = "ratioval";195  slider.setAttribute("type", "range");196  slider.style.cssText = 'margin-bottom: 10px; margin-top: 5px';197  slider.addEventListener('change', function(e) {198    ENV.mutations(e.target.value / 100);199    document.querySelector('#ratioval').innerHTML = 'mutations : ' + (ENV.mutations() * 100).toFixed(0) + '%';200  });201  sliderContainer.appendChild( text );202  sliderContainer.appendChild( slider );203  body.insertBefore( sliderContainer, theFirstChild );204205  return  {206    generateData: getData,207    rows: 50,208    timeout: 0,209    mutations: mutations210  };211})();

Code quality findings 36

Remove event listeners when no longer needed to prevent memory leaks
warning performance event-listener-leak
slider.addEventListener('change', function(e) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var ENV = ENV || (function() {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var first = true;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var counter = 0;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var data;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var _base;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var str = parseFloat(value).toFixed(2);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var className = 'Query elapsed';
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var countClassName = "label";
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var elapsed = Math.random() * 15;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var nbQueries = Math.floor((Math.random() * 10) + 1);
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var l = 0; l < 12; l++) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var l = 0; l < 12; l++) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var j in object.lastSample.queries) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var j in object.lastSample.queries) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var value = object.lastSample.queries[j];
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var j = 0; j < 12; j++) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var j = 0; j < 12; j++) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var value = updateQuery(cleanQuery());
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var i = 0; i < 5; i++) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var i = 0; i < 5; i++) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var source = object.lastSample.queries[i];
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var oldData = data;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var i = 1; i <= ENV.rows; i++) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var i = 1; i <= ENV.rows; i++) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var i = 1; i <= ENV.rows; i++) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var i = 1; i <= ENV.rows; i++) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
for (var i in data) {
Use let instead of var in loops to avoid scope issues
info correctness var-in-loop
for (var i in data) {
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var row = data[i];
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var mutationsValue = 0.5;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var body = document.querySelector('body');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var theFirstChild = body.firstChild;
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var sliderContainer = document.createElement( 'div' );
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var slider = document.createElement('input');
Use let or const to avoid scope issues and hoisting
info correctness var-declaration
var text = document.createElement('label');

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings