WriteData Plugin¶
Description¶
The WriteData plugin provides file system data export capabilities for CVEDIA-RT. It enables saving analytics results, events, and metadata to various file formats including JSON, CSV, XML, and custom formats.
Key Features¶
- Multiple output formats (JSON, CSV, XML, TXT)
- Structured and unstructured data support
- Automatic file rotation and compression
- Directory structure management
- Timestamp-based file naming
- Custom formatting templates
- Event-driven and batch export modes
When to Use¶
- Exporting analytics results to files
- Creating audit logs and reports
- Data backup and archival
- Integration with external systems via file exchange
- Compliance and regulatory reporting
Requirements¶
Software Dependencies¶
- File system write permissions
- Sufficient disk space for data storage
- Optional: Compression libraries (zlib, gzip)
Storage Requirements¶
- Varies by data volume and retention period
- Recommend monitoring disk usage
- Consider file rotation policies
Configuration¶
Basic Configuration¶
{
"writedata": {
"outputPath": "/data/analytics/",
"format": "json",
"filenameTemplate": "detections_{{timestamp}}.json"
}
}
Configuration Schema¶
Parameter | Type | Default | Description |
---|---|---|---|
outputPath |
string | "./output/" | Directory path for output files |
format |
string | "json" | Output format (json, csv, xml, txt) |
filenameTemplate |
string | "data_{{timestamp}}" | Filename template with variables |
rotation.enabled |
boolean | false | Enable file rotation |
rotation.maxFileSize |
string | "50MB" | Maximum file size before rotation |
API Reference¶
C++ API¶
class WriteDataImpl : public iface::WriteData {
public:
bool writeData(const std::string& filename, pCValue data);
bool appendData(const std::string& filename, pCValue data);
bool writeStructuredData(const std::string& filename, pCValue data);
void setOutputPath(const std::string& path);
void setFormat(const std::string& format);
};
Lua API¶
-- Create WriteData instance (following SecuRT patterns)
local instance = api.thread.getCurrentInstance()
local writeData = api.factory.writedata.get(instance, "WriteData")
if not writeData then
writeData = api.factory.writedata.create(instance, "WriteData")
end
-- Basic operations
writeData:writeData("events.json", eventData)
writeData:appendData("log.txt", logMessage)
writeData:setOutputPath("/data/export/")
Examples¶
Basic JSON Export¶
-- Initialize WriteData (following SecuRT event_video_recorder.lua pattern)
local instance = api.thread.getCurrentInstance()
local writeInst = api.factory.writedata.get(instance, "WriteData")
if not writeInst then
writeInst = api.factory.writedata.create(instance, "WriteData")
end
-- Configure output
writeInst:setOutputPath("/data/analytics/")
writeInst:setFormat("json")
-- Export detection data
function exportDetections(detections)
local data = {
timestamp = os.time(),
camera = "camera-001",
detections = {}
}
for _, detection in ipairs(detections) do
table.insert(data.detections, {
id = detection.id,
class = detection.class,
confidence = detection.confidence,
bbox = detection.bbox
})
end
local filename = "detections_" .. os.date("%Y%m%d_%H%M%S") .. ".json"
local success = writeInst:writeStructuredData(filename, data)
if success then
api.logging.LogInfo("Exported " .. #detections .. " detections to " .. filename)
else
api.logging.LogError("Failed to export detections to " .. filename)
end
end
CSV Event Logging¶
-- CSV format writer (following SecuRT frame_data_debugger.lua pattern)
local instance = api.thread.getCurrentInstance()
local writeInst = api.factory.writedata.get(instance, "WriteData")
if not writeInst then
writeInst = api.factory.writedata.create(instance, "WriteData")
api.logging.LogInfo("Created WriteData instance for CSV logging")
end
writeInst:setOutputPath("/logs/events/")
writeInst:setFormat("csv")
-- Write CSV header
local header = "timestamp,event_type,object_class,confidence,x,y,width,height\n"
local headerSuccess = writeInst:writeData("events.csv", header)
if not headerSuccess then
api.logging.LogError("Failed to write CSV header")
return
end
-- Log events to CSV
function logEventToCSV(event)
local csvLine = string.format("%d,%s,%s,%.2f,%d,%d,%d,%d\n",
event.timestamp,
event.type,
event.object.class,
event.object.confidence,
event.object.bbox.x,
event.object.bbox.y,
event.object.bbox.width,
event.object.bbox.height
)
local success = writeInst:appendData("events.csv", csvLine)
if not success then
api.logging.LogWarning("Failed to append event data to CSV: " .. tostring(event.timestamp))
end
end
Troubleshooting¶
File System Issues¶
-
"Permission denied"
- Check directory write permissions
- Verify user/group ownership
- Ensure parent directories exist
-
"Disk full" errors
- Monitor disk space usage
- Implement file rotation
- Configure cleanup policies
Performance Issues¶
- Slow write performance
- Use SSD storage for high-frequency writes
- Batch multiple writes together
- Consider asynchronous writing
See Also¶
- Data Plugins Overview
- MQTT Plugin - Real-time messaging
- REST Plugin - UDP data transmission