def brand_value(request): """ 返回微品牌价值的EChart数据 """ freq_type = request.GET.get('freq_type','W') end_date = util_dateutil.now() webapp_id = request.user_profile.webapp_id # 以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=DISPLAY_PERIODS_IN_APP_CHARTS, freq=freq_type) date_list = [] values = [] # TODO: 需要优化。可以一次计算完成 for date in date_range: date_str = util_dateutil.date2string(date.to_datetime()) # 将pd.Timestamp转成datetime date_list.append(date_str[date_str.find('-')+1:]) values.append(get_brand_value(webapp_id, date_str)) response = create_line_chart_response( "", "", date_list, [{ "name": "品牌价值", "values" : values }] ) return response
def sales_chart(request): freq_type = request.GET.get('freq_type','W') priods = 7 if freq_type == 'W' else 30 end_date = util_dateutil.now() # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=priods, freq='D') start_date = util_dateutil.date2string(date_range[0].to_datetime()) try: webapp_id = request.user.get_profile().webapp_id orders = Order.by_webapp_id(webapp_id).filter(created_at__gte=start_date,created_at__lte=end_date,status__in=ORDER_SOURCE).order_by('created_at') order_price2time = OrderedDict() for order in orders: created_at = order.created_at.strftime("%Y-%m-%d") use_price = order.final_price + order.weizoom_card_money if not order_price2time.has_key(created_at): order_price2time[created_at] = use_price else: order_price2time[created_at] += use_price date_list = [] values =[] for date in date_range: date = util_dateutil.date2string(date.to_datetime()) date_list.append(date[date.find('-')+1:]) if not order_price2time.has_key(date): values.append(0) else: values.append(int(order_price2time[date])) response = create_line_chart_response( "", "", date_list, [{ "name": "销售额", "values" : values }] ) return response except: response = create_response(500) response.errMsg = u'系统繁忙,请稍后重试' return response.get_response()
def order_value(request): webapp_id = request.user.get_profile().webapp_id freq_type = request.GET.get('freq_type','W') priods = 7 if freq_type == 'W' else 30 end_date = util_dateutil.now() # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=priods, freq='D') start_date = util_dateutil.date2string(date_range[0].to_datetime()) try: orders = Order.by_webapp_id(webapp_id).filter(created_at__gte=start_date,created_at__lte=end_date,status__in=ORDER_SOURCE).order_by('created_at') order_items = OrderedDict() for order in orders: date = order.created_at.strftime('%Y-%m-%d') if not order_items.has_key(date): order_items[date] = 1 else: order_items[date] += 1 date_list = [] values =[] for date in date_range: date = util_dateutil.date2string(date.to_datetime()) date_list.append(date[date.find('-')+1:]) if not order_items.has_key(date): values.append(0) else: values.append(order_items[date]) response = create_line_chart_response( "", "", date_list, [{ "name": "订单量", "values" : values }] ) return response except: response = create_response(500) response.errMsg = u'系统繁忙,请稍后重试' return response.get_response()
def api_get(request): webapp_id = request.user_profile.webapp_id if request.GET.get('get_outline_data', None): # 获得昨日订单数据 today = '%s 23:59:59' % dateutil.get_yesterday_str('today') yesterday = '%s 00:00:00' % dateutil.get_yesterday_str('today') order_money, order_count = stats_util.get_transaction_money_order_count( webapp_id, yesterday, today) # 获取会员数 update by bert at 20150817 subscribed_member_count = member_models.Member.objects.filter( webapp_id=webapp_id, is_subscribed=True, is_for_test=False).count() new_member_count = member_models.Member.objects.filter( webapp_id=webapp_id, created_at__range=(yesterday, today), status=member_models.SUBSCRIBED, is_for_test=False).count() response = create_response(200) response.data = { 'unread_message_count': get_unread_message_count(request.manager), 'new_member_count': new_member_count, 'order_count': order_count, 'order_money': round(order_money, 2), 'subscribed_member_count': subscribed_member_count, 'shop_hint_data': _get_shop_hint_data(request), 'tomorrow': dateutil.get_tomorrow_str('today') } return response.get_response() else: type = request.GET.get('type', None) days = request.GET.get('days', 6) total_days, low_date, cur_date, high_date = dateutil.get_date_range( dateutil.get_today(), days, 0) # high_date -= timedelta(days=1) date_list = [ date.strftime("%Y-%m-%d") for date in dateutil.get_date_range_list(low_date, high_date) ] #当最后一天是今天时,折线图中不显示最后一天的数据 duhao 2015-09-17 #当起止日期都是今天时,数据正常显示 today = dateutil.get_today() if len(date_list) > 1 and date_list[-1] == today: del date_list[-1] if type and type == 'purchase_trend': try: date2count = dict() date2price = dict() # 11.20从查询mall_purchase_daily_statistics变更为直接统计订单表,解决mall_purchase_daily_statistics遗漏统计订单与统计时间不一样导致的统计结果不同的问题。 orders = mall_models.Order.objects.belong_to( webapp_id).filter( created_at__range=(low_date, (high_date) + timedelta(days=1))) statuses = set([ mall_models.ORDER_STATUS_PAYED_SUCCESSED, mall_models.ORDER_STATUS_PAYED_NOT_SHIP, mall_models.ORDER_STATUS_PAYED_SHIPED, mall_models.ORDER_STATUS_SUCCESSED ]) orders = [ order for order in orders if (order.type != 'test') and ( order.status in statuses) ] for order in orders: # date = dateutil.normalize_date(order.created_at) date = order.created_at.strftime("%Y-%m-%d") if order.webapp_id != webapp_id: order_price = mall_models.Order.get_order_has_price_number( order) + order.postage else: order_price = order.final_price + order.weizoom_card_money if date in date2count: old_count = date2count[date] date2count[date] = old_count + 1 else: date2count[date] = 1 if date in date2price: old_price = date2price[date] date2price[date] = old_price + order_price else: date2price[date] = order_price count_trend_values = [] price_trend_values = [] for date in date_list: count_trend_values.append(date2count.get(date, 0)) price_trend_values.append( round(date2price.get(date, 0.0), 2)) result = create_line_chart_response( '', '', date_list, [{ "name": "销售额", "values": price_trend_values }, { "name": "订单数", "values": count_trend_values }], use_double_y_lable=True, get_json=True) result['yAxis'][0]['name'] = '销售额' result['yAxis'][1]['name'] = '订单数' result['yAxis'][1]['splitLine'] = {'show': False} response = create_response(200) response.data = result return response.get_response() except: if settings.DEBUG: raise else: response = create_response(500) response.innerErrMsg = unicode_full_stack() return response.get_response() elif type and type == 'visit_daily_trend': # 已弃用 """ 获得每日pv、uv统计 """ # 对当天的统计结果进行更新 if settings.IS_UPDATE_PV_UV_REALTIME: # 先删除当天的pv,uv统计结果,然后重新进行统计 today = dateutil.get_today() webapp_models.PageVisitDailyStatistics.objects.filter( webapp_id=webapp_id, data_date=today).delete() webapp_statistics_util.count_visit_daily_pv_uv( webapp_id, today) statisticses = webapp_models.PageVisitDailyStatistics.objects.filter( webapp_id=webapp_id, url_type=webapp_models.URL_TYPE_ALL, data_date__range=(low_date, high_date)) date2pv = dict([(s.data_date.strftime('%Y-%m-%d'), s.pv_count) for s in statisticses]) date2uv = dict([(s.data_date.strftime('%Y-%m-%d'), s.uv_count) for s in statisticses]) pv_trend_values = [] uv_trend_values = [] for date in date_list: pv_trend_values.append(date2pv.get(date, 0)) uv_trend_values.append(date2uv.get(date, 0)) return create_line_chart_response('', '', date_list, [{ "name": "PV", "values": pv_trend_values }, { "name": "UV", "values": uv_trend_values }])
def api_get(request): low_date, high_date, date_range = stats_util.get_date_range(request) webapp_id = request.user_profile.webapp_id date_formatter = stats_util.TYPE2FORMATTER['day'] #新增会员数 date2new_member_count = stats_util.get_date2new_member_count( webapp_id, low_date, high_date) #下单会员数 date2bought_member_count = stats_util.get_date2bought_member_count( webapp_id, low_date, high_date) #绑定手机会员数 date2binding_phone_member_count = stats_util.get_date2binding_phone_member_count( webapp_id, low_date, high_date) formatted_date_list = stats_util.get_formatted_date_list( date_range, date_formatter) #准备X轴日期数据 x_values = formatted_date_list #准备Y轴数据 new_member_count_list = [] bought_member_count_list = [] binding_phone_member_count_list = [] for date in formatted_date_list: new_member_count = 0 #新增会员数 if date2new_member_count.has_key(date): new_member_count = date2new_member_count[date] new_member_count_list.append(new_member_count) bought_member_count = 0 #下单会员数 if date2bought_member_count.has_key(date): bought_member_count = date2bought_member_count[date] bought_member_count_list.append(bought_member_count) binding_phone_member_count = 0 #绑定手机会员数 if date2binding_phone_member_count.has_key(date): binding_phone_member_count = date2binding_phone_member_count[ date] binding_phone_member_count_list.append(binding_phone_member_count) y_values = [ { "name": "新增会员数", "values": new_member_count_list, }, { "name": "下单会员数", "values": bought_member_count_list, # },{ # "name": "绑定手机会员数", # "values" : binding_phone_member_count_list, } ] return create_line_chart_response('', '', x_values, y_values)
def api_get(request): """ 返回微品牌价值的EChart数据 """ start_date = request.GET.get('start_date') end_date = request.GET.get('end_date') freq_type = request.GET.get('freq_type') try: periods = int(request.GET.get('periods', DISPLAY_PERIODS_IN_CHARTS)) except: periods = DISPLAY_PERIODS_IN_CHARTS if freq_type == 'week': freq = 'W' elif freq_type == 'month': freq = 'M' else: freq = 'D' if end_date is None: end_date = util_dateutil.now() else: try: end_date = util_dateutil.parse_date(end_date) except: notify_msg = u"参数错误, cause:\n{}".format(unicode_full_stack()) watchdog_warning(notify_msg) end_date = util_dateutil.now() webapp_id = request.user_profile.webapp_id if start_date is not None: date_range = pd.date_range(start=start_date, end=end_date, freq=freq) else: # 如果不指定start_date,则以end_date为基准倒推DISPLAY_PERIODS_IN_CHARTS 个日期(点) date_range = pd.date_range(end=end_date, periods=periods, freq=freq) date_list = [] values = [] # TODO: 需要优化。可以一次计算完成 for date in date_range: date_str = util_dateutil.date2string( date.to_datetime()) # 将pd.Timestamp转成datetime date_list.append(date_str) values.append(get_brand_value(webapp_id, date_str)) print("date_list: {}".format(date_list)) response = create_line_chart_response( "", "", #['2010-10-10', '2010-10-11', '2010-10-12', '2010-10-13'], date_list, [{ "name": "品牌价值", "values": values }]) return response
def api_get(request): low_date, high_date, date_range = stats_util.get_date_range(request) try: webapp_id = request.user_profile.webapp_id date_list = [ date.strftime("%Y-%m-%d") for date in dateutil.get_date_range_list(low_date, high_date) ] date2count = dict() date2price = dict() # 11.20从查询mall_purchase_daily_statistics变更为直接统计订单表,解决mall_purchase_daily_statistics遗漏统计订单与统计时间不一样导致的统计结果不同的问题。 orders = Order.objects.belong_to(webapp_id).filter( created_at__range=(low_date, (high_date + timedelta(days=1)))) statuses = set([ ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED ]) orders = [ order for order in orders if (order.type != 'test') and (order.status in statuses) ] for order in orders: # date = dateutil.normalize_date(order.created_at) date = order.created_at.strftime("%Y-%m-%d") if order.webapp_id != webapp_id: order_price = Order.get_order_has_price_number( order) + order.postage else: order_price = order.final_price + order.weizoom_card_money if date in date2count: old_count = date2count[date] date2count[date] = old_count + 1 else: date2count[date] = 1 if date in date2price: old_price = date2price[date] date2price[date] = old_price + order_price else: date2price[date] = order_price price_trend_values = [] #当最后一天是今天时,折线图中不显示最后一天的数据 duhao 2015-08-12 #当起止日期都是今天时,数据正常显示 today = dateutil.get_today() if len(date_list) > 1 and date_list[-1] == today: del date_list[-1] for date in date_list: price_trend_values.append("%.2f" % (date2price.get(date, 0.0))) return create_line_chart_response('', '', date_list, [{ "name": "销售额", "values": price_trend_values }]) except: if settings.DEBUG: raise else: response = create_response(500) response.innerErrMsg = unicode_full_stack() return response.get_response()