/**********************************************************************
 * lucystat -- see what lucy's lag time is in answering questions
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/other/RCS/lucystat.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/other/RCS/lucystat.c,v 1.2 92/08/24 10:48:01 brlewis Exp $
 *
 * Copyright 1992 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_lucystat_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/other/RCS/lucystat.c,v 1.2 92/08/24 10:48:01 brlewis Exp $";
#endif /* lint */

#ifdef POSIX
#include <time.h>
#else
#include <sys/types.h>
#endif
#include <lucy/lucy.h>

main(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  int curtrn = LUCY_NULLQ;
  ltrn *tinfo;
  time_t right_now, how_early, submit_time, answer_time, age, total=0L;
  char time_string[17];
  int age_count = 0, unanswered_count = 0;

  /* check usage */
  if (argc != 2)
    {
      fprintf(stderr, "Usage: %s days\n", argv[0]);
      exit(1);
    }

  /* get time range; */
  right_now = (time_t) time(0);
  how_early = right_now - atoi(argv[1]) * (24 * 60 * 60);

  /* connect to lucy */
  code = luc_init();
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
    /* exit(1); except dsc_get_mtg_info returns bogus error? */
  }

  /* find first question to check */
  do
    {
      if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	goto ABORT;
      if (!curtrn)
	{
	  fprintf(stderr, "%s: No answers in time range.\n", argv[0]);
	  exit(1);
	}
      if (code = luc_trn_info(&luv_qmtg, curtrn, &tinfo))
	goto ABORT;
    } while (tinfo->date_entered < how_early);

  while(curtrn)
    {
      if (code = luc_trn_info(&luv_qmtg, curtrn, &tinfo))
	goto ABORT;
      if (! (tinfo->flags & (LUCY_REPLIED|LUCY_POSTED)))
	{
  	  unanswered_count++;
	  if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	    goto ABORT;
	  continue;
	}
      submit_time = (time_t) tinfo->date_entered;
      if (tinfo->flags & LUCY_POSTED)
	{
	  if (code = luc_lagtime(curtrn, LUCY_POSTED, &answer_time))
	    goto ABORT;
	  if (answer_time)
	    {
	      total += (age = answer_time - submit_time);
	      age_count++;
	      strncpy(time_string, ctime(&submit_time), 16);
	      printf("%5d P %4d\t%16.16s\t%16.16s\n", curtrn,
		     age / (24 * 60 * 60),
		     time_string, ctime(&answer_time));
	    }
	}
      if (tinfo->flags & LUCY_REPLIED)
	{
	  if (code = luc_lagtime(curtrn, LUCY_REPLIED, &answer_time))
	    goto ABORT;
	  if (answer_time)
	    {
	      total += (age = answer_time - submit_time);
	      age_count++;
	      strncpy(time_string, ctime(&submit_time), 16);
	      printf("%5d R %4d\t%16.16s\t%16.16s\n", curtrn,
		     age / (24 * 60 * 60),
		     time_string, ctime(&answer_time));
	    }
	}
      if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	goto ABORT;
    }
  printf("A total of %d answers, averaging %d-day response.\n", age_count,
	 total / age_count / (24 * 60 * 60));
  printf("There are %d unanswered questions from the past %d days.\n",
	 unanswered_count, atoi(argv[1]));
  exit(0);

 ABORT:
  com_err(argv[0], code, luv_context);
  exit(1);
}
