build_ad_aggregates.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import datetime
  4. import click
  5. from script.base import init_env
  6. init_env(interactive=True)
  7. import simplejson as json
  8. import redis
  9. from apps.web.agent.models import Agent
  10. from apps.web.dealer.models import Dealer
  11. from apps.web.ad.models import AdRecord, Advertisement
  12. from apps.web.core.db import Query
  13. from apps.web.constant import Const
  14. redis_client = redis.Redis(host='121.41.75.233', port=6379, db=0)
  15. class NotSupported(Exception): pass
  16. @click.group()
  17. def cli():
  18. click.echo(u'build ad aggregate')
  19. def _build_query(category, start_date, end_date):
  20. if not category or category not in ('ad', 'dealer', 'agent', 'all'): raise NotSupported('only support ad or dealer or agent or all')
  21. click.echo('building query for %s' % (category,))
  22. pre_records = []
  23. rsd_manager_id = '5abc5f5c4864d0265c654cb0'
  24. if category == 'dealer':
  25. agents = [ str(_.id) for _ in Agent.objects(managerId=rsd_manager_id)]
  26. pre_records = [ {'dealerId': str(_.id), 'agentId': str(_.agentId)} for _ in Dealer.objects(agentId__in=agents) ]
  27. elif category == 'ad':
  28. pre_records = [ {'adId': str(_.adId)} for _ in Advertisement.objects(managerId=rsd_manager_id) ]
  29. elif category == 'agent':
  30. pre_records = [ {'agentId' :str(_.id)} for _ in Agent.objects(managerId=rsd_manager_id)]
  31. elif category == 'all':
  32. agents = [str(_.id) for _ in Agent.objects(managerId=rsd_manager_id)]
  33. pre_records = [{'dealerId': str(_.id), 'agentId': str(_.agentId)} for _ in Dealer.objects(agentId__in=agents)] \
  34. + [{'adId': str(_.adId)} for _ in Advertisement.objects(managerId=rsd_manager_id)] \
  35. + [{'agentId': str(_.id)} for _ in Agent.objects(managerId=rsd_manager_id)]
  36. l = []
  37. for _ in pre_records:
  38. attrs = _.copy()
  39. startTime = datetime.datetime.strptime(start_date, Const.DATE_FMT).replace(hour=0, minute=0, second=0, microsecond=0)
  40. endTime = datetime.datetime.strptime(end_date, Const.DATE_FMT).replace(hour=23, minute=59, second=59, microsecond=999999)
  41. attrs['dateTimeAdded__gte'] = startTime
  42. attrs['dateTimeAdded__lte'] = endTime
  43. _['startTime'] = start_date
  44. _['endTime'] = end_date
  45. l.append(Query(raw=_, attrs=attrs))
  46. click.echo('%d queries built' % (len(l),))
  47. click.echo('\n'.join([_.to_string() for _ in l]))
  48. return l
  49. @cli.command()
  50. @click.option('--category', '-c', prompt=u'为广告生成还是为经销商生成(ad, dealer, agent)')
  51. @click.option('--start_date', '-s', prompt=u'起始日期')
  52. @click.option('--end_date', '-e', prompt=u'终止日期')
  53. def build_query(category, start_date, end_date):
  54. _build_query(category, start_date, end_date)
  55. @cli.command()
  56. @click.option('--category', '-c', prompt=u'为广告生成还是为经销商生成(ad, dealer, agent)')
  57. @click.option('--start_date', '-s', prompt=u'起始日期')
  58. @click.option('--end_date', '-e', prompt=u'终止日期')
  59. def build_reports(category, start_date, end_date):
  60. """
  61. 为了加快广告效果统计的展现,考虑加入增量报表
  62. 考虑加入的key
  63. (managerId,), (managerId, agentId), (managerId, agentId, dealerId)...
  64. :return:
  65. """
  66. queries = _build_query(category, start_date, end_date)
  67. for q in queries:
  68. objects = AdRecord.objects(converted=True, **q.attrs).timeout(False)
  69. _ = [_.to_dict_in_cn() for _ in objects]
  70. print 'processing', q.to_string()
  71. redis_client.setnx(q.hashed, json.dumps(_))
  72. del objects
  73. del _
  74. if __name__ == '__main__':
  75. cli()