Skip to content

Commit

Permalink
new API
Browse files Browse the repository at this point in the history
Signed-off-by: hubin6 <[email protected]>
  • Loading branch information
hubin6 committed Nov 7, 2017
1 parent 78e5588 commit b81ff90
Show file tree
Hide file tree
Showing 39 changed files with 829 additions and 63 deletions.
5 changes: 5 additions & 0 deletions dashboard-domain-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.fresh.commons</groupId>-->
<!--<artifactId>fresh-commons-data</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jd.logistics.cloud.data.domain;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @Author hubin
* @Description:
* @Date 2017/11/6 10:39
*/
@Data
public class BaseModel implements Serializable {
private static final long serialVersionUID = 7593767886021869685L;
private String name;
private List<ChartModel> charts;
private List<ValueModel> values;

public String getName() {
return name;
}

public List<ChartModel> getCharts() {
return charts;
}

public List<ValueModel> getValues() {
return values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jd.logistics.cloud.data.domain;

import lombok.Data;

import java.io.Serializable;

/**
* @Author hubin
* @Description:
* @Date 2017/11/6 10:40
*/
@Data
public class ChartModel implements Serializable {
private static final long serialVersionUID = -1320829926542972100L;
private String name;
private String sql;

public String getName() {
return name;
}

public String getSql() {
return sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jd.logistics.cloud.data.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author hubin
* @Description:
* @Date 2017/11/2 11:21
*/
public class CommonRes implements Serializable {
private static final long serialVersionUID = 2211076189595925918L;
private Map<String, List<Object>> arrayResult = new HashMap<>();
private Map<String, Object> singleResult = new HashMap<>();

public void addItem(String key, Object value) {
if (arrayResult.get(key) == null) {
List<Object> i = new ArrayList<>();
i.add(value);
arrayResult.put(key, i);
} else {
List<Object> i = arrayResult.get(key);
i.add(value);
}
}

public void putItem(String key, Object value) {
singleResult.put(key, value);
}

public Map<String, List<Object>> getArrayResult() {
return arrayResult;
}

public Map<String, Object> getSingleResult() {
return singleResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @Date 2017/10/31 14:38
*/
@Data
public class Dim implements Serializable{
public class DateCycle implements Serializable {
private static final long serialVersionUID = 8657041672009519925L;
private long id;
private String name;
private String dateCycle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jd.logistics.cloud.data.domain;

import lombok.Data;

import java.io.Serializable;

/**
* @Author hubin
* @Description:
* @Date 2017/11/6 10:40
*/
@Data
public class ValueModel implements Serializable {
private static final long serialVersionUID = 5265311276161051035L;
private String name;
private String sql;

public String getName() {
return name;
}

public String getSql() {
return sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jd.logistics.cloud.data.domain;

import lombok.Data;

import java.io.Serializable;

/**
* @Author hubin
* @Description:
* @Date 2017/10/31 14:38
*/
@Data
public class Warehouse implements Serializable {
private static final long serialVersionUID = 8657041672009519925L;
private long id;
private String warehouseName;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.jd.logistics.cloud.data.repository;

import com.jd.logistics.cloud.data.domain.Dim;
import com.jd.logistics.cloud.data.domain.DimQuery;
import com.jd.logistics.cloud.data.domain.Function;
import com.jd.logistics.cloud.data.domain.GenericRes;
import com.jd.logistics.cloud.data.domain.DateCycle;
import com.jd.logistics.cloud.data.domain.Warehouse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

Expand All @@ -18,10 +14,10 @@
*/
@Mapper
public interface DimRepository {
@Select("SELECT * FROM T_WAREHOUSE")
List<Dim> getWarehouses();
@Select("SELECT id, warehouse_name as warehouseName FROM T_WAREHOUSE")
List<Warehouse> getWarehouses();

@Select("SELECT * FROM T_DATECYCLE")
List<Dim> getDateCycles();
@Select("SELECT id, date_cycle as dateCycle FROM T_DATE_CYCLE")
List<DateCycle> getDateCycles();

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.jd.logistics.cloud.data.repository;

import com.jd.logistics.cloud.data.domain.Function;
import com.jd.logistics.cloud.data.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

Expand All @@ -14,12 +16,12 @@
@Mapper
public interface FuncRepository {

@Select("SELECT id, name as funcName, type as funcType, seq FROM T_FUNCTION where type=#{type}order by seq")
@Select("SELECT id, func_name as funcName, func_type_id as funcType, seq FROM T_FUNCTION where func_type_id=#{type} order by seq")
List<Function> getByType(@Param("type") int type);

@Select("SELECT id, name as funcName, type as funcType, seq FROM T_FUNCTION order by seq")
@Select("SELECT id, func_name as funcName, func_type_id as funcType, seq FROM T_FUNCTION order by seq")
List<Function> getAll();

@Update("UPDATE T_FUNCTION SET type=#{func.funcType}, seq=#{func.seq} WHERE name=#{func.funcName}")
@Update("UPDATE T_FUNCTION SET func_type_id=#{func.funcType}, seq=#{func.seq} WHERE func_name=#{func.funcName}")
void update(@Param("func") Function func);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jd.logistics.cloud.data.service;

import java.util.Map;

/**
* @Author hubin
* @Description:
* @Date 2017/11/7 9:22
*/
public interface ChartService {
Map getFuncChart(String funcName);

String getFuncChartOption(String funcName, String chartId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.jd.logistics.cloud.data.service;

import com.alibaba.fastjson.JSON;
import com.jd.logistics.cloud.data.domain.BaseModel;
import com.jd.logistics.cloud.data.domain.ChartModel;
import com.jd.logistics.cloud.data.domain.CommonRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author hubin
* @Description:
* @Date 2017/11/7 9:26
*/
@Service
public class ChartServiceImpl implements ChartService {
@Autowired
JdbcTemplate jdbcTemplate;

private Map<String, String> getChartSql(String funcName) {
String json = StringUtils.getStringFromResourcePath(Constant.TEMPLATE_PARENT_FOLDER + "/" +
funcName + "/" + Constant.TEMPLATE_MODEL_FILE);
BaseModel bm = JSON.parseObject(json, BaseModel.class);
System.out.println(bm.getName());
Map<String, String> sqlList = new HashMap<>();
for (ChartModel model : bm.getCharts()) {
sqlList.put(model.getName(), StringUtils.getStringFromResourcePath(Constant.TEMPLATE_PARENT_FOLDER + "/" +
funcName + "/" + model.getSql()));
}
return sqlList;
}

private Map<String, List<Object>> RowSet2ArrayRes(SqlRowSet rowSet) {
SqlRowSetMetaData metaData = rowSet.getMetaData();
String[] colNames = metaData.getColumnNames();
CommonRes cr = new CommonRes();
while (rowSet.next()) {
for (String col : colNames) {
String c = col.toLowerCase();
cr.addItem(c, rowSet.getObject(col));
}
}
return cr.getArrayResult();
}

@Override
public Map getFuncChart(String funcName) {
Map<String, String> sqlList = getChartSql(funcName);
Map res = new HashMap<>();
for (Map.Entry<String, String> e : sqlList.entrySet()) {
String sql = e.getValue();
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
res.put(e.getKey(), RowSet2ArrayRes(rowSet));
}
return res;
}

@Override
public String getFuncChartOption(String funcName, String chartId) {
return StringUtils.getStringFromResourcePath(Constant.TEMPLATE_PARENT_FOLDER + "/" +
funcName + "/" + chartId + Constant.CHART_OPTION_SUFFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jd.logistics.cloud.data.service;

/**
* @Author hubin
* @Description:
* @Date 2017/11/7 9:32
*/
public class Constant {
public static final String TEMPLATE_PARENT_FOLDER = "metrics";
public static final String CHART_OPTION_SUFFIX = ".chart.json";
public static final String CHART_TEMPLATE_SUFFIX = "chart.html";
public static final String VALUE_TEMPLATE_SUFFIX = "number.html";
public static final String TEMPLATE_MODEL_FILE = "model.json";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.jd.logistics.cloud.data.service;

import com.jd.logistics.cloud.data.domain.Dim;
import com.jd.logistics.cloud.data.domain.DimQuery;
import com.jd.logistics.cloud.data.domain.GenericRes;
import com.jd.logistics.cloud.data.domain.DateCycle;
import com.jd.logistics.cloud.data.domain.Warehouse;

import java.util.List;

Expand All @@ -12,8 +11,8 @@
* @Date 2017/10/31 14:39
*/
public interface DimService {
List<Dim> getWarehouses();
List<Warehouse> getWarehouses();

List<Dim> getDateCycles();
List<DateCycle> getDateCycles();

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.jd.logistics.cloud.data.service;

import com.jd.logistics.cloud.data.domain.Dim;
import com.jd.logistics.cloud.data.domain.DimQuery;
import com.jd.logistics.cloud.data.domain.GenericRes;
import com.jd.logistics.cloud.data.domain.DateCycle;
import com.jd.logistics.cloud.data.domain.Warehouse;
import com.jd.logistics.cloud.data.repository.DimRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -19,12 +18,12 @@ public class DimServiceImpl implements DimService {
@Autowired
DimRepository dimRepository;
@Override
public List<Dim> getWarehouses() {
public List<Warehouse> getWarehouses() {
return dimRepository.getWarehouses();
}

@Override
public List<Dim> getDateCycles() {
public List<DateCycle> getDateCycles() {
return dimRepository.getDateCycles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GenStatService {
public static List<Stat> statList = new ArrayList<>();
public static List<Function> functionList = new ArrayList<>();
static {
gen(rec_num);
// gen(rec_num);
}
private static void gen(int num){
Calendar date = Calendar.getInstance();
Expand Down Expand Up @@ -62,7 +62,7 @@ private static void gen(int num){
funcValue = 20000 + r.nextInt(3000) - 1500;
for(String index: item.getValue()) {
double indexValue;
if(idx<=1) indexValue = funcValue * 0.95;
if(idx<=1) indexValue = funcValue * (r.nextDouble()+0.45);
else indexValue = (double)((int)((r.nextDouble()-0.25)*1000))/100;
System.out.println(function+','+warehouse+','+item.getKey()+','+tmp_date+','+funcValue+','+index+','+indexValue);
idx++;
Expand Down
Loading

0 comments on commit b81ff90

Please sign in to comment.