Pages

Monday, September 25, 2017

Putty and vimrc : paste with right click not working

I met a simple but annoying problem with VIM and putty.I can't make paste using right click.

Thanks to Fabio's blog for his solution : here
This behaviour was intriduce with a System Update. To disable it, you have to do the following in you VIM console :

:set mouse-=a

Then paste with right clic will work


 Hope this help !


Note :

To set this behaviour every times you edit a file, you have to add a .vimrc file in you user home  :

vi ~/.vimrc
set mouse-=a

Tuesday, September 12, 2017

Grails form : No mapping found for HTTP request with URI

Lately, I have a problem with a simple form.
When I clicked on submit  button, I have the following error :


    Error: Page Not Found (404)

    Path: /QCM/qcm


and in the console log, the following warning appeared :

       
WARN --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound             :

No mapping found for HTTP request with URI [/QCM/qcm] in DispatcherServlet with name 'grailsDispatcherServlet'


Nevertheless, my gsp page is really simple :
 
       
 <g:form name="qcm" controller="QCM" action="qcm">
        <table>
            <tr><p>Question</p></tr>
            <tr>
                <td><g:checkBox name="qcmcheck1" /></td>
                <td>Solution 1</td>
            </tr>
            <tr>
                <td><g:checkBox name="qcmcheck2" /></td>
                <td>Solution 2</td>
            </tr>
            <tr>
                <td><g:checkBox name="qcmcheck3" /></td>
                <td>Solution 3</td>
            </tr>
            <tr>
                <td>
                    <g:actionSubmit value="Suivant" />
                </td>
            </tr>
        </table>
    </g:form>
    ...


Same things for my controller (QCMController.gsp) :

       
package openlearning

class QCMController {

    def index() {
    }

    def qcm() {
        println("action !");
    }
}

I searched on Stackoverflow and the following post help me to find the solution.My problem was not with the return value of the controller's action but with the submit button.
The grails submit button doesn't work ! So if you want to solve this issue, you have to use a simple submit button :

       <input type="submit" value="Submit" />

I don't know if it is a bug or a bad use of g:actionSubmit but this workaround helps me.


Thursday, September 7, 2017

Uml class Diagram for exam

I started thinking about exam class diagram for my OpenLearning project.
In this project, we need to manage different kinds of Exam.So, what is the best way to design exam class ?

To now I think the design pattern factory is best solution.In the following UML class diagram, we define an abstract class with a createExam() method and we let subclasses to decide about creation.

For example, QcmExamFactory has the responsability to create the correct qcm exam.

There is lot QCM and real exams rules, so it is highly probable that implementation change.Moreover, in this time, there is only three types of exam : test, practice and real.
But I can imagine, it will exist other.That's why, I think the design pattern factory is a good solution.If you aren't agree with me, let me know !







Note : I use draw.io to make this UML picture.

Friday, September 1, 2017

PostgresSQL administration


In my line of work and also in my personal activity (See OpenLearning Project Page), I need to administrate a Postgresql Database.I'm going to share what I need to do.In this paper, I don't explain each parameters : I only give my configuration for my purpose.If you want details about configuration, see the official website od PostgreSQL.
I encourage you to let me know you advice and what you think of my paper.Please do not hesitate to report.It's important for me and other users.

My server configuration :
  • Debian 8.0 x64 (Jessie)
  • PostgreSQL 9.4.13

Backup the database



Here is my backup script :

       
#!/bin/sh
###################################################################
# Save [DATABASE_NAME] database
#
# @author  : ...
# @version : 1.0
#
###################################################################


BACKUP_DIR=/opt/[DATABASE_NAME]/postgresql-backup
MAILTO="[MAIL]"

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
backup_file="$BACKUP_DIR/[DATABASE_NAME]-dump_${current_time}.sql"

# Log script output
log()
{
    echo $1
}


##########################################################################################
# MAIN
##########################################################################################


log ""
log ""
log "#####################################################################"
log "START $0 script at $current_time"
log "#####################################################################"

