--[[----------------------------------------------------------------------------

WFTestMetrics.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2012 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

local WFPlatformInfo = require "WFPlatformInfo"
local WFDeviceInfo = require 'WFDeviceInfo'
local WFPlatformInfoImp
local WFFileUtils = require "WFFileUtils"

--============================================================================--
--[[!
	\class WFTestMetrics
	\brief This class provides a way to capture a
		metrics about hardware and software where tests are run.
]]--

local WFTestMetrics = {}

--------------------------------------------------------------------------------

function WFTestMetrics.populateTestMetrics( test, optName )
	
	if not WFPlatformInfoImp then
		WFPlatformInfoImp = require 'WFPlatformInfoImp'
	end

	local metrics = {}

	local fileName = test:testName()
	local className = test:luaClassName()
	
	metrics[ "wfp_file_name" ] = fileName
	metrics[ "wfp_class_name" ] = className
	metrics[ "wfp_extra_name" ] = optName
	metrics[ "wfp_test_name" ] = test:testName()
	metrics[ "product_name" ] = "WichitaFoundation"
	metrics[ "product_family" ] = "Revel"
	metrics[ "branch" ] = WFPlatformInfo.getBranchName()
	metrics[ "platform" ] = WFPlatformInfo.getAppDeviceClass()
	metrics[ "build_target" ] = WFPlatformInfo.getBuildTarget()
	metrics[ "changelist" ] = WFPlatformInfo.getBuild and WFPlatformInfo.getBuild()
	metrics[ "test_machine" ] = WFDeviceInfo.deviceName()
	metrics[ "model" ] = WFDeviceInfo.deviceModel()
	metrics[ "os" ] = WFPlatformInfo.getOS and WFPlatformInfo.getOS()
	metrics[ "cpu" ] = WFDeviceInfo.cpuType()
	metrics[ "os_version" ] = WFPlatformInfoImp.getOSVersion and WFPlatformInfoImp.getOSVersion()
	metrics[ "test" ] = "Performance"
	metrics[ "name" ] = WFDeviceInfo.userName()
	
	return metrics

end

--------------------------------------------------------------------------------

function WFTestMetrics.writeTestMetrics( metrics )

	local KSJsonEncoder = require 'KSJsonEncoder'
	local KSData = require 'KSData'
	local KSPathUtils = require 'KSPathUtils'
	
	local testMetrics = {}
	local homeDir
	local platform
	
	if WFPlatformInfo.isiOS then 
		homeDir = KSPathUtils.child( "/Users", WFDeviceInfo.userName() )
		platform = metrics[ "os" ]
	else 
		homeDir = KSPathUtils.getStandardFilePath( "home")
		platform = metrics[ "platform" ]
	end
	local metricsDir = KSPathUtils.child( KSPathUtils.child( homeDir, "WFPerformanceTestMetrics" ), platform )
	
	WFFileUtils.mkdir( metricsDir )
	
	testMetrics[ "wf_performance_test" ] = { metrics }
	local jsonData = KSJsonEncoder.prettyPrint( testMetrics )
	jsonData = KSData.make( jsonData )
	
	local fileName = metrics [ "wfp_test_name" ]
	fileName = string.format( "%s.%s", fileName, 'json' )
	if jsonData and metricsDir then
		KSData.saveToUniquelyNamedFile( jsonData, KSPathUtils.child( metricsDir, fileName ) )
	end
	
end

--------------------------------------------------------------------------------

return WFTestMetrics
