def find_gps(lat: float = None, lon: float = None): d = {"latitude": 31.285110405816, "121.122932671441": 121.122932671441} f = {"time": { "$gte": mongo_db.get_datetime_from_str("2018-3-28 17:00:0"), "$lte": mongo_db.get_datetime_from_str("2018-3-29 02:59:59.999") } } g = GPS.find_plus(filter_dict=f, to_dict=True) g.sort(key=lambda obj: obj['time'], reverse=False) count = 0 flag = False for x in g: if str(x['latitude']) == "31.296568196614583": flag = True else: pass if flag: count += 1 l = x['loc']['coordinates'] print("时间: {}, 坐标: lat={},lon={}".format(x['time'].strftime("%Y-%m-%d %H:%M:%S"), l[1], l[0])) if count >= 7: break
def calculate_win_per_by_teacher_mix(t_id: (str, ObjectId), begin: str = None, end: str = None) -> dict: """ 计算老师的胜率和应利率,不再细分每个产品的胜率,而是统一计算 :param t_id: :param begin: 开始时间 :param end: 截至时间 :return: """ if end is None: end = datetime.datetime.now() else: end = mongo_db.get_datetime_from_str(end) if begin is None: begin = end - datetime.timedelta(days=30) else: begin = mongo_db.get_datetime_from_str(begin) raw = draw_data_dict_from_db_mix2(t_id, begin, end) res = dict() p_win_count = 0 p_count = len(raw) for x in raw: if x['each_profit'] >= 0: p_win_count += 1 else: pass p_win_per = p_win_count / p_count p_win_per = round(p_win_per * 100, 1) res['t_id'] = t_id res['win_ratio'] = p_win_per return res
def resume_extend_info_func(user: dict = None): """ param user: 用户字典 对简历的扩展信息的操作. 简历的扩展信息包括: 1. :return: """ mes = {"success": "error"} u_id = user['_id'] # u_id = ObjectId("5b56bdba7b3128ec21daa4c7") arg_dict = get_args(request) if "begin" in arg_dict: arg_dict['begin'] = get_datetime_from_str(arg_dict['begin']) if "end" in arg_dict: arg_dict['end'] = get_datetime_from_str(arg_dict['end']) if arg_dict is None or len(arg_dict) == 0: mes['message'] = "参数不能为空" else: resume_id = arg_dict.pop("resume_id", "") if isinstance(resume_id, str) and len(resume_id) == 24: resume_id = ObjectId(resume_id) opt = arg_dict.pop("opt", "") mes = WXUser.opt_extend_info(u_id=u_id, resume_id=resume_id, opt=opt, arg_dict=arg_dict) else: mes['message'] = "简历id错误" return json.dumps(mes)
def calculate_win_per_by_teacher(begin: str = None, end: str = None) -> dict: """ 以老师为分组依据计算的胜率 :param begin: 开始时间 :param end: 截至时间 :return: """ begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) raw = draw_data_dict_from_db(begin, end) res = dict() for t_name, t_value in raw.items(): t_dict = dict() for p_name, p_value in t_value.items(): """计算单个产品的胜率""" p_dict = dict() p_win_count = 0 for record in p_value: if record['each_profit'] >= 0: p_win_count += 1 else: pass p_count = len(p_value) p_win_per = p_win_count / p_count p_dict["per"] = p_win_per p_dict['count'] = p_count t_dict[p_name] = p_dict res[t_name] = t_dict return res
def calculate_win_per_by_product(begin: str = None, end: str = None) -> dict: """ 以产品为分组依据计算的胜率 :param begin: 开始时间 :param end: 截至时间 :return: """ begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) raw = draw_data_dict_from_db(begin, end) res = dict() for t_name, t_value in raw.items(): for p_name, p_value in t_value.items(): out_dict = res.get(p_name) out_dict = dict() if out_dict is None else out_dict """计算单个产品的胜率""" inner_dict = dict() p_win_count = 0 for record in p_value: if record['each_profit'] >= 0: p_win_count += 1 else: pass p_count = len(p_value) p_win_per = p_win_count / p_count inner_dict["per"] = p_win_per inner_dict['count'] = p_count out_dict[t_name] = inner_dict res[p_name] = out_dict return res
def page(cls, _id: str = None, begin_date: (str, datetime.datetime) = None, end_date: (str, datetime.datetime) = None, index: int = 1, num: int = 20, can_json: bool = True, reverse: bool = True) -> dict: """ 分页注册客户记录 :param _id: 客户id,为空表示所有 :param begin_date: 开始时间 :param end_date: 截至时间 :param index: 页码 :param can_json: 是否进行can json转换 :param num: 每页多少条记录 :param reverse: 是否倒序排列? :return: 事件记录的列表和统计组成的dict """ filter_dict = dict() if _id is not None: filter_dict['_id'] = mongo_db.get_obj_id(_id) end_date = datetime.datetime.now() if mongo_db.get_datetime_from_str(end_date) is None else \ mongo_db.get_datetime_from_str(end_date) begin_date = mongo_db.get_datetime_from_str("2010-1-1 0:0:0") if \ mongo_db.get_datetime_from_str(begin_date) is None else mongo_db.get_datetime_from_str(begin_date) filter_dict['time'] = {"$lte": end_date, "$gte": begin_date} index = 1 if index is None else index skip = (index - 1) * num sort_dict = {"time": -1 if reverse else 1} count = cls.count(filter_dict=filter_dict) res = cls.find_plus(filter_dict=filter_dict, sort_dict=sort_dict, skip=skip, limit=num, to_dict=True) if can_json: res = [mongo_db.to_flat_dict(x) for x in res] data = {"count": count, "data": res} return data
def single_info(cls, t_id: (str, ObjectId), begin: (str, datetime.datetime) = None, end: (str, datetime.datetime) = None) -> dict: """ 老师的个人页面,有图表,持仓和历史数据 图表数据改为柱装图 2019-9-17 :param t_id: :param begin: :param end: :return: """ t_id = t_id if isinstance(t_id, ObjectId) else ObjectId(t_id) now = datetime.datetime.now() begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) if end is None: end = now if begin is None: begin = now - datetime.timedelta(days=60) """ calculate_win_per_by_week_single和cls.get_hold方法一样,本质上都是调用了hold_info_from_db函数 """ data = calculate_win_per_by_week_single(t_id=t_id, begin=begin, end=end) t = Teacher.find_by_id(o_id=t_id, can_json=True) data.update(t) return data
def calculate_win_per_by_teacher_mix(begin: str = None, end: str = None) -> dict: """ 以老师为分组依据计算的胜率,不再细分每个产品的胜率,而是统一计算 :param begin: 开始时间 :param end: 截至时间 :return: """ if end is None: end = datetime.datetime.now() else: end = mongo_db.get_datetime_from_str(end) if begin is None: begin = end - datetime.timedelta(days=30) else: begin = mongo_db.get_datetime_from_str(begin) raw = draw_data_dict_from_db_mix(begin, end) res = dict() for t_id, t_value in raw.items(): t_dict = dict() p_win_count = 0 for record in t_value: if record['each_profit'] >= 0: p_win_count += 1 else: pass p_count = len(t_value) p_win_per = p_win_count / p_count p_win_per = round(p_win_per * 100, 1) t_dict["win"] = p_win_per t_dict['count'] = p_count res[t_id] = t_dict return res
def direction_map(cls, include_raw: bool = True) -> dict: """ 获取以方向为key,老师的doc的list为val的字典. 用于在生成虚拟信号的时候提供随机特性 : param include_raw: 是否包含原生的方向. :return: """ f = { "create_date": { "$gt": mongo_db.get_datetime_from_str("2018-9-3 0:0:0") } } ts = Teacher.find(filter_dict=f) t_dict = dict() # 方向和老师的字典 for t in ts: d = t.get("direction") if include_raw: if d is not None: if d in t_dict: t_dict[d].append(t) else: t_dict[d] = [t] else: if d is not None and d != "raw": if d in t_dict: t_dict[d].append(t) else: t_dict[d] = [t] return t_dict
def send_reg_info(cls, **kwargs) -> dict: """ 向简道云发送注册信息 :param kwargs: :return: """ data = dict() page_url = kwargs.get('page_url') spread_keywords = SpreadChannel.analysis_url(page_url) desc1 = '' # 备注1 try: desc1 = spread_keywords[0] except IndexError as e: logger.exception("to_jiandao_cloud error!") except Exception as e: logger.exception("to_jiandao_cloud error!") raise e finally: pass desc2 = '' # 备注2 try: desc2 = spread_keywords[1] except IndexError as e: logger.exception(e) except Exception as e: logger.exception(e) finally: pass desc3 = '' # 备注3 try: desc3 = spread_keywords[2] except IndexError as e: logger.exception(e) except Exception as e: logger.exception(e) finally: pass data['page_url'] = page_url data['desc1'] = desc1 data['desc2'] = desc2 data['desc3'] = desc3 customer = kwargs.get('user_name') data['customer'] = customer phone = kwargs.get('phone') data['phone'] = phone content = kwargs.get('description') data['content'] = content search_keyword = kwargs.get('search_keyword') data['search_keyword1'] = search_keyword group_by = kwargs.get('group_by') data['group_by'] = group_by reg_time = kwargs.get("time") reg_time = mongo_db.get_datetime_from_str(reg_time) reg_time = reg_time - datetime.timedelta(hours=8) data['reg_time'] = reg_time.strftime("%Y-%m-%dT%H:%M:%S.000Z") data['save_id'] = kwargs.pop("save_id", None) resp = cls._send(**data) return resp
def analysis_request(cls, req: request, auto_save: bool = False) -> (list, None): """ 从一个flask的request中分析发送来的信息,符合标准的化,就返回一个本类初始化字典(多条报价)组成的. 否则返回None :param req: :param auto_save: auto_save 是否需要保存实例? :return: """ now = datetime.datetime.now() form = req.form form = {k: v for k, v in form.items()} platform_name = form.get("platform_name", "") if platform_name == "": platform_name = form.get("platform_name ", "") platform_account = form.get("platform_account", "") platform_time = form.get("platform_time", now) platform_time = platform_time.replace(".", "-") if isinstance( platform_time, str) else platform_time platform_time = mongo_db.get_datetime_from_str(platform_time) if isinstance(platform_time, str) \ else platform_time r = { "platform_name": platform_name, "platform_account": platform_account, "platform_time": platform_time, "receive_time": now } prices = form.pop('data', '') if prices == '': res = None else: t1 = [[y for y in x.split("*")] for x in prices.split("^")] res = list() res2 = list() for x in t1: if len(x) >= 3: temp = { "code": x[0], "product": x[1], "price": float(x[2]) } temp2 = { "code": x[0], "product": x[1], "price": float(x[2]), "_id": ObjectId() } temp.update(r) temp2.update(r) res.append(temp) res2.append(temp2) if auto_save: ses = cls.get_collection() insert_res = ses.insert_many(documents=res2) print(insert_res) else: pass return res
def _get_rank(cls, cur_time: datetime.datetime, prev_week: int = 1, re_build: bool = False) -> dict: """ 根据日期,返回日期所在的周(或者往前推几周)的排行字典,底层函数,无缓存 :param cur_time: :param prev_week:往前提前几周?默认是1,也就是上一周的排行,0就是取本周的排行 :param re_build:强制重新生成排行榜? :return:{t_id: {"rank": rank, "win": win},...} """ the_time = cur_time - datetime.timedelta(days=7 * prev_week) y, w, d = the_time.isocalendar() # 年, 全年的第几周? 这天是星期几? f = {"year": y, "week": w} res = cls.find_one(filter_dict=f) if res is None or re_build: """没有查到/强行重新生成,生成一个新的记录""" begin = mongo_db.get_datetime_from_str("{}".format( (the_time - datetime.timedelta(days=d - 1)).strftime("%F"))) end = mongo_db.get_datetime_from_str("{} 23:59:59.999999".format( (the_time + datetime.timedelta(days=(7 - d))).strftime("%F"))) rank_dict = calculate_win_per_by_teacher_mix(begin, end) """ { ObjectId('5a1e680642f8c1bffc5dbd69'): {'win': 50.0, 'count': 8}, ObjectId('5a1e680642f8c1bffc5dbd6f'): {'win': 75.0, 'count': 16}, ObjectId('5b4c01fffd259807ff92ad65'): {'win': 66.7, 'count': 15} } """ rank = [{"t_id": k, "win": v['win']} for k, v in rank_dict.items()] rank.sort(key=lambda obj: obj['win'], reverse=True) u = { "$set": { "begin": begin, "end": end, "time": datetime.datetime.now(), "rank": rank } } res = cls.find_one_and_update(filter_dict=f, update_dict=u, upsert=True) else: pass return res
def backup(show_today: bool = False): begin = "2018-03-26" today = datetime.datetime.today() end = today.strftime("%F") filter_dict = {'create_date': { "$gte": mongo_db.get_datetime_from_str("{} 0:0:0".format(begin)), "$lte": mongo_db.get_datetime_from_str("{} 23:59:59.999".format(end)) }, "description": {"$exists": False} } es = Employee.find_plus(filter_dict=filter_dict, can_json=True) # es = EventRecord.find_plus(filter_dict={}) print("总共的注册人数:{}".format(len(es))) file_path = os.path.join(dir_path, "h5", "{}.pkl".format(datetime.datetime.now().strftime("%Y-%m-%d %H_%M_%S"))) f = open(file_path, 'wb') pickle.dump(es, f) f.flush() f.close() """返回前一日的注册数据组成的title和content""" es.sort(key=lambda obj: obj['create_date'], reverse=True) yesterday = today - datetime.timedelta(days=1) y_str = yesterday.strftime("%F") b2 = mongo_db.get_datetime_from_str("{} 0:0:0".format(y_str)) e2 = mongo_db.get_datetime_from_str("{} 23:59:59.999".format(y_str)) re = list() for e in es: c_date = mongo_db.get_datetime_from_str(e['create_date']) if b2 <= c_date <= e2: temp = { "手机": e['phone_num'], "注册时间": c_date.strftime("%Y-%m-%d %H:%M:%S"), "真实姓名": e.get("real_name", '') } re.append(temp) else: pass f res = { "title": "{}新增注册{}人".format(y_str, len(re)), "content": re } return res
def log_func(): """记录老师操作日志""" t_id = session.get("t_id", '') content = get_arg(request, "args", "") url = get_arg(request, "url", "") error = get_arg(request, "error", "") error_time = get_arg(request, "error_time", "") error_time_t = mongo_db.get_datetime_from_str(error_time) error_time = error_time_t if isinstance(error_time_t, datetime.datetime) else error_time TeacherLog.log(t_id=t_id, url=url, error_time=error_time, error=error, content=content) return json.dumps("ok")
def read_sheet_01(sh) -> list: """ 读取excel文件里的工作簿,这里代码要根据读取的文件不同自行该比那 :param sh: :return: """ res = list() now = datetime.datetime.now() for i, tr in enumerate(sh): if i == 0: pass else: create_time = tr[0].value name = tr[10].value update_time = tr[11].value init = { "op": "data_update", "datetime": create_time, "create_time": create_time, "update_time": update_time, "product": tr[1].value, "the_type": tr[2].value, "direction": tr[3].value, "enter_price": tr[4].value, "exit_price": tr[5].value, "profit": tr[6].value, "each_profit_dollar": tr[7].value, "each_profit": tr[8].value, "each_cost": tr[9].value, "creator_name": name, "updater_name": name, "from": "excel" } if tr[8].value != "徐立杰": res.append(init) f = {"creator_name": {"$ne": "徐立杰"}} s = {'receive_time': 1} insert = list() al = Signal.find_one_plus(filter_dict=f, sort_dict=s, instance=False) if isinstance(al, dict): last = get_datetime_from_str(al['datetime']) for x in res: create_time = x['create_time'] if create_time < last: insert.append(x) else: insert = res print(len(insert)) conn = get_conn("signal_info") for i in insert: f = {"datetime": i.pop("datetime"), "creator_name": i.pop("creator_name")} conn.update_one(filter=f, update={"$set": i}, upsert=True) return res
def export_alarm_event(user_id: (str, ObjectId), begin: (str, datetime.datetime), end: (str, datetime.datetime) = None) -> list: """ 导出指定用户,制定日期的行车事件. :param user_id: :param begin: :param end: :return: """ user_id = str(user_id) if isinstance(user_id, ObjectId) else user_id begin = mongo_db.get_datetime_from_str(begin) if isinstance(begin, str) else begin end = begin + datetime.timedelta(days=1) if end is None else end f = {"vehicle_id": user_id, "start_time": {"$gte": begin}, "end_time": {"$lte": end}}
def calculate_win_per_by_month(begin: str = None, end: str = None) -> dict: """ 以时间(月)切分,以老师为分组依据, 以产品分类计算的胜率 :param begin: 开始时间 :param end: 截至时间 :return: """ begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) raw = draw_data_list_from_db(begin, end) res = dict() pattern = re.compile(r'^20\d{2}-\d{2}') for record in raw: p_name = record['product'] t_name = record['teacher'] p_dict = res.get(p_name) p_dict = dict() if p_dict is None else p_dict t_dict = p_dict.get(t_name) t_dict = dict() if t_dict is None else t_dict month = re.match(pattern, record['enter_date']).group() m_dict = dict() if t_dict.get(month) is None else t_dict[month] win_count = 0 if m_dict.get( "win_count") is None else m_dict['win_count'] all_count = 0 if m_dict.get( "all_count") is None else m_dict['all_count'] all_count += 1 if record['win'] == 1: win_count += 1 else: pass m_dict['win_count'] = win_count m_dict['all_count'] = all_count t_dict[month] = m_dict p_dict[t_name] = t_dict res[p_name] = p_dict return res
def calculate_win_per_by_week(begin: str = None, end: str = None) -> dict: """ 以时间(周)切分,以老师为分组依据, 以产品分类计算的胜率 :param begin: 开始时间 :param end: 截至时间 :return: """ begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) raw = draw_data_list_from_db(begin, end) res = dict() pattern = re.compile(r'^20\d{2}-\d{2}') for record in raw: p_name = record['product'] t_name = record['teacher'] p_dict = res.get(p_name) p_dict = dict() if p_dict is None else p_dict t_dict = p_dict.get(t_name) t_dict = dict() if t_dict is None else t_dict week = record['week'] w_dict = dict() if t_dict.get(week) is None else t_dict[week] win_count = 0 if w_dict.get( "win_count") is None else w_dict['win_count'] all_count = 0 if w_dict.get( "all_count") is None else w_dict['all_count'] all_count += 1 if record['win'] == 1: win_count += 1 else: pass w_dict['win_count'] = win_count w_dict['all_count'] = all_count t_dict[week] = w_dict p_dict[t_name] = t_dict res[p_name] = p_dict return res
def process_praise(**kwargs) -> None: """ 处理简道云发送过来的战报海报消息 :param kwargs: :return: """ args = dict() args['order'] = kwargs['_id'] args['the_type'] = kwargs['the_type'] args['customer'] = kwargs['customer'] args['sales'] = kwargs['sales'] args['manager'] = kwargs['manager'] args['director'] = kwargs['director'] args['event_time'] = mongo_db.get_datetime_from_str(kwargs['time']) money = kwargs['money'] desc = kwargs.get('desc', '') try: money = int(money) except Exception as e: ms = "转换入金宣传表中的金额时出错,money={}, error:{}".format(money, e) logger.exception(msg=ms) print(ms) finally: args['money'] = money """创建并保存入金宣传信息""" r = Praise.create(**args) if isinstance(r, ObjectId): """发送钉订消息""" data = dict() data['msgtype'] = 'markdown' markdown_doc = dict() markdown_doc['title'] = "入金喜报" i = str(r) if desc == '': text = "![screenshot](http://47.106.68.161:8000/normal/praise_image/view?fid={})".format( r) else: text = "![screenshot](http://47.106.68.161:8000/normal/praise_image/view?fid={}) \n ##### {} [胜利][胜利][胜利],继续加油![加油][加油][加油]".format( r, desc) print("text = {}".format(text)) markdown_doc['text'] = text data['markdown'] = markdown_doc token_name = '入金宣传' send_signal(send_data=data, token_name=token_name) else: """发送钉订失败""" title = "使用钉钉机器人发送入金宣传失败{}".format(datetime.datetime.now()) content = "args: {}".format(args) send_mail(title=title, content=content) ms = "{}, {}".format(title, content) logger.exception(msg=ms)
def sum_commission(cls, begin: str, end: str) -> float: """ 统计佣金 :param begin: :param end: :return: """ begin = mongo_db.get_datetime_from_str(begin) end = mongo_db.get_datetime_from_str(end) filter_dict = { "close_time": { "$exists": True }, "close_time": { "$lte": end, "$gte": begin } } res = cls.find_plus(filter_dict=filter_dict, to_dict=True) f = 0 for x in res: f += x['commission'] return f
def __init__(self, **kwargs): if "name" not in kwargs: ms = "name必须" logger.exception(ms) raise ValueError(ms) else: name = kwargs['name'] if not isinstance(name, str): ms = "name类型错误,期待str,得到:{}".format(type(name)) logger.exception(ms) raise ValueError(ms) else: pass if "status" not in kwargs: kwargs['status'] = "normal" else: status = kwargs['status'] if status not in ['normal', 'stop', 'suspend', 'invalid']: ms = "status错误,必须是normal/stop/suspend/invalid,得到:{}".format( status) logger.exception(ms) raise ValueError(ms) else: pass if "project_id" not in kwargs: ms = "project_id必须" logger.exception(ms) raise ValueError(ms) else: project_id = kwargs['project_id'] if not isinstance(project_id, DBRef): ms = "project_id类型错误,期待DBRef,得到:{}".format(type(project_id)) logger.exception(ms) raise ValueError(ms) else: pass if "create_date" not in kwargs: kwargs['create_date'] = datetime.datetime.now() else: create_date = kwargs['create_date'] create_date = create_date if isinstance(create_date, datetime.datetime) else \ mongo_db.get_datetime_from_str(create_date) if not isinstance(create_date, datetime.datetime): ms = "create_date类型错误,期待datetime,得到:{}".format( type(create_date)) logger.exception(ms) raise ValueError(ms) else: pass mongo_db.BaseDoc.__init__(self, **kwargs)
def simple_query(begin, end): filter_dict = {'create_date': { "$gte": mongo_db.get_datetime_from_str("{} 0:0:0".format(begin)), "$lte": mongo_db.get_datetime_from_str("{} 23:59:59.999".format(end)) }, "description": {"$exists": False} } es = Employee.find_plus(filter_dict=filter_dict, to_dict=False) print("总共的注册人数:{}".format(len(es))) for e in es: print(e.get_attr("phone_num"), e.get_attr("create_date")) dbrefs = [e.get_dbref() for e in es] f = { "user_id": {"$in": dbrefs}, "time": { "$gte": mongo_db.get_datetime_from_str("{} 0:0:0".format(begin)), "$lte": mongo_db.get_datetime_from_str("{} 23:59:59.999".format(end)), } } r = GPS.find_plus(filter_dict=f, to_dict=True) print(len(r)) l2 = list(set([x['user_id'].id for x in r])) l1 = list(set([x.get_attr("_id") for x in es])) for x in l2: if x in l1: l2.remove(x) l1.remove(x) f = {"_id": {"$in": l2}} # 注册没登录的 r_2 = Employee.find_plus(f, to_dict=True) phone2 = [x['phone_num'] for x in r_2] f = {"_id": {"$in": l1}} # 活跃的用户 r_1 = Employee.find_plus(f, to_dict=True) phone1 = [x['phone_num'] for x in r_1] print(phone2) print(phone1)
def get_user_data(filter_dict: dict = None): """ 获取用户的注册信息,用于数据分析 :param filter_dict: :return: """ if filter_dict is None: filter_dict = {'create_date': {"$gte": mongo_db.get_datetime_from_str("2018-3-20 0:0:0")}} users = Employee.find_plus(filter_dict=filter_dict, to_dict=True) objs = list() for user in users: obj = Series(user) objs.append(obj) frame = DataFrame(data=objs, index=range(len(objs))) return frame
def today_register_count(cls) -> int: """ 以每日早上6点计算,当前的注册用户是本日的第几个用户 ? 以1开始基数, :return: """ key = "today_register_count" count_dict = cache.get(key=key) """计算查询条件""" now = datetime.datetime.now() current_hour = now.hour if current_hour >= 6: days = 0 else: days = 1 day_str = (now - datetime.timedelta(days=days)).strftime("%F") need_query = True if count_dict is None: pass else: prev_day_str = count_dict.get("day_str") if prev_day_str != day_str: pass else: count = count_dict.get("count") if isinstance(count, int): need_query = False else: pass if need_query: begin_str = "{} 06:00:00".format(day_str) begin = mongo_db.get_datetime_from_str(begin_str) f = {"time": {"$gte": begin}} """从数据库查询""" r = cls.count(filter_dict=f) count = r - 1 # -1 是应为cls.reg方法先保存了用户信息 else: count = count_dict['count'] count += 1 new_dict = dict() new_dict['day_str'] = day_str new_dict['count'] = count cache.set(key=key, value=new_dict, timeout=86400) return count
def get_online_report(): """获取在线报告""" ses = get_conn("gps_info") now = datetime.datetime.now() year = now.year month = now.month begin = get_datetime_from_str("{}-{}-1 0:0:0".format(year, month)) # begin = now - datetime.timedelta(days=30) query = {"time": {"$gte": begin}} s = {"time": -1} out = "online_report_result" # 保存数据的表,每次map_reduce都会提前清空这个表 map_func = Code(""" function(){ emit(this.user_id.$id, 1); } """) reduce_func = Code(""" function(key, values){ return Array.sum(values); } """) result_conn = ses.map_reduce(map=map_func, reduce=reduce_func, query=query, sort=s, out=out, full_response=False) res = result_conn.find(filter=dict()) count_dict = {x['_id']: int(x['value']) for x in res} ids = list(count_dict.keys()) ses = get_conn("user_info") f = {"_id": {"$in": ids}} s = [("last_update", -1)] users = ses.find(filter=f, sort=s) res = list() for user in users: user_id = user['_id'] temp = to_flat_dict(user) temp['count'] = count_dict[user_id] res.append(temp) res.sort(key=lambda obj: obj['count'], reverse=True) return res
def get_hold(cls, t_id: (str, ObjectId), h_id: (str, ObjectId) = None) -> (None, dict, list): """ 获取老师的持仓记录 相对history_and_hold,这是一个较旧的函数.仅仅为了提供持仓数据 :param t_id: Teacher._id :param h_id: Trade._id :return: """ begin = mongo_db.get_datetime_from_str("2018-07-01 17:00:00") end = datetime.datetime.now() res = hold_info_from_db(t_id=t_id, begin=begin, end=end, h_id=h_id) if h_id is None: return res else: if len(res) == 0: return None else: return res[0]
def produce_honor(user_id: DBRef) -> object: """ 随机生成荣誉证书的数据.保存数据,并返回实例本身 :param user_id: :return: """ p = "/home/walle/图片/2018-05-28 18-42-07屏幕截图.png" f = open(file=p, mode="rb") image = f.read() f.close() init = { "_id": ObjectId(), "driver_id": user_id, "image": image, "time": mongo_db.get_datetime_from_str("2018-01-01"), "info": "xxx同志获得2017年度工作标兵称号", "create_date": datetime.datetime.now() } obj = Honor(**init) obj.save_plus() return obj
def instance(cls, **kwargs): """ 推荐的从参数获取实例的方法 :param kwargs: :return: """ num = kwargs['num'] if isinstance(num, int): pass elif isinstance(num, float): kwargs['num'] = int(num) elif isinstance(num, str) and num.isdigit(): kwargs['num'] = int(num) else: ms = "错误的num:{}".format(num) logger.exception(msg=ms) raise ValueError(ms) user_id = kwargs.get('user_id') if isinstance(user_id, ObjectId): pass elif isinstance(user_id, str) and len(user_id) == 24: kwargs['user_id'] = ObjectId(user_id) else: ms = "错误的user_id:{}".format(user_id) logger.exception(msg=ms) raise ValueError(ms) t = kwargs.get('time') if t is None: kwargs['time'] = datetime.datetime.now() elif isinstance(t, str): t = mongo_db.get_datetime_from_str(t) kwargs['time'] = t if t else datetime.datetime.now() else: ms = "错误的time:{}".format(t) logger.exception(msg=ms) raise ValueError(ms) return cls(**kwargs)
def read_json() -> list: """ 从本地读取json文件 :return: """ d_path = os.path.join(__project_dir, "tool_box", "json") f_path = os.path.join(d_path, "all_gps.json") with open(f_path, mode="r", encoding="utf-8") as file: data = json.load(file) data.sort(key=lambda obj: mongo_db.get_datetime_from_str(obj['time']), reverse=True) data_by_user = dict() count_by_user = dict() for x in data: u_id = x['user_id'] if u_id not in data_by_user: data_by_user[u_id] = [x] count_by_user[u_id] = 1 else: data_by_user[u_id].append(x) count = count_by_user[u_id] count += 1 count_by_user[u_id] = count count_by_user = [{"id": k, "count": v} for k, v in count_by_user.items()] count_by_user.sort(key=lambda obj: obj['count'], reverse=True) ids = count_by_user[0:3] dd_path = os.path.join(d_path, "single_by_user") if not os.path.exists(dd_path): os.makedirs(dd_path) ids = [x['id'] for x in ids] for i, id in enumerate(ids): name = "user_{}_gps.json".format(i + 1) v = data_by_user[id] with open(os.path.join(dd_path, name), mode="w", encoding="utf-8") as f: json.dump(v, f)
def parse_withdraw_tr(tr: PyQuery) -> dict: """ 解析一页二号平台站点的出金申请的tr :param tr: :return: """ init_dict = dict() tds = tr.find("td") if len(tds) < 15: return None else: domain = domain2 init_dict['system'] = domain """第一个td,取mt帐号和mt分组""" first = PyQuery(tds[0]) texts_1 = first.text().split("\n") account = int(re.search(r'\d{4,}', texts_1[0].lower()).group()) # mt账户 group = texts_1[-1].lower()[5:] # mt分组 init_dict['account'] = account init_dict['group'] = group """第二个td,取客户2""" second = PyQuery(tds[1]) init_dict['manager'] = second.text().strip() """第三个td,取英文名""" third = PyQuery(tds[2]) texts_3 = third.text().split("\n") nick_name = texts_3[0][4:].strip("") init_dict['nick_name'] = nick_name """第四个,金额""" fourth = PyQuery(tds[3]) texts_4 = fourth.text().split("\n") amount_usd = float(texts_4[0].split("$")[-1].strip()) # 金额/美元 amount_cny = float(texts_4[-1].split("¥")[-1].strip()) # 金额/人民币 init_dict['amount_usd'] = amount_usd init_dict['amount_cny'] = amount_cny """ 第五个,取手续费 """ fifth = PyQuery(tds[4]) texts_5 = fifth.text().split("\n") commission_usd = float(texts_5[0].split("$")[-1].strip()) # 手续费/美元 commission_cny = float(texts_5[-1].split("¥")[-1].strip()) # 手续费/人民币 init_dict['commission_usd'] = commission_usd init_dict['commission_cny'] = commission_cny """ 第六个,转账方式, """ sixth = PyQuery(tds[5]) init_dict['channel'] = sixth.text().strip() """ 第七个,时间 """ seventh = PyQuery(tds[6]) seventh = seventh.text().split("\n") apply_time = seventh[0][5:].strip("") apply_time = get_datetime_from_str(apply_time) close_time = seventh[-1][5:].strip("") close_time = get_datetime_from_str(close_time) init_dict['apply_time'] = apply_time init_dict['close_time'] = close_time """第八个,开户行""" eighth = PyQuery(tds[7]).text() init_dict['blank_name'] = eighth.strip() """第九个,开户行代码""" ninth = PyQuery(tds[8]).text() init_dict['blank_code'] = ninth.strip() """第十个,银行""" tenth = PyQuery(tds[9]).text() init_dict['code_id'] = tenth.strip() """第十一个,状态""" eleventh = PyQuery(tds[10]).text() init_dict['status'] = eleventh.strip() """第十二个,账户余额""" twelfth = PyQuery(tds[11]).text() init_dict['account_balance'] = float(twelfth.strip()[1:]) """第十三个,账户净值""" thirteenth = PyQuery(tds[12]).text() init_dict['account_value'] = float(thirteenth.strip()[1:]) """第十四个,持仓量""" fourteenth = PyQuery(tds[13]).text() init_dict['open_interest'] = float(fourteenth.strip()[0:-1]) """第十五个,可用保证金""" fifteenth = PyQuery(tds[14]).text() init_dict['account_margin'] = float(fifteenth.strip()[1:]) """第十六个,单号""" sixth = PyQuery(tds[15].find("a")) init_dict['ticket'] = int(sixth.attr("href").split("/")[-1]) init_dict = {k: v for k, v in init_dict.items()} """只记录指定类型的单子""" if init_dict['status'] == "审核中": return init_dict else: return None