log ""
log "Saving Postgresql database in ${backup_file}"

# Remarque .pgpass present dans /root/
`pg_dump --format=custom --no-owner -U postgres -v -b -f $backup_file -h 127.0.0.1 [DATABASE_NAME]`
if [ -s $backup_file ] ; then
  echo 'Dump succeeded'
  echo 'Dump succeeded'  | mail -s "[DATABASE_NAME] save database SUCCESS" $MAILTO
else
  echo 'Dump failed'
  echo 'Dump failed'  | mail -s "[DATABASE_NAME] save database FAILED" $MAILTO
fi

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
log "#####################################################################"
log "END synchro script at $current_time"
log "#####################################################################"
exit 0
       

Note :
- We use postgres user to avoid permission problem.
- You have to change [DATABASE_NAME] and [MAIL] in order to use this script.
- This script ask you a password.To avoid this, see next section.

Automatic connection

Automatic connection is necessary by the backup script for crontab purpose.First of all, we need to set a password for postgres user :

       
su - postgres
psql
ALTER USER postgres WITH PASSWORD 'MY_PASSWORD';
       
 

To be sure that is work, you have to test it :

       
psql -U postgres -h 127.0.0.1 -W
       

It should ask a password and you will be able to connect at Postgresql.
Then, we create and fill a .pgpass file like the following :

       
vi /root/.pgpass
127.0.0.1:5432:[DATABASE_NAME]:postgres:[MY_PASSWORD]
       

After, make a chmod :

       
chmod 600 /root/.pgpass
       

Now, the following command should work :

       
psql -U postgres -h 127.0.0.1 -W
       

That's all ! Now a script using pg_backup should work.
To finish, we execute the backup script in crontab every night at 22:00 :

       
crontab -e
00 22 * * * /root/app/scripts/crontab/save_db.sh >> /var/app/archive/postgresql/save_db.log
       

Restore the database

Advice : the most important is to test restore of the database !

The following script ask you a password.Usually, it should not because of .pg_pass file ! It is not really important because restore isn't use in crontab script.


       
#!/bin/sh

###################################################################
# Restore [DATABASE_NAME] database
#
###################################################################
clear
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
echo ""
echo "#####################################################################"
echo "START $0 script to RESTORE [DATABASE_NAME] DATABASE  at $current_time"
echo "#####################################################################"
echo ""


filepath=""
echo "Please enter PGSQL's backup file path ( e.g : /opt/[DATABASE_NAME]/postgresql-backup/[DATABASE_NAME]-dump_2017.08.24-10.28.35.sql) : "
read filepath
if [ "$filepath" = "" ];then
        echo "You must write a backup file path !"
        echo "END"
        exit 1
fi

if [ -e "$filepath" ];then
        echo "Restoring Postgresql database from ${filepath}..."
        pg_restore --if-exists -v -c -h 127.0.0.1 -U postgres -d [DATABASE_NAME] ${filepath}
else
        echo "File not found : $filepath"
fi

current_time=$(date "+%Y.%m.%d-%H.%M.%S")

echo "#####################################################################"
echo "END synchro script at $current_time"

echo "#####################################################################"
exit 0     
 

Note : You need to create an empty database before restoring.

Monitoring your database


To improve our application, we need to identify heavy request which have a duration upper than 50 ms.That's why we activate logging :


 vi /etc/postgresql/9.4/main/postgresql.conf
logging_collector = on
log_rotation_age = 1d
log_rotation_size = 100MB

Then we modify following parameters :

       
log_min_duration_statement = 50
log_statement = 'mod'

After, you only need restart the database :

service postgresql restart
ls /var/lib/postgresql/9.4/main/pg_log/

You will log file and you can test logging with simple request : select pg_sleep(0.5);


Source :
https://www.depesz.com/2011/05/06/understanding-postgresql-conf-log/

Débutez en crypto-monnaie de manière ludique avec les projets "Move to earn"

Les projets "Move to earn" sont des projets très en vogue en ce moment dans le monde de la crypto-monnaie. Le principe est simple ...