tmp_tv_avg_ratings_fatt0.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. """按月统计电视剧的收视情况
  4. province_input: odl.ad_television
  5. province_output: tmp.month_channel_stat
  6. area_input: odl.area_ad_television
  7. area_output: tmp.area_month_channel_stat
  8. """
  9. import sys
  10. from fty_util.common import Mysql, Util
  11. reload(sys)
  12. sys.setdefaultencoding('utf8')
  13. class channel_history_month_stat():
  14. def province(self):
  15. # 清空电视台数据统计表
  16. conn = Mysql.createOfflineConn()
  17. sql = """
  18. truncate table tmp.month_channel_stat
  19. """
  20. Mysql.execute(sql, conn=conn)
  21. sql = """
  22. select distinct month from tmp.ad_television_month order by month asc
  23. """
  24. rows = Mysql.getAll(sql, conn=conn)
  25. for row in rows:
  26. month = row['month']
  27. print month
  28. month_max_date = Util.get_max_date_of_month(month)
  29. # 统计电视台当月电视剧收视数据
  30. sql = """
  31. select adt.channel, adt.theater_attribute, adt.tv_name, adt.tv_id,
  32. min(adt.tv_date) as tv_date, sum(adt.audience_rating) as sum_value, count(adt.id) as count_value from odl.ad_television adt
  33. where adt.tv_date >= '%s' and adt.tv_date <= '%s' and adt.audience_rating > 0
  34. group by adt.channel, adt.theater_attribute, adt.tv_name, adt.tv_id
  35. """
  36. sql = sql % (month, month_max_date)
  37. rows_all = Mysql.getAll(sql, conn=conn)
  38. data_list = []
  39. sql_insert = """
  40. insert into tmp.month_channel_stat (channel, theater_attribute, tv_name, tv_id, tv_date, sum_value, count_value, year, month)
  41. values (%s, %s, %s, %s, %s, %s, %s, %s, %s)
  42. """
  43. year = month.year
  44. for row_all in rows_all:
  45. channel = row_all['channel']
  46. theater_attribute = row_all['theater_attribute']
  47. tv_name = row_all['tv_name']
  48. tv_id = row_all['tv_id']
  49. tv_date = row_all['tv_date']
  50. sum_value = row_all['sum_value']
  51. count_value = row_all['count_value']
  52. data_list.append((channel, theater_attribute, tv_name, tv_id, tv_date, sum_value, count_value, year, month))
  53. Mysql.insertMany(sql_insert, data_list, conn)
  54. Mysql.close(conn)
  55. def area(self):
  56. # 清空电视台数据统计表
  57. conn = Mysql.createOfflineConn()
  58. sql = """
  59. truncate table tmp.month_channel_stat
  60. """
  61. Mysql.execute(sql, conn=conn)
  62. sql = """
  63. select distinct month from tmp.ad_television_month order by month asc
  64. """
  65. rows = Mysql.getAll(sql, conn=conn)
  66. for row in rows:
  67. month = row['month']
  68. print month
  69. month_max_date = Util.get_max_date_of_month(month)
  70. # 统计电视台当月电视剧收视数据
  71. sql = """
  72. select adt.channel, adt.theater_attribute, adt.tv_name, adt.tv_id,
  73. min(adt.tv_date) as tv_date, sum(adt.audience_rating) as sum_value, count(adt.id) as count_value from odl.area_ad_television adt
  74. where adt.tv_date >= '%s' and adt.tv_date <= '%s' and adt.audience_rating > 0
  75. group by adt.channel, adt.theater_attribute, adt.tv_name, adt.tv_id
  76. """
  77. sql = sql % (month, month_max_date)
  78. rows_all = Mysql.getAll(sql, conn=conn)
  79. data_list = []
  80. sql_insert = """
  81. insert into tmp.area_month_channel_stat (channel, theater_attribute, tv_name, tv_id, tv_date, sum_value, count_value, year, month)
  82. values (%s, %s, %s, %s, %s, %s, %s, %s, %s)
  83. """
  84. year = month.year
  85. for row_all in rows_all:
  86. channel = row_all['channel']
  87. theater_attribute = row_all['theater_attribute']
  88. tv_name = row_all['tv_name']
  89. tv_id = row_all['tv_id']
  90. tv_date = row_all['tv_date']
  91. sum_value = row_all['sum_value']
  92. count_value = row_all['count_value']
  93. data_list.append((channel, theater_attribute, tv_name, tv_id, tv_date, sum_value, count_value, year, month))
  94. Mysql.insertMany(sql_insert, data_list, conn=conn)
  95. Mysql.close(conn)
  96. if __name__ == '__main__':
  97. if len(sys.argv) != 2:
  98. print '没有输入参数,退出'
  99. sys.exit(0)
  100. print 'method name is ' + sys.argv[1]
  101. obj = channel_history_month_stat()
  102. try:
  103. getattr(obj, sys.argv[1])()
  104. except Exception, e:
  105. print e