Elasticsearch+spring boot(二)logstash自动生成的数据如何清洗+聚合?(多折线图)

一、entity 

由于日志生成类型全部是String,所以只能用String类型的接收数据

但是可以在get方法中改变类型!

Document(indexName = "consume",type = "client")
public class Consume {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Id
    private String id;
    @JsonProperty(value = "gender")
    private String userGender;
    @JsonProperty(value = "time")
    private String consumeDate;
    @JsonProperty(value = "money")
    private String consumeMoney;

    public void setId(String id) {
        this.id = id;
    }

    public void setUserGender(String userGender) {
        this.userGender = userGender;
    }

    public void setConsumeDate(String consumeDate) {
        this.consumeDate = consumeDate;
    }

    public void setConsumeMoney(String consumeMoney) {
        this.consumeMoney = consumeMoney;
    }

    public String getUserGender() {
        return userGender;
    }

    public Date getConsumeDate() throws ParseException {
        return sdf.parse(consumeDate);
    }

    public double getConsumeMoney() {
        return Double.parseDouble(consumeMoney.replaceAll(",",""))/10000;
    }

二、chart包中有

chart<T>  ---- 父类,呈现在前端

Line<T>  ------  存放不同分组的data,比如"男""女"的分组--> List<Line<T>>

    -------  查的时候可以根据结构塞值进去

MultiLine<T> ----- 继承chart<T>,包含legendData(多折线图的分组)

class Chart<T> {
    private List<String> xAxisData;
    private List<T> seriesData;


    protected Chart(){
        this.xAxisData = new ArrayList<>();
        this.seriesData = new ArrayList<>();
    }

    public void setxAxisData(String xAixs){
        this.xAxisData.add(xAixs);
    }

    public void setSeriesData(T t){
        this.seriesData.add(t);
    }

    public List<String> getxAxisData() {
        return xAxisData;
    }

    public List<T> getSeriesData() {
        return seriesData;
    }
}
public class MultiLine<T> extends Chart<T> {
    private List<String> legendData;

  // 当子类get(),父类的x,y自动new完成,子类的legenddata也new完成;
public static <T> MultiLine<T> get(){ return new MultiLine<>(); } private MultiLine() { // 当一个类继承了某个类时,在子类的构造方法里,super()必须先被调用;如果你没有写,编译器会自动调用super()方法 super(); this.legendData = new ArrayList<>(); } public List<String> getLegendData() { return legendData; } public void setLegendData(String legend) { this.legendData.add(legend) ; } }
public class Line<T> {
    private String name;
    private String type = "line";
    private String stack = "总量";
    private List<T> data;

  // get时自动new num大小list的data
public static Line get(String name,int...initialCapacity){ return new Line(name,initialCapacity); } public void setData(T value){ this.data.add(value); } private Line(String name,int...initialCapacity){ this.name = name; this.data = new ArrayList<>(null != initialCapacity && 0<initialCapacity[0] ? initialCapacity[0] : 10); } public String getName() { return name; } public String getType() { return type; } public String getStack() { return stack; } public List<T> getData() { return data; } }

三、service

***核心代码***

@Service
public class ConsumeService {
    @Autowired
    private ConsumeDao cd;

    private Iterable<Consume> findAll(){
        return cd.findAll();
    }

    public class Value{
        private double value = 0;

        public void setValue(double value){
            this.value += value;
        }

        public double getValue(){
            return value;
        }
    }

    public MultiLine<Line<Value>> lines() throws ParseException {
        MultiLine<Line<Value>> lines = MultiLine.get();
    // 1. 分组   lines.getLegendData().addAll(Arrays.asList(
"男","女")); // 2. x轴数据 List<String> xAxis = new ArrayList<>(12); for (int i = 1; i <=12 ; i++) { xAxis.add(i+"月"); } // list -> 男line 女line // -> 每个line装12个data(list<Value>) lines.getxAxisData().addAll(xAxis);
     // 3. 结构创建:男Line,女Line List
<Line<Value>> list = Arrays.asList( Line.get("男",12), Line.get("女",12) ); for (int i = 0; i < list.size(); i++) { for (int j = 0; j <12 ; j++) { // 第一个line,i=1时,进12个Value; // 第二个line 同理 list.get(i).setData(new Value()); } } // 4. 塞值进结构 Iterator<Consume> it = findAll().iterator(); while (it.hasNext()){ Consume c = it.next(); // 先判断男女?月份?作为index塞进list的值中 int genderIndex = c.getUserGender().equals("男")? 0 : 1; int month = c.getConsumeDate().getMonth(); list.get(genderIndex).getData().get(month).setValue(c.getConsumeMoney()); }
     // 5. 塞值进前端
for (Line<Value> line : list) { lines.setSeriesData(line); } return lines; }

 

posted @ 2020-08-23 22:24  PEAR2020  阅读(417)  评论(0编辑  收藏  举报