Bool_t gApacheExec=1; 

// TestInfoVersion.C
// This displays the information about the tests for the version of Gleam which the
// user has chosen. The information displayed is:
// Revision, Systest_id, Standardtest_id, CPU, Memory, Histogram File, Metadata Values,
// and Threshold Check
// Histogram File:  Link to the file HistogramSelect.C which allows the
//                  user to choose the histograms he wants to view. 
// Metadata Values: Link to the file MetadataDisplay.C which displays the metadata values 
//                  for all the histograms for that particular test and version
// Threshold Check: This allows the user to either view the histograms within the threshold
//                  or outside the threshold. 

#include <TApache.h>
#include <TSQLServer.h>
#include <TSQLRow.h>
#include <TSQLResult.h> 
#include <TCanvas.h>
#include <TFrame.h>
#include <TH1.h>
#include <string.h>
#include <iostream.h>
Bool_t gApacheNoCache=kTRUE;

void TestInfoVersion(){

  TApache ap;  
  char *sql;


  // Set background on the browser display
  ap.Puts("<body bgcolor=\"darkolivegreen\" topmargin=\"0\" leftmargin=\"0\" marginwidth=\"5\" marginheight=\"5\">");

  // These display the test data for a particular version
  char *sql1="select test_name, systest_id, standardtest_id, cpu, memory, histogram_file from glast_data.release_tests, glast_data.system_tests where pkg_name = \'Gleam\' and glast_data.release_tests.version = \'";
  char *sql2="\' and glast_data.system_tests.reltest_id = glast_data.release_tests.reltest_id and glast_data.release_tests.version=glast_data.system_tests.version";

  // This connects to the database
  char *connection="oracle://sage/SLAC_TCP";
  char *user="LAT ";
  char *pass="LATREAD";
  int length;

 
  // Get the version for the package Gleam
  TString release_type = gSystem->Getenv("version");

  // Assign it to a string variable
  TString release_type_strip = release_type.Strip();
  char *rel = release_type_strip.Data();
  
  // The total length of the sql query
  length = std::strlen(sql1) + std::strlen(sql2) + std::strlen(rel) + 1; 
  sql = new char[length];

  // Generate the sql query
  std::strcpy(sql, sql1);
  std::strcat(sql, rel);
  std::strcat(sql, sql2);

  // connect to oracle server
  TSQLServer *db=TSQLServer::Connect(connection, user, pass);
  
  

  //-------------------------------------------------------------------

  const char* count_sql_temp = "select count(systest_id) from glast_data.system_tests, glast_data.release_tests where pkg_name = \'Gleam\' and glast_data.release_tests.version = \'%s\' and glast_data.system_tests.reltest_id = glast_data.release_tests.reltest_id and glast_data.release_tests.version=glast_data.system_tests.version";
  char count_sql[4096];
  std::sprintf(count_sql, count_sql_temp, rel);
  

  TSQLResult *s = db->Query(count_sql);    
  TSQLRow *rows = s->Next();
  int num_sys = std::atoi(rows->GetField(0));  
  delete s;
  delete rows;
  
  
  struct Aaa{
    int systest_id;
    int count_within;
    int count_total;
    char summary[64];
  };

  // assign array size
  hist_threshold = (struct Aaa *)malloc(num_sys * sizeof(struct Aaa));


  // Get the systest_ids of tests for this particular version
  const char* get_systestid_temp = "select systest_id from glast_data.system_tests, glast_data.release_tests where pkg_name = \'Gleam\' and glast_data.release_tests.version = \'%s\' and glast_data.system_tests.reltest_id = glast_data.release_tests.reltest_id and glast_data.release_tests.version=glast_data.system_tests.version";
  char get_systestid_sql[4096];
  std::sprintf(get_systestid_sql, get_systestid_temp, rel);
  
  TSQLResult *g = db->Query(get_systestid_sql);    
  TSQLRow *rowg = g->Next();
  int i = 0;
  do{
    hist_threshold[i].systest_id = std::atoi(rowg->GetField(0));
    i++;
  }while(g->Next());
  delete g;
  delete rowg;


  // Get the number of histograms within threshold for each of the tests
  for(int i=0; i<num_sys; i++){
    const char* summary_sql_temp = "select count(meta_id) from glast_data.metadata m, glast_data.standard_test_thresholds stdtt, glast_data.info_labels il  where m.systest_id = %d and m.info = il.info and il.label like '%_KS' and m.systest_id = stdtt.systest_id and m.info = stdtt.info and to_number(m.info_value) > to_number(stdtt.info_value)";
    char summary_sql[4096];
    std::sprintf(summary_sql, summary_sql_temp, hist_threshold[i].systest_id);
    TSQLResult *res_summary=db->Query(summary_sql);   
    TSQLRow *row_summary=res_summary->Next();    
    hist_threshold[i].count_within = std::atoi(row_summary->GetField(0));
  }// end for
  delete res_summary;
  delete row_summary;

  
  // Get the total number of histograms whose KS values fall within the threshold 
  for(int i=0; i<num_sys; i++){
    const char* summary_sql_temp1 = "select count(meta_id) from glast_data.metadata m, glast_data.standard_test_thresholds stdtt, glast_data.info_labels il  where m.systest_id = %d and m.info = il.info and il.label like '%_KS' and m.systest_id = stdtt.systest_id and m.info = stdtt.info";
    char summary_sql1[4096];
    std::sprintf(summary_sql1, summary_sql_temp1, hist_threshold[i].systest_id);
    TSQLResult *res_summary1=db->Query(summary_sql1);   
    TSQLRow *row_summary1=res_summary1->Next();    
    hist_threshold[i].count_total = std::atoi(row_summary1->GetField(0));
    
  }// end for
  

  // Get the label in the 45/50 form within_threshold/total_histograms
  for(int i=0; i<num_sys; i++){
    std::sprintf(hist_threshold[i].summary, "%d\/%d within threshold", hist_threshold[i].count_within, hist_threshold[i].count_total);
  }// end for


  //----------------------------------------------------------------
  
  // Get the ouptputs of the summaries before doing this query

  //----------------------------------------------------------------
  // Get the version of all the standards for this version of the package

  // First we get the standardtest_id of just one of the system_tests for this version
  const char *std_sql_temp = "select standardtest_id from glast_data.system_tests where version=\'%s\'";
  char std_sql[4096];
  std::sprintf(std_sql, std_sql_temp, rel);
  TSQLResult *vres = db->Query(std_sql);    
  TSQLRow *rowvres = vres->Next();
  TString standard = rowvres->GetField(0);
  
  delete vres;
  delete rowvres;
  

  // Get the version for this standard
  const char *version_sql_temp = "select version from glast_data.system_tests syst, glast_data.standard_tests stdt where stdt.standardtest_id = %s and stdt.systest_id = syst.systest_id";
  char version_sql[4096];
  std::sprintf(version_sql, version_sql_temp, standard.Data());
  
  TSQLResult *vres = db->Query(version_sql);    
  TSQLRow *rowvres = vres->Next();
  TString verString = rowvres->GetField(0);

  delete vres;
  delete rowvres;
  
  //----------------------------------------------------------------


  TSQLResult *res=db->Query(sql);   
  TSQLRow *row1=res->Next();
   
  // The HTML form
  // Also the table for the results display
  ap.Puts("<HTML><BODY><CENTER>");
  ap.Puts("<BR><BR>");
  ap.Puts("<h2>The Tests for package Gleam version ");
  ap.Puts(rel);
  ap.Puts("<BR>The version of the Standard for these tests are: ");
  ap.Puts(verString.Data());
  ap.Puts("</h2>");
  ap.Puts("<TABLE>");
  ap.Puts("<CAPTION>Tests performed</CAPTION>");
  ap.Puts("<TR>");
  ap.Puts("<TH>System Test Name</TH>");
  ap.Puts("<TH>CPU secs </TH>");
  ap.Puts("<TH>Memory MB</TH>");
  ap.Puts("<TH>Histogram File</TH>");
  ap.Puts("<TH>Metadata Values</TH>");
  ap.Puts("<TH>Threshold Check</TH>");
  ap.Puts("<TH>Threshold Summary</TH>");
  ap.Puts("</TR>");
  ap.Puts("<BR>");
  

  do{
          
    ap.Puts("<TR>");
    for (int i=0; i<res->GetFieldCount();i++)
      { 
	if(i==1 || i==2) continue;

	ap.Puts("<TD>");

	
	// This generates the link containing parameters to be passed
	// to HistogramSelect.C. There are 3 parameters to be passed
	// histogram_file, systest_id, standardtest_id
	TString href1 = "<A HREF= \"HistogramSelect.C?hf=";
	
	// href 2 will contain the histogram files
	TString href3 = "&systestid=";
	// href 4 will contain the systest_id
	TString href5 = "&stdtestid=";
	// href6 will contain the stdtest_id
	TString href7 = "&histname=";
	// href8 will contain the name of the histogram file
	TString href9 = "\">";
	  
	// generating the URL
	
	// the histogram file
	TString href2 = row1->GetField(5);
	href1.Append(href2.Strip());
	href1.Append(href3.Strip());
	
	// the systest_id
	TString href4 = row1->GetField(1);
	href1.Append(href4.Strip());
	href1.Append(href5.Strip());

	// the standardtest_id
	TString href6 = row1->GetField(2);
	href1.Append(href6.Strip());
	href1.Append(href7.Strip());
		
	// the histogram file name not the path name
	TString href8 = row1->GetField(0);
	href1.Append(href8.Strip());
	href1.Append(href9.Strip());

	// Only the histogram_file field (field 5) is a URL so link displayed
	// only for that entry in the table
	if(i==5) {
	  ap.Puts(href1);
	  ap.Puts("<img src=""normal.gif"" width=""65"" height=""56"">");
	}
	else
	  ap.Printf(row1->GetField(i));
       	

	if(i==5) ap.Puts("</A>");
	ap.Puts("</TD>");


      }// end for
    
    // Put in the link for going to a page containing metadata
    ap.Puts("<TD>");
    TString meta = "<A HREF= \"MetadataDisplay.C?systestid=";

    // the systest_id
    TString meta1 = row1->GetField(1);
    TString meta2 = "\">";
    meta.Append(meta1.Strip());
    meta.Append(meta2.Strip());
    ap.Puts(meta);

    ap.Puts("Values");
    ap.Puts("</A>");	
    ap.Puts("</TD>");
    
    // Put in a link to display those histograms which were within 
    // and outside the threshold of KS tests
    ap.Puts("<TD>");
    ap.Puts("Histograms<br>");
    TString threshold = "<A HREF= \"HistWithinThreshold.C?systestid=";
    // the systest_id
    TString threshold1 = row1->GetField(1);
    TString threshold2 = "\">";
    threshold.Append(threshold1.Strip());
    threshold.Append(threshold2.Strip());
    ap.Puts(threshold);
    ap.Puts("Within Threshold<br>");
    ap.Puts("</A>");
    
    TString threshold = "<A HREF= \"HistOutsideThreshold.C?systestid=";
    // the systest_id
    TString threshold1 = row1->GetField(1);
    TString threshold2 = "\">";
    threshold.Append(threshold1.Strip());
    threshold.Append(threshold2.Strip());
    ap.Puts(threshold);
    ap.Puts("Outside Threshold");
    ap.Puts("</A>");

    ap.Puts("</TD>");
    ap.Puts("<TD>");
    int systestid_sum = std::atoi(row1->GetField(1));
    for(int l=0; l<num_sys; l++)
      if(hist_threshold[l].systest_id == systestid_sum) {
      	ap.Puts(hist_threshold[l].summary);
	break;
      }
    ap.Puts("</TD>");        
    ap.Puts("</TR>");
        
  
  }// end while
  while (res->Next());

  ap.Puts("<TABLE>");
  ap.Puts("<BR></form>");
  ap.Puts("</CENTER></BODY></HTML>");

 delete[] sql;

}// end of TestInfoVersion
