34 class DatabaseWrapper ;
46 virtual void add( time_t ,
unsigned int n ,
unsigned int dx ,
unsigned int dy ,
unsigned int squelch ,
unsigned int apathy )
override ;
47 virtual std::string
graph( std::string & , time_t start_time , time_t end_time )
override ;
51 unsigned int m_items ;
52 unsigned int m_total ;
67 std::string graph( std::string & , time_t start_time , time_t end_time ) ;
68 void add( time_t time ,
unsigned int total ,
unsigned int items ,
unsigned int max ,
69 unsigned int dx ,
unsigned int dy ,
unsigned int squelch ,
unsigned int apathy ) ;
72 static void run(
const std::string & s ) ;
73 static std::string png(
const std::string & rrd ) ;
74 void writeGraph( time_t start_time , time_t end_time ) ;
75 void readGraph( std::string & data ) ;
78 std::string m_rrd_filename ;
79 std::string m_png_filename ;
87 Gv::DatabaseWrapper::DatabaseWrapper(
const std::string & path ) :
98 Gv::DatabaseWrapper::~DatabaseWrapper()
103 void Gv::DatabaseWrapper::add( time_t time ,
unsigned int n ,
unsigned int dx ,
unsigned int dy ,
unsigned int squelch ,
unsigned int apathy )
105 if( m_imp ==
nullptr )
108 if( !m_imp->exists() )
114 if( m_time == 0 || time == m_time )
118 m_max = std::max(m_max,n) ;
122 m_imp->add( time , m_total , m_items , m_max , dx , dy , squelch , apathy ) ;
132 if( m_imp ==
nullptr )
return std::string() ;
133 return m_imp->graph( buffer , start_time , end_time ) ;
138 Gv::DatabaseRrd::DatabaseRrd(
const std::string & rrd_filename ) :
139 m_rrd_filename(rrd_filename) ,
140 m_png_filename(png(rrd_filename))
142 G_LOG(
"Gv::DatabaseRrd::ctor: rrd=[" << m_rrd_filename <<
"] png=[" << m_png_filename <<
"]" ) ;
145 Gv::DatabaseRrd::~DatabaseRrd()
149 std::string Gv::DatabaseRrd::png(
const std::string & rrd )
151 G_ASSERT( !rrd.empty() ) ;
153 return (path.dirname()+(path.withoutExtension().basename()+
".png")).str() ;
156 bool Gv::DatabaseRrd::exists()
const
161 void Gv::DatabaseRrd::create()
163 G_ASSERT( !m_rrd_filename.empty() ) ;
166 std::ostringstream ss ;
168 <<
"rrdtool create " << m_rrd_filename <<
" "
169 <<
"--start " << now.s <<
" "
171 <<
"DS:items:GAUGE:3:U:U "
172 <<
"DS:total:GAUGE:3:U:U "
173 <<
"DS:maximum:GAUGE:3:U:U "
174 <<
"DS:squelch:GAUGE:3:U:U "
175 <<
"DS:apathy:GAUGE:3:U:U "
176 <<
"DS:dx:GAUGE:3:U:U "
177 <<
"DS:dy:GAUGE:3:U:U "
178 <<
"RRA:AVERAGE:0:1:108000 "
179 <<
"RRA:MAX:0:1:108000 " ;
183 G_LOG(
"Gv::DatabaseRrd::create: " << ss.str() ) ;
186 throw std::runtime_error(
"failed to create rrd" ) ;
189 void Gv::DatabaseRrd::run(
const std::string & s )
191 int rc = system( s.c_str() ) ;
193 G_WARNING_ONCE(
"Gv::DatabaseRrd::run: command failed: [" << s <<
"]" ) ;
196 void Gv::DatabaseRrd::add( time_t time ,
unsigned int total ,
unsigned int items ,
unsigned int max ,
197 unsigned int dx ,
unsigned int dy ,
unsigned int squelch ,
unsigned int apathy )
199 std::ostringstream ss ;
201 <<
"rrdtool update " << m_rrd_filename <<
" "
211 G_LOG(
"Gv::DatabaseRrd::emit: " << ss.str() ) ;
215 std::string Gv::DatabaseRrd::graph( std::string & data , time_t start_time , time_t end_time )
217 writeGraph( start_time , end_time ) ;
222 void Gv::DatabaseRrd::writeGraph( time_t start_time , time_t end_time )
224 std::ostringstream ss ;
226 <<
"rrdtool graph " << m_png_filename <<
" "
227 <<
"--start " << start_time <<
" " ;
229 <<
"--end " << end_time <<
" " ; ss
230 <<
"DEF:c=" << m_rrd_filename <<
":maximum:MAX "
231 <<
"DEF:a=" << m_rrd_filename <<
":apathy:AVERAGE "
232 <<
"LINE2:c#802020 LINE2:a#208020" ;
233 G_LOG(
"Gv::DatabaseRrd::emitDatum: " << ss.str() ) ;
237 void Gv::DatabaseRrd::readGraph( std::string & data )
239 std::stringstream ss ;
240 std::ifstream png( m_png_filename.c_str() ) ;
A subsecond-resolution timestamp based on a time_t.
virtual std::string graph(std::string &, time_t start_time, time_t end_time) override
Creates a graph image in the supplied string buffer and returns the image type.
An abstract interface for a timeseries database that holds video analysis statistics.
virtual void add(time_t, unsigned int n, unsigned int dx, unsigned int dy, unsigned int squelch, unsigned int apathy) override
Adds a data point for an analysis value 'n' at time 't', together with the analysis parameters (image...
static EpochTime now()
Returns the current epoch time.
A concrete Database class that aggregates up to one second intervals and then delegates to (eg...
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
static Database * create(const std::string &path)
Factory function.
A Path object represents a file system path.
Manages an rrdtool database under the Gv::Database interface.