#!/bin/sh
# artrate 1.8 1995/09/29 18:21:43 davidsen Prod

if [ -z "$AWK" ]; then AWK="mawk"; fi

# Set input file(s)
if [ $# -lt 1 ]; then
  filelist=server.log
else
  filelist="$*"
fi

# analyze
${AWK} '

# fractional day function (time in hundredths of an hour)
function thh(ds)
{
  split(ds, dt, /:/)
  sec = 3600*dt[1] + 60*dt[2] + dt[3]
  return int(sec/36)
}
# process log lines
{
  # convert "MMM DD" date to MMM0DD" format
  date = substr($0, 1 ,6)
  gsub(/ /, "0", date)
# see if the start of a new day
  if (date != olddate) {
    # new date, close old file if open and create new filename
    if (length(xyfile)) {
      close(xyfile)
    }
    xyfile = "artrate_xy."date

    # progress report
    print "Start date "date > "/dev/tty"

    # clear count and value used as flag
    olddate = date
    oldtime = 0
  }
  # get time as 100th of an hour
  time = thh($3)
  # if same 100th, increment count, else process
  # change to new time slice
  if (time == oldtime) count++
  else {
    # if time or count are non-zero, output a plot point
    if (oldtime || count) {
      printf("%.3f %.3f\n", oldtime/100.0, count/36.0) > xyfile

      # times with no data are zero rate, fill in zeros
      # between input points
      lapse = time - oldtime - 1
      if (lapse < 0) lapse += 2400
      current = oldtime + lapse
      while (++oldtime < current) printf("%.3f 0\n", oldtime/100.0) > xyfile
    }
    # clear the count and value used as flag
    oldtime = time
    count = 1
  }
}

END {
  # if left-over count, output last point
  if (count) printf("%.3f %.3f\n", oldtime/100.0, count/36.0) > xyfile
}' $filelist
