PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/indico/ext/livesync/chrome.py

https://github.com/davidmorrison/indico
Python | 267 lines | 215 code | 1 blank | 51 comment | 1 complexity | f4286a201c2e22d899fd888587855a11 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ##
  3. ##
  4. ## This file is part of CDS Indico.
  5. ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
  6. ##
  7. ## CDS Indico is free software; you can redistribute it and/or
  8. ## modify it under the terms of the GNU General Public License as
  9. ## published by the Free Software Foundation; either version 2 of the
  10. ## License, or (at your option) any later version.
  11. ##
  12. ## CDS Indico is distributed in the hope that it will be useful, but
  13. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ## General Public License for more details.
  16. ##
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
  19. ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20. """
  21. Contains definitions for the plugin's web interface
  22. """
  23. # system lib imports
  24. import os, datetime
  25. # 3rd party lib imports
  26. import zope.interface
  27. # plugin imports
  28. from indico.ext.livesync import SyncManager
  29. from indico.ext.livesync.struct import EmptyTrackException
  30. from indico.ext.livesync.handlers import AgentTypeInspector
  31. import indico.ext.livesync
  32. # indico extpoint imports
  33. from indico.core.extpoint import Component
  34. from indico.core.extpoint.plugins import IPluginSettingsContributor
  35. from indico.web.rh import RHHtdocs
  36. from indico.util.date_time import nowutc, int_timestamp
  37. # legacy indico imports
  38. from MaKaC.webinterface.wcomponents import WTemplated
  39. from MaKaC.webinterface.rh.admins import RHAdminBase
  40. from MaKaC.webinterface.urlHandlers import URLHandler
  41. from MaKaC.webinterface.pages.admins import WPAdminPlugins
  42. from MaKaC.webinterface import wcomponents
  43. class UHAdminLiveSyncManagement(URLHandler):
  44. """
  45. URL handler for livesync agent management
  46. """
  47. _relativeURL = "livesync/manage"
  48. class UHAdminLiveSyncStatus(URLHandler):
  49. """
  50. URL handler for livesync status
  51. """
  52. _relativeURL = "livesync/status"
  53. # Request Handlers
  54. class RHLiveSyncHtdocs(RHHtdocs):
  55. """
  56. Static file handler for LiveSync plugin
  57. """
  58. _url = r"^/livesync/(?P<filepath>.*)$"
  59. _local_path = os.path.join(indico.ext.livesync.__path__[0], 'htdocs')
  60. class RHAdminLiveSyncManagement(RHAdminBase):
  61. """
  62. LiveSync management page - request handler
  63. """
  64. _url = r'^/livesync/manage/?$'
  65. def _process(self):
  66. return WPLiveSyncAdmin(self, WPluginAgentManagement).display()
  67. class RHAdminLiveSyncStatus(RHAdminBase):
  68. """
  69. LiveSync status page - request handler
  70. """
  71. _url = r'^/livesync/status/?$'
  72. def _process(self):
  73. return WPLiveSyncAdmin(self, WPluginAgentStatus).display()
  74. # Plugin Settings
  75. class PluginSettingsContributor(Component):
  76. """
  77. Plugs to the IPluginSettingsContributor extension point, providing a "plugin
  78. settings" web interface
  79. """
  80. zope.interface.implements(IPluginSettingsContributor)
  81. def hasPluginSettings(self, obj, ptype, plugin):
  82. if ptype == 'livesync' and plugin == None:
  83. return True
  84. else:
  85. return False
  86. def getPluginSettingsHTML(self, obj, ptype, plugin):
  87. if ptype == 'livesync' and plugin == None:
  88. return WPluginSettings.forModule(indico.ext.livesync).getHTML()
  89. else:
  90. return None
  91. class WPluginSettings(WTemplated):
  92. def getVars(self):
  93. tplVars = WTemplated.getVars(self)
  94. tplVars['adminLiveSyncURL'] = UHAdminLiveSyncManagement.getURL()
  95. tplVars['statusLiveSyncURL'] = UHAdminLiveSyncStatus.getURL()
  96. return tplVars
  97. # Settings sub-sections
  98. class WPLiveSyncAdmin(WPAdminPlugins):
  99. def __init__(self, rh, templateClass):
  100. WPAdminPlugins.__init__(self, rh, 'livesync', '')
  101. self._templateClass = templateClass
  102. def getJSFiles(self):
  103. return WPAdminPlugins.getJSFiles(self) + \
  104. self._includeJSFile('livesync/js', 'livesync')
  105. def getCSSFiles(self):
  106. return WPAdminPlugins.getCSSFiles(self) + \
  107. ['livesync/livesync.css']
  108. def _getPageContent(self, params):
  109. return wcomponents.WTabControl(self._tabCtrl, self._getAW()).getHTML(
  110. self._templateClass.forModule(
  111. indico.ext.livesync).getHTML(params))
  112. class WPluginAgentManagement(WTemplated):
  113. def getVars(self):
  114. tplVars = WTemplated.getVars(self)
  115. smanager = SyncManager.getDBInstance()
  116. avtypes = AgentTypeInspector.getAvailableTypes()
  117. tplVars['syncManager'] = smanager
  118. tplVars['agents'] = smanager.getAllAgents()
  119. tplVars['availableTypes'] = avtypes.keys()
  120. tplVars['extraAgentOptions'] = dict((typeName, typeClass._extraOptions)
  121. for (typeName, typeClass) in
  122. avtypes.iteritems())
  123. tplVars['agentTableData'] = dict((agentId, agent.fossilize()) for \
  124. (agentId, agent) in \
  125. smanager.getAllAgents().iteritems())
  126. return tplVars
  127. class WPluginAgentStatus(WTemplated):
  128. NUM_CELLS = 10
  129. def _tsToDate(self, ts, granularity):
  130. return datetime.datetime.fromtimestamp(ts * granularity)
  131. def _calculateTrackData(self, smanager):
  132. """
  133. Builds a semi-graphical representation of the current agent status
  134. """
  135. # TODO: Reduce, split this!
  136. track = smanager.getTrack()
  137. try:
  138. first = int(track.oldestTS())
  139. last = int(track.mostRecentTS())
  140. except EmptyTrackException:
  141. return None, None
  142. self._agentMap = {}
  143. lastAgentTS = 0
  144. granularity = smanager.getGranularity()
  145. # create a map of agents, indexed by timestamp
  146. for aid in smanager.getAllAgents():
  147. ts = track.getPointerTimestamp(aid)
  148. if ts:
  149. ts = int(ts)
  150. self._agentMap.setdefault(ts, []).append(aid)
  151. lastAgentTS = max(ts, lastAgentTS)
  152. queue = []
  153. self._breakContinuity = False
  154. extra = self._sumElems = self._numBreakTS = 0
  155. self._agentsLeft = sorted(self._agentMap)
  156. self._agentsLeft.reverse()
  157. for ts, elems in track._container.iteritems():
  158. if int(ts) in [first, last] or ts in self._agentMap or \
  159. extra < self.NUM_CELLS:
  160. self._drawCell(queue, int(ts), elems,
  161. granularity)
  162. extra += 1
  163. else:
  164. self._sumElems += len(elems)
  165. self._numBreakTS += 1
  166. self._breakContinuity = True
  167. for ts in self._agentsLeft:
  168. self._drawCell(queue, int(ts), [],
  169. granularity)
  170. queue.reverse()
  171. # return ordered rows
  172. return queue, lastAgentTS
  173. def _drawCell(self, queue, ts, elems, granularity):
  174. if ts not in self._agentMap:
  175. for agentTS in self._agentsLeft[:]:
  176. if ts < agentTS:
  177. queue.append((agentTS,
  178. self._tsToDate(agentTS, granularity),
  179. 0, self._agentMap[agentTS]))
  180. self._agentsLeft.remove(agentTS)
  181. else:
  182. if ts in self._agentsLeft:
  183. self._agentsLeft.remove(ts)
  184. if self._breakContinuity:
  185. queue.append(('break', self._numBreakTS, self._sumElems, []))
  186. queue.append((ts, self._tsToDate(ts, granularity),
  187. len(elems), self._agentMap.get(ts)))
  188. self._breakContinuity = False
  189. self._sumElems = self._numBreakTS = 0
  190. return 0
  191. def getVars(self):
  192. tplVars = WTemplated.getVars(self)
  193. smanager = SyncManager.getDBInstance()
  194. tplVars['trackData'], tplVars['lastAgentTS'] = \
  195. self._calculateTrackData(smanager)
  196. tplVars['agents'] = smanager.getAllAgents()
  197. tplVars['currentTS'] = int_timestamp(nowutc())
  198. tplVars['granularity'] = smanager.getGranularity()
  199. return tplVars