/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#include "base/polygraph.h"

#include "base/StatPhaseRec.h"
#include "base/OLog.h"
#include "base/ILog.h"


StatPhaseRec::StatPhaseRec(): theConnPipelineDepths(0,100) {
}

void StatPhaseRec::repAll(TmSzHistStat &all) const {
	TmSzHistStat imsAll;
	imsAll += theImsXacts.hits();
	imsAll += theImsXacts.misses();

	TmSzHistStat basicAll;
	basicAll += theBasicXacts.hits();
	basicAll += theBasicXacts.misses();

	all += basicAll;
	all += theRediredReqXacts;
	all += theRepToRedirXacts;
	all += imsAll;
	all += theReloadXacts;
	all += theHeadXacts;
	all += thePostXacts;
	all += thePutXacts;
}

OLog &StatPhaseRec::store(OLog &log) const {
	StatIntvlRec::store(log);
	return log 
		<< theName 
		<< theGoal
		<< theContType
		<< theConnClose
		<< theSockRdSzH << theSockWrSzH
		<< theBasicXacts << theRediredReqXacts << theRepToRedirXacts 
		<< theImsXacts << theReloadXacts
		<< theHeadXacts << thePostXacts << thePutXacts 
		<< theIcpXacts
		<< theOidGen
		<< theErrors
		<< theConnPipelineDepths
		<< thePageHist
		;
}

ILog &StatPhaseRec::load(ILog &log) {
	StatIntvlRec::load(log);
	return log
		>> theName
		>> theGoal
		>> theContType
		>> theConnClose
		>> theSockRdSzH >> theSockWrSzH
		>> theBasicXacts >> theRediredReqXacts >> theRepToRedirXacts 
		>> theImsXacts >> theReloadXacts
		>> theHeadXacts >> thePostXacts >> thePutXacts 
		>> theIcpXacts
		>> theOidGen
		>> theErrors
		>> theConnPipelineDepths
		>> thePageHist
		;
}

void StatPhaseRec::concat(const StatPhaseRec &r) {
	if (theName && r.theName && theName != r.theName)
		theName += "|" + r.theName;
	StatIntvlRec::concat(r);
	theGoal.concat(r.theGoal);
	join(r);
}

void StatPhaseRec::merge(const StatPhaseRec &r) {
	if (theName && r.theName && theName != r.theName)
		theName += "&" + r.theName;
	StatIntvlRec::merge(r);
	theGoal.merge(r.theGoal);
	join(r);
}

void StatPhaseRec::join(const StatPhaseRec &r) {
	if (!theName)
		theName = r.theName;

	theContType.add(r.theContType);
	theConnClose.add(r.theConnClose);
	theSockRdSzH.add(r.theSockRdSzH);
	theSockWrSzH.add(r.theSockWrSzH);
	theBasicXacts += r.theBasicXacts;
	theRediredReqXacts += r.theRediredReqXacts;
	theRepToRedirXacts += r.theRepToRedirXacts;
	theImsXacts += r.theImsXacts;
	theReloadXacts += r.theReloadXacts;
	theHeadXacts += r.theHeadXacts;
	thePostXacts += r.thePostXacts;
	thePutXacts += r.thePutXacts;
	theConnPipelineDepths += r.theConnPipelineDepths;
	thePageHist += r.thePageHist;
	theIcpXacts  += r.theIcpXacts;
	theOidGen += r.theOidGen;
	theErrors.add(r.theErrors);
}

ostream &StatPhaseRec::print(ostream &os, const String &pfx) const {
	os << "name:\t " << theName << endl;

	StatIntvlRec::print(os, pfx);

	theGoal.print(os, pfx + "goal.");

	TmSzHistStat imsAll;
	imsAll += theImsXacts.hits();
	imsAll += theImsXacts.misses();

	TmSzHistStat basicAll;
	basicAll += theBasicXacts.hits();
	basicAll += theBasicXacts.misses();

	TmSzHistStat all;
	this->repAll(all);

	all.print(os, pfx + "rep.");
	basicAll.print(os, pfx + "basic.");
	imsAll.print(os, pfx + "ims.");
	theBasicXacts.print(os, "hit", "miss", pfx);
	theRediredReqXacts.print(os, pfx + "redired_req.");
	theRepToRedirXacts.print(os, pfx + "rep_to_redir.");
	theImsXacts.print(os, "sc200", "sc304", pfx + "ims.");
	theReloadXacts.print(os, pfx + "reload.");
	theHeadXacts.print(os, pfx + "head.");
	thePostXacts.print(os, pfx + "post.");
	thePutXacts.print(os, pfx + "put.");
	theConnPipelineDepths.print(os, pfx + "conn.pipeline.depth.");
	thePageHist.print(os, pfx + "page.");

	theIcpXacts.print(os, "hit", "miss",  pfx + "icp.");

	theContType.print(os, pfx + "cont_type.");
	theConnClose.print(os, pfx + "conn_close.");
	theSockRdSzH.print(os, pfx + "so_read.size.");
	theSockWrSzH.print(os, pfx + "so_write.size.");

	theOidGen.print(os, pfx + "oid_gen.");
	theErrors.print(os, pfx + "errors.");

	return os;
}


syntax highlighted by Code2HTML, v. 0.9.1