Skip to content

Commit

Permalink
Merge pull request #3019 from SCADA-LTS/fix/#3017_errors_in_statistics
Browse files Browse the repository at this point in the history
#3017 errors in statistics:
  • Loading branch information
Limraj authored Sep 27, 2024
2 parents fc84a87 + 973a2bd commit 684a394
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
20 changes: 0 additions & 20 deletions src/com/serotonin/mango/rt/dataImage/DataPointRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,12 @@ public PointValueTime getPointValueAt(long time) {
public List<PointValueTime> getPointValues(long since) {
List<PointValueTime> result = pointValueService.getPointValues(
vo.getId(), since);

for (PointValueTime pvt : valueCache.getCacheContents()) {
if (pvt.getTime() >= since) {
int index = Collections.binarySearch(result, pvt,
pvtTimeComparator);
if (index < 0)
result.add(-index - 1, pvt);
}
}

return result;
}

public List<PointValueTime> getPointValuesBetween(long from, long to) {
List<PointValueTime> result = pointValueService
.getPointValuesBetween(vo.getId(), from, to);

for (PointValueTime pvt : valueCache.getCacheContents()) {
if (pvt.getTime() >= from && pvt.getTime() < to) {
int index = Collections.binarySearch(result, pvt,
pvtTimeComparator);
if (index < 0)
result.add(-index - 1, pvt);
}
}

return result;
}

Expand Down
10 changes: 4 additions & 6 deletions src/com/serotonin/mango/view/chart/StatisticsChartRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,23 @@ public void addDataToModel(Map<String, Object> model, DataPointVO point) {

// The start value is the value of the point at the start of the period for this renderer.
PointValueTime startValue = null;
if (values.size() == 0 || values.get(0).getTime() > startTime) {
if (!values.isEmpty()) {
// Get the value of the point at the start time
PointValueTime valueTime = pointValueFacade.getPointValueBefore(startTime);
if (valueTime != null)
startValue = new PointValueTime(valueTime.getValue(), startTime);
startValue = values.get(0);
}

if (startValue != null || values.size() > 0) {
if (dataTypeId == DataTypes.BINARY || dataTypeId == DataTypes.MULTISTATE) {
// Runtime stats
StartsAndRuntimeList stats = new StartsAndRuntimeList(startValue, values, startTime, startTime
+ getDuration());
model.put("start", stats.getRealStart());
model.put("start", stats.getStart());
model.put("end", stats.getEnd());
model.put("startsAndRuntimes", stats.getData());
}
else if (dataTypeId == DataTypes.NUMERIC) {
AnalogStatistics stats = new AnalogStatistics(startValue, values, startTime, startTime + getDuration());
model.put("start", stats.getRealStart());
model.put("start", stats.getStart());
model.put("end", stats.getEnd());
model.put("minimum", stats.getMinimum());
model.put("minTime", stats.getMinTime());
Expand Down
25 changes: 22 additions & 3 deletions src/com/serotonin/mango/view/stats/AnalogStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AnalogStatistics implements StatisticsGenerator {
private boolean noData = true;
private long realStart;
private final long end;
private final long start;

// State values.
private long lastTime = -1;
Expand All @@ -56,6 +57,7 @@ public AnalogStatistics(Double startValue, List<? extends IValueTime> values, lo
}

public AnalogStatistics(Double startValue, long start, long end) {
this.start = start;
this.end = end;

if (startValue != null) {
Expand Down Expand Up @@ -175,11 +177,28 @@ public String getHelp() {
return toString();
}

public long getStart() {
return start;
}

@Override
public String toString() {
return "{minimum: " + minimum + ", minTime=" + minTime + ", maximum: " + maximum + ", maxTime=" + maxTime
+ ", average: " + average + ", sum: " + sum + ", count: " + count + ", noData: " + noData
+ ", realStart: " + realStart + ", end: " + end + "}";
return "AnalogStatistics{" +
"minimum=" + minimum +
", minTime=" + minTime +
", maximum=" + maximum +
", maxTime=" + maxTime +
", average=" + average +
", sum=" + sum +
", count=" + count +
", noData=" + noData +
", realStart=" + realStart +
", start=" + start +
", end=" + end +
", lastTime=" + lastTime +
", realDuration=" + realDuration +
", lastValue=" + lastValue +
'}';
}

public static void main(String[] args) {
Expand Down
6 changes: 6 additions & 0 deletions src/com/serotonin/mango/view/stats/StartsAndRuntimeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public StartsAndRuntimeList(MangoValue startValue, List<? extends IValueTime> va
private long realStart = -1;
private MangoValue lastValue;
private StartsAndRuntime sar;
private final long start;

public StartsAndRuntimeList(MangoValue startValue, long start, long end) {
this.start = start;
this.end = end;
if (startValue != null) {
lastTime = start;
Expand Down Expand Up @@ -142,6 +144,10 @@ public String getHelp() {
return toString();
}

public long getStart() {
return start;
}

@Override
public String toString() {
return "{realStart: " + realStart + ", end: " + end + ", data: " + data.toString() + "}";
Expand Down
16 changes: 8 additions & 8 deletions src/org/scada_lts/dao/pointvalues/PointValueDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,30 @@ public class PointValueDAO implements GenericDaoCR<PointValue>, IPointValueDAO {

public static final String POINT_VALUE_FILTER_BASE_ON_DATA_POINT_ID_AND_TIME_STAMP = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? and "
+ "pv."+COLUMN_NAME_TIME_STAMP+" >= ? order by "+COLUMN_NAME_TIME_STAMP;
+ "pv."+COLUMN_NAME_TIME_STAMP+" >= ? order by pv."+COLUMN_NAME_TIME_STAMP+", pv." + COLUMN_NAME_ID;

public static final String POINT_VALUE_FILTER_BASE_ON_DATA_POINT_ID_AND_TIME_STAMP_FROM_TO = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? and "
+ "pv."+COLUMN_NAME_TIME_STAMP+">=? and pv."+COLUMN_NAME_TIME_STAMP+"<? order by "+COLUMN_NAME_TIME_STAMP;
+ "pv."+COLUMN_NAME_TIME_STAMP+">=? and pv."+COLUMN_NAME_TIME_STAMP+"<? order by pv."+COLUMN_NAME_TIME_STAMP+", pv." + COLUMN_NAME_ID;

public static final String POINT_VALUE_FILTER_LAST_BASE_ON_DATA_POINT_ID = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? "
+ "order by pv."+COLUMN_NAME_TIME_STAMP+" desc";
+ "order by pv."+COLUMN_NAME_TIME_STAMP+" desc, pv." + COLUMN_NAME_ID + " desc";

public static final String POINT_VALUE_FILTER_LATEST_BASE_ON_DATA_POINT_ID = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? and "
+ "pv."+COLUMN_NAME_TIME_STAMP+"<? "
+ "order by pv."+COLUMN_NAME_TIME_STAMP+" desc";
+ "order by pv."+COLUMN_NAME_TIME_STAMP+" desc, pv." + COLUMN_NAME_ID + " desc";

public static final String POINT_VALUE_FILTER_BEFORE_TIME_STAMP_BASE_ON_DATA_POINT_ID = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? and "
+ "pv."+COLUMN_NAME_TIME_STAMP+"<? "
+ "order by pv."+COLUMN_NAME_TIME_STAMP;
+ "order by pv."+COLUMN_NAME_TIME_STAMP+", pv." + COLUMN_NAME_ID;

public static final String POINT_VALUE_FILTER_AT_TIME_STAMP_BASE_ON_DATA_POINT_ID = " "
+ "pv."+COLUMN_NAME_DATA_POINT_ID+"=? and "
+ "pv."+COLUMN_NAME_TIME_STAMP+"=? "
+ "order by pv."+COLUMN_NAME_TIME_STAMP;
+ "order by pv."+COLUMN_NAME_TIME_STAMP+", pv." + COLUMN_NAME_ID;

public static final String POINT_VALUE_ID_OF_LAST_VALUE = ""
+ "select"
Expand Down Expand Up @@ -246,7 +246,7 @@ public class PointValueDAO implements GenericDaoCR<PointValue>, IPointValueDAO {
+ "select id "
+ "from pointValues "
+ "where dataPointId =? "
+ "order by id DESC "
+ "order by ts DESC, id DESC "
+ "limit 2 "
+ ") lastId ) and " + COLUMN_NAME_TIME_STAMP + "<? ";

Expand Down Expand Up @@ -279,7 +279,7 @@ public class PointValueDAO implements GenericDaoCR<PointValue>, IPointValueDAO {
"(select " + COLUMN_NAME_ID +
" from pointValues " +
"where " + COLUMN_NAME_DATA_POINT_ID + " =? " +
"order by " + COLUMN_NAME_ID + " desc " +
"order by " + COLUMN_NAME_TIME_STAMP + " desc, " + COLUMN_NAME_ID + " desc " +
"limit 1 offset ?) lastId)";

private static final String SELECT_MAX_TIME_WHERE_DATA_POINT_ID = ""
Expand Down
1 change: 0 additions & 1 deletion src/org/scada_lts/mango/service/PointValueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ private long savePointValueImpl(int pointId, int dataType, double dvalue, long t
//TODO rewrite
private List<PointValueTime> getLstPointValueTime(List<PointValue> lstIn) {
List<PointValueTime> lst = new ArrayList<PointValueTime>();
lstIn.sort(Comparator.comparing(PointValue::getId).reversed());
for (PointValue pv : lstIn) {
lst.add(pv.getPointValue());
}
Expand Down

0 comments on commit 684a394

Please sign in to comment.