BrightSide Workbench Full Report + Source Code
SystemLogAccumulator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.log;
20 
21 import java.util.Date;
22 import org.amic.util.date.CheckDate;
23 import org.turro.elephant.db.ElephantPU;
24 import org.turro.elephant.entities.db.SystemLog;
25 import org.turro.jpa.Dao;
26 import org.turro.sql.SqlClause;
27 import org.turro.string.Words;
28 
33 public class SystemLogAccumulator implements Runnable {
34 
35  private static final int MAX_YEARS = 5;
36  private final Dao dao;
37 
38  public static void accumulate() {
39  SystemLogAccumulator accumulator = new SystemLogAccumulator(new ElephantPU());
40  new Thread(accumulator, "ElephantLog accumulator").start();
41  }
42 
43  public SystemLogAccumulator(Dao dao) {
44  this.dao = dao;
45  }
46 
47 /*
48  @Id private Date dateLog;
49  @Id private SystemLogType logType;
50  @Id private String generatorPath;
51  @Id private String entityPath;
52  private String generatorName;
53  private String entityName;
54  private String comment;
55  private int count;
56  private byte[] data;
57  */
58 
59  @Override
60  public void run() {
61  Date until = new CheckDate().addYears(-MAX_YEARS).getDate();
62  ensureCounts();
63  SqlClause.select(Words.csv("l.logType").add("l.generatorPath").add("l.entityPath")
64  .add("l.generatorName").add("l.entityName").add("l.comment")
65  .add("sum(l.counts)").add("max(l.dateLog)").toString())
66  .from("SystemLog l")
67  .where().smallerOrEqual("l.dateLog", until)
68  .groupBy(Words.csv("l.logType").add("l.generatorPath").add("l.entityPath")
69  .add("l.generatorName").add("l.entityName").add("l.comment").toString())
70  .dao(dao)
71  .resultList(Object[].class).forEach(c -> {
72  SystemLog sl = new SystemLog();
73  sl.setLogType((SystemLogType) c[0]);
74  sl.setGeneratorPath((String) c[1]);
75  sl.setEntityPath((String) c[2]);
76  sl.setGeneratorName((String) c[3]);
77  sl.setEntityName((String) c[4]);
78  sl.setComment((String) c[5]);
79  sl.setCounts((int)(long) c[6]);
80  sl.setDateLog(until);
81  sl.setData(getDataFrom(sl, (Date) c[7]));
82  dao.saveObject(sl);
83  });
84  SqlClause.delete("SystemLog").where().smaller("logDate", until);
85  }
86 
87  private void ensureCounts() {
88  SqlClause.select(Words.csv("l.logType").add("l.generatorPath").add("l.entityPath")
89  .add("l.generatorName").add("l.entityName").add("l.comment")
90  .add("min(l.dateLog)").toString())
91  .from("SystemLog l")
92  .groupBy(Words.csv("l.logType").add("l.generatorPath").add("l.entityPath")
93  .add("l.generatorName").add("l.entityName").add("l.comment").toString())
94  .dao(dao)
95  .resultList(Object[].class).forEach(c -> {
96  SqlClause.update("SystemLog")
97  .set("counts", 1)
98  .where().equal("logType", (SystemLogType) c[0])
99  .and().equal("generatorPath", (String) c[1])
100  .and().equal("entityPath", (String) c[2])
101  .and().equal("generatorName", (String) c[3])
102  .and().equal("entityName", (String) c[4])
103  .and().equal("comment", (String) c[5])
104  .and().greater("dateLog", (Date) c[6])
105  .dao(dao)
106  .execute();
107  });
108  /*
109  SqlClause.update("SystemLog sl")
110  .set("sl.counts", 1)
111  .where("sl.dateLog >").sub(
112  SqlClause.select("min(l.dateLog)").from("SystemLog l")
113  .where("l.logType = sl.logType")
114  .and("l.generatorPath = sl.generatorPath")
115  .and("l.entityPath = sl.entityPath")
116  .and("l.generatorName = sl.generatorName")
117  .and("l.entityName = sl.entityName")
118  .and("l.comment = sl.comment")
119  .groupBy(Words.csv("l.logType").add("l.generatorPath").add("l.entityPath")
120  .add("l.generatorName").add("l.entityName").add("l.comment").toString()))
121  .dao(dao)
122  .execute();
123  */
124  SqlClause.update("SystemLog")
125  .set("counts", 1)
126  .where().smallerOrEqual("counts", 0)
127  .dao(dao)
128  .execute();
129  }
130 
131  private byte[] getDataFrom(SystemLog sl, Date date) {
132  return SqlClause.select("l.data")
133  .from("SystemLog l")
134  .where().equal("l.dateLog", date)
135  .and().equal("l.logType", sl.getLogType())
136  .and().equal("l.generatorPath", sl.getGeneratorPath())
137  .and().equal("l.entityPath", sl.getEntityPath())
138  .and().equal("l.generatorName", sl.getGeneratorName())
139  .and().equal("l.entityName", sl.getEntityName())
140  .and().equal("l.comment", sl.getComment())
141  .dao(dao)
142  .singleResult(byte[].class);
143  }
144 
145 }
void setEntityName(String entityName)
Definition: SystemLog.java:108
void setLogType(SystemLogType logType)
Definition: SystemLog.java:76
void setGeneratorPath(String generatorPath)
Definition: SystemLog.java:84
void setGeneratorName(String generatorName)
Definition: SystemLog.java:100
void setEntityPath(String entityPath)
Definition: SystemLog.java:92