#!/bin/sh
# connrate 1.6 1995/09/29 17:51:19 davidsen Prod
#	connrate - generate connect rate logging

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

# analyze
mawk '

# 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)
}

# get process if from field
function getpid(field)
{
  field = sub(/^.*\[/, "", field)
  field = sub(/\]/, "", field)
  return field
}

# process the connect rates
/connect/ {
  # convert the "MMM DD" date to a string, "MMM0DD"
  date = substr($0, 1 ,6)
  gsub(/ /, "0", date)

  # if this is a new date since the last output, start
  # a new output file with the date name
  if (date != olddate) {
    # if old files exist, close them
    if (length(xyfile)) {
      close(xyfile)
      close(xxyfile)
    }

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

    # generate new filenames, files created on write
    xyfile = "connrate_xy."date
    xxyfile = "readrate_xxy."date

    # clear counters and flags
    olddate = date
    oldtime = 0
  }

  # set time in hundredths of an hour
  time = thh($3)

  # if still in the same 100th, add to the count
  # else transition to a new 100th
  if (time == oldtime) count++
  else {
    # except for startup (time == count == 0) output
    # the data to the plotfile
    if (oldtime || count) printf("%.3f %.2f\n", oldtime/100.0, count/36.0) > xyfile

    # reset the counters and flag
    oldtime = time
    count = 1
  }
}

# look for article vs e.t. info
/exit articles/ {
  # save the article count ref by PID
  procid = getpid($5)
  arts[procid] = $9
}

# this line contains the end time, match it up
# with the number of arts
/times user/ {
  # skip if we dont have a file defined yet
  if (length(xxyfile) == 0) next

  # see if article data for this process
  if (arts[procid]) {
    # generate the start and end time, plot will be
    # start, end, data format
    procid = getpid($5)
    time_end = thh($3)
    time_srt = time_end - ($13/36)

    # output the data
    printf("%.3f %.6f %.3f\n", time_srt/100.0, $13/3600.0, arts[procid]/$13) > xxyfile

    # release the memory
    free arts[procid]
  }
}' $filelist
