示例#1
0
def add_report(request):
    report_info = QueryDict(request.body)
    report_config = {
        'mid_id': report_info['mid'],
        'create_time': MyTime.date_to_dt(MyTime.get_date(0)),
        'report_name': report_info['report_name'],
        'create_uid': 1,
        'update_time': MyTime.date_to_dt(MyTime.get_date(0)),
        'report_type': report_info['report_type'],
        'online': report_info['online'],
        'update_uid': 1
    }
    if models.BdpVisualReport.objects.filter(
            mid_id=report_info['mid'], report_name=report_info['report_name']):
        response = {
            'code': 1,
            'message': 'this report name is exist, please change it',
            "data": None
        }
    else:
        re_obj = models.BdpVisualReport.objects.create(**report_config)
        auto_add_permission(re_obj.vid)
        response = {'code': 0, 'message': 'add success', 'data': None}

    return response
示例#2
0
def update_report(request):
    report_info = QueryDict(request.body)
    vid = report_info['vid']
    online = report_info['online']
    report_name = report_info['report_name']
    report_config = {
        'create_uid': 1,
        'update_time': MyTime.date_to_dt(MyTime.get_date(0)),
        'online': online,
        'report_name': report_name,
        'update_uid': 1
    }
    models.BdpVisualReport.objects.filter(vid=vid).update(**report_config)
    response = {'code': 0, 'message': 'update success', 'data': None}
    return response
示例#3
0
def get_report_sql(report_cid, args_dict):
    """
    设置报表cid,获取报表查询sql,时间变量用${date_start},${date_end}
    :param report_cid:
    :param args_dict get参数dict,同data platform页面url传参格式
    :return:
    """
    api_url = 'http://192.168.1.119:8889/DataApi/GetReportSQL/'
    post_args = {
        'app': 'office.DataMonitor',
        'token': '6d44a776091069dad798de3d3df5653f',
        'cid': report_cid,
        'date_start': '${date_start}',
        'date_end': '${date_end}'
    }
    post_args = dict(post_args, **args_dict)

    try:
        req = requests.post(url=api_url, data=post_args)

        if req.status_code == 200:
            result = req.json()
            if result['status'] == 'success':
                return result['value']['sql']
            else:
                log_str = 'request report api data status is not success'
                print MyTime.get_local_time(), '==>', log_str
                return False
        else:
            log_str = 'request report url status is not 200'
            print MyTime.get_local_time(), '==>', log_str
            return False

    except Exception, ex:
        log_str = 'connect %s error ' % api_url + str(ex)
        print MyTime.get_local_time(), '==>', log_str
示例#4
0
def get_filter_date(request):
    """
    日期处理
    :param request:
    :return:
    """
    if request.method == 'GET':
        bid = request.GET.get('bid')
    else:
        bid = json.loads(request.POST.get('bid'))
    report_type = models.BdpVisualReport.objects.filter(bdpvisualblockconfig__bid=bid).values('report_type')[0]['report_type']
    date_type = models.BdpVisualBlockConfig.objects.filter(bid=bid).values('date_type')[0]['date_type']
    # 初始化日期
    tmp_start_week, tmp_end_week = MyTime.get_week(MyTime.get_date(0))
    tmp_start_month, tmp_end_month = MyTime.get_month(MyTime.get_date(0))
    if request.method == 'GET':
        # 判断可视化报表类型月报或周报
        if report_type == 0:
            # 判断是否是按日类型0表示默认
            if date_type == 0:
                start_date = MyTime.date_to_dt(MyTime.date_before_day(tmp_start_week, 140))
                end_date = MyTime.date_to_dt(tmp_end_week)
            else:
                start_date = MyTime.date_to_dt(tmp_start_week)
                end_date = MyTime.date_to_dt(tmp_end_week)
        else:
            if date_type == 0:
                start_date = MyTime.add_months(datetime.today().replace(
                   int(tmp_start_month[0:4]), int(tmp_start_week[5:7]), int(tmp_start_month[8:])), -12)
                start_date = MyTime.date_to_dt(str(start_date)[0:10])
                end_date = MyTime.date_to_dt(tmp_end_month)
            else:
                start_date = MyTime.date_to_dt(tmp_start_month)
                end_date = MyTime.date_to_dt(tmp_end_month)
    # 含筛选条件的时间生成
    else:
        tmp_date = request.POST.get('date').split(' - ')
        if len(str(tmp_date[0])) == 7:
            if date_type == 0:
                start_date = ''.join(tmp_date[0].split('-'))+'01'
                end_date = ''.join(tmp_date[1].split('-'))+'01'
            else:
                start_date, end_date = MyTime.get_month(tmp_date[1]+'-01')
                start_date = MyTime.date_to_dt(start_date)
                end_date = MyTime.date_to_dt(end_date)
        else:
            if date_type == 0:
                start_date = ''.join(tmp_date[0].split('-'))
                end_date = MyTime.date_to_dt(MyTime.get_week(tmp_date[1])[0])
            else:
                start_date = MyTime.date_to_dt(MyTime.date_before_day(tmp_date[1], 7))
                end_date = MyTime.date_to_dt(tmp_date[1])
    return start_date, end_date
