#!/bin/sh
# Small watching script based on Sébastien idea.

function show_help ( )
{
  script_name=`basename $0`
  echo """
  Usage:
    $script_name <mysql_opt> [interval seconds]
  Interval is default 5.
  mysql_opt are default mysql command line options.
  Put them in quotes if more than one option is passed.

  Typical usage:
    $script_name erp5
    $script_name \"-h remotehost -u user erp5remote\" 3
  """
}

MYSQL_OPT=$1
INTERVAL=$2

if [ "$MYSQL_OPT" == "" ] ; then
  show_help
  exit 1
fi

if [ "$INTERVAL" == "" ] ; then
  INTERVAL=5
fi

SELECT=""
for t in message message_queue ; do
  SELECT=$SELECT"""
  SELECT count(path) AS $t, method_id, processing, processing_node AS node, min(priority) AS min_pri, max(priority) AS max_pri FROM $t GROUP BY method_id, processing, processing_node;
  SELECT count(path) AS $t, processing, processing_node, min(priority) AS min_pri, max(priority) AS max_pri FROM $t GROUP BY processing, processing_node;
  SELECT count(path) AS ${t}_count FROM $t;
  """
done
watch -n $INTERVAL "mysql $MYSQL_OPT --disable-pager -t -e '$SELECT' "