Wednesday, September 13, 2017

Get database size in MySQL

Another micropost. Use following query to get database size.


SELECT table_schema AS "Database", 
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema;

Use following query to get individual table sizes. Modify database_name parameter.


SELECT table_name AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "database_name"
ORDER BY (data_length + index_length) DESC;

Tuesday, November 22, 2016

Install mongodb server in ubuntu 16.04

Another micropost. Ubuntu also has a mongodb-server package which installs an older version. This post will guide you to install the official mongodb server. [docs]

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

If you are on a proxy, add --keyserver-options

sudo apt-key adv --keyserver-options http-proxy=PROXY_URL:PORT --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Then run,

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Finally,

sudo apt-get update
sudo apt-get install mongodb-org-server

Run mongo demon

mongod

That's all.

Tuesday, March 10, 2015

Different Versions of jQuery in the Same Page

Another MicroPost. Sometimes there will be requirements to use multiple jQuery versions in the same page. Scenarios like cross browser compatibility, support an old jQuery plugin etc. This can be accomplished by changing the global jQuery variable and using $.noConflict() method. Hope following snippet is self explaining.


<!-- load jQuery 1.11.2 -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
  var old$ = $.noConflict(true);
</script>

<!-- load jQuery 2.1.3 -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
  var new$ = $.noConflict(true);
</script>

<script type="text/javascript">
  // Using jQuery 1.11.2
  console.log(old$.fn.jquery);

  // Using jQuery 2.1.3
  console.log(new$.fn.jquery);
</script>

Use this Plunker to play with this.

Wednesday, February 25, 2015

Angular.js Visible Toggle Button

From this post onward I am planning to blog more frequently with solutions for random issues I face along the day. These MicroPosts may not include much content, but will be direct and simplely answered solutions for issues. Hope these might save someone's time.

Not like jQuery, which has many toggle functions, As per my knowledge, Angular do not have any straight forward way to toggle the DOM. Many examples and fiddles I went through suggest unwanted coding stuff. I am not going to write a controller for a simple thing like this. Finally found a solution which worth mentioning. Hope following snippet is self explaining.


<button ng-model="isVisible"
        ng-init="isVisible = false" 
        ng-click="isVisible = !isVisible">
        {{ isVisible ? "Hide Section" : "Show Section"}}
</button>

<div ng-show="isVisible">
  <!-- content -->
</div>    
    

Thursday, January 29, 2015

Simple Pure Node Server

I bet everyone who tried node, has written that famous sample server code which appears in nodejs.org homepage. Then without a blink most of us moved to express or some other node framework, with or without knowing what's happening under the hood. Well, for educational purposes I wrote a simple node server without using any npm module. For anyone who is interested in what's happening behind the scene, this would be a great starting point.

Following are the core modules used. "http" module for server creation, "fs" module for read static content from the disk and "path" module for work with file paths.

var http = require("http"),
    fs = require("fs"),
    path = require("path");

Static files are filtered using simple switch statement.

switch(extName) {
    case ".html": {
        contentType = "text/html";
        dirPath = "/public/views/";
    } break;
    case ".css": {
        contentType = "text/css";
        dirPath = "/public/css/";
    } break;      
    case ".js": {
        contentType = "text/javascript";
        dirPath = "/public/js/";
    } break;
}    
If requested file is available on the disk, read and send the response. If not redirect to the error page.

fs.exists(filePath, function(isExists) {
    if(isExists) {
        readAndSendFile(res, filePath, contentType,200);
    }
    else {
        redirect404();
    }
});    

Please find the complete code at github. Please note that there are many ways to improve this and I would recommend to checkout Danial Khosravi's blog on the same topic for improved version.

Friday, January 23, 2015

Useful Git Commands

When I'm working with git, once in a while I found myself searching through google for proper command syntax. For those who struggle like me and for myself, I decided to maintain a command list.

If you are using git for the first time you need to setup git with your email and name.

git config --global user.email "you@example.com"
git config --global user.name "john doe"

Create a new git repository in current directory

git init

Add created repository in github as "origin" using remote repository url.

git remote add origin https://github.com/someuser/anyrepo.git

Check current status of the repository.

git status

Add modified files to commit.

git add .
git add file.ext

Commit all modified files or commit file by file with a proper commit message.

git commit -m "commit message."
git commit file.ext -m "commit message."

Push committed files to the origin from master. usually goes like push to <remote> from <master>

git push origin master

Undo your last pushed commit

git push -f origin HEAD^:master

If you need to ignore certain files or folders you need to create a .gitignore file and the folder names as bellow.

#ignore all files in node_modules.
node_modules/

#ignore .ext files.
*.ext

#but track this file, even though all .ext files are ignored
!importantFile.ext

Thursday, January 15, 2015

Sorting Algorithms with Sound

Found this amazing video on youtube which shows sorting algorithms with sound. take a look.