#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquerymfile_LOADMVBRFAUSWMULTI.txt
# 	DESCRIPTION:	dquery definition for LOADMVBRFAUSWMULTI
#
# 
@group			SVCLMVBRFAUSWDATALOAD
@querytitle 	Alle noch nicht geladenen Träger-MVB-Auswertung_MeldungenClearingfaelle-Dateien laden
@description	lädt alle managed files mit Auswertung_MeldungenClearingfaelle mit Hilfe des Query LOADMVBRFAUSW, die noch nicht status="loaded" haben
@attributenames	sql_select:string:{{sql_selectoptions}}
@sql_select		SELECT MFILEID,MFILENAME,MFILEPATH,MFILELPATH,MFILEATTRIBS FROM CCMFILES WHERE MFILETYPE='dmvbrfausw' AND MFILEATTRIBS not like '%"loadstate":"loaded"%' AND MFILEATTRIBS not like '%"loadstate":"inhibited"%'
@sql_selectoptions	{"labeltext":"Auswahl-SQL-Statement","typedesc":"SQL-Statement für Abfrage in CCMFILES um die zu ladenden Dateien auszuwählen"}
@querytype		function
@function		seqtrans.seqtrans

~query.tsteps
#
# <<TSTEPNUM:0>>: 
#
if	(this.init_done)
goto	record_proper

#
# <<TSTEPNUM:1>>: initialize iterators
#
qexpression
	proc: {
		if (!this.init_done) {
			this.startstime = aux.nowstring();
			this.startmillis = Date.now();
			this.filesprocessed = 0;
			this.filestoprocess = this.inputresult.getRowCount();
			this.protocol = new Array();
			this.fresults = new Array();
			this.ppush([this.phead("started"),"TIME="+aux.nowsvtime(),"FILES_TO_LOAD="+this.filestoprocess,""]);
		}
	}

#
# <<TSTEPNUM:2>>: init done
#
qexpression
	this.init_done = true;

#
# <<TSTEPNUM:3>>: load one file
#
record_proper:
query.dataname	LOADMVBRFAUSW
query.mfileid	@@MFILEID@@
post_qexpression
	this.filesprocessed++;
	this.fresults.push(this.stepresult);
	this.ppush([this.phead("file_processed"),"MFILEID="+this.cdata.MFILEID,"MFILENAME="+this.cdata.MFILENAME,this.stepresult]);

#
# <<TSTEPNUM:4>>: check if this was the last record and skip final actions if not
#
if (!this.is_last_record)
goto record_done

#
# <<TSTEPNUM:5>>: finalize protocol, seqtrans will create a visible result from the protocol
#
qexpression
	this.finishstime = aux.nowstring().substring(0,14);
	this.endmillis = Date.now();
	this.elapsedmillis = this.endmillis - this.startmillis;
	this.elapsedseconds = this.elapsedmillis/1000;
	let totalrecs = 0;
	let totalmillis = 0;
	let cfr;
	if (this.fresults.length>0) {
		this.ppush(["Verarbeitungs-Statistik:","","",""]);
		this.ppush(["Träger/Zeitstempel","Anzahl verarbeiteter Datensätze","Durchlaufzeit","Datensätze/Sekunde"]);
	}
	for (let fi=0; fi<this.fresults.length; fi++) {
		cfr = this.fresults[fi];
		totalrecs += cfr.recsprocessed;
		totalmillis += cfr.elapsedmillis;
		this.ppush([cfr.traeger+"/"+cfr.ftimestamp,cfr.recsprocessed+" records",cfr.elapsedseconds+" seconds",cfr.recspersecond+" recspersecond"]);
	}
	this.overallrecspersecond = totalrecs/this.elapsedseconds;
	this.ppush(["TOTAL",totalrecs+" records",totalmillis/1000+" seconds",this.overallrecspersecond+" overall records per second"]);
	this.query.toptext = 	"<table><tr><td>Vorgang begonnen:       </td><td>"+this.startstime+"</td></tr>"+
							"       <tr><td>Vorgang beendet:        </td><td>"+this.finishstime+"</td></tr>"+
							"	    <tr><td>Durchlaufzeit(Sekunden):</td><td>"+this.elapsedseconds+"</td></tr>"+
							"       <tr><td>Datensätze/Sekunde:     </td><td>"+this.overallrecspersecond+"</td></tr>"+
							"</table>";
	this.query.result_filename = "LOADRESULT_"+this.finishstime;
	this.ppush([this.phead("finished"),
				{ rawhtml:	"<p>TIME=" + aux.nowsvtime() + "<br/>ELAPSED_SECONDS="+(this.elapsedmillis/1000)+"</br>" },
				"FILES_PROCESSED="+this.filesprocessed,
				""]);
	this.query.title = " "+this.filesprocessed+" Dateien mit Auswertung_MeldungenClearingfaelle geladen";

#
# <<TSTEPNUM:6>>: end of record processing
#
record_done:
end