random_result.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import random, hashlib, datetime
  2. from sqlalchemy.sql.expression import insert
  3. from terroroftinytown.tracker.bootstrap import Bootstrap
  4. from terroroftinytown.tracker.database import Database
  5. from terroroftinytown.tracker.model import new_session, Result, Project
  6. class MockProject(Bootstrap):
  7. def __init__(self, delete_everything=False):
  8. super().__init__()
  9. self.delete_everything = delete_everything
  10. def setup_database(self):
  11. self.database = Database(
  12. path=self.config['database']['path'],
  13. delete_everything=self.delete_everything
  14. )
  15. def start(self, args=None):
  16. super().start(args=args)
  17. self.generate_mock()
  18. def setup_args(self):
  19. super().setup_args()
  20. self.arg_parser.add_argument('--count', help='Number of projects to generate', default=1, type=int)
  21. def generate_mock(self):
  22. with new_session() as session:
  23. for project_num in range(1, self.args.count + 1):
  24. project_id = 'test_{}'.format(project_num)
  25. project = Project(name=project_id)
  26. if project_num == 2:
  27. project.url_template = 'http://example.com/{shortcode}/slash/'
  28. print('Running insertion')
  29. session.add(project)
  30. class MockResult(Bootstrap):
  31. def start(self, args=None):
  32. super().start(args=args)
  33. self.generate_mock()
  34. def setup_args(self):
  35. super().setup_args()
  36. self.arg_parser.add_argument('--count', help='Number of items to generate', default=int(1E6), type=int)
  37. self.arg_parser.add_argument('--projects', help='Number of projects to generate', default=1, type=int)
  38. def generate_mock(self):
  39. with new_session() as session:
  40. items = []
  41. for i in range(self.args.count):
  42. if i % 100 == 0:
  43. print(i)
  44. if self.args.projects == 1:
  45. project_id = 'test'
  46. else:
  47. project_id = 'test_{}'.format(random.randint(1, self.args.projects))
  48. items.append({
  49. 'project_id': project_id,
  50. 'shortcode': self.generate_shortcode(),
  51. 'url': self.generate_url(),
  52. 'encoding': 'ascii',
  53. 'datetime': datetime.datetime.utcnow()
  54. })
  55. print('Running insertion')
  56. session.execute(insert(Result), items)
  57. def generate_shortcode(self):
  58. # todo: non duplicated
  59. return hashlib.md5(str(random.random()).encode('ascii')).hexdigest()[:random.randrange(1, 9)]
  60. def generate_url(self):
  61. return 'http://' + self.generate_shortcode() + '.com/' + self.generate_shortcode()
  62. if __name__ == '__main__':
  63. MockResult().start()