use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.
the class ReportDao method runReportSQL.
/**
* SQL Database Report
* @param instance
* @param points
* @return
*/
public int runReportSQL(final ReportInstance instance, List<PointInfo> points) {
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
int count = 0;
// The timestamp selection code is used multiple times for different tables
String timestampSql;
Object[] timestampParams;
if (instance.isFromInception() && instance.isToNow()) {
timestampSql = "";
timestampParams = new Object[0];
} else if (instance.isFromInception()) {
timestampSql = "and ${field}<?";
timestampParams = new Object[] { instance.getReportEndTime() };
} else if (instance.isToNow()) {
timestampSql = "and ${field}>=?";
timestampParams = new Object[] { instance.getReportStartTime() };
} else {
timestampSql = "and ${field}>=? and ${field}<?";
timestampParams = new Object[] { instance.getReportStartTime(), instance.getReportEndTime() };
}
// For each point.
for (PointInfo pointInfo : points) {
DataPointVO point = pointInfo.getPoint();
int dataType = point.getPointLocator().getDataTypeId();
DataValue startValue = null;
if (!instance.isFromInception()) {
// Get the value just before the start of the report
PointValueTime pvt = pointValueDao.getPointValueBefore(point.getId(), instance.getReportStartTime());
if (pvt != null)
startValue = pvt.getValue();
// Make sure the data types match
if (DataTypes.getDataType(startValue) != dataType)
startValue = null;
}
// Insert the reportInstancePoints record
String name = Functions.truncate(point.getName(), 100);
int reportPointId = doInsert(REPORT_INSTANCE_POINTS_INSERT, new Object[] { instance.getId(), point.getDeviceName(), name, pointInfo.getPoint().getXid(), dataType, DataTypes.valueToString(startValue), SerializationHelper.writeObject(point.getTextRenderer()), pointInfo.getColour(), pointInfo.getWeight(), boolToChar(pointInfo.isConsolidatedChart()), pointInfo.getPlotType() }, new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.BLOB, Types.VARCHAR, Types.FLOAT, Types.CHAR, Types.INTEGER });
// Insert the reportInstanceData records
String insertSQL = //
"insert into reportInstanceData " + " select id, " + reportPointId + //
", pointValue, ts from pointValues " + //
" where dataPointId=? and dataType=? " + StringUtils.replaceMacro(timestampSql, "field", "ts");
count += ejt.update(insertSQL, appendParameters(timestampParams, point.getId(), dataType));
// Insert the reportInstanceDataAnnotations records
ejt.update(//
"insert into reportInstanceDataAnnotations " + //
" (pointValueId, reportInstancePointId, textPointValueShort, textPointValueLong, sourceMessage) " + //
" select rd.pointValueId, rd.reportInstancePointId, pva.textPointValueShort, " + //
" pva.textPointValueLong, pva.sourceMessage " + //
" from reportInstanceData rd " + //
" join reportInstancePoints rp on rd.reportInstancePointId = rp.id " + //
" join pointValueAnnotations pva on rd.pointValueId = pva.pointValueId " + " where rp.id = ?", new Object[] { reportPointId });
// Insert the reportInstanceEvents records for the point.
if (instance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
String eventSQL = //
"insert into reportInstanceEvents " + //
" (eventId, reportInstanceId, typeName, subtypeName, typeRef1, typeRef2, activeTs, " + //
" rtnApplicable, rtnTs, rtnCause, alarmLevel, message, ackTs, ackUsername, " + //
" alternateAckSource)" + " select e.id, " + instance.getId() + //
", e.typeName, e.subtypeName, e.typeRef1, " + //
" e.typeRef2, e.activeTs, e.rtnApplicable, e.rtnTs, e.rtnCause, e.alarmLevel, " + //
" e.message, e.ackTs, u.username, e.alternateAckSource " + //
" from events e join userEvents ue on ue.eventId=e.id " + //
" left join users u on e.ackUserId=u.id " + //
" where ue.userId=? " + //
" and e.typeName=? " + " and e.typeRef1=? ";
if (instance.getIncludeEvents() == ReportVO.EVENTS_ALARMS)
eventSQL += "and e.alarmLevel > 0 ";
eventSQL += StringUtils.replaceMacro(timestampSql, "field", "e.activeTs");
ejt.update(eventSQL, appendParameters(timestampParams, instance.getUserId(), EventType.EventTypeNames.DATA_POINT, point.getId()));
}
// Insert the reportInstanceUserComments records for the point.
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_POINT + //
", " + reportPointId + //
", uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + " where uc.commentType=" + //
UserCommentVO.TYPE_POINT + " and uc.typeKey=? ";
// Only include comments made in the duration of the report.
commentSQL += StringUtils.replaceMacro(timestampSql, "field", "uc.ts");
ejt.update(commentSQL, appendParameters(timestampParams, point.getId()));
}
}
// Insert the reportInstanceUserComments records for the selected events
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_EVENT + //
", uc.typeKey, " + //
" uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + //
" join reportInstanceEvents re on re.eventId=uc.typeKey " + " where uc.commentType=" + //
UserCommentVO.TYPE_EVENT + " and re.reportInstanceId=? ";
ejt.update(commentSQL, new Object[] { instance.getId() });
}
// If the report had undefined start or end times, update them with values from the data.
if (instance.isFromInception() || instance.isToNow()) {
ejt.query(//
"select min(rd.ts), max(rd.ts) " + "from reportInstancePoints rp " + " join reportInstanceData rd on rp.id=rd.reportInstancePointId " + "where rp.reportInstanceId=?", new Object[] { instance.getId() }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
if (instance.isFromInception())
instance.setReportStartTime(rs.getLong(1));
if (instance.isToNow())
instance.setReportEndTime(rs.getLong(2));
}
});
}
return count;
}
use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.
the class ReportDao method runReportNoSQL.
/**
* Generate a report using the NoSQL DB for point value storage
* @param instance
* @param points
* @return
*/
public int runReportNoSQL(final ReportInstance instance, List<PointInfo> points) {
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
final MappedCallbackCounter count = new MappedCallbackCounter();
final NoSQLDao dao = Common.databaseProxy.getNoSQLProxy().createNoSQLDao(ReportPointValueTimeSerializer.get(), "reports");
// The timestamp selection code is used multiple times for different tables
long startTime, endTime;
String timestampSql;
Object[] timestampParams;
if (instance.isFromInception() && instance.isToNow()) {
timestampSql = "";
timestampParams = new Object[0];
startTime = 0l;
endTime = Common.timer.currentTimeMillis();
} else if (instance.isFromInception()) {
timestampSql = "and ${field}<?";
timestampParams = new Object[] { instance.getReportEndTime() };
startTime = 0l;
endTime = instance.getReportEndTime();
} else if (instance.isToNow()) {
timestampSql = "and ${field}>=?";
timestampParams = new Object[] { instance.getReportStartTime() };
startTime = instance.getReportStartTime();
endTime = Common.timer.currentTimeMillis();
} else {
timestampSql = "and ${field}>=? and ${field}<?";
timestampParams = new Object[] { instance.getReportStartTime(), instance.getReportEndTime() };
startTime = instance.getReportStartTime();
endTime = instance.getReportEndTime();
}
// For each point.
List<Integer> pointIds = new ArrayList<Integer>();
// Map the pointId to the Report PointId
final Map<Integer, Integer> pointIdMap = new HashMap<Integer, Integer>();
// the reports table/data store
for (PointInfo pointInfo : points) {
DataPointVO point = pointInfo.getPoint();
pointIds.add(point.getId());
int dataType = point.getPointLocator().getDataTypeId();
DataValue startValue = null;
if (!instance.isFromInception()) {
// Get the value just before the start of the report
PointValueTime pvt = pointValueDao.getPointValueBefore(point.getId(), instance.getReportStartTime());
if (pvt != null)
startValue = pvt.getValue();
// Make sure the data types match
if (DataTypes.getDataType(startValue) != dataType)
startValue = null;
}
// Insert the reportInstancePoints record
String name = Functions.truncate(point.getName(), 100);
int reportPointId = doInsert(REPORT_INSTANCE_POINTS_INSERT, new Object[] { instance.getId(), point.getDeviceName(), name, pointInfo.getPoint().getXid(), dataType, DataTypes.valueToString(startValue), SerializationHelper.writeObject(point.getTextRenderer()), pointInfo.getColour(), pointInfo.getWeight(), boolToChar(pointInfo.isConsolidatedChart()), pointInfo.getPlotType() }, new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.BLOB, Types.VARCHAR, Types.FLOAT, Types.CHAR, Types.INTEGER });
// Keep the info in the map
pointIdMap.put(pointInfo.getPoint().getId(), reportPointId);
// Insert the reportInstanceDataAnnotations records
ejt.update(//
"insert into reportInstanceDataAnnotations " + //
" (pointValueId, reportInstancePointId, textPointValueShort, textPointValueLong, sourceMessage) " + //
" select rd.pointValueId, rd.reportInstancePointId, pva.textPointValueShort, " + //
" pva.textPointValueLong, pva.sourceMessage " + //
" from reportInstanceData rd " + //
" join reportInstancePoints rp on rd.reportInstancePointId = rp.id " + //
" join pointValueAnnotations pva on rd.pointValueId = pva.pointValueId " + " where rp.id = ?", new Object[] { reportPointId });
// Insert the reportInstanceEvents records for the point.
if (instance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
String eventSQL = //
"insert into reportInstanceEvents " + //
" (eventId, reportInstanceId, typeName, subtypeName, typeRef1, typeRef2, activeTs, " + //
" rtnApplicable, rtnTs, rtnCause, alarmLevel, message, ackTs, ackUsername, " + //
" alternateAckSource)" + " select e.id, " + instance.getId() + //
", e.typeName, e.subtypeName, e.typeRef1, " + //
" e.typeRef2, e.activeTs, e.rtnApplicable, e.rtnTs, e.rtnCause, e.alarmLevel, " + //
" e.message, e.ackTs, u.username, e.alternateAckSource " + //
" from events e join userEvents ue on ue.eventId=e.id " + //
" left join users u on e.ackUserId=u.id " + //
" where ue.userId=? " + //
" and e.typeName=? " + " and e.typeRef1=? ";
if (instance.getIncludeEvents() == ReportVO.EVENTS_ALARMS)
eventSQL += "and e.alarmLevel > 0 ";
eventSQL += StringUtils.replaceMacro(timestampSql, "field", "e.activeTs");
ejt.update(eventSQL, appendParameters(timestampParams, instance.getUserId(), EventType.EventTypeNames.DATA_POINT, point.getId()));
}
// Insert the reportInstanceUserComments records for the point.
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_POINT + //
", " + reportPointId + //
", uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + " where uc.commentType=" + //
UserCommentVO.TYPE_POINT + " and uc.typeKey=? ";
// Only include comments made in the duration of the report.
commentSQL += StringUtils.replaceMacro(timestampSql, "field", "uc.ts");
ejt.update(commentSQL, appendParameters(timestampParams, point.getId()));
}
}
// end for all points
// Insert the data into the NoSQL DB and track first/last times
// The series name is reportInstanceId_reportPointId
final AtomicLong firstPointTime = new AtomicLong(Long.MAX_VALUE);
final AtomicLong lastPointTime = new AtomicLong(-1l);
final String reportId = Integer.toString(instance.getId()) + "_";
pointValueDao.getPointValuesBetween(pointIds, startTime, endTime, new MappedRowCallback<IdPointValueTime>() {
@Override
public void row(final IdPointValueTime ipvt, int rowId) {
dao.storeData(reportId + Integer.toString(pointIdMap.get(ipvt.getId())), ipvt);
count.increment();
if (ipvt.getTime() < firstPointTime.get())
firstPointTime.set(ipvt.getTime());
if (ipvt.getTime() > lastPointTime.get())
lastPointTime.set(ipvt.getTime());
}
});
// Insert the reportInstanceUserComments records for the selected events
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_EVENT + //
", uc.typeKey, " + //
" uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + //
" join reportInstanceEvents re on re.eventId=uc.typeKey " + " where uc.commentType=" + //
UserCommentVO.TYPE_EVENT + " and re.reportInstanceId=? ";
ejt.update(commentSQL, new Object[] { instance.getId() });
}
// If the report had undefined start or end times, update them with values from the data.
if (instance.isFromInception() || instance.isToNow()) {
if (instance.isFromInception()) {
if (firstPointTime.get() != Long.MAX_VALUE)
instance.setReportStartTime(firstPointTime.get());
}
if (instance.isToNow()) {
instance.setReportEndTime(lastPointTime.get());
}
}
return count.getCount();
}
use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.
the class ReportDao method reportInstanceDataSQL.
public void reportInstanceDataSQL(int instanceId, final ExportDataStreamHandler handler) {
// Retrieve point information.
List<ExportPointInfo> pointInfos = query(REPORT_INSTANCE_POINT_SELECT + "where reportInstanceId=?", new Object[] { instanceId }, new RowMapper<ExportPointInfo>() {
@Override
public ExportPointInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
int i = 0;
ExportPointInfo rp = new ExportPointInfo();
rp.setReportPointId(rs.getInt(++i));
rp.setDeviceName(rs.getString(++i));
rp.setPointName(rs.getString(++i));
rp.setXid(rs.getString(++i));
rp.setDataType(rs.getInt(++i));
String startValue = rs.getString(++i);
if (startValue != null)
rp.setStartValue(DataValue.stringToValue(startValue, rp.getDataType()));
rp.setTextRenderer((TextRenderer) SerializationHelper.readObjectInContext(rs.getBlob(++i).getBinaryStream()));
rp.setColour(rs.getString(++i));
rp.setWeight(rs.getFloat(++i));
rp.setConsolidatedChart(charToBool(rs.getString(++i)));
rp.setPlotType(rs.getInt(++i));
return rp;
}
});
final ExportDataValue edv = new ExportDataValue();
for (final ExportPointInfo point : pointInfos) {
if (point.getDataType() == DataTypes.IMAGE) {
DataPointVO vo = DataPointDao.instance.getByXid(point.getXid());
if (vo != null)
point.setDataPointId(vo.getId());
else
point.setDataPointId(-1);
}
handler.startPoint(point);
edv.setReportPointId(point.getReportPointId());
final int dataType = point.getDataType();
ejt.query(REPORT_INSTANCE_DATA_SELECT + "where rd.reportInstancePointId=? order by rd.ts", new Object[] { point.getReportPointId() }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
switch(dataType) {
case (DataTypes.NUMERIC):
edv.setValue(new NumericValue(rs.getDouble(1)));
break;
case (DataTypes.BINARY):
edv.setValue(new BinaryValue(rs.getDouble(1) == 1));
break;
case (DataTypes.MULTISTATE):
edv.setValue(new MultistateValue(rs.getInt(1)));
break;
case (DataTypes.ALPHANUMERIC):
edv.setValue(new AlphanumericValue(rs.getString(2)));
if (rs.wasNull())
edv.setValue(new AlphanumericValue(rs.getString(3)));
break;
case (DataTypes.IMAGE):
edv.setValue(new ImageValue(Integer.parseInt(rs.getString(2)), rs.getInt(1)));
break;
default:
edv.setValue(null);
}
edv.setTime(rs.getLong(4));
edv.setAnnotation(BaseDao.readTranslatableMessage(rs, 5));
handler.pointData(edv);
}
});
}
handler.done();
}
use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.
the class PointValueImportResult method saveValue.
public void saveValue(XidPointValueTimeModel model) {
if (valid) {
// Validate the model against our point
long timestamp = model.getTimestamp();
if (timestamp == 0)
timestamp = Common.timer.currentTimeMillis();
int dataTypeId = DataTypeEnum.convertFrom(model.getType());
if (dataTypeId != vo.getPointLocator().getDataTypeId()) {
result.addContextualMessage("dataType", "event.ds.dataType");
return;
}
DataValue value;
switch(model.getType()) {
case ALPHANUMERIC:
value = new AlphanumericValue((String) model.getValue());
break;
case BINARY:
value = new BinaryValue((Boolean) model.getValue());
break;
case MULTISTATE:
if (model.getValue() instanceof String) {
try {
value = vo.getTextRenderer().parseText((String) model.getValue(), dataTypeId);
} catch (Exception e) {
// Lots can go wrong here so let the user know
result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
return;
}
} else {
value = new MultistateValue(((Number) model.getValue()).intValue());
}
break;
case NUMERIC:
value = new NumericValue(((Number) model.getValue()).doubleValue());
break;
case IMAGE:
default:
result.addContextualMessage("dataType", "common.default", model.getType() + " data type not supported yet");
return;
}
PointValueTime pvt;
if (model.getAnnotation() == null) {
pvt = new PointValueTime(value, timestamp);
} else {
pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
}
if (rt == null) {
dao.savePointValueAsync(vo.getId(), pvt, null);
} else {
rt.savePointValueDirectToCache(pvt, null, true, true);
}
total++;
}
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class PointEventDetectorVO method getImplementations.
public static List<ImplDefinition> getImplementations(int dataType) {
if (definitions == null) {
List<ImplDefinition> d = new ArrayList<>();
d.add(new ImplDefinition(TYPE_ANALOG_HIGH_LIMIT, null, "pointEdit.detectors.highLimit", new int[] { DataTypes.NUMERIC }));
d.add(new ImplDefinition(TYPE_ANALOG_LOW_LIMIT, null, "pointEdit.detectors.lowLimit", new int[] { DataTypes.NUMERIC }));
d.add(new ImplDefinition(TYPE_POINT_CHANGE, null, "pointEdit.detectors.change", new int[] { DataTypes.BINARY, DataTypes.MULTISTATE, DataTypes.NUMERIC, DataTypes.ALPHANUMERIC }));
d.add(new ImplDefinition(TYPE_BINARY_STATE, null, "pointEdit.detectors.state", new int[] { DataTypes.BINARY }));
d.add(new ImplDefinition(TYPE_MULTISTATE_STATE, null, "pointEdit.detectors.state", new int[] { DataTypes.MULTISTATE }));
d.add(new ImplDefinition(TYPE_ALPHANUMERIC_STATE, null, "pointEdit.detectors.state", new int[] { DataTypes.ALPHANUMERIC }));
d.add(new ImplDefinition(TYPE_ALPHANUMERIC_REGEX_STATE, null, "pointEdit.detectors.regexState", new int[] { DataTypes.ALPHANUMERIC }));
d.add(new ImplDefinition(TYPE_STATE_CHANGE_COUNT, null, "pointEdit.detectors.changeCount", new int[] { DataTypes.BINARY, DataTypes.MULTISTATE, DataTypes.ALPHANUMERIC }));
d.add(new ImplDefinition(TYPE_NO_CHANGE, null, "pointEdit.detectors.noChange", new int[] { DataTypes.BINARY, DataTypes.MULTISTATE, DataTypes.NUMERIC, DataTypes.ALPHANUMERIC }));
d.add(new ImplDefinition(TYPE_NO_UPDATE, null, "pointEdit.detectors.noUpdate", new int[] { DataTypes.BINARY, DataTypes.MULTISTATE, DataTypes.NUMERIC, DataTypes.ALPHANUMERIC, DataTypes.IMAGE }));
d.add(new ImplDefinition(TYPE_POSITIVE_CUSUM, null, "pointEdit.detectors.posCusum", new int[] { DataTypes.NUMERIC }));
d.add(new ImplDefinition(TYPE_NEGATIVE_CUSUM, null, "pointEdit.detectors.negCusum", new int[] { DataTypes.NUMERIC }));
d.add(new ImplDefinition(TYPE_ANALOG_RANGE, null, "pointEdit.detectors.range", new int[] { DataTypes.NUMERIC }));
// d.add(new ImplDefinition(TYPE_ANALOG_CHANGE, null, "pointEdit.detectors.analogChange",
// new int[] { DataTypes.NUMERIC }));
d.add(new ImplDefinition(TYPE_SMOOTHNESS, null, "pointEdit.detectors.smoothness", new int[] { DataTypes.NUMERIC }));
definitions = d;
}
List<ImplDefinition> impls = new ArrayList<>();
for (ImplDefinition def : definitions) {
if (def.supports(dataType))
impls.add(def);
}
return impls;
}
Aggregations