<cfset msStartAt=getTickCount()/>
<cfparam name="dt_finish" type="date" default=#createDateTime(year(Now()),month(Now()),1,0,0,0)#/>
<cfparam name="dt_start" type="date" default=#dateAdd('m',-1,dt_finish)#/>
<cfset hours=dateDiff('h',dt_start,dt_finish)/>
<cfset tenant_wz_index=val(mid(request.auth.wz,3,5))/><!--- cut off WZ --->

<cffunction name="snake2camel" 
	returntype="string" 
	output="false"
	hint="convert snake style name to camel style name">	
	<cfargument name="snake" type="string" required="true" />
	<cfreturn #reReplace(ARGUMENTS.snake,"_([a-z])","\u\1","ALL")#/>	
</cffunction>

<cffunction name="camel2snake" 
	returntype="string" 
	output="false"
	hint="convert camel style name to snake style name">	
	<cfargument name="camel" type="string" required="true" />
	<cfreturn #reReplace(ARGUMENTS.camel,"([A-Z])","_\l\1","ALL")#/>	
</cffunction>


<cffunction name="iso8601dtFormat" 
	returntype="string" 
	output="false"
	hint="formates date and time like this: 2023-12-18T15:41:42.516+0300">	
	<cfargument name="input" required="true" />
	<cfreturn "#dateFormat( input, "yyyy-mm-dd" )#T#timeFormat( input, "HH:mm:ss.lXX" )#"/>	
</cffunction>


<cffunction name="query2json" 
	output="false"
	hint="print query in json format as array of structures">	
	<cfargument name="qry" type="query" required="true"/><!--- column names should not contain commas --->
	<cfargument name="convertSnakeToCamel" type="boolean" default=true/>
	
	<cfset var columnsIn=#arguments.qry.columnList()#/>
	<cfset var columnsOut=""/>
	
	<cfif arguments.convertSnakeToCamel>
		<cfloop list=#columnsIn# item="col">
			<cfset columnsOut=listAppend(columnsOut,snake2camel(col))/>
		</cfloop>
	
		<cfset var outArray=arrayNew(1)/>
		//copy fields to structure, renaming fields, this preserves data type
		<cfloop query=#arguments.qry#>
			<cfset var structRow=structNew()/>
			<cfloop index="i" from="1" to=#listLen(columnsOut)#>
				<cfset structInsert(structRow,"#listGetAt(#columnsOut#,i)#",arguments.qry["#listGetAt(#columnsIn#,i)#"])/>
			</cfloop>
			<cfset arrayAppend(outArray,structRow)/>
		</cfloop>
		
		<cfreturn serializeJSON(outArray)/>
	<cfelse>
		<cfreturn serializeJSON(arguments.qry,"struct")/>
	</cfif>
</cffunction>


<cfmodule template="mod/payg.cfm"
	dt_finish=#dt_finish#
	dt_start=#dt_start#
	tenant_wz_index=#tenant_wz_index#
	output="report"
	debug=#isDefined("DEBUG")#
/> 
<cfset out={
	"appVersion"=#request.APP_VERSION#,
	"hours"=#hours#,
	"dtStart="=#iso8601dtFormat(dt_start)#,
	"dtFinish"=#iso8601dtFormat(dt_finish)#,
	"vcdComputingLoadedAt"="#iso8601dtFormat(report.qComputingAge.dt_load)#",
	"vcdStorageLoadedAt"="#iso8601dtFormat(report.qStorageAge.dt_load)#",
	"s3StorageLoadedAt"="#iso8601dtFormat(report.qComputingAge.dt_load)#",
	"s3OpsTrfLoadedAt"="#iso8601dtFormat(report.qS3OpsTrfAge.dt_load)#",	
	"records"=#report.qCharge.recordCount#,
	"queryDurationMs"=#(getTickCount() - msStartAt)#,
	"wz"=#request.auth.wz#,
	"login"=#request.auth.login#,
	"report"=#report.qCharge.reduce((result, row) => {
		result.append(row);
		return result;
	}, [])#
}/> 

<cfcontent
    type="application/json"
    /><cfoutput>#serializeJSON(out)#</cfoutput>
	
	
