Search in sources :

Example 1 with LabelIndexModel

use of org.sagacity.sqltoy.config.model.LabelIndexModel in project sagacity-sqltoy by chenrenfei.

the class GroupSummaryTest method testParseColumns.

@Test
public void testParseColumns() {
    String columns = "1..column9?2";
    LabelIndexModel labelIndexMap = new LabelIndexModel();
    for (int i = 0; i < 10; i++) {
        labelIndexMap.put("column" + i, i);
    }
    int width = 10;
    List<Integer> result = parseColumns(labelIndexMap, columns, width);
    System.err.println(JSON.toJSONString(result));
}
Also used : LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) Test(org.junit.jupiter.api.Test)

Example 2 with LabelIndexModel

use of org.sagacity.sqltoy.config.model.LabelIndexModel in project sagacity-sqltoy by chenrenfei.

the class UnPivotListTest method testUnpivotSingle.

@Test
public void testUnpivotSingle() {
    // |------- 1月-------|------- 2月 ------|------ 3月--------|
    // |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
    Object[][] values = { { "5月", "香蕉", 2000, 20000 } };
    List result = CollectionUtil.arrayToDeepList(values);
    UnpivotModel unpivotModel = new UnpivotModel();
    unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
    unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
    DataSetResult resultModel = new DataSetResult();
    resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT" });
    resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal" });
    LabelIndexModel labelIndexMap = new LabelIndexModel();
    labelIndexMap.put("month", 0);
    labelIndexMap.put("fruitname", 1);
    labelIndexMap.put("quantity", 2);
    labelIndexMap.put("amt", 3);
    List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
    for (int i = 0; i < value.size(); i++) {
        System.out.println(JSON.toJSONString(result.get(i)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 3 with LabelIndexModel

use of org.sagacity.sqltoy.config.model.LabelIndexModel in project sagacity-sqltoy by chenrenfei.

the class UnPivotListTest method testUnpivotMiddle.

@Test
public void testUnpivotMiddle() {
    // |------- 1月-------|------- 2月 ------|------ 3月--------|
    // |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
    Object[][] values = { { "5月", "香蕉", 2000, 20000, "好" }, { "5月", "苹果", 1900, 38999, "好" }, { "4月", "香蕉", 1800, 21000, "好" }, { "4月", "苹果", 1800, 400000, "好" } };
    List result = CollectionUtil.arrayToDeepList(values);
    UnpivotModel unpivotModel = new UnpivotModel();
    unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
    unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
    DataSetResult resultModel = new DataSetResult();
    resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT", "greet" });
    resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal", "string" });
    LabelIndexModel labelIndexMap = new LabelIndexModel();
    labelIndexMap.put("month", 0);
    labelIndexMap.put("fruitname", 1);
    labelIndexMap.put("quantity", 2);
    labelIndexMap.put("amt", 3);
    List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
    for (int i = 0; i < value.size(); i++) {
        System.out.println(JSON.toJSONString(result.get(i)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 4 with LabelIndexModel

use of org.sagacity.sqltoy.config.model.LabelIndexModel in project sagacity-sqltoy by chenrenfei.

the class UnPivotListTest method testUnpivot.

@Test
public void testUnpivot() {
    // |------- 1月-------|------- 2月 ------|------ 3月--------|
    // |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
    Object[][] values = { { "5月", "香蕉", 2000, 20000 }, { "5月", "苹果", 1900, 38999 }, { "4月", "香蕉", 1800, 21000 }, { "4月", "苹果", 1800, 400000 } };
    List result = CollectionUtil.arrayToDeepList(values);
    UnpivotModel unpivotModel = new UnpivotModel();
    unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
    unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
    DataSetResult resultModel = new DataSetResult();
    resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT" });
    resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal" });
    LabelIndexModel labelIndexMap = new LabelIndexModel();
    labelIndexMap.put("month", 0);
    labelIndexMap.put("fruitname", 1);
    labelIndexMap.put("quantity", 2);
    labelIndexMap.put("amt", 3);
    List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
    for (int i = 0; i < value.size(); i++) {
        System.out.println(JSON.toJSONString(result.get(i)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 5 with LabelIndexModel

use of org.sagacity.sqltoy.config.model.LabelIndexModel in project sagacity-sqltoy by chenrenfei.

the class ResultUtils method calculate.

/**
 * @todo 对查询结果进行计算处理:字段脱敏、格式化、数据旋转、同步环比、分组汇总等
 * @param desensitizeProvider
 * @param sqlToyConfig
 * @param dataSetResult
 * @param pivotCategorySet
 * @param extend
 * @return
 */
public static boolean calculate(DesensitizeProvider desensitizeProvider, SqlToyConfig sqlToyConfig, DataSetResult dataSetResult, List pivotCategorySet, QueryExecutorExtend extend) {
    List items = dataSetResult.getRows();
    // 数据为空直接跳出处理
    if (items == null || items.isEmpty()) {
        return false;
    }
    boolean changedCols = false;
    List<SecureMask> secureMasks = sqlToyConfig.getSecureMasks();
    List<FormatModel> formatModels = sqlToyConfig.getFormatModels();
    List resultProcessors = new ArrayList();
    if (!sqlToyConfig.getResultProcessor().isEmpty()) {
        resultProcessors.addAll(sqlToyConfig.getResultProcessor());
    }
    if (extend != null && !extend.calculators.isEmpty()) {
        resultProcessors.addAll(extend.calculators);
    }
    // 整理列名称跟index的对照map
    LabelIndexModel labelIndexMap = null;
    if (!secureMasks.isEmpty() || !formatModels.isEmpty() || (extend != null && (!extend.secureMask.isEmpty() || !extend.colsFormat.isEmpty())) || !resultProcessors.isEmpty()) {
        labelIndexMap = wrapLabelIndexMap(dataSetResult.getLabelNames());
    }
    // 字段脱敏
    if (!secureMasks.isEmpty()) {
        secureMask(desensitizeProvider, items, secureMasks.iterator(), labelIndexMap);
    }
    // 自动格式化
    if (!formatModels.isEmpty()) {
        formatColumn(items, formatModels.iterator(), labelIndexMap);
    }
    // 扩展脱敏和格式化处理
    if (extend != null) {
        if (!extend.secureMask.isEmpty()) {
            secureMask(desensitizeProvider, items, extend.secureMask.values().iterator(), labelIndexMap);
        }
        if (!extend.colsFormat.isEmpty()) {
            formatColumn(items, extend.colsFormat.values().iterator(), labelIndexMap);
        }
    }
    // 计算
    if (!resultProcessors.isEmpty()) {
        Object processor;
        for (int i = 0; i < resultProcessors.size(); i++) {
            processor = resultProcessors.get(i);
            // 数据旋转(行转列)
            if (processor instanceof PivotModel) {
                items = pivotResult((PivotModel) processor, labelIndexMap, items, pivotCategorySet);
                changedCols = true;
            } else // 列转行
            if (processor instanceof UnpivotModel) {
                items = UnpivotList.process((UnpivotModel) processor, dataSetResult, labelIndexMap, items);
            } else if (processor instanceof SummaryModel) {
                // 数据汇总合计
                GroupSummary.process((SummaryModel) processor, labelIndexMap, items);
            } else if (processor instanceof ColsChainRelativeModel) {
                // 列数据环比
                ColsChainRelative.process((ColsChainRelativeModel) processor, labelIndexMap, items);
                changedCols = true;
            } else if (processor instanceof RowsChainRelativeModel) {
                // 行数据环比
                RowsChainRelative.process((RowsChainRelativeModel) processor, labelIndexMap, items);
            } else if (processor instanceof ReverseModel) {
                // 数据反序
                ReverseList.process((ReverseModel) processor, labelIndexMap, items);
            }
        }
        dataSetResult.setRows(items);
    }
    return changedCols;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) ColsChainRelativeModel(org.sagacity.sqltoy.config.model.ColsChainRelativeModel) RowsChainRelativeModel(org.sagacity.sqltoy.config.model.RowsChainRelativeModel) FormatModel(org.sagacity.sqltoy.config.model.FormatModel) ArrayList(java.util.ArrayList) SummaryModel(org.sagacity.sqltoy.config.model.SummaryModel) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) SecureMask(org.sagacity.sqltoy.config.model.SecureMask) ReverseModel(org.sagacity.sqltoy.config.model.ReverseModel)

Aggregations

LabelIndexModel (org.sagacity.sqltoy.config.model.LabelIndexModel)8 List (java.util.List)6 Test (org.junit.jupiter.api.Test)6 UnpivotModel (org.sagacity.sqltoy.config.model.UnpivotModel)4 UnpivotList (org.sagacity.sqltoy.plugins.calculator.UnpivotList)4 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)3 ColsChainRelativeModel (org.sagacity.sqltoy.config.model.ColsChainRelativeModel)2 RowsChainRelativeModel (org.sagacity.sqltoy.config.model.RowsChainRelativeModel)2 ArrayList (java.util.ArrayList)1 FormatModel (org.sagacity.sqltoy.config.model.FormatModel)1 PivotModel (org.sagacity.sqltoy.config.model.PivotModel)1 ReverseModel (org.sagacity.sqltoy.config.model.ReverseModel)1 SecureMask (org.sagacity.sqltoy.config.model.SecureMask)1 SummaryModel (org.sagacity.sqltoy.config.model.SummaryModel)1 ReverseList (org.sagacity.sqltoy.plugins.calculator.ReverseList)1