PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/CustomTags/com/sebtools/ServiceWatcher.cfc

http://github.com/sebtools/Neptune
ColdFusion CFScript | 258 lines | 211 code | 47 blank | 0 comment | 0 complexity | dd34e23b8312cfc376a4fdf516bf22a2 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. <cfcomponent displayname="Service Watcher" output="no">
  2. <cffunction name="init" access="public" returntype="any" output="no">
  3. <cfargument name="ServiceFactory" type="any" required="yes">
  4. <cfargument name="Observer" type="any" required="yes">
  5. <cfargument name="DataMgr" type="any" required="yes">
  6. <cfset Variables.DataMgr = Arguments.DataMgr>
  7. <cfset Variables.Observer = Arguments.Observer>
  8. <cfset Variables.ServiceFactory = Arguments.ServiceFactory>
  9. <cfset Variables.DataMgr.loadXml(getDbXml(),true,true)>
  10. <cfset Variables.datasource = Variables.DataMgr.getDatasource()>
  11. <cfset registerListener()>
  12. <cfreturn This>
  13. </cffunction>
  14. <cffunction name="getBigHitters" access="public" returntype="query" output="no" hint="I return information about slow loading services.">
  15. <cfargument name="SinceDate" type="date" required="false">
  16. <cfset var qServiceData = 0>
  17. <cfquery name="qServiceData" datasource="#Variables.datasource#">
  18. SELECT time_rank,ServiceName,AvgLoadTime,LastLoaded
  19. FROM (
  20. SELECT ServiceName,
  21. AVG(LoadTime) AS AvgLoadTime,
  22. count(*) AS NumLoads,
  23. RANK( ) OVER (ORDER BY Avg(LoadTime) DESC) time_rank,
  24. NTILE(10) OVER (ORDER BY Avg(LoadTime)) AS Tenths,
  25. Max(DateLoaded) AS LastLoaded
  26. FROM svcLoads
  27. WHERE 1 = 1
  28. <cfif StructKeyExists(Arguments,"SinceDate")>
  29. AND DateLoaded >= #CreateODBCDateTime(Arguments.SinceDate)#
  30. </cfif>
  31. GROUP BY ServiceID, ServiceName
  32. ) data
  33. WHERE Tenths >= 10
  34. AND AvgLoadTime >= (
  35. SELECT SUM(AvgLoadTime) / 10
  36. FROM (
  37. SELECT AVG(LoadTime) AS AvgLoadTime,
  38. NTILE(10) OVER (ORDER BY Avg(LoadTime)) AS Tenths
  39. FROM svcLoads
  40. WHERE 1 = 1
  41. <cfif StructKeyExists(Arguments,"SinceDate")>
  42. AND DateLoaded >= #CreateODBCDateTime(Arguments.SinceDate)#
  43. </cfif>
  44. GROUP BY ServiceID
  45. ) avgs
  46. WHERE Tenths >= 9
  47. )
  48. ORDER BY time_rank ASC
  49. </cfquery>
  50. <cfreturn qServiceData>
  51. </cffunction>
  52. <cffunction name="getLastLoadData" access="public" returntype="query" output="no" hint="I return information about the last time that the given service was loaded.">
  53. <cfargument name="ServiceName" type="string" required="true">
  54. <cfset var qServiceData = 0>
  55. <cfquery name="qServiceData" datasource="#Variables.datasource#">
  56. SELECT TOP 1 LoadID,ServiceID,ServiceName,DateLoaded,LoadTime,DateTracked
  57. FROM svcLoads
  58. WHERE ServiceID = <cfqueryparam value="#Val(getServiceID(Arguments.ServiceName))#" cfsqltype="CG_SQL_INTEGER">
  59. ORDER BY LoadID DESC
  60. </cfquery>
  61. <cfreturn qServiceData>
  62. </cffunction>
  63. <cffunction name="getLastLoadTime" access="public" returntype="numeric" output="no" hint="I return the time to load the given service last time it was loaded.">
  64. <cfargument name="ServiceName" type="string" required="true">
  65. <cfset var qServiceData = getLastLoadData(Arguments.ServiceName)>
  66. <cfreturn qServiceData.LoadTime>
  67. </cffunction>
  68. <cffunction name="getServicesReport" access="public" returntype="struct" output="no" hint="I return a report about services that have been loaded.">
  69. <cfset var qServiceData = 0>
  70. <cfset var sResult = StructNew()>
  71. <cfquery name="qServiceData" datasource="#Variables.datasource#">
  72. SELECT ServiceID,
  73. ServiceName,
  74. (
  75. SELECT AVG(LoadTime) AS AvgLoadTime
  76. FROM svcLoads
  77. WHERE ServiceID = svcServices.ServiceID
  78. ) AS AvgLoadTime,
  79. (
  80. SELECT Max(DateLoaded) AS LastLoadTime
  81. FROM svcLoads
  82. WHERE ServiceID = svcServices.ServiceID
  83. ) AS LastLoadTime,
  84. (
  85. SELECT Max(DateUpdated) AS LastUpdateDate
  86. FROM svcServicesStates
  87. WHERE ServiceID = svcServices.ServiceID
  88. ) AS LastUpdateDate,
  89. (
  90. SELECT TOP 1 FileHash
  91. FROM svcServicesStates
  92. WHERE ServiceID = svcServices.ServiceID
  93. ORDER BY DateUpdated DESC
  94. ) AS FileHash,
  95. (
  96. SELECT TOP 1 [Path]
  97. FROM svcServicesStates
  98. WHERE ServiceID = svcServices.ServiceID
  99. ORDER BY DateUpdated DESC
  100. ) AS FilePath
  101. FROM svcServices
  102. </cfquery>
  103. <cfoutput query="qServiceData" group="ServiceName">
  104. <cfset sResult[ServiceName] = {
  105. ServiceName=ServiceName,
  106. AvgLoadTime=AvgLoadTime,
  107. FileHash=FileHash,
  108. LastLoadTime=LastLoadTime,
  109. LastUpdateDate=LastUpdateDate,
  110. FilePath=FilePath
  111. }>
  112. </cfoutput>
  113. <cfreturn sResult>
  114. </cffunction>
  115. <cffunction name="logServiceLoad" access="public" returntype="numeric" output="no" hint="I log information about the loading of a service.">
  116. <cfargument name="ServiceName" type="string" required="true">
  117. <cfargument name="Service" type="any" required="true">
  118. <cfargument name="Meta" type="any" required="true">
  119. <cfset var sService = StructNew()>
  120. <cfset var sServiceState = StructNew()>
  121. <cfset var sServiceLoad = StructNew()>
  122. <cfset var sFileInfo = 0>
  123. <cfset sService["ServiceName"] = Arguments.ServiceName>
  124. <cfif StructKeyExists(Arguments.Meta,"DisplayName")>
  125. <cfset sService["DisplayName"] = Arguments.Meta.DisplayName>
  126. </cfif>
  127. <cfif StructKeyExists(Arguments.Meta,"FullName")>
  128. <cfset sService["FullName"] = Arguments.Meta.FullName>
  129. </cfif>
  130. <cfset sService["Path"] = Arguments.Meta.Path>
  131. <cfset Arguments.ServiceID = Variables.DataMgr.saveRecord(tablename="svcServices",data=sService,checkFields="ServiceName")>
  132. <cfset sFileInfo = GetFileInfo(sService["Path"])>
  133. <cfset sServiceState["ServiceID"] = Arguments.ServiceID>
  134. <cfset sServiceState["DateUpdated"] = sFileInfo.lastmodified>
  135. <cfset sServiceState["FullName"] = sService["FullName"]>
  136. <cfset sServiceState["Path"] = sService["Path"]>
  137. <cfif StructKeyExists(sService,"DisplayName")>
  138. <cfset sServiceState["DisplayName"] = sService["DisplayName"]>
  139. </cfif>
  140. <cfif NOT Variables.DataMgr.hasRecords(tablename='svcServicesStates',data=sServiceState)>
  141. <cfset sServiceState["FileHash"] = Hash(FileRead(sService["Path"]))>
  142. <cfset Variables.DataMgr.saveRecord(tablename="svcServicesStates",data=sServiceState)>
  143. </cfif>
  144. <cfscript>
  145. sServiceLoad["ServiceID"] = Arguments["ServiceID"];
  146. sServiceLoad["ServiceName"] = Arguments["ServiceName"];
  147. sServiceLoad["DateLoaded"] = Arguments.Meta.DateLoaded;
  148. sServiceLoad["LoadTime"] = Arguments.Meta.LoadTime;
  149. return Variables.DataMgr.insertRecord(tablename="svcLoads",data=sServiceLoad,onExists="insert");
  150. </cfscript>
  151. </cffunction>
  152. <cffunction name="getServiceLoadTime" access="public" returntype="string" output="no" hint="I return the average load time for the give service from the given state (or the average since the last change if no date is given).">
  153. <cfargument name="ServiceName" type="string" required="true">
  154. <cfargument name="SinceDate" type="date" required="false">
  155. <cfset var qServiceData = 0>
  156. <cfquery name="qServiceData" datasource="#Variables.datasource#">
  157. SELECT AVG(LoadTime) AS AvgLoadTime
  158. FROM svcLoads
  159. WHERE 1 = 1
  160. <cfif StructKeyExists(Arguments,"ServiceDate")>
  161. AND DateTracked >= #CreateODBCDate(Arguments.ServiceDate)#
  162. </cfif>
  163. </cfquery>
  164. <cfreturn qServiceData.AvgLoadTime>
  165. </cffunction>
  166. <cffunction name="getServiceID" access="public" returntype="numeric" output="no" hint="I return the ServiceID for the given ServiceName.">
  167. <cfargument name="ServiceName" type="string" required="true">
  168. <cfreturn Variables.DataMgr.getPKFromData(tablename="svcServices",fielddata=Arguments)>
  169. </cffunction>
  170. <cffunction name="registerListener" access="private" returntype="void" output="no" hint="I register a listener with Observer to listen for services being loaded.">
  171. <cfset Variables.Observer.registerListener(
  172. Listener = This,
  173. ListenerName = "ServiceWatcher",
  174. ListenerMethod = "logServiceLoad",
  175. EventName = "ServiceFactory:loadService"
  176. )>
  177. </cffunction>
  178. <!--- Ability to track changes to component as well (from date and by Hash?) --->
  179. <cffunction name="getDbXml" access="private" returntype="string" output="no" hint="I return the XML for the tables needed for ServiceWatcher to work.">
  180. <cfset var result = "">
  181. <cfsavecontent variable="result"><cfoutput>
  182. <tables>
  183. <table name="svcLoads">
  184. <field ColumnName="LoadID" CF_DataType="CF_SQL_BIGINT" PrimaryKey="true" Increment="true" />
  185. <field ColumnName="ServiceID" CF_DataType="CF_SQL_INTEGER" />
  186. <field ColumnName="ServiceName" CF_DataType="CF_SQL_VARCHAR" Length="50" />
  187. <field ColumnName="DateLoaded" CF_DataType="CF_SQL_DATE" />
  188. <field ColumnName="LoadTime" CF_DataType="CF_SQL_INTEGER" />
  189. <field ColumnName="DateTracked" CF_DataType="CF_SQL_DATE" Special="CreationDate" />
  190. </table>
  191. <table name="svcServices">
  192. <field ColumnName="ServiceID" CF_DataType="CF_SQL_INTEGER" PrimaryKey="true" Increment="true" />
  193. <field ColumnName="ServiceName" CF_DataType="CF_SQL_VARCHAR" Length="50" />
  194. <field ColumnName="FullName" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  195. <field ColumnName="Path" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  196. <field ColumnName="DisplayName" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  197. </table>
  198. <table name="svcServicesStates">
  199. <field ColumnName="ServiceStateID" CF_DataType="CF_SQL_BIGINT" PrimaryKey="true" Increment="true" />
  200. <field ColumnName="ServiceID" CF_DataType="CF_SQL_INTEGER" />
  201. <field ColumnName="DateUpdated" CF_DataType="CF_SQL_DATE" />
  202. <field ColumnName="FullName" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  203. <field ColumnName="Path" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  204. <field ColumnName="DisplayName" CF_DataType="CF_SQL_VARCHAR" Length="500" />
  205. <field ColumnName="FileHash" CF_DataType="CF_SQL_LONGVARCHAR" />
  206. </table>
  207. </tables>
  208. </cfoutput></cfsavecontent>
  209. <cfreturn result>
  210. </cffunction>
  211. </cfcomponent>