use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-modules-public by infiniteautomation.
the class AnalogAttractorChangeRT method change.
@Override
public DataValue change(DataValue currentValue) {
double current = currentValue.getDoubleValue();
// Get the value we're attracted to.
DataPointRT point = Common.runtimeManager.getDataPoint(vo.getAttractionPointId());
if (point == null) {
if (log.isDebugEnabled())
log.debug("Attraction point is not enabled");
// Point is not currently active.
return new NumericValue(current);
}
DataValue attractorValue = PointValueTime.getValue(point.getPointValue());
if (attractorValue == null) {
if (log.isDebugEnabled())
log.debug("Attraction point has not vaue");
return new NumericValue(current);
}
double attraction = attractorValue.getDoubleValue();
// Move half the distance toward the attractor...
double change = (attraction - current) / 2;
// ... subject to the maximum change allowed...
if (change < 0 && -change > vo.getMaxChange())
change = -vo.getMaxChange();
else if (change > vo.getMaxChange())
change = vo.getMaxChange();
// ... and a random fluctuation.
change += RANDOM.nextDouble() * vo.getVolatility() * 2 - vo.getVolatility();
if (log.isDebugEnabled())
log.debug("attraction=" + attraction + ", change=" + change);
return new NumericValue(current + change);
}
use of com.serotonin.m2m2.rt.dataImage.types.DataValue 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.rt.dataImage.types.DataValue 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.rt.dataImage.types.DataValue in project ma-modules-public by infiniteautomation.
the class IdPointValueRollupCalculator method generateStream.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.AbstractPointValueRollupCalculator#generateStream(org.joda.time.DateTime, org.joda.time.DateTime, com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVPojoWriter)
*/
@Override
protected void generateStream(DateTime from, DateTime to, CSVPojoWriter<PointValueTimeModel> writer) {
BucketCalculator bc = this.getBucketCalculator(from, to);
IdPointValueStatisticsQuantizerCsvCallback callback = new IdPointValueStatisticsQuantizerCsvCallback(writer.getWriter(), this.voMap, this.useRendered, this.unitConversion, this.rollup, this.dateTimeFormat, timezone);
Iterator<Integer> it = this.voMap.keySet().iterator();
ParentDataQuantizer quantizer = new ParentDataQuantizer(bc, callback);
while (it.hasNext()) {
DataPointVO vo = this.voMap.get(it.next());
DataValue startValue = this.getStartValue(vo.getId());
if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC) {
quantizer.startQuantizer(vo.getId(), startValue, new AnalogStatisticsChildQuantizer(vo.getId(), quantizer));
} else {
quantizer.startQuantizer(vo.getId(), startValue, new ValueChangeCounterChildQuantizer(vo.getId(), quantizer));
}
}
this.calculate(quantizer, from, to);
}
use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-modules-public by infiniteautomation.
the class DataImportController method importCsv.
/**
* The file needs to be in the format:
*
* Data Point XID, Device Name, Point name, Time, Value, Rendered, Annotation, Modify(Not used yet)
*
* @param reader
* @param model
* @return
* @throws IOException
* @throws TranslatableException
*/
private void importCsv(CSVReader csvReader, Map<String, Object> model, Translations translations, List<String> errorMessages) throws IOException, TranslatableException {
DataPointDao dataPointDao = DataPointDao.instance;
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
int rowErrors = 0;
int rowsImported = 0;
int rowsDeleted = 0;
// Basic validation of header
String[] nextLine = csvReader.readNext();
if (nextLine == null) {
errorMessages.add(new TranslatableMessage("dataImport.import.noData").translate(translations));
return;
}
if (nextLine.length != ExportCsvStreamer.columns) {
errorMessages.add(new TranslatableMessage("dataImport.import.invalidHeaders", nextLine.length, ExportCsvStreamer.columns).translate(translations));
return;
}
// Map of XIDs to non-running data points
Map<String, DataPointVO> voMap = new HashMap<String, DataPointVO>();
// Map of XIDs to running data points
Map<String, DataPointRT> rtMap = new HashMap<String, DataPointRT>();
// Read in all the rows
int row = 1;
String xid;
DataPointVO vo;
DataPointRT rt;
long time;
DataValue value;
PointValueTime pvt;
while ((nextLine = csvReader.readNext()) != null) {
if (nextLine.length != ExportCsvStreamer.columns) {
errorMessages.add(new TranslatableMessage("dataImport.import.invalidLength", row).translate(translations));
rowErrors++;
row++;
continue;
}
// Check XID
xid = nextLine[0];
if (StringUtils.isBlank(xid)) {
errorMessages.add(new TranslatableMessage("dataImport.import.badXid", xid, row).translate(translations));
rowErrors++;
row++;
continue;
}
// First Check to see if we already have a point
vo = voMap.get(xid);
rt = rtMap.get(xid);
// We will always have the vo in the map but the RT may be null if the point isn't running
if (vo == null) {
vo = dataPointDao.getDataPoint(xid);
if (vo == null) {
errorMessages.add(new TranslatableMessage("dataImport.import.xidNotFound", xid, row).translate(translations));
rowErrors++;
row++;
continue;
}
rt = Common.runtimeManager.getDataPoint(vo.getId());
rtMap.put(xid, rt);
voMap.put(xid, vo);
}
// Add or delete or nothing
String modify = nextLine[7];
if (StringUtils.equalsIgnoreCase("add", modify)) {
// Going to insert some data
time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
value = DataValue.stringToValue(nextLine[4], vo.getPointLocator().getDataTypeId());
// Get Annotation
String annotation = nextLine[6];
if (annotation != null)
pvt = new AnnotatedPointValueTime(value, time, new TranslatableMessage("common.default", annotation));
else
pvt = new PointValueTime(value, time);
if (rt == null) {
// Insert Via DAO
pointValueDao.savePointValueAsync(vo.getId(), pvt, null);
} else {
// Insert Via RT
rt.savePointValueDirectToCache(pvt, null, true, true);
}
rowsImported++;
} else if (StringUtils.equalsIgnoreCase("delete", modify)) {
// Delete this entry
time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
rowsDeleted += Common.runtimeManager.purgeDataPointValue(vo.getId(), time);
}
row++;
}
// Setup results
model.put("rowsImported", rowsImported);
model.put("rowsDeleted", rowsDeleted);
model.put("rowsWithErrors", rowErrors);
}
Aggregations