Sunday, June 28, 2020

Come Ear Boya Bot

My 12 year old son asked to create a web crawler bot. The project will progress at the link below.

Saturday, September 23, 2017

Guitar Fretboard Notes in PHP

Jump to here to see how the specific strings are populated with correct note values.

<?php


$notes = [ 
 ['C' , '16.35'] , 
 ['C♯' , '17.32'] , 
 ['D' , '18.35'] , 
 ['E♭' , '19.45'] , 
 ['E' , '20.60'] , 
 ['F' , '21.83'] , 
 ['F♯' , '23.12'] , 
 ['G' , '24.50'] , 
 ['G♯' , '25.96'] , 
 ['A' , '27.50'] , 
 ['B♭' , '29.14'] , 
 ['B' , '30.87'] , 
 ['C' , '32.70'] , 
 ['C♯' , '34.65'] , 
 ['D' , '36.71'] , 
 ['E♭' , '38.89'] , 
 ['E' , '41.20'] , 
 ['F' , '43.65'] , 
 ['F♯' , '46.25'] , 
 ['G' , '49.00'] , 
 ['G♯' , '51.91'] , 
 ['A' , '55.00'] , 
 ['B♭' , '58.27'] , 
 ['B' , '61.74'] , 
 ['C' , '65.41'] , 
 ['C♯' , '69.30'] , 
 ['D' , '73.42'] , 
 ['E♭' , '77.78'] , 
 ['E' , '82.41'] , 
 ['F' , '87.31'] , 
 ['F♯' , '92.50'] , 
 ['G' , '98.00'] , 
 ['G♯' , '103.80'] , 
 ['A' , '110.00'] , 
 ['B♭' , '116.50'] , 
 ['B' , '123.50'] , 
 ['C' , '130.80'] , 
 ['C♯' , '138.60'] , 
 ['D' , '146.80'] , 
 ['E♭' , '155.60'] , 
 ['E' , '164.80'] , 
 ['F' , '174.60'] , 
 ['F♯' , '185.00'] , 
 ['G' , '196.00'] , 
 ['G♯' , '207.70'] , 
 ['A' , '220.00'] , 
 ['B♭' , '233.10'] , 
 ['B' , '246.90'] , 
 ['C' , '261.60'] , 
 ['C♯' , '277.20'] , 
 ['D' , '293.70'] , 
 ['E♭' , '311.10'] , 
 ['E' , '329.60'] , 
 ['F' , '349.20'] , 
 ['F♯' , '370.00'] , 
 ['G' , '392.00'] , 
 ['G♯' , '415.30'] , 
 ['A' , '440.00'] , 
 ['B♭' , '466.20'] , 
 ['B' , '493.90'] , 
 ['C' , '523.30'] , 
 ['C♯' , '554.40'] , 
 ['D' , '587.30'] , 
 ['E♭' , '622.30'] , 
 ['E' , '659.30'] , 
 ['F' , '698.50'] , 
 ['F♯' , '740.00'] , 
 ['G' , '784.00'] , 
 ['G♯' , '830.60'] , 
 ['A' , '880.00'] , 
 ['B♭' , '932.30'] , 
 ['B' , '987.80'] , 
 ['C' , '1047.00'] , 
 ['C♯' , '1109.00'] , 
 ['D' , '1175.00'] , 
 ['E♭' , '1245.00'] , 
 ['E' , '1319.00'] , 
 ['F' , '1397.00'] , 
 ['F♯' , '1480.00'] , 
 ['G' , '1568.00'] , 
 ['G♯' , '1661.00'] , 
 ['A' , '1760.00'] , 
 ['B♭' , '1865.00'] , 
 ['B' , '1976.00'] , 
 ['C' , '2093.00'] , 
 ['C♯' , '2217.00'] , 
 ['D' , '2349.00'] , 
 ['E♭' , '2489.00'] , 
 ['E' , '2637.00'] , 
 ['F' , '2794.00'] , 
 ['F♯' , '2960.00'] , 
 ['G' , '3136.00'] , 
 ['G♯' , '3322.00'] , 
 ['A' , '3520.00'] , 
 ['B♭' , '3729.00'] , 
 ['B' , '3951.00'] , 
 ['C' , '4186.00'] , 
 ['C♯' , '4435.00'] , 
 ['D' , '4699.00'] , 
 ['E♭' , '4978.00'] , 
 ['E' , '5274.00'] , 
 ['F' , '5588.00'] , 
 ['F♯' , '5920.00'] , 
 ['G' , '6272.00'] , 
 ['G♯' , '6645.00'] , 
 ['A' , '7040.00'] , 
 ['B♭' , '7459.00'] , 
 ['B' , '7902.00']  
    ] ;