示例#5
0
def get_init_sql_where(request, block_obj):
    sql_where = ''

    if block_obj.report_type == 1:
        start_date = MyTime.date_to_dt(MyTime.get_date(20))
        end_date = MyTime.date_to_dt(MyTime.get_date(0))
    # 周报
    elif block_obj.report_type == 2:
        start_date, end_date = MyTime.get_week2(MyTime.get_date(180),
                                                MyTime.get_date(0))
        start_date = MyTime.date_to_dt(start_date)
        end_date = MyTime.date_to_dt(end_date)
    # 月报
    elif block_obj.report_type == 3:
        start_date, end_date = MyTime.get_month2(MyTime.get_date(360),
                                                 MyTime.get_date(0))
        start_date = MyTime.date_to_dt(start_date)
        end_date = MyTime.date_to_dt(end_date)
    # 年报
    elif block_obj.report_type == 4:
        start_date = '20090101'
        end_date = MyTime.date_to_dt(MyTime.get_date(0))
    elif block_obj.report_type == 5:
        start_date = None
        end_date = None
    else:
        start_date = MyTime.date_to_dt(MyTime.get_date(360))
        end_date = MyTime.date_to_dt(MyTime.get_date(0))

    if start_date:
        sql_where = "stat_date between %s and %s" % (start_date, end_date)
    return sql_where
示例#6
0
def get_date_change_sql_where(request, block_obj):
    sql_where = None
    # 对于表格来说:今天, 昨天, 最近7天, 最近30天, 上翻, 下翻

    # 对于图表来说:日报, 周报, 月报, 年报: 起始月份
    # 从那一天开始: 到那一天结束, 若没有传入结束日期, 则默认是后30个数据
    if block_obj == 0:
        start_date = MyTime.date_to_dt(request.GET.get('start_date'))
        if request.GET.get('end_date'):
            end_date = request.GET.get('end_date')
        else:
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
    elif block_obj == 1:
        if request.GET.get('get_today'):
            start_date = MyTime.date_to_dt(MyTime.get_date(0))
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
        elif request.GET.get('get_yesterday'):
            start_date = MyTime.date_to_dt(MyTime.get_date(1))
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
        elif request.GET.get('get_7_day'):
            start_date = MyTime.date_to_dt(MyTime.get_date(7))
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
        elif request.GET.get('get_30_today'):
            start_date = MyTime.date_to_dt(MyTime.get_date(30))
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
        else:
            start_date = MyTime.date_to_dt(MyTime.get_date(30))
            end_date = MyTime.date_to_dt(MyTime.get_date(0))
    else:
        start_date = None
        end_date = None

    if start_date:
        sql_where = "stat_date between %s and %s" % (start_date, end_date)

    return sql_where
示例#7
0
        pass

    report_name = sys.argv[1]
    sql = get_report_sql(sys.argv[2], {})
    col_name = sys.argv[3]
    col_name = col_name.replace(' ', '').split(',')

    mn_config = {
        'db': {
            'host': '192.168.1.118',
            'database': 'to8to_result',
            'user': '******',
            'password': '******'
        },
        'sql': sql,
        'col_name': col_name
    }

    today_date = MyTime.get_date(1)
    today_date = today_date.replace('-', '')
    dt_args = {'date_start': today_date, 'date_end': today_date}

    result = get_report_data(mn_config, dt_args)

    format_str = ''
    for key in result:
        format_str += key + ':' + str(result[key][0]) + ';'
    format_str = report_name + ':[' + format_str + ']'

    MyAlarm.send_mail_sms(format_str)