Beispiel #1
0
def handleTable(request):
    """
    选择数据表
    """
    if 'POST' == request.method:
        tables_str   = request.POST.get('table', '[]')
        chosen_tables = json.loads(tables_str)

        hk  = request.session.get('hk')
        st  = SqlExecutorMgr.stRestore(hk)
        tables_list = st.listTables() + st.listViews()

        # 注意传多个来怎么办
        unkonwn_tables = list(set(chosen_tables) - set(tables_list))

        if 0 == len(unkonwn_tables):
            map(lambda x: st.getStorage().reflect(x), chosen_tables)
            return HttpResponseRedirect( \
                '/connect/field/' + '?tables={}'.format(tables_str) \
            )
        else:
            res_dict = {'succ': False, 'msg': 'xxxxx'}
            return HttpResponse(res_dict, content_type='application/json')
    else:
        hk  = request.session.get('hk')
        st  = SqlExecutorMgr.stRestore(hk)
        tables_list = st.listTables() + st.listViews()

        return MyHttpJsonResponse( {'succ': True, \
                                    'data': json.dumps(tables_list)} )
Beispiel #2
0
def searchLatestData(hk, factor_list, group_list):
    """
    根据时间列,获取最新数据
    """
    select_factors, group_factors  = [], []
    for factor in factor_list:
        kind    = factor.getProperty(Protocol.Kind)
        if 0 == kind:
            select_factors.append(factor)
        elif 1 == kind:
            select_factors.append(factor)
            group_factors.append(factor)
        else:
            select_factors.append(factor)
            group_factors.append(factor)


    for factor in group_list:
        select_factors.append(factor)
        group_factors.append(factor)       

    st  = SqlExecutorMgr.stRestore(hk)
    resultes = st.exeSelect(selects = select_factors, groups = group_factors) \
                    .fetchone()
    return resultes
Beispiel #3
0
def genWidgetImageData(req_data, hk):
    """
    生成返回前端数据
    """
    logger.debug("function genWidgetImageData() is called")
    st = SqlExecutorMgr.stRestore(hk)

    # 地图先特殊对待
    if 'china_map' == req_data.get(u'graph') or \
            'world_map' == req_data.get(u'graph'):
        data = formatData('', '', '', '', req_data.get(u'graph'))
        return {u'type': 'map', u'data': data}

    shape_list, shape_in_use    = judgeWhichShapes(req_data)
    shape_in_use                = req_data.get(u'graph', u'bar')

    # 获取画出图形所必须相关数据
    factors_lists_dict = classifyFactors(req_data)
    sql_obj         = transReqDataToSqlObj(req_data, st)
    result          = st.conn.execute(sql_obj).fetchall()
    data_from_db    = cleanDataFromDb(result)
    strf_data_from_db = strfDataAfterFetchDb(data_from_db)
    echart_data     = formatData(strf_data_from_db, factors_lists_dict['msu'], \
                                    factors_lists_dict['msn'], factors_lists_dict['group'], \
                                    shape_in_use)

    return {u'type': shape_in_use, u'data': echart_data}
Beispiel #4
0
def handleUsedTable(request, wi_id):
    try:
        model = WidgetModel.objects.select_related().get(pk = wi_id)
        hk = model.getConn().getPk()
        st = SqlExecutorMgr.stRestore(hk)
        used = model.restoreUsedTables()
        all = st.listTables() + st.listViews()
    except ExternalDbModel.DoesNotExist, e:
        return MyHttpJsonResponse({'succ': False})
Beispiel #5
0
def handleField(request):
    """
    处理自定义数据表请求
    """
    hk = request.session.get('hk')
    st = SqlExecutorMgr.stRestore(hk)
    logger.info('hk = {}'.format(hk))
    try:
        conn = ExternalDbModel.objects.get(pk = hk)
    except ExternalDbModel.DoesNotExist, e:
        return MyHttpJsonResponse({'succ': False, 'msg': 'xxxxx'})
Beispiel #6
0
def reqTimelyData(request, wi_id):
    '''
    获取及时的新数据
    '''
    hk = request.session.get('hk')
    st = SqlExecutorMgr.stRestore(hk)

    widget_model = WidgetModel.objects.get(pk = wi_id)
    if not widget_model.m_if_update:
        return MyHttpJsonResponse({'succ': False})

    req_data = widget_model.restoreReqDataDict()
    factors_lists_dict = classifyFactors(req_data)

    # 看看用户选择的轴上面的项,有没有涉及到DateTime类型
    # 如果没有,那么就是整体刷新; 如果有,那么来逐步更新
    time_factor = filterDateTimeColumn(factors_lists_dict['msn'], st)
    if time_factor:
        update_way = 'add'
        origin_sql_obj = transReqDataToSqlObj(req_data, st)
        time_column_obj = st.sql_relation.getColumnObj(time_factor)

        # 分查询条件里面,有没有聚合运算
        #if hasAggreate():
        if False:
            sql_obj = select([]).over(f()).order_by()
            origin_sql_obj.over(f()).order_by(time_column_obj)
        else:
            sql_obj = origin_sql_obj.order_by(time_column_obj)

            factor_dict = dict(zip(EXPRESS_FACTOR_KEYS_TUPLE, ( \
                time_factor.getProperty(Protocol.Table) 
                , time_factor.getProperty(Protocol.Attr)
                , time_factor.getProperty(Protocol.Kind)
                , 'max'
            )))
            latest_time_obj = st.makeSelectSql([FactorFactory.make(**factor_dict)])
            sql_obj = origin_sql_obj.where(time_column_obj == latest_time_obj)

        data_from_db = st.execute(sql_obj).fetchall()
        str_data = strfDataAfterFetchDb(data_from_db)
        data = EChartManager().get_echart(widget_model.m_graph) \
                                .makeAddData( \
                                    str_data \
                                    , len(factors_lists_dict['msu']) \
                                    , len(factors_lists_dict['msn']) \
                                    , len(factors_lists_dict['group']) \
                                )
    else:
        update_way = 'all'
        data = genWidgetImageData(req_data, hk)

    return MyHttpJsonResponse({'succ': True, 'way': update_way, 'data': data})
Beispiel #7
0
def handleDistinct(request):
    req = json.loads(request.GET.get(Protocol.FilterColumn))
    info = map(lambda x: (x, req.get(x)), EXPRESS_FACTOR_KEYS_TUPLE)
    factor = FactorFactory.make(dict(info))

    try:
        hk  = request.session.get('hk')
        st  = SqlExecutorMgr.stRestore(hk)
        sql_obj = st.getSwither().makeSelectSql( \
            [factor], [], [], distinct = True \
        )
        data = st.execute(sql_obj).fetchall()
        result = zip(*data)[0]
    except ExternalDbModel.DoesNotExist, e:
        logExcInfo()
        resp = {'succ': False, 'msg': 'yyyyyyyyy'}
Beispiel #8
0
 def __init__(self, wi_id):
     self.widget = WidgetModel.objects.get(pk = wi_id)
     self.hk = self.widget.getConn().pk
     self.st = SqlExecutorMgr.stRestore(self.hk)
     self.processor = None
Beispiel #9
0
 def __init__(self, hk):
     self.st = SqlExecutorMgr.stRestore(hk)
Beispiel #10
0
 def __init__(self, evModel, hk):
     self.ev = evModel
     self.st = SqlExecutorMgr.stRestore(hk)