#! /bin/bash
# Last edited on 2014-07-04 00:02:46 by stolfilocal

# Plots bitcointalk forum stats by month
# 
# Reads from standard input a file
# containing a table with lines of the form  
# "{yyyy}-{mm} | {NT} | {NP} | {NM} | {PV}
# where 
# 
#   {NT} = new topics
#   {NP} = new posts
#   {NM} = new members
#   {PV} = page views
#  

show="SHOW"

tmp="/tmp/$$"

cat \
  | gawk '/^[ ]*20[0-9][0-9]/{ print $1, $3, $5, $7, $9; }' \
  > ${tmp}.dat

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2400,1200 font "courbd,24"
set output "${tmp}-full.png"
set title "Bitcointalk.org Statistics by Month"

set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

# Input time format:
set timefmt "%Y-%m"

ttics = 24  # Time between major tics (weeks)

set xdata time
# Warning - placing parens around "ttics*..." confuses the parser!
set format x "%m/%y"
#set xtics ttics*7*24*3600
#set mxtics ttics
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set xzeroaxis lt 1 lw 3 lc rgb '#ffddaa'
set key left top

nozero(z) = (z == 0 ? 0/0 : z) 
#sNT = 1.0      # Scale factor for new topics.  
#sNP = 100.0    # Scale factor for new posts.   
#sNM = 1.0      # Scale factor for new members. 
#sPV = 10000.0  # Scale factor for page views.  
sNT = 1.0  # Scale factor for new topics.  
sNP = 1.0  # Scale factor for new posts.   
sNM = 1.0  # Scale factor for new members. 
sPV = 1.0  # Scale factor for page views.  

set logscale y

stat(k,s) = nozero(column(k))/s; 
titf(t,s) = (s == 1 ? t : sprintf("%s/%d", t, s))

tNT = titf("New Topics",  sNT)
tNP = titf("New Posts",   sNP)
tNM = titf("New Members", sNM)
tPV = titf("Page Views",  sPV)

plot \
  "${tmp}.dat" using (timecolumn(1)):(stat(2,sNT)) title tNT with linespoints pt 7 ps 1.0 lw 1.0 lc rgb '#0022ff', \
  ""           using (timecolumn(1)):(stat(3,sNP)) title tNP with linespoints pt 7 ps 1.0 lw 1.0 lc rgb '#ff0000', \
  ""           using (timecolumn(1)):(stat(4,sNM)) title tNM with linespoints pt 7 ps 1.0 lw 1.0 lc rgb '#008800', \
  ""           using (timecolumn(1)):(stat(5,sPV)) title tPV with linespoints pt 7 ps 1.0 lw 1.0 lc rgb '#8800dd'
  
quit
EOF

plotfile="${tmp}.png"
convert ${tmp}-full.png -resize '50%' ${plotfile}

if [[ "/${show}" == "/SHOW" ]]; then
  display ${plotfile}
fi

cat ${plotfile}

rm -fv ${tmp}{-*,}.*
    
        
