123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import csv
- import os
- import sys
- from base import init_env
- env = sys.argv[1]
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', env)
- init_env(interactive=False)
- from apps.web.agent.models import Agent
- from apps.web.user.models import MyUser
- def get_ids(path, key):
- lis = list()
- with open(path) as f:
- reader = csv.reader(f)
- for index, row in enumerate(reader):
- if index == 0:
- num = row.index(key)
- else:
- lis.append(row[num])
- return lis
- def agent_state(agentIds):
- state_dict = dict()
- agentIds = set(agentIds)
- for agentId in agentIds:
- try:
- agent = Agent.objects.get(id=agentId)
- print "ok"
- except Exception as e:
- print "error"
- else:
- if agent.productName not in state_dict:
- state_dict[agent.productName] = 1
- else:
- state_dict[agent.productName] += 1
- return [{"name": key, "nums": value} for key, value in state_dict.items()]
- def user_state(userIds):
- state_lis1 = dict()
- state_lis2 = dict()
- for _id in userIds:
- try:
- user = MyUser.objects.get(id=_id)
- except Exception as e:
- pass
- else:
- addTime = str(user.dateTimeAdded)[:7]
- lastLogin = str(user.last_login)[:7]
- if addTime not in state_lis1:
- state_lis1[addTime] = 1
- else:
- state_lis1[addTime] += 1
- if lastLogin not in state_lis2:
- state_lis2[lastLogin] = 1
- else:
- state_lis2[lastLogin] += 1
- return [{"addTime": key, "nums": value} for key, value in state_lis1.items()], [{"lastLogin": key, "nums": value} for key, value in state_lis2.items()]
- # def user_state_2(userIds):
- # agent_map = {}
- # for _id in userIds:
- # try:
- # user = MyUser.objects.get(id=_id)
- # except Exception as e:
- # pass
- # else:
- # addTime = user.dateTimeAdded
- # agent = Agent.objects.get(id=user.agentId)
- # if user.agentId not in agent_map:
- # agent_map[user.agentId] = agent
- #
- # if addTime > datetime.datetime(year=2019, month=12, day=1):
- # data = {
- # "id": str(user.id),
- # "productName": agent_map.get(user.agentId).productName,
- # }
- def write_csv(**kwargs):
- name = kwargs.keys()[0]
- lis = kwargs.get(name)
- if lis:
- fieldNames = lis[0].keys()
- with open("{}.csv".format(name), "w", ) as csvFile:
- writer = csv.DictWriter(csvFile, fieldnames=fieldNames,lineterminator='\n')
- writer.writeheader()
- writer.writerows(lis)
- def main(path):
- agentIds = get_ids(path, "agentId")
- userIds = get_ids(path, "userId")
- agent_states = agent_state(agentIds)
- user_states1, user_states2 = user_state(userIds)
- write_csv(agent_states=agent_states)
- write_csv(user_states1=user_states1)
- write_csv(user_states2=user_states2)
- if __name__ == '__main__':
- path = "error_user.csv"
- main(path)
|