no_product_app_user_state.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import csv
  2. import os
  3. import sys
  4. from base import init_env
  5. env = sys.argv[1]
  6. os.environ.setdefault('DJANGO_SETTINGS_MODULE', env)
  7. init_env(interactive=False)
  8. from apps.web.agent.models import Agent
  9. from apps.web.user.models import MyUser
  10. def get_ids(path, key):
  11. lis = list()
  12. with open(path) as f:
  13. reader = csv.reader(f)
  14. for index, row in enumerate(reader):
  15. if index == 0:
  16. num = row.index(key)
  17. else:
  18. lis.append(row[num])
  19. return lis
  20. def agent_state(agentIds):
  21. state_dict = dict()
  22. agentIds = set(agentIds)
  23. for agentId in agentIds:
  24. try:
  25. agent = Agent.objects.get(id=agentId)
  26. print "ok"
  27. except Exception as e:
  28. print "error"
  29. else:
  30. if agent.productName not in state_dict:
  31. state_dict[agent.productName] = 1
  32. else:
  33. state_dict[agent.productName] += 1
  34. return [{"name": key, "nums": value} for key, value in state_dict.items()]
  35. def user_state(userIds):
  36. state_lis1 = dict()
  37. state_lis2 = dict()
  38. for _id in userIds:
  39. try:
  40. user = MyUser.objects.get(id=_id)
  41. except Exception as e:
  42. pass
  43. else:
  44. addTime = str(user.dateTimeAdded)[:7]
  45. lastLogin = str(user.last_login)[:7]
  46. if addTime not in state_lis1:
  47. state_lis1[addTime] = 1
  48. else:
  49. state_lis1[addTime] += 1
  50. if lastLogin not in state_lis2:
  51. state_lis2[lastLogin] = 1
  52. else:
  53. state_lis2[lastLogin] += 1
  54. return [{"addTime": key, "nums": value} for key, value in state_lis1.items()], [{"lastLogin": key, "nums": value} for key, value in state_lis2.items()]
  55. # def user_state_2(userIds):
  56. # agent_map = {}
  57. # for _id in userIds:
  58. # try:
  59. # user = MyUser.objects.get(id=_id)
  60. # except Exception as e:
  61. # pass
  62. # else:
  63. # addTime = user.dateTimeAdded
  64. # agent = Agent.objects.get(id=user.agentId)
  65. # if user.agentId not in agent_map:
  66. # agent_map[user.agentId] = agent
  67. #
  68. # if addTime > datetime.datetime(year=2019, month=12, day=1):
  69. # data = {
  70. # "id": str(user.id),
  71. # "productName": agent_map.get(user.agentId).productName,
  72. # }
  73. def write_csv(**kwargs):
  74. name = kwargs.keys()[0]
  75. lis = kwargs.get(name)
  76. if lis:
  77. fieldNames = lis[0].keys()
  78. with open("{}.csv".format(name), "w", ) as csvFile:
  79. writer = csv.DictWriter(csvFile, fieldnames=fieldNames,lineterminator='\n')
  80. writer.writeheader()
  81. writer.writerows(lis)
  82. def main(path):
  83. agentIds = get_ids(path, "agentId")
  84. userIds = get_ids(path, "userId")
  85. agent_states = agent_state(agentIds)
  86. user_states1, user_states2 = user_state(userIds)
  87. write_csv(agent_states=agent_states)
  88. write_csv(user_states1=user_states1)
  89. write_csv(user_states2=user_states2)
  90. if __name__ == '__main__':
  91. path = "error_user.csv"
  92. main(path)