You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. import os
  2. import logging
  3. from datetime import datetime
  4. from tempfile import mkstemp
  5. import openerp.addons.web.http as openerpweb
  6. from . import core
  7. logger = logging.getLogger(__name__)
  8. class ProfilerController(openerpweb.Controller):
  9. _cp_path = '/web/profiler'
  10. player_state = 'profiler_player_clear'
  11. """Indicate the state(css class) of the player:
  12. * profiler_player_clear
  13. * profiler_player_enabled
  14. * profiler_player_disabled
  15. """
  16. @openerpweb.jsonrequest
  17. def enable(self, request):
  18. logger.info("Enabling")
  19. core.enabled = True
  20. ProfilerController.player_state = 'profiler_player_enabled'
  21. @openerpweb.jsonrequest
  22. def disable(self, request):
  23. logger.info("Disabling")
  24. core.enabled = False
  25. ProfilerController.player_state = 'profiler_player_disabled'
  26. @openerpweb.jsonrequest
  27. def clear(self, request):
  28. core.profile.clear()
  29. logger.info("Cleared stats")
  30. ProfilerController.player_state = 'profiler_player_clear'
  31. @openerpweb.httprequest
  32. def dump(self, request, token):
  33. """Provide the stats as a file download.
  34. Uses a temporary file, because apparently there's no API to
  35. dump stats in a stream directly.
  36. """
  37. handle, path = mkstemp(prefix='profiling')
  38. core.profile.dump_stats(path)
  39. stream = os.fdopen(handle)
  40. os.unlink(path) # TODO POSIX only ?
  41. stream.seek(0)
  42. filename = 'openerp_%s.stats' % datetime.now().isoformat()
  43. # can't close the stream even in a context manager: it'll be needed
  44. # after the return from this method, we'll let Python's GC do its job
  45. return request.make_response(
  46. stream,
  47. headers=[
  48. ('Content-Disposition',
  49. 'attachment; filename="%s"' % filename),
  50. ('Content-Type', 'application/octet-stream')
  51. ], cookies={'fileToken': token})
  52. @openerpweb.jsonrequest
  53. def initial_state(self, request):
  54. user = request.session.model('res.users')
  55. return {
  56. 'has_player_group': user.has_group('profiler.group_profiler_player'),
  57. 'player_state': ProfilerController.player_state,
  58. }