/*
 * graph.c
 * (C) Peter Salanki 2002
 * This program is copyright, and covered by the Gnu Public License.
 * The Natasha bot.
 * sorcer@linux.se
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <time.h>
#include "../../settings.h"
#include "../../globals.h"
#include "../../bottypes.h"
#include "settings.h"

#define NAME "Graph"
#define VERSION 1.00

void graphmain(void);
MODULE_INIT _module_init(MODULE m);
MODULE_DESTROY _module_destroy(MODULE m);

int nextrrd;

MODULE_INIT _module_init(MODULE m) {
  strncpy(m->name, NAME, 20);
  strncpy(m->compiledate, __DATE__ " " __TIME__, 30);
  m->version = VERSION;  

  nextrrd = 0;
}

MODULE_DESTROY _module_destroy(MODULE m) {

}

void graphmain(void) {
  char query[MAX_QUERY];
  int bots, linked, rrdchannels, users, authed, techs, queue;
  struct activeuser *u;

  if(time(NULL) >= nextrrd) {
      bots = linked = rrdchannels = users = techs = authed = queue = 0;

      nextrrd = time(NULL) + 300;

      dbquery("SELECT `status` FROM `arms`");
      while((row = mysql_fetch_row(res))) {
	++bots;
	if(atoi(row[0]) == 1) ++linked;
      }
      EndDbQuery();

      /* Number of channels */
      dbquery("SELECT count(*) FROM `channels`");
      row = mysql_fetch_row(res);
      rrdchannels = atoi(row[0]);
      EndDbQuery();

      sprintf(query, "SELECT COUNT(*) FROM users");
      dbquery(query);

      if((row = mysql_fetch_row(res))) users = atoi(row[0]);
      EndDbQuery();

      u = firstauser;

      while (u != NULL) {
	if(u->auth == 1) ++authed;
	else if(u->auth >= NORMAL && u->auth < BOT_NORMAL) {
	  ++techs;
	  ++authed;
	}
	u = u->next;
      }

      dbquery("SELECT count(*) FROM `request` WHERE status = '0'");
      row = mysql_fetch_row(res);
      queue = atoi(row[0]);
      EndDbQuery();

      /* Make botsgraphs */
      sprintf(query, "rrdtool update natasha-link.rrd %i:%i:%i", time(NULL), bots, linked);
      system(query);

      sprintf(query, "rrdtool graph %sbots1.gif --title=\"ARMs/Online (60 mins)\" --start %i --end %i DEF:Bots=natasha-link.rrd:Bots:LAST LINE1:Bots#FF0000 DEF:Linked=natasha-link.rrd:Linked:LAST LINE1:Linked#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH1_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %sbots2.gif --title=\"ARMs/Online (24 hours)\" --start %i --end %i DEF:Bots=natasha-link.rrd:Bots:LAST LINE1:Bots#FF0000 DEF:Linked=natasha-link.rrd:Linked:LAST LINE2:Linked#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH2_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %sbots3.gif --title=\"ARMs/Online (30 days)\" --start %i --end %i DEF:Bots=natasha-link.rrd:Bots:LAST LINE1:Bots#FF0000 DEF:Linked=natasha-link.rrd:Linked:LAST LINE2:Linked#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH3_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %sbots4.gif --title=\"ARMs/Online (365 days)\" --start %i --end %i DEF:Bots=natasha-link.rrd:Bots:LAST LINE1:Bots#FF0000 DEF:Linked=natasha-link.rrd:Linked:LAST LINE2:Linked#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH4_START, time(NULL));
      system(query);

      /* Make channel graphs */
      sprintf(query, "rrdtool update natasha-channels.rrd %i:%i", time(NULL), rrdchannels);
      system(query);

      sprintf(query, "rrdtool graph %schannels1.gif --title=\"Channels (60 mins)\" --start %i --end %i DEF:Channels=natasha-channels.rrd:Channels:LAST LINE1:Channels#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH1_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %schannels2.gif --title=\"Channels (24 hours)\" --start %i --end %i DEF:Channels=natasha-channels.rrd:Channels:LAST LINE1:Channels#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH2_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %schannels3.gif --title=\"Channels (30 days)\" --start %i --end %i DEF:Channels=natasha-channels.rrd:Channels:LAST LINE1:Channels#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH3_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %schannels4.gif --title=\"Channels (365 days)\" --start %i --end %i DEF:Channels=natasha-channels.rrd:Channels:LAST LINE1:Channels#0000FF &", RRD_PATH, time(NULL) - RRD_GRAPH4_START, time(NULL));
      system(query);
      /* Make users graph */

      sprintf(query, "rrdtool update natasha-users.rrd %i:%i:%i", time(NULL), authed, techs);
      system(query);

      sprintf(query, "rrdtool graph %susers1.gif --title=\"Authed Users (60 mins)\" --start %i --end %i DEF:Authed=natasha-users.rrd:Authed:LAST LINE1:Authed#0000FF DEF:Techs=natasha-users.rrd:Techs:LAST LINE2:Techs#00cc00 &", RRD_PATH, time(NULL) - RRD_GRAPH1_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %susers2.gif --title=\"Authed Users (24 hours)\" --start %i --end %i DEF:Authed=natasha-users.rrd:Authed:LAST LINE1:Authed#0000FF  DEF:Techs=natasha-users.rrd:Techs:LAST LINE2:Techs#00cc00 &", RRD_PATH, time(NULL) - RRD_GRAPH2_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %susers3.gif --title=\"Authed Users (30 days)\" --start %i --end %i DEF:Authed=natasha-users.rrd:Authed:LAST LINE1:Authed#0000FF DEF:Techs=natasha-users.rrd:Techs:LAST LINE2:Techs#00cc00 &", RRD_PATH, time(NULL) - RRD_GRAPH3_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %susers4.gif --title=\"Authed Users (365 days)\" --start %i --end %i DEF:Authed=natasha-users.rrd:Authed:LAST LINE1:Authed#0000FF DEF:Techs=natasha-users.rrd:Techs:LAST LINE2:Techs#00cc00 &", RRD_PATH, time(NULL) - RRD_GRAPH4_START, time(NULL));
      system(query);

      /* Make total-users graph */
      sprintf(query, "rrdtool update natasha-totusers.rrd %i:%i", time(NULL), users);
      system(query);

      sprintf(query, "rrdtool graph %stotusers1.gif --title=\"Total Users (60 mins)\" --start %i --end %i DEF:Total=natasha-totusers.rrd:Total:LAST LINE1:Total#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH1_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %stotusers2.gif --title=\"Total Users (24 hours)\" --start %i --end %i DEF:Total=natasha-totusers.rrd:Total:LAST LINE1:Total#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH2_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %stotusers3.gif --title=\"Total Users (30 days)\" --start %i --end %i DEF:Total=natasha-totusers.rrd:Total:LAST LINE1:Total#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH3_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %stotusers4.gif --title=\"Total Users (365 days)\" --start %i --end %i DEF:Total=natasha-totusers.rrd:Total:LAST LINE1:Total#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH4_START, time(NULL));
      system(query);

      /* Make queue graph */
      sprintf(query, "rrdtool update natasha-queue.rrd %i:%i", time(NULL), queue);
      system(query);

      sprintf(query, "rrdtool graph %squeue1.gif --title=\"Items in queue (60 mins)\" --start %i --end %i DEF:Items=natasha-queue.rrd:Items:LAST LINE1:Items#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH1_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %squeue2.gif --title=\"Items in queue(24 hours)\" --start %i --end %i DEF:Items=natasha-queue.rrd:Items:LAST LINE1:Items#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH2_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %squeue3.gif --title=\"Items in queue (30 days)\" --start %i --end %i DEF:Items=natasha-queue.rrd:Items:LAST LINE1:Items#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH3_START, time(NULL));
      system(query);
      sprintf(query, "rrdtool graph %squeue4.gif --title=\"Items in queue (365 days)\" --start %i --end %i DEF:Items=natasha-queue.rrd:Items:LAST LINE1:Items#FF0000 &", RRD_PATH, time(NULL) - RRD_GRAPH4_START, time(NULL));
      system(query);
      
      printf("Graphs done.\n");    
  }
}
