Example #1
0
def slowquery_review(request):
    instance_name = request.POST.get('instance_name')
    DBName = request.POST.get('db_name')
    StartTime = request.POST.get('StartTime')
    EndTime = request.POST.get('EndTime')
    limit = request.POST.get('limit')
    offset = request.POST.get('offset')

    # 计算页数
    PageNumber = (int(offset) + int(limit)) / int(limit)
    values = {"PageSize": int(limit), "PageNumber": int(PageNumber)}
    # DBName非必传
    if DBName:
        values['DBName'] = DBName

    # UTC时间转化成阿里云需求的时间格式
    StartTime = '%sZ' % StartTime
    EndTime = '%sZ' % EndTime

    # 通过实例名称获取关联的rds实例id
    instance_info = AliyunRdsConfig.objects.get(instance_name=instance_name)
    # 调用aliyun接口获取SQL慢日志统计
    slowsql = Aliyun().DescribeSlowLogs(instance_info.rds_dbinstanceid, StartTime, EndTime, **values)

    # 解决table数据丢失精度、格式化时间
    SQLSlowLog = json.loads(slowsql)['Items']['SQLSlowLog']
    for SlowLog in SQLSlowLog:
        SlowLog['SQLId'] = str(SlowLog['SQLId'])
        SlowLog['CreateTime'] = Aliyun.aliyun_time_format(SlowLog['CreateTime'])

    result = {"total": json.loads(slowsql)['TotalRecordCount'], "rows": SQLSlowLog,
              "PageSize": json.loads(slowsql)['PageRecordCount'], "PageNumber": json.loads(slowsql)['PageNumber']}
    # 返回查询结果
    return result
Example #2
0
def slowquery_review_history(request):
    instance_name = request.POST.get('instance_name')
    start_time = request.POST.get('StartTime')
    end_time = request.POST.get('EndTime')
    db_name = request.POST.get('db_name')
    sql_id = request.POST.get('SQLId')
    limit = request.POST.get('limit')
    offset = request.POST.get('offset')

    # 计算页数
    page_number = (int(offset) + int(limit)) / int(limit)
    values = {"PageSize": int(limit), "PageNumber": int(page_number)}
    # SQLId、DBName非必传
    if sql_id:
        values['SQLHASH'] = sql_id
    if db_name:
        values['DBName'] = db_name

    # UTC时间转化成阿里云需求的时间格式
    start_time = datetime.datetime.strptime(
        start_time, "%Y-%m-%d").date() - datetime.timedelta(days=1)
    start_time = '%sT16:00Z' % start_time
    end_time = '%sT15:59Z' % end_time

    # 通过实例名称获取关联的rds实例id
    instance_info = AliyunRdsConfig.objects.get(
        instance__instance_name=instance_name)
    # 调用aliyun接口获取SQL慢日志统计
    slowsql = Aliyun().DescribeSlowLogRecords(instance_info.rds_dbinstanceid,
                                              start_time, end_time, **values)

    # 格式化时间\过滤HostAddress
    sql_slow_record = json.loads(slowsql)['Items']['SQLSlowRecord']
    for SlowRecord in sql_slow_record:
        SlowRecord['ExecutionStartTime'] = Aliyun.aliyun_time_format(
            SlowRecord['ExecutionStartTime']).strftime("%Y-%m-%d %H:%M:%S")
        SlowRecord['HostAddress'] = SlowRecord['HostAddress'].split('[')[0]

    result = {
        "total": json.loads(slowsql)['TotalRecordCount'],
        "rows": sql_slow_record,
        "PageSize": json.loads(slowsql)['PageRecordCount'],
        "PageNumber": json.loads(slowsql)['PageNumber']
    }

    # 返回查询结果
    return result