use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-core-public by infiniteautomation.
the class ChartExportServlet method exportExcel.
/**
* Do the export as Excel XLSX File
* @param response
* @param from
* @param to
* @param def
* @param user
* @throws IOException
*/
private void exportExcel(HttpServletResponse response, long from, long to, DataExportDefinition def, User user) throws IOException {
DataPointDao dataPointDao = DataPointDao.instance;
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
// Stream the content.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
final List<PointValueEmporter> sheetEmporters = new ArrayList<PointValueEmporter>();
final AtomicInteger sheetIndex = new AtomicInteger();
sheetEmporters.add(new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get()));
final SpreadsheetEmporter emporter = new SpreadsheetEmporter(FileType.XLSX);
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
emporter.prepareExport(bos);
emporter.prepareSheetExport(sheetEmporters.get(0));
final ExportDataValue edv = new ExportDataValue();
MappedRowCallback<PointValueTime> callback = new MappedRowCallback<PointValueTime>() {
@Override
public void row(PointValueTime pvt, int rowIndex) {
edv.setValue(pvt.getValue());
edv.setTime(pvt.getTime());
if (pvt instanceof AnnotatedPointValueTime)
edv.setAnnotation(((AnnotatedPointValueTime) pvt).getSourceMessage());
else
edv.setAnnotation(null);
sheetEmporters.get(sheetIndex.get()).exportRow(edv);
if (sheetEmporters.get(sheetIndex.get()).getRowsAdded() >= emporter.getMaxRowsPerSheet()) {
ExportPointInfo info = sheetEmporters.get(sheetIndex.get()).getPointInfo();
sheetIndex.incrementAndGet();
PointValueEmporter sheetEmporter = new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get());
sheetEmporter.setPointInfo(info);
sheetEmporters.add(sheetEmporter);
emporter.prepareSheetExport(sheetEmporters.get(sheetIndex.get()));
}
}
};
for (int pointId : def.getPointIds()) {
DataPointVO dp = dataPointDao.getDataPoint(pointId, false);
if (Permissions.hasDataPointReadPermission(user, dp)) {
ExportPointInfo pointInfo = new ExportPointInfo();
pointInfo.setXid(dp.getXid());
pointInfo.setPointName(dp.getName());
pointInfo.setDeviceName(dp.getDeviceName());
pointInfo.setTextRenderer(dp.getTextRenderer());
sheetEmporters.get(sheetIndex.get()).setPointInfo(pointInfo);
pointValueDao.getPointValuesBetween(pointId, from, to, callback);
}
}
emporter.finishExport();
}
use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-core-public by infiniteautomation.
the class PointValueEmporter method importRow.
/*
* (non-Javadoc)
* @see com.serotonin.m2m2.vo.emport.AbstractSheetEmporter#importRow(org.apache.poi.ss.usermodel.Row)
*/
@Override
protected void importRow(Row rowData) throws SpreadsheetException {
int cellNum = 0;
// Data Point XID
Cell xidCell = rowData.getCell(cellNum++);
if (xidCell == null)
throw new SpreadsheetException(rowData.getRowNum(), "emport.error.xidRequired");
if ((xidCell.getStringCellValue() == null) || (xidCell.getStringCellValue().isEmpty()))
throw new SpreadsheetException("emport.error.xidRequired");
// First Check to see if we already have a point
String xid = xidCell.getStringCellValue();
DataPointVO dp = voMap.get(xid);
DataPointRT dpRt = 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 (dp == null) {
dp = dataPointDao.getDataPoint(xid);
if (dp == null)
throw new SpreadsheetException(rowData.getRowNum(), "emport.error.missingPoint", xid);
dpRt = Common.runtimeManager.getDataPoint(dp.getId());
rtMap.put(xid, dpRt);
voMap.put(xid, dp);
}
PointValueTime pvt;
// Cell Device name (Not using Here)
cellNum++;
// Cell Point name (Not using Here)
cellNum++;
// Cell Time
Date time = rowData.getCell(cellNum++).getDateCellValue();
// delete/add column
Cell modifyCell = rowData.getCell(7);
boolean add = false;
boolean delete = false;
if (modifyCell != null) {
String modification = (String) modifyCell.getStringCellValue();
if (modification.equalsIgnoreCase("delete")) {
delete = true;
} else if (modification.equalsIgnoreCase("add")) {
add = true;
} else {
throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.modifyCellUnknown");
}
}
// What do we do with the row
if (delete) {
if (time == null) {
throw new SpreadsheetException(rowData.getRowNum(), "emport.error.deleteNew", "no timestamp, unable to delete");
} else {
try {
this.rowsDeleted += Common.runtimeManager.purgeDataPointValue(dp.getId(), time.getTime());
} catch (Exception e) {
if (e instanceof DataIntegrityViolationException)
throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDeleteDueToConstraints");
else
throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDelete", e.getMessage());
}
}
// Done now
return;
} else if (add) {
// Cell Value
Cell cell;
cell = rowData.getCell(cellNum++);
// Create a data value
DataValue dataValue;
switch(dp.getPointLocator().getDataTypeId()) {
case DataTypes.ALPHANUMERIC:
dataValue = new AlphanumericValue(cell.getStringCellValue());
break;
case DataTypes.BINARY:
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
dataValue = new BinaryValue(new Boolean(cell.getBooleanCellValue()));
break;
case Cell.CELL_TYPE_NUMERIC:
if (cell.getNumericCellValue() == 0)
dataValue = new BinaryValue(new Boolean(false));
else
dataValue = new BinaryValue(new Boolean(true));
break;
case Cell.CELL_TYPE_STRING:
if (cell.getStringCellValue().equalsIgnoreCase("false"))
dataValue = new BinaryValue(new Boolean(false));
else
dataValue = new BinaryValue(new Boolean(true));
break;
default:
throw new SpreadsheetException(rowData.getRowNum(), "common.default", "Invalid cell type for extracting boolean");
}
break;
case DataTypes.MULTISTATE:
dataValue = new MultistateValue((int) cell.getNumericCellValue());
break;
case DataTypes.NUMERIC:
dataValue = new NumericValue(cell.getNumericCellValue());
break;
default:
throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.unsupportedDataType", dp.getPointLocator().getDataTypeId());
}
// Cell Rendered Value (Not using yet)
cellNum++;
// Cell Annotation
Cell annotationRow = rowData.getCell(cellNum++);
if (annotationRow != null) {
String annotation = annotationRow.getStringCellValue();
// TODO These methods here do not allow updating the Annotation. We need to be a set point source for that to work
TranslatableMessage sourceMessage = new TranslatableMessage("common.default", annotation);
pvt = new AnnotatedPointValueTime(dataValue, time.getTime(), sourceMessage);
} else {
pvt = new PointValueTime(dataValue, time.getTime());
}
// Save to cache if running
if (dpRt != null)
dpRt.savePointValueDirectToCache(pvt, null, true, true);
else {
if (pointValueDao instanceof EnhancedPointValueDao) {
DataSourceVO<?> ds = getDataSource(dp.getDataSourceId());
((EnhancedPointValueDao) pointValueDao).savePointValueAsync(dp, ds, pvt, null);
} else {
pointValueDao.savePointValueAsync(dp.getId(), pvt, null);
}
}
// Increment our counter
this.rowsAdded++;
}
}
use of com.serotonin.m2m2.db.dao.PointValueDao 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.db.dao.PointValueDao 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.db.dao.PointValueDao in project ma-modules-public by infiniteautomation.
the class PointValueFftCalculator method calculate.
/**
* Generate FFT
* @return
*/
public FftGenerator calculate(DateTime from, DateTime to) {
PointValueDao pvd = Common.databaseProxy.newPointValueDao();
long count = pvd.dateRangeCount(vo.getId(), from.getMillis(), to.getMillis());
final FftGenerator generator = new FftGenerator(count);
// Make the call to get the data and quantize it
pvd.getPointValuesBetween(vo.getId(), from.getMillis(), to.getMillis(), new MappedRowCallback<PointValueTime>() {
@Override
public void row(PointValueTime pvt, int row) {
generator.data(pvt);
}
});
generator.done(getEndValue());
return generator;
}
Aggregations