function wrapNote($input) {
         $letter=$input[0];
         $hz=$input[1];                     
         return "<span class='note ".$hz."'>".$letter."</span> <span class='note '>".$hz."</span>";
                    }
 


 foreach($notes as $key=>$note) {
  switch($notes[$key][1]) {
    case 82.41:
       $E=$key;
       break;
    case 110.00:
       $a=$key;
        break;
    case 146.80:
       $d=$key;
       break; 
    case 196.00:
       $g=$key;
       break;
       case 246.90:
       $b=$key;
       break;
    case 329.60:
       $e=$key;
       break;    
            }
           }
   

$E = array_slice($notes, $E, 13); 
$a = array_slice($notes, $a, 13); 
$d = array_slice($notes, $d, 13); 
$g = array_slice($notes, $g, 13); 
$e = array_slice($notes, $e, 13); 

foreach($E as $key=>$value) {
           print wrapNote($e[$key]);
           print wrapNote($b[$key]);
           print wrapNote($g[$key]);
           print wrapNote($d[$key]);
           print wrapNote($a[$key]);
           print wrapNote($E[$key]);
           print "<br />";
           print "<br />"; 
          }




Tuesday, August 8, 2017

Javascript Memory Limit and Usage in Chrome



<script>
// Thank you stackoverflow user B. Gruenbaum
console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
// will give you the JS heap size
console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
// how much you're currently using
</script>

Slightly off topic from post title, the following may be an issue in the problem set being thought on in regards to memory limits: JSON object sizes.
In my scenario I had static reference data in CSV format. I converted to JSON files ( e.g. materials.json).  Here is standalone PHP script you can place in directory of JSON files and get file sizes and javascript memory info all together.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<meta name="format-detection" ></meta>
<title>JS Memory Size</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<?php
foreach(glob('*.json') as $file) {
   print "<script type='text/javascript' src='".$file."' ></script>".PHP_EOL;
}
?>

<script>
$(document).ready(function(){
  console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
  // will give you the JS heap size
  console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
  // how much you're currently using
  $('#jsHeapSizeLimit').text(performance.memory.jsHeapSizeLimit);
  $('#usedJSHeapSize').text(performance.memory.usedJSHeapSize);
});
</script>
<style>
td {
align:left;
border:1px dotted #000000;
padding:5px;
}
</style>
</head>
<body>
<h1>JS Memory Size</h1>
<table><tr>
<th>Size</th><th>Info</th>
</tr>
 <td id='jsHeapSizeLimit'></td><td>Size Limit</td>
</tr><tr>
 <td id='usedJSHeapSize'></td><td>Currently Used</td>
</tr>
<?php
foreach(glob('*.json') as $file) {
   print "<tr><td>".filesize($file)."</td><td>".$file."</td></tr>".PHP_EOL;
}
?>
</table>
</body>
</html>
<?php
die();
?>

Thursday, June 8, 2017

Dynamically create input name and id via associated label text

var groupA = "";
$('label').each(function(index, item) {
var a = $(this).text();
a = a.replace(/\W/g, '');
a = a.replace(/\s/g, '');
a = a.toLowerCase();
groupA = $(this).attr('group');
$('input:text').each(function(index,item) {
if ($(this).attr('group') == groupA) {
$(this).attr('name', a + $(this).attr('typeB') );
$(this).attr('id', a + $(this).attr('typeB') );
}
});
});

Tuesday, April 4, 2017

Force use of HTTPS with Javascript

<script>
location.protocol=='https:' ? console.log(window.location.href) : replace('https:'+location.hostname+location.pathname);
</script>

Friday, March 17, 2017

See inventory of function in javascript files

Place this script in same directory your javascript files are, view in browser.
(and yes I know there is grep -n 'function' *.js but I like things I can see in browser)
 <?php
header("Content-Type:text/plain") ;
$files=glob('*.js');
$searchfor = 'function';
foreach($files as $file) {
print $file.PHP_EOL;
$contents=file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
  print count($matches[0])." total functions".PHP_EOL;
  print implode("\n", $matches[0]);
}
}
?>

Example out:

