conversion_runner.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Licensed to Cloudera, Inc. under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. Cloudera, Inc. licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import time
  17. import logging
  18. from django.contrib.auth.models import User
  19. from hue_converters import DocumentConverterHueScripts
  20. LOG = logging.getLogger(__name__)
  21. class DocumentConversionRunner(object):
  22. """
  23. Given a user, converts any existing Document objects to Document2 objects
  24. """
  25. def __init__(self, usernames, allowdupes=False, startqueryname=None, startuser=None):
  26. self.usernames = usernames
  27. self.allowdupes = allowdupes
  28. self.startqueryname = startqueryname
  29. self.startuser = startuser
  30. def runconversions(self):
  31. if not self.usernames:
  32. users = User.objects.all()
  33. else:
  34. userlist = self.usernames.split(",")
  35. users = User.objects.filter(username__in = userlist)
  36. if self.startqueryname or self.startuser:
  37. processdocs = False
  38. else:
  39. processdocs = True
  40. LOG.info("Converting docs for %s users" % users.count())
  41. for user in users:
  42. LOG.info("Converting docs for user: %s" % user.username)
  43. if user.username == self.startuser:
  44. processdocs = True
  45. start = time.time()
  46. try:
  47. converter = DocumentConverterHueScripts(user, allowdupes = self.allowdupes, startqueryname = self.startqueryname, startuser = self.startuser, processdocs = processdocs)
  48. processdocs = converter.convertfailed()
  49. except:
  50. LOG.warn("Conversions failed for user: %s" % user.username)
  51. end = time.time()
  52. elapsed = (end - start) / 60
  53. LOG.info("Finished user: %s : elapsed time: %s" % (user.username, elapsed))