Tuesday, September 25, 2012

vertically center content in bootstrap

This is how you vertically center stuff in bootstrap:



This doesnt work in IE8 or less.

Monday, September 24, 2012

install syntax highlighter 3.x on blogger

To install syntax highligher on blogger here what you need to do:

Edit raw template html and put this after the HEAD tag:


and put this at the bottom of the template before the closing HTML tag: ]


NOTE:
If the javascript snippet above you must escape all html entities. They are shown as non-escaped becase of limitations of blogger. You will have to replace all of ' with @quot; and < with <

Monday, September 17, 2012

A/B Testing with nginx via rewrite

Here is how you can setup 50% of your users to see a different homepage via nginx.

We split them based on the last digit of their IP address:

location / {
    if ($remote_addr ~ "[02468]$") {
        rewrite ^(.+)$ /a$1 last;
    }
    rewrite ^(.+)$ /b$1 last;
}
location /a {
    alias   /Users/dogself/site;
    index  index.html index.htm;
    ssi    on;
}
location /b {
    alias   /Users/dogself/site;
    index  index2.html index2.htm;
    ssi    on;
}

The trick here is that instead of using the root directive you should use the alias directive. Using alias means that you dont need to create an 'a' and 'b' directories on your server for this to work.

The downside is that now you have /a/ and /b/ in your path. If you know a way around that, let me know!

How To Install Node.js on RasberryPi

This will let you install the latest version of node.js on a Raspberry Pi running occidentalis 0.2:


git clone https://github.com/joyent/node.git
cd node
export GYP_DEFINES="armv7=0"
export CXXFLAGS='-march=armv6 -mfpu=vfp -mfloat-abi=hard -DUSE_EABI_HARDFLOAT'
export CCFLAGS='-march=armv6 -mfpu=vfp -mfloat-abi=hard -DUSE_EABI_HARDFLOAT'
./configure --shared-openssl --without-snapshot
make
make install


Compiling node will take about 4-5 hours.

but at the end you will be rewarded with:

pi@raspberrypi ~ $ node --version
v0.9.2-pre

Wednesday, August 15, 2012

Instrument javascript functions with before and after hooks

Use-Case:

I have a javascript class that i want to intrument in an aspect oriented programming (AOP) style by adding before and after hooks to every method in the class. I want to do this generically.

The before function should take the same arguments as the instrumented function and return a set of modified (or not) arguments for the instrumented function.

The after function should take the same arguments as the arguments passed to the instrumented function.

You can also do other things, for example the before function could abort the call to the instrumented function and the after function could munge the returned value.

Solution:

I decided to add the before and after functions as properties of the instrumented function. Remember, in javascript functions are also objects and can have properties added just like any javascript object.

Once my class's functions are instrumented the code that uses the class remains the same, but new code can now define the new before and after functions:
MyClass.someMethod.before = function(...) { ... }

Code:
Note: This code makes use of underscore.js

Final Thoughts:
The code as it stands now does not allow the programmer to define multiple before and after functions, only a single one can exist at one time. In the future I may consider supporting binding multiple functions.

Wednesday, July 25, 2012

get table sizes of MySQL database

mysql> SELECT count(*) tables,
    ->   concat(round(sum(table_rows)/1000000,2),'M') rows,
    ->   concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
    ->   concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
    ->   concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
    ->   round(sum(index_length)/sum(data_length),2) idxfrac
    -> FROM information_schema.TABLES;
+--------+---------+--------+--------+------------+---------+
| tables | rows    | data   | idx    | total_size | idxfrac |
+--------+---------+--------+--------+------------+---------+
|    389 | 325.92M | 74.15G | 26.63G | 100.78G    |    0.36 |
+--------+---------+--------+--------+------------+---------+
1 row in set, 14 warnings (15.45 sec)

To get the details for one table, using something like:

mysql> SELECT count(*) tables,
    ->        concat(round(sum(table_rows)/1000000,2),'M') rows,
    ->        concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
    ->        concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
    ->        concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
    ->        round(sum(index_length)/sum(data_length),2) idxfrac
    ->        FROM information_schema.TABLES
    ->        WHERE  table_name like "users";
+--------+-------+-------+-------+------------+---------+
| tables | rows  | data  | idx   | total_size | idxfrac |
+--------+-------+-------+-------+------------+---------+
|      1 | 3.47M | 0.58G | 0.21G | 0.78G      |    0.36 |
+--------+-------+-------+-------+------------+---------+
1 row in set (0.27 sec)