#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquermfile_TDRESA.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "TDRESA"

#
@querytitle				Test-Sequence mit DCRESDATA
@querydescription		Testet die Sequenz-Funktion mit einer Abfolge aus DCRESDATA-Queries.
						die eigentliche Abarbeitung der sequence ist im Query "seq2trans", welches als chain_query angestoßen wird.
@group					TEST
@querytype				function
@function				seqtrans.seqtrans
@sequence				TRUTTAB,GETLASTPLOGCRETTAB,CRETTAB,DCRESDATA,GETTABRECNUM,LOGCRETTAB
@title					Test-Sequenz-Ergebnis von TDRESA

~query.tsteps

#
# <<TSTEPNUM:0>>: TRUTTAB
#
pre_qexpression
	this.ppfx = this.fname+"("+this.query.dataname+")";
	ppush(this,this.ppfx+".START.SEQUENCE="+this.query.sequence);
sql				DROP TABLE IF EXISTS CCTTAB
result_varname	dropres
post_qexpression
	ppush(this,this.ppfx+".TRUTTAB.RESULT: "+aux.objTxt(this.dropres.resultobject));

#
# <<TSTEPNUM:1>>: GETLASTPLOGCRETTAB
#
sql
	select LTIME,LCLASS,LCODE,LDESC,LUNIT,LVALUE from CCPLOG 
	 where	LCLASS='CPERF' and LCODE='TDRESA' 
			and LTIME=(select MAX(LTIME) from CCPLOG WHERE LCLASS='CPERF' and LCODE='TDRESA')
result_varname	lpres
post_qexpression
	this.last_timing = {};
	let lt = this.last_timing;
	if (this.lpres.getRowCount()>0) {
		lt.desc = this.lpres.getCellValue(0,3);
		lt.unit = this.lpres.getCellValue(0,4);
		lt.count = this.lpres.getCellValue(0,5);
		if (lt.desc.charAt(0)=='{') {
			let flag = 0;
			lt.descobj = JSON.parse(lt.desc);
			if (lt.descobj.RECORD_COUNT) {
				lt.records_processed = lt.descobj.RECORD_COUNT;
				flag += 1;
			} else {
				lt.records_processed = 'NO_RECORDS_COUNT_IN_CCPLOG_DESC';
			} 
			if (lt.descobj.ACTION) {
				lt.action = lt.descobj.ACTION;
				flag += 1;
			} else {
				lt.action = 'NO_ACTION_IN_CCPLOG_DESC';
			}
			if (flag>=2) {
				delete lt.desc;
				delete lt.descobj;
			}
		} else {
			lt.records_processed = 'LAST_CCPLOG_DESC_NOT_JSON';
			lt.action = 'LAST_CCPLOG_DESC_NOT_JSON';
		}
	} else {
		lt.desc = 'No data got for last TDRESA in CCPLOG';
	}
	ppush(this,this.ppfx+".GETLASTPLOGCRETTAB.LAST_TIMING: "+aux.objTxt(this.last_timing));

#
# <<TSTEPNUM:2>>: CRETTAB
#	for test purposes, a long query is constructed by joining CCNUM repeatedly against itself
#
pre_qexpression
	if (!this.query.options) {
		this.query.options = {};
	}
	if (!this.query.options.stat_init) {
		this.query.options.stat_init = {};
	}
	if (!this.query.options.stat_init.last_timing) {
		this.query.options.stat_init.last_timing = {};
	}
	let lt = this.last_timing;
	if (lt) {
		aux.copyObject(this.query.options.stat_init.last_timing,lt);
	} else {
		this.query.options.stat_init.last_timing.desc="NO_LAST_TIMING_DETECTED";
	}
	ppush(this,this.ppfx+".CRETTAB.STAT_INIT: "+aux.objTxt(this.query.options.stat_init));
	this.cretab_starttime = new Date();
sql
	CREATE TABLE CCTTAB as
	select num.NUM, num2.NUM+0 as NUM2, num3.NUM+0 as NUM3, num4.NUM+0 as NUM4
	  from CCNUM num
	    left outer join CCNUM num2 on 1=1
	    left outer join CCNUM num3 on num3.NUM=((num.NUM*100+num2.NUM) MOD 99)
	    left outer join CCNUM num4 on num4.NUM=((num.NUM*100+num2.NUM) MOD 97)
	order by NUM,NUM2,NUM3,NUM4 asc
result_varname	creres
post_qexpression
	this.cretab_endtime = new Date();
	this.timing = {};
	this.timing.count = Math.abs(this.cretab_starttime - this.cretab_endtime);
	this.timing.unit = "ms";
	this.timing.resultobject = this.creres.resultobject;
	ppush(this,this.ppfx+".CRETTAB.TIMING="+aux.objTxt(this.timing));

#
# <<TSTEPNUM:3>>: DCRESDATA
#
pre_qexpression
	this.drd_starttime = new Date();
query.dataname		DCRESDATA
result_varname		dcrdres
post_qexpression
	this.drd_endtime = new Date();
	ppush(this,this.ppfx+".DCRESDATA.RUNTIME="+Math.abs(this.drd_endtime-this.drd_starttime)+"ms");
	this.dcrdres.title = this.ppfx+".DCRESDATA.RESULT";
	ppush(this,{junktype: "result", result: this.dcrdres });

#
# <<TSTEPNUM:4>>: GETTABRECNUM
#
sql				SELECT COUNT(*) from CCTTAB
result_varname	rnres
post_qexpression	
	this.recnum = this.rnres.getCellValue(0,0);
	ppush(this,this.ppfx+".GETTABRECNUM.RECNUM="+this.recnum);
	
#
# <<TSTEPNUM:5>>: LOGCRETTAB
#
pre_qexpression
	this.cdata.ltime = aux.noweutime();
	this.cdata.ldesc = "{\"ACTION\":\"CRETTAB\",\"RECORD_COUNT\":\""+this.recnum+"\"}";
	this.cdata.lunit = this.timing.unit;
	this.cdata.lvalue = this.timing.count;
	ppush(this,this.ppfx+".LOGCRETTAB.LTIME="+this.cdata.ltime+".LDESC="+this.cdata.ldesc+".LUNIT="+this.cdata.lunit+".LVALUE="+this.cdata.lvalue);	
sql				INSERT INTO CCPLOG(LTIME,LCLASS,LCODE,LDESC,LUNIT,LVALUE) values(::ltime::,'CPERF','TDRESA',::ldesc::,::lunit::,::lvalue::)
named_params	ltime,ldesc,lunit,lvalue
result_varname	crtres
	ppush(this,this.ppfx+".LOGCRETTAB.RESULT: "+aux.objTxt(this.crtres));