main.js
37 total functions
function checkExists(myVar) {
function consoleLogTime(input) {
function debugMsg(input) {
function getQuerystring(key, default_)
function selectCustomer(input) {
function listCreate(arrayValues, arrayText, myClass) {
function optionListCreate(arrayValues, arrayText) {
function validateEmail(x) {
function checkEmailExists(email) {
      success: function(result) {
      error: function(result) {
function passwordReset(email) {
      success: function(result) {
      error: function(result) {
function listArray(myArray) { 
function getJsonTable(url, table, tag) {
 $.getJSON(url+'?table='+table, function(data){
        $.each(data, function(index,item) {
$(document).ready(function(){
     success: function(result) {
     error: function(result) {
$('#signin').click(function() {
$('#signup').click(function() {
$("#passwordReset").click(function() { 
$("#emailB").blur(function() { 
$('.mytype').click(function() {
$("#passwordconfirmB").keyup(function() { 
$("#passwordconfirmB").blur(function() { 
$("#workorder").ready(function() { 
$("select").ready(function() { 
$("#bids").change(function() { 
$("#about").click(function() { 
$('.submit').click(function() {
      success: function(result) {
      error: function(result) {
$("#selectacustomer").click(function() { 
$(".customerSelect").click(function() { 


Thursday, February 2, 2017

Wednesday, February 1, 2017

Web Browser Sniffing

function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) return 'opera';
    elseif (strpos($user_agent, 'edge')) return 'edge';
    elseif (strpos($user_agent, 'chrome')) return 'chrome';
    elseif (strpos($user_agent, 'safari')) return 'safari';
    elseif (strpos($user_agent, 'firefox')) return 'firefox';
    elseif (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) return 'internet explorer';
    elseif (strpos($user_agent, 'ipad')) return 'ipad';
    
    return 'Other';
}

$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$browser = get_browser_name($userAgent);

(javascript)
var userAgent = '';
var browser = '';
console.log('userAgent '+userAgent);
console.log('browser '+browser);

Tuesday, January 17, 2017

Javascript checkExists

function checkExists(myVar) {

 if (typeof myVar === 'undefined')  { return false;  }
 if ( myVar === null ) { return false;  }
 if ( myVar.length == 0) { return false; } 
 
 return true;
 
}

Friday, January 13, 2017

MYSQL Query for Timestamp Age in Hours

SELECT `route_id` FROM `route` where `is_active`=1
AND TIMESTAMPDIFF(HOUR,`dt_update`,CURRENT_TIMESTAMP())< 48;

Monday, January 9, 2017

Javascript misterClean string cleaner

function misterClean(x) {
   x = x.replace(/^\s+|\s+$/gm,''); // remove end whitespace
   x = x.replace(/(\r\n|\n|\r)/gm,""); // remove line breaks
   return x;
}

Thursday, December 29, 2016

No width parameter for SPAN tags, so limit width with javascript

$('span').each(function(index, value) {
  var limit = 30;
  var myID = $(this).attr('id');
  if(myID=='decoderip') {
   limit=60;
  }
  var mytext = $(this).text();
  if (mytext.length > limit) {
   // console.log('span text: '+mytext);
   $(this).text(mytext.slice(0, limit) + '...');
  }
 });

Monday, December 19, 2016

Time seconds increment on class of elements


function updateAge() {
 $('.age').each(function(){
  var age=$(this).text();
  age++;
  $(this).text(age);
 })
}

$(document).ready(function(){
  console.log('document ready'); 
  setInterval(updateAge, 1000); 
})

Thursday, December 1, 2016

Debian Package Creator and Installer

I made this for a particular "product", change the directory variables and final archived .deb filename per your needs.

debian.build.install.sh
#!/usr/bin/env bash
# this.is.lance.miller@gmail.com
clear;
basedir='/home/';
microdir=$basedir"micro/";
chmod 0775 $microdir'DEBIAN/postinst';
myfile=$microdir'DEBIAN/control';
echo $myfile;
myversion=`cat $myfile | grep Version | cut -d' ' -f2`;
echo $myfile' declares version '$myversion;
dpkg-deb --build $microdir;
ls -l $basedir*.deb;
sleep 5;
dpkg -i $basedir'micro.deb';
ls -l $basedir*.deb;
newfile='our_product_name_'$myversion'_all.deb';
echo 'creating this versioned deb archive: '$newfile;
mv $basedir'micro.deb' $basedir$newfile;
ls -l $basedir*.deb;

Monday, November 14, 2016

Kill process

target='Encoder'
kill `ps -x | grep $target | cut -d" " -f2`

Friday, October 28, 2016

CPU Analysis PHP

<?php 
header("Content-Type:text/plain") ;

$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;


print "Report for host: ".gethostname()." ".$_SERVER['SERVER_ADDR']."   timestamp: ".date("Y:m:d:h:i:s a")." year/month/date/hour/minute/seconds ".PHP_EOL.PHP_EOL;
$loadArray = sys_getloadavg();
$cpu = $loadArray[2];
$memoryMsg = "Memory ".$memory_usage." percent in use.".PHP_EOL;
print PHP_EOL."++++++++++++ operating system profile +++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 echo 'Operating System: '.php_uname('s').PHP_EOL;
 echo 'Release Name: '.php_uname('r').PHP_EOL;
 echo 'Version: '.php_uname('v').PHP_EOL;
 echo 'Machine Type: '.php_uname('m').PHP_EOL;
print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;


if(file_exists('/proc/cpuinfo')) {
 $retval=file_get_contents('/proc/cpuinfo');
 $mycount=explode(PHP_EOL, $retval);
 $cores = round( (count($mycount)/26), 0, PHP_ROUND_HALF_DOWN);
 $load = $cpu/$cores;
 $load = $load*100;


 print PHP_EOL."++++++++++++ hardware profile +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 print "CPU has ".$cores." cores".PHP_EOL;
 print PHP_EOL.'Memory:'.PHP_EOL.$free.PHP_EOL;
 print PHP_EOL."+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL.PHP_EOL;

 print "CPU load ".$load." percent averaged over last 15 minutes. [ sys_getloadavg() is ".$loadArray[0]." ".$loadArray[1]." ".$loadArray[2]." ]".PHP_EOL;
 print $memoryMsg;
 print PHP_EOL."Detailed CPU Information ( /proc/cpuinfo ) below.".PHP_EOL;
 print $retval.PHP_EOL;
} else {
 print "CPU load ".$cpu.PHP_EOL;
 print $memoryMsg;
}

print PHP_EOL."Uptime information  below.".PHP_EOL;
exec('uptime', $retval); 
foreach($retval as $ret) {
 print $ret.PHP_EOL;
}  


?>

Thursday, October 6, 2016

Unix/Linux how to run a command in background

nohup myFooCommand > /dev/null 2>&1 &

Tuesday, September 6, 2016

Perform post install scripting for Debian packages. Especially useful for setting file permissions

There are plenty of thorough references for creating Debian .deb packages, but not enough short examples that present the bare essentials. This is a minimal example with post install scripting -especially useful for setting file permissions- which is done specifically with the DEBIAN/postinst file.



The files that configure and command the Debian packaging and subsequent installation all reside in a directory named DEBIAN. Here are all the files I include in my DEBIAN directory:
  1. DEBIAN/conffiles
  2. DEBIAN/control
  3. DEBIAN/postinst
  4. DEBIAN/rules



Below are the contents of the files (contents changed to protect the proprietary)

  1. DEBIAN/conffiles
    ...blank...
    
  2. DEBIAN/control
    Package: foo-for-bar
    Version: 1.0.40
    Section: Network
    Priority: optional
    Architecture: all
    Depends:
    Maintainer: Jane Doe jane.doe@example.com
    Description: Acme FooBar WWW UI
     User interface for Acme FooBar.
    
  3. DEBIAN/postinst
    #!/usr/bin/env bash
    cat $0
    chmod 777 /var/lib/foobar/conf/www/*
    chmod 777 /var/lib/foobar/*
    chmod 777 /var/lib/foobar/foobaringdaemon.py
    chmod 777 /etc/local.d/80_figlets_with_foobar.sh
    echo "hello there from postinst file"
    
  4. DEBIAN/rules
    ...blank...
    

General File Structure

Let's say I want a .deb package to install files that will reside in the /var/www and /etc/local.d directories. In my case I'll create everything in the /home/ directory. Here is the root and first layer of directories.
  • /home/FOOBAR/DEBIAN
  • /home/FOOBAR/etc
  • /home/FOOBAR/var
With all the above as environment to operate on, here is a .deb creation command:

cd /home/
dpkg-deb --build FOOBAR

The above dpkg-deb would create a file named FOOBAR.deb
To install the FOOBAR.deb package on a machine, you would run:

dpkg -i FOOBAR.deb

A parting note. I would rename my FOOBAR.deb to FOOBAR.1.0.40.deb to make evident the version number of the package.

Wednesday, August 10, 2016

jQuery Dialog zIndex


$( ".selector" ).dialog();

$( ".selector" ).css("background-color", "red");

console.log( 'z-index ' + $( ".selector" ).zIndex()     );

Tuesday, April 5, 2016

Make Sure Element is Visible and Displayed : Javascript

function makeVisible(id) {
 if($('#'+id).css('display') == 'none') {
 $('#'+id).css('display','block');
 }
 if($('#'+id).css('visibility') == 'hidden') {
 $('#'+id).css('visibility','visible');
 }
 console.log('makeVisible visibility '+$('#'+id).css('visibility'));
 console.log('makeVisible display  '+$('#'+id).css('display'));
}

function testPreviewImage() {
 makeVisible('Preview_Div');
 makeVisible('Preview_Image');
 $('#Preview_Div').zIndex(16777261);
 $('#Preview_Image').zIndex(16777271);
}