### External Jar rrd4j

This is an example to access methods from the RrdDb class (requires rrd4j-2.1.1.jar in the B4J additional libraries folder).
Reference: http://rrd4j.org/site/apidocs/.
RRD4J is a high performance data logging and graphing system for time series data.
This example implements the class RRD4J to access a rrd database, nHA_Energy_PowerConsumption.rrd located in the project Objects folder.
Class Main
`Sub Process_Globals
Dim rrd As RRD4J
End Sub`
`Sub AppStart (Args() As String)
'Init the rrd object with rrd archive - located in the app (for tests Objects folder)
rrd.Initialize("nHA_Energy_PowerConsumption.rrd")
'Set the time frame YMDHM to fetch the data
rrd.FetchDataYMDHM(2018, 0, 31, 22, 0, 2018, 0, 31, 23, 0)
'Set the time frame YMD to fetch the data
' rrd.FetchDataYMD(2018, 0, 30, 2018, 0, 31)
'Get the result which is a list of maps timestamp:value
If rrd.DataPoints.Size > 0 Then
Log($"Data points=${rrd.DataPoints.Size}"$)
For Each m As Map In rrd.DataPoints
Log($"${rrd.ConvertUNIXTimeStamp(m.GetKeyAt(0), "dd-MMM-YYYY")} = ${NumberFormat(m.GetValueAt(0),0,0)}"$)
Next
Else
Log("No data points found for given time frame.")
End If
ExitApplication
End Sub`
Class RRD4J.bas
`Private Sub Class_Globals
Private DBReadOnly As Boolean = True
Private joRrdDb As JavaObject
Private joConsolFun As JavaObject
Private joFetchRequest As JavaObject
Private joFetchData As JavaObject
Private joUtil As JavaObject
Private StartTime As Long
Private EndTime As Long
Private RowCount As Long
Public DataPoints As List
End Sub`
'Create a new RrdDb instance (javaobject) and a static Console Function Object (javaobject, field access only)
'Path - Full path to the rrd archive
`Public Sub Initialize(Path As String)
'Create a new rrdDb object (constructor) And open in readonly mode
Try
'Init the javaobjects
joRrdDb.InitializeNewInstance("org.rrd4j.core.RrdDb", Array(Path, DBReadOnly))
'Console functions used to get the enumeration AVERAGE
joConsolFun.InitializeStatic("org.rrd4j.ConsolFun")
'Utility function for datetime
joUtil.InitializeStatic("org.rrd4j.core.Util")
' Log($"Last Update: ${GetLastUpdateTimeStamp}"$)
'Init the list holding the fetched data points
DataPoints.Initialize
Catch
Log(LastException)
End Try
End Sub`
'Fetch the date for Year, Month, Day and build list of maps timestamp:value.
'IMPORTANT: Month is zero based, i.e.first month JAN = 0, DEC = 11!
'Example`
''Set the start and end time - IMPORTANT: Month is zero based, i.e.first month JAN = 0, DEC = 11!
'FetchDataYMD(2018,0,30,2018,0,31)
'`
`Public Sub FetchDataYMD(StartYear As Int, StartMonth As Int, StartDay As Int, EndYear As Int, EndMonth As Int, EndDay As Int)
StartTime = joUtil.RunMethod("getTimestamp", Array(StartYear, StartMonth, StartDay))
EndTime = joUtil.RunMethod("getTimestamp", Array(EndYear, EndMonth, EndDay))
FetchData(StartTime, EndTime)
End Sub`
'Fetch and list the date for Year, Month, Day, Hour, Min and build list of maps timestamp:value.
'IMPORTANT: Month is zero based, i.e.first month JAN = 0, DEC = 11!
'Example`
''Set the start and end time - IMPORTANT: Month is zero based, i.e.first month JAN = 0, DEC = 11!
'FetchDataYMD(2018,0,30,22,0,2018,0,31,2,0)
'`
`Public Sub FetchDataYMDHM(StartYear As Int, StartMonth As Int, StartDay As Int, StartHour As Int, StartMin As Int, EndYear As Int, EndMonth As Int, EndDay As Int, EndHour As Int, EndMin As Int)
StartTime = joUtil.RunMethod("getTimestamp", Array(StartYear, StartMonth, StartDay, StartHour, StartMin))
EndTime = joUtil.RunMethod("getTimestamp", Array(EndYear, EndMonth, EndDay, EndHour, EndMin))
FetchData(StartTime, EndTime)
End Sub`
'Fetch the datapoints and build list of maps timestamp:value.
'StartDate - Long
'EndDate - Long
'Example`
''Set the start and end time - IMPORTANT: Month is zero based, i.e.first month JAN = 0, DEC = 11!
'Private StartTime As Long
'Private EndTime As Long
'StartTime = joUtil.RunMethod("getTimestamp", Array(2018, 0, 30))
'EndTime = joUtil.RunMethod("getTimestamp", Array(2018, 0, 31))
'FetchData(StartTime, EndTime)
'`
'Note: The timestamp accepts also hours and mins:
'StartTime = joUtil.RunMethod("getTimestamp", Array(2018, 0, 30, 22, 0))
`Public Sub FetchData(StartDate As Long, EndDate As Long)
StartDate = ConvertTimeStampToUNIX(StartDate * 1000)
EndDate = ConvertTimeStampToUNIX(EndDate * 1000)
Try
'Create fetch request with start & end date
joFetchRequest = joRrdDb.RunMethod("createFetchRequest", Array(joConsolFun.GetField("AVERAGE"), StartDate, EndDate))
'Dump the the data using the fetch request result
joFetchRequest.RunMethod("dump", Null)
'Fetch the data from the dump
joFetchData = joFetchRequest.RunMethod("fetchData", Null)
'Get the number of data points
RowCount = joFetchData.RunMethod("getRowCount", Null)
' Log($"Data points=${RowCount}"$)
'Option 1 = List the result as string
' Log($"${joFetchData.RunMethod("toString", Null)}"$)
'Option 2 = List the result using arrays; add the datapoints to the global map datapoints
Dim timestamps() As Long = joFetchData.RunMethod("getTimestamps", Null)
Dim values() As Double = joFetchData.RunMethod("getValues", Array(0))
DataPoints.Clear
For i = 0 To RowCount - 1
Dim m As Map = CreateMap(timestamps(i):values(i))
DataPoints.Add(m)
' Log($"${ConvertUNIXTimeStamp(timestamps(i), "dd-MMM-YYYY")} = ${NumberFormat(values(i),0,0)}"$)
Next
Catch
Log(LastException)
End Try
End Sub`
'Get the time as ticks of the last update
`Public Sub GetLastUpdateTimeStamp As String
Dim lut As Long = joRrdDb.RunMethod("getLastUpdateTime", Null)
Return ConvertUNIXTimeStamp(lut, "dd-MMM-YYYY")
End Sub`
'Get the time as ticks of the last update
`Public Sub GetLastUpdateTimeStampTicks As Long
Return joRrdDb.RunMethod("getLastUpdateTime", Null)
End Sub`
Region Helpers
'Convert UNIX datetime ticks to datetime string
'Example:
'Log(ConvertUNIXTimeStamp(1445348164.10837, "dd-MM-YYYY"))
'Result: 20-10-2015 15:36:04
`Public Sub ConvertUNIXTimeStamp(dt As Long, dtf As String) As String 'ignore
dt = dt * 1000
DateTime.DateFormat = dtf
Return $"${DateTime.Date(dt)} ${DateTime.Time(dt)}"$
End Sub`
'Convert datetime to UNIX ticks
'Example:
'Dim unixnow As Long = ConvertTimeStampToUNIX(DateTime.Now)
'Log(unixnow)
'Result: 1487924548
'which is 24-02-2017 09:22:28
`Public Sub ConvertTimeStampToUNIX(dt As Long) As Long 'ignore
Return dt / 1000.0D
End Sub`
End Region

Changed: 2018-03-20 10:24, Category: Jar, File: T6C204M2149.htm, ID: 2149