Friday, December 11, 2015

How to add a timestamp to your log files in your bash scripts in Linux


Today I show how to add a timestamp to your log files in your bash scripts. That is easy. In this solution we have to include the function

adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}

in your script file and add pipe the command to the function using the systax:

command | adddate

For example:

#!/bin/bash
adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}
echo -e "Doing something"
ls -la | adddate

Friday, August 21, 2015

Multiple DB Connections in Laravel 5. How to connect to a different Schema in Postgresql


What happens if you are using Laravel and PostgrSQL and you have to connect to multiple schemas. This post shows how to solve this problem easily in a few steps:



1) Create a new connection in the file app/config/database.php. For example the "test" connection linked to the "your_schema" schema.

...

'connections' => [

...

'test' => [
'driver'   => 'pgsql',
'host'     => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset'  => 'utf8',
'prefix'   => '',
'schema'   => 'your_schema',
],

...
],

...

2) Create a new Model using artisan. For example: OtherDB model.


use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\DB;

class OtherDB extends Model {

protected $connection = 'test'; // Connection name
protected $table = 'masterscale'; // table name

public $timestamps = false;

public static function loanSearch(){

$query = new OtherDB;

// Example 1: Using Eloquent
//dd($query::all());

// Example 2: Using a raw query
$masterscale = $query::where('risk_class','B1')->get();
foreach ($masterscale as $value) {
echo "
Risk class ". $value->risk_class . " has score " . $value->score;
}

//dd($masterscale);

return;
}

}


3) Create a controller with artisan or use an existing one. For example: otherDBController


use App\OtherDB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class otherDBController extends Controller {

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$loans = OtherDB::loanSearch();
dd($loans);
}

}

4) Create the route to the controller is it does not exist

Route::resource('test', 'otherDBController');

5) Test it :)


Monday, June 15, 2015

Searching for packages in R. What is the best package to analyse my data?


Many times we want to apply some method or we need an algorithm but we don't have the time to implemented or simply we don't know how to do it. For this situations is good to know how to look for the best or more standardized packages that R offered.

Here is my trick.


  1. Install the package "sos"
  2. Use the function findFn to find the related packages to a given topic.
Let's first install the package sos using the code:


install.packages("sos")
library("sos")

Now we want to know which packages are available for example for "Particle Swarm Optimisation". Let's use the function findFn as follow:

findFn("Particle Swarm Optimisation")
found 18 matches;  retrieving 1 page

Downloaded 10 links in 4 packages.

And automatically we will be redirected to a website with the list of packages founded:



I don't know which is the best package for PSO but at least I know the best scored.




Two-Vector Dictionary in R using "match"


Something common in R is to have two vectors representing a key-value dictionary, for example:


  • Car name
  • Car price

Let's see an example:

We created two vectors using the dataset "car.test.frame" included in the package rpart. The first vector contains the name of the cars (key) and the second vector the list of prices (value). The function "match" is used to look for a couple of cars in the vector of names. If there is a match we get the index in the car_name vector (in our case, 37 and 43). Now we can use the indexes to work with the vector car_price or with the original data frame as a key-value dictionary.

Here is the code:

library(rpart)
str(car.test.frame)

# Get the name of the cars
car_name = row.names(car.test.frame)
car_price = car.test.frame$Price

# List of car's names
cars = c("Volvo 240 4", "Ford Taurus V6")
match(cars, car_name)

# Get the price of the car
car.test.frame[match(cars, car_name),]$Price

# or using the value-vector
car_price[match(cars, car_name)]

Tuesday, June 9, 2015

Problems printing graphical contents / canvas in lastest version of Chrome.


In the last release of Chrome something was wrong printing canvas contents. To see the problem visit the website:

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

  1. Do you see the circles on the map?
  2. Print the page
  3. Observe the differents with the printing version. Do you see the circles? 
If the answer is not, this post solves your problem.


We can solve temporarily the problem until Google launch a new version of Chrome or an update, following the next steps:

  1. Go to: chrome://flags/   (writing "chome://flags/" in your navigation bar)
  2. Deactivate display list 2D canvas.
  3. Restart Chrome (clicking on "Restart" at the bottom of the flags page).
Sorry my Chrome is in German. chrome://flags


Try again printing the previous page and observe the map. Now you should see the circles on the map.






Wednesday, February 11, 2015

How to speed up your site with htaccess


Loading speed is a common problem considering that your website is downloading again and  again the same files (css, js, png,...).

Let's load a couple of modules:

  • mod_expires
  • mod_header

The idea is to use the max-age header parameter which lets us says "this file expires 1 week from today", which is simpler than setting an explicit date. The max-age is measured in seconds, ie. 1 day = 86400, 1 week=604800, and so on.

In uuntu we can write:
sudo a2enmod headers
sudo a2enmod expires
sudo service apache2 restart
Then edit your .htaccess and insert the folowing lines:
#
# Configure mod_expires
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
#

    ExpiresActive On
    ExpiresDefault A3600
    ExpiresByType image/x-icon A2592000
    ExpiresByType application/x-javascript A86400
    ExpiresByType application/javascript A86400
    ExpiresByType text/javascript A86400
    ExpiresByType text/css A86400
    ExpiresByType image/gif A604800
    ExpiresByType image/png A604800
    ExpiresByType image/jpeg A604800
    ExpiresByType application/font-woff A604800
    ExpiresByType application/octet-stream A604800

#
# Configure mod_headers
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
#

   
        Header set Cache-Control "max-age=86400, public"
   

   
        Header set Cache-Control "max-age=600, private, must-revalidate"
   

    Header unset ETag
    Header unset Last-Modified

Done!. You can try now and check if your page is using the cache.

Hope help