#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_SYNCDRESA.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "SYNCDRESA"
#
@querytitle			Auswerte-Tabelle für RZ-Verbrauchsdaten erneuern
@querydescription	synchronisiert CCDCRESA aus dem View CCVDCRESA
@group				DCRES
@querytype			function
@function			seqtrans.seqtrans
@sequence			{"name":"SYNCDRESA", "sequence":"TRUDRESA,GETLASTPLOGDRESA,CREDRESA,DCRESDATA,GETDRESARECNUM,LOGCREDRESA"}

~query.tsteps

#
# <<TSTEPNUM:0>>:	TRUDRESA, start protocol, drop table CCDRESA
#
pre_qexpression
	this.ppfx = this.fname+"("+this.query.dataname+")";
	this.query.sequence = aux.txtObjDecode(this.query.sequence);
	ppush(this,[this.ppfx+".START","SEQUENCE: "+aux.objTxt(this.query.sequence),"",""]);
sql				DROP TABLE IF EXISTS CCDCRESA
result_varname	trures
post_qexpression
	
	

#
# <<TSTEPNUM:1>>:	GETLASTPLOGDRESA
#
sql
	select LTIME,LCLASS,LCODE,LDESC,LUNIT,LVALUE from CCPLOG 
	 where LCLASS='CPERF' and LCODE='SYNCDRESA' 
       and LTIME=(select MAX(LTIME) from CCPLOG WHERE LCLASS='CPERF' and LCODE='SYNCDRESA')
result_varname	lpres
post_qexpression
	this.last_timing = {};
	let lt = this.last_timing;
	if (this.lpres.getRowCount()>0) {
		let flag = 0;
		lt.desc = this.lpres.getCellValue(0,3);
		lt.unit = this.lpres.getCellValue(0,4);
		lt.count = this.lpres.getCellValue(0,5);
		lt.descobj = aux.txtObjDecode(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.desc = 'No data got for last SYNCDRESA in CCPLOG';
	}
	ppush(this,[this.ppfx+".GETLASTPLOGDRESA",(lt.desc?(".DESC=\""+lt.desc+"\""):""),"LAST_TIMING: "+aux.objTxt(this.last_timing),""]);
	
#
# <<TSTEPNUM:2>>: 	CREDRESA, Tabelle CCDCRESA für RZ-Ressourcen-Verbrauch neu anlegen
#
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+".CREDRESA","STAT_INIT: "+aux.objTxt(this.query.options.stat_init),"",""]);
	this.cretab_starttime = new Date();
sql
	CREATE TABLE CCDCRESA as SELECT * FROM CCVDCRESA
result_varname	creres
post_qexpression
	this.cretab_endtime = new Date();
	this.ctiming = {};
	this.ctiming.count = Math.abs(this.cretab_endtime - this.cretab_starttime);
	this.ctiming.unit = "ms";
	this.ctiming.resultobject = this.creres.resultobject;
	ppush(this,[this.ppfx+".CRETTAB","TIMING="+aux.objTxt(this.ctiming),"",""]);

#
# <<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 });

#
# 4: GETDRESARECNUM
#
sql				SELECT COUNT(*) FROM CCDCRESA
result_varname	rnres
post_qexpression	
	this.crerecnum = this.rnres.getCellValue(0,0);
	ppush(this,[this.ppfx+".GETTABRECNUM","RECNUM="+this.crerecnum,"",""]);

#
# <<TSTEPNUM:5>>: LOGCREDRESA
#
pre_qexpression
	this.cdata.ltime = aux.noweutime();
	this.cdata.ldesc = "{\"ACTION\":\"CREDRESA\",\"RECORD_COUNT\":\""+this.crerecnum+"\"}";
	this.cdata.lunit = this.ctiming.unit;
	this.cdata.lvalue = this.ctiming.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','SYNCDRESA',::ldesc::,::lunit::,::lvalue::)
named_params	ltime,ldesc,lunit,lvalue
result_varname	crtres
	ppush(this,[this.ppfx+".LOGCRETTAB","RESULT: "+aux.objTxt(this.crtres),"",""]);
