Javascript timed array iteration

Sometimes you need to iterate on an array with a time delay between each iteration. For example, you have a list of UI elements. And you want to perform animation or process each components in a way to make it visible to user. If you dont use any delay there will be no UI feedback. User will think everything happened at once. Its better to provide an UI feedback on the work in progress.

Timed array iteration means you use a delay on each iteration. JavaScript has no sleep method. So its not possible to add delay like other languages. I used `window.setTimeout ` to achieve this. This is only available on browser based JavaScript. In server side JavaScript like nodejs, v8, rhino its not available. Here is the function.

function arrayWalkTimed(arr, dur, func){
    var p = arr[0]? arr[0]: null;
    if(p){
        func(p);
        window.setTimeout(function(){
            timed_array_walk(arr.slice(1), dur, func);
        }, dur);
    }
}

Check if a php method is called statically or as an instance method

PHP allows to call you any method of a class both as static and as instance method. Recent version of PHP is more strict though. But still you can call non-static function as static. Calling a non-static function that uses $this statically will cause a fatal error. You’d  want to catch this and throw exception with meaningful message.

If you can determine how this method is called whether statically or as instance method you can handle this situation.

See this example class.

class C {
    private $value = "value";
    public function method () { 
        echo __METHOD__. "\n"; 
        echo "Value:". $this->value. "\n"; 
    }
}

This method will raise a fatal error you if call it as C::method().

To determine if its this is called statically or as an instance method there are two techniques.

  1. Using debug_backtrace
    1. class A { 
       public function m(){ 
       $bt = debug_backtrace(); 
       if($bt[0]['type']=='::')
       throw new Exception(__METHOD__." is called statically");
       }
      }
      
      
  2. Using isset on $this.
    1. class A { 
       public function m(){ 
       if(!isset($this))
       throw new Exception(__METHOD__." is not called from class instance");
       }
      }

 

The last one is shorter. But first one is more explicit.

Determine outgoing apache bandwidth usage with built in commands

To find the bandwidth usage of your Apache server, you can use many existing tools. Like vnstat, awstat.  The most common thing about these tools is they need the utility installed. What if you dont have this installed and you want to calculate your bandwidth? This is can be easily done by parsing Apache access logs. This technique will only work if you are a web master and you have no other bandwidth eating service other than apache. Most web developers will fall in this category. So here is the technique to find apache bandwidth usage.

Note: You need ssh access to perform these actions. Also I assume you have not deleted your log files.

  1. Determine the date range for which you want to find the bandwidth usage for. For example I want to deter mine bandwidth usage from Oct 1st 2012 to Oct 30 2012. Note you must have access log files for that range.
  2. Now the big command. Assuming your apache log directory is /var/log/apache2
    1. find /var/log/apache2 -type f \
          -name '*.gz' \
          -newermt "2012-10-01 00:00:00" \
          -not \
          -newermt "2012-10-30 23:59:59" \ 
          -exec zcat '{}' \; | 
      egrep '"[^"]+" +200 [0-9]+' -o | 
      awk '{sum+=$(NF)} END {print sum/1024/1024/1024 " GB"}'
  3. This will print something like “34.345 GB