jamulus/src/historygraph.cpp

512 lines
18 KiB
C++
Raw Normal View History

2019-05-26 20:23:13 +02:00
/******************************************************************************\
2020-01-01 15:41:43 +01:00
* Copyright (c) 2004-2020
2019-05-26 20:23:13 +02:00
*
* Author(s):
* Volker Fischer
* Peter L Jones <pljones@users.sf.net>
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
2019-05-20 21:00:09 +02:00
#include "historygraph.h"
2019-05-27 18:04:24 +02:00
2019-05-26 20:16:45 +02:00
/* Abstract class *************************************************************/
2020-03-22 20:24:30 +01:00
AHistoryGraph::AHistoryGraph ( const int iMaxDaysHistory ) :
2019-05-27 18:04:24 +02:00
sFileName ( "" ),
bDoHistory ( false ),
2020-03-22 18:45:00 +01:00
vHistoryDataFifo ( NUM_ITEMS_HISTORY ),
2019-05-27 18:04:24 +02:00
iNumTicksX ( 0 ), // number of days in history
2020-03-21 19:57:18 +01:00
iHistMaxDays ( iMaxDaysHistory ),
2019-05-20 21:00:09 +02:00
BackgroundColor ( "white" ), // background
FrameColor ( "black" ), // frame
GridColor ( "gray" ), // grid
TextColor ( "black" ), // text
MarkerNewColor ( "darkCyan" ), // marker for new connection
MarkerNewLocalColor ( "blue" ), // marker for new local connection
MarkerStopColor ( "red" ), // marker for server stop
2019-05-27 18:04:24 +02:00
canvasRectX ( 0 ),
canvasRectY ( 0 ),
canvasRectWidth ( 640 ),
canvasRectHeight ( 450 ),
iGridFrameOffset ( 10 ),
iGridWidthWeekend ( 3 ), // should be an odd value
iXAxisTextHeight ( 22 ),
gridFrameX ( canvasRectX + iGridFrameOffset ),
gridFrameY ( canvasRectY + iGridFrameOffset ),
gridFrameWidth ( canvasRectWidth - 2 * iGridFrameOffset ),
gridFrameHeight ( canvasRectHeight - 2 * iGridFrameOffset - iXAxisTextHeight ),
gridFrameRight ( gridFrameX + gridFrameWidth - 1 ),
gridFrameBottom ( gridFrameY + gridFrameHeight - 1 ),
axisFontFamily ( "Arial" ),
axisFontWeight ( "100" ),
axisFontSize ( 12 ),
iYAxisStart ( 0 ),
iYAxisEnd ( 24 ),
iNumTicksY ( 5 ),
iTextOffsetToGrid ( 3 ),
iTextOffsetX ( 18 ),
2020-03-21 19:47:07 +01:00
iMarkerSizeNewCon ( 10 ),
iMarkerSizeServSt ( 6 )
2019-05-20 21:00:09 +02:00
{
}
void AHistoryGraph::Start ( const QString& sNewFileName )
{
QTextStream& tsConsoleStream = *( ( new ConsoleWriterFactory() )->get() );
2019-05-27 18:04:24 +02:00
tsConsoleStream << QString ( "AHistoryGraph::Start ( %1 )" ).arg ( sNewFileName ) << endl; // on console
2019-05-20 21:00:09 +02:00
if ( !sNewFileName.isEmpty() )
{
// save file name
sFileName = sNewFileName;
// set enable flag
bDoHistory = true;
// enable timer (update once a day)
TimerDailyUpdate.start ( 3600000 * 24 );
// initial update (empty graph)
Update();
}
}
void AHistoryGraph::Add ( const QDateTime& newDateTime, const EHistoryItemType curType )
{
if ( bDoHistory )
{
// create and add new element in FIFO
SHistoryData curHistoryData;
curHistoryData.DateTime = newDateTime;
curHistoryData.Type = curType;
vHistoryDataFifo.Add ( curHistoryData );
}
}
void AHistoryGraph::Add ( const QDateTime& newDateTime, const QHostAddress ClientInetAddr )
{
if ( bDoHistory )
{
// add element to history, distinguish between a local connection
// and a remote connection
if ( ( ClientInetAddr == QHostAddress ( "127.0.0.1" ) ) ||
( ClientInetAddr.toString().left ( 7 ).compare ( "192.168" ) == 0 ) )
{
// local connection
Add ( newDateTime, HIT_LOCAL_CONNECTION );
}
else
{
// remote connection
Add ( newDateTime, HIT_REMOTE_CONNECTION );
}
}
}
void AHistoryGraph::Update ( )
{
if ( bDoHistory )
{
int i;
// store current date for reference
curDate = QDate::currentDate();
// set oldest date to draw
2020-03-22 20:24:30 +01:00
QDate minDate = curDate.addDays ( iHistMaxDays * -1 );
2019-05-20 21:00:09 +02:00
// get oldest date in history
2019-05-27 18:04:24 +02:00
QDate oldestDate = curDate.addDays ( 1 ); // one day in the future
2019-05-20 21:00:09 +02:00
const int iNumItemsForHistory = vHistoryDataFifo.Size();
2019-05-27 18:04:24 +02:00
2019-05-20 21:00:09 +02:00
for ( i = 0; i < iNumItemsForHistory; i++ )
{
// only use valid dates
if ( vHistoryDataFifo[i].DateTime.date().isValid() )
{
if ( vHistoryDataFifo[i].DateTime.date() < oldestDate )
{
oldestDate = vHistoryDataFifo[i].DateTime.date();
}
}
}
2020-03-21 09:54:43 +01:00
if ( oldestDate < minDate )
{
oldestDate = minDate;
}
2020-03-21 09:54:43 +01:00
2019-05-20 21:00:09 +02:00
const int iNumDaysInHistory = -curDate.daysTo ( oldestDate ) + 1;
// draw frame of the graph
DrawFrame ( iNumDaysInHistory );
// add markers
for ( i = 0; i < iNumItemsForHistory; i++ )
{
// only use valid dates
2020-03-21 09:54:43 +01:00
if ( vHistoryDataFifo[i].DateTime.date().isValid() && ( oldestDate <= vHistoryDataFifo[i].DateTime.date() ) )
2019-05-20 21:00:09 +02:00
{
AddMarker ( vHistoryDataFifo[i] );
}
}
// save graph as picture in file
Save ( sFileName );
}
}
void AHistoryGraph::DrawFrame ( const int iNewNumTicksX )
{
int i;
// store number of x-axis ticks (number of days we want to draw
// the history for
iNumTicksX = iNewNumTicksX;
// Create actual plot region (grid frame) ----------------------------------
rect( gridFrameX, gridFrameY, gridFrameWidth, gridFrameHeight );
// calculate step for x-axis ticks so that we get the desired number of
// ticks -> 5 ticks
// TODO the following equation does not work as expected but results are acceptable
// we want to have "floor ( iNumTicksX / 5 )" which is the same as
// "iNumTicksX / 5" since "iNumTicksX" is an integer variable
const int iXAxisTickStep = iNumTicksX / 5 + 1;
// grid (ticks) for x-axis
dayXSpace = static_cast<double> ( gridFrameWidth ) / ( iNumTicksX + 1 );
2019-05-27 18:04:24 +02:00
for ( i = 0; i < static_cast<int> ( iNumTicksX ); i++ )
2019-05-20 21:00:09 +02:00
{
int iBottomExtraTickLen = 0;
const int iCurX = gridFrameX + static_cast<int> ( dayXSpace * ( i + 1 ) );
2019-05-27 18:04:24 +02:00
const QDate curXAxisDate = curDate.addDays ( 0 - static_cast<signed> ( iNumTicksX ) + i + 1 );
2019-05-20 21:00:09 +02:00
// text (print only every "iXAxisTickStep" tick)
if ( !( i % iXAxisTickStep ) )
{
2019-05-27 18:04:24 +02:00
text ( iCurX - iTextOffsetX, gridFrameBottom + iXAxisTextHeight + iTextOffsetToGrid, curXAxisDate.toString ( "dd.MM." ) );
2019-05-20 21:00:09 +02:00
iBottomExtraTickLen = 5;
}
// regular grid
2019-05-27 18:04:24 +02:00
line ( iCurX, 1 + gridFrameY, iCurX, gridFrameBottom + iBottomExtraTickLen );
2019-05-20 21:00:09 +02:00
// different grid width for weekends (overwrite regular grid)
if ( ( curXAxisDate.dayOfWeek() == 6 ) || // check for Saturday
( curXAxisDate.dayOfWeek() == 7 ) ) // check for Sunday
{
const int iGridWidthWeekendHalf = iGridWidthWeekend / 2;
2019-05-27 18:04:24 +02:00
line ( iCurX, 1 + gridFrameY + iGridWidthWeekendHalf, iCurX, gridFrameBottom - iGridWidthWeekendHalf, iGridWidthWeekend );
2019-05-20 21:00:09 +02:00
}
}
// grid (ticks) for y-axis, draw iNumTicksY - 2 grid lines and
// iNumTicksY - 1 text labels (the lowest grid line is the grid frame)
iYSpace = gridFrameHeight / ( iNumTicksY - 1 );
2019-05-27 18:04:24 +02:00
for ( i = 0; i < ( static_cast<int> ( iNumTicksY ) - 1 ); i++ )
2019-05-20 21:00:09 +02:00
{
const int iCurY = gridFrameY + iYSpace * ( i + 1 );
// text
2019-05-27 18:04:24 +02:00
text ( gridFrameX + iTextOffsetToGrid,
2019-05-20 21:00:09 +02:00
iCurY - iTextOffsetToGrid,
QString ( "%1:00" ).arg ( ( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) * ( ( iNumTicksY - 2 ) - i ) ) );
// grid (do not overwrite frame)
2019-05-27 18:04:24 +02:00
if ( i < ( static_cast<int> ( iNumTicksY ) - 2 ) )
2019-05-20 21:00:09 +02:00
{
2019-05-27 18:04:24 +02:00
line ( gridFrameX, iCurY, gridFrameRight, iCurY );
2019-05-20 21:00:09 +02:00
}
}
}
void AHistoryGraph::AddMarker ( const SHistoryData& curHistoryData )
{
// calculate x-axis offset (difference of days compared to
// current date)
const int iXAxisOffs =
curDate.daysTo ( curHistoryData.DateTime.date() );
// check range, if out of range, do not plot anything
2019-05-27 18:04:24 +02:00
if ( -iXAxisOffs > ( static_cast<signed> ( iNumTicksX ) - 1 ) )
2019-05-20 21:00:09 +02:00
{
return;
}
// calculate y-axis offset (consider hours and minutes)
const double dYAxisOffs = 24 - curHistoryData.DateTime.time().hour() -
static_cast<double> ( curHistoryData.DateTime.time().minute() ) / 60;
// calculate the actual point in the graph (in pixels)
2019-05-27 18:04:24 +02:00
int curPointX = gridFrameX + static_cast<int> ( dayXSpace * ( static_cast<signed> ( iNumTicksX ) + iXAxisOffs ) );
2019-05-20 21:00:09 +02:00
int curPointY = gridFrameY + static_cast<int> ( static_cast<double> (
gridFrameHeight ) / ( iYAxisEnd - iYAxisStart ) * dYAxisOffs );
2019-05-27 18:04:24 +02:00
2019-05-20 21:00:09 +02:00
QString curPointColour = MarkerNewColor;
2019-05-27 18:04:24 +02:00
int curPointSize = iMarkerSizeNewCon;
2019-05-20 21:00:09 +02:00
// we use different markers for new connection and server stop items
switch ( curHistoryData.Type )
{
case HIT_SERVER_STOP:
curPointColour = MarkerStopColor;
2019-05-27 18:04:24 +02:00
curPointSize = iMarkerSizeServSt;
2019-05-20 21:00:09 +02:00
break;
case HIT_LOCAL_CONNECTION:
curPointColour = MarkerNewLocalColor;
break;
case HIT_REMOTE_CONNECTION:
curPointColour = MarkerNewColor;
break;
}
2019-05-27 18:04:24 +02:00
point ( curPointX - curPointSize / 2, curPointY - curPointSize / 2, curPointSize, curPointColour );
2019-05-20 21:00:09 +02:00
}
2019-05-26 20:16:45 +02:00
2019-05-27 18:04:24 +02:00
2019-05-26 20:16:45 +02:00
/* JPEG History Graph implementation ******************************************/
2020-03-22 20:24:30 +01:00
CJpegHistoryGraph::CJpegHistoryGraph ( const int iMaxDaysHistory ) :
2020-03-22 18:45:00 +01:00
AHistoryGraph ( iMaxDaysHistory ),
2020-03-21 19:57:18 +01:00
PlotPixmap ( 1, 1, QImage::Format_RGB32 ),
2019-05-26 20:16:45 +02:00
iAxisFontWeight ( -1 )
{
// scale pixmap to correct size
PlotPixmap = PlotPixmap.scaled ( canvasRectWidth, canvasRectHeight );
// axisFontWeight is a string matching the CSS2 font-weight definition
// Thin = 0, // 100
// ExtraLight = 12, // 200
// Light = 25, // 300
// Normal = 50, // 400
// Medium = 57, // 500
// DemiBold = 63, // 600
// Bold = 75, // 700
// ExtraBold = 81, // 800
// Black = 87 // 900
bool ok;
int weight = axisFontWeight.toInt( &ok );
2019-05-27 18:04:24 +02:00
if ( !ok )
2019-05-26 20:16:45 +02:00
{
2019-05-27 18:04:24 +02:00
if ( !QString ( "normal" ).compare ( axisFontWeight, Qt::CaseSensitivity::CaseInsensitive ) )
{
iAxisFontWeight = 50;
}
else if ( !QString ( "bold" ).compare ( axisFontWeight, Qt::CaseSensitivity::CaseInsensitive ) )
{
weight = 75;
}
2019-05-26 20:16:45 +02:00
}
else
{
2019-05-27 18:04:24 +02:00
if ( weight <= 100 ) { iAxisFontWeight = 0; }
else if ( weight <= 200 ) { iAxisFontWeight = 12; }
else if ( weight <= 300 ) { iAxisFontWeight = 25; }
else if ( weight <= 400 ) { iAxisFontWeight = 50; }
else if ( weight <= 500 ) { iAxisFontWeight = 57; }
else if ( weight <= 600 ) { iAxisFontWeight = 63; }
else if ( weight <= 700 ) { iAxisFontWeight = 75; }
else if ( weight <= 800 ) { iAxisFontWeight = 81; }
else if ( weight <= 900 ) { iAxisFontWeight = 87; }
2019-05-26 20:16:45 +02:00
}
// if all else fails, it's left at -1
// QTextStream& tsConsoleStream = *( ( new ConsoleWriterFactory() )->get() );
// tsConsoleStream << "CJpegHistoryGraph" << endl; // on console
2019-05-26 20:16:45 +02:00
2019-05-27 18:04:24 +02:00
2019-05-26 20:16:45 +02:00
// Connections -------------------------------------------------------------
QObject::connect ( &TimerDailyUpdate, SIGNAL ( timeout() ),
this, SLOT ( OnTimerDailyUpdate() ) );
}
// Override Update to blank out the plot area each time
void CJpegHistoryGraph::Update()
{
if ( bDoHistory )
{
// create JPEG image
PlotPixmap.fill ( BackgroundColor ); // fill background
AHistoryGraph::Update();
}
}
void CJpegHistoryGraph::Save ( const QString sFileName )
{
// save plot as a file
PlotPixmap.save ( sFileName, "JPG", 90 );
}
void CJpegHistoryGraph::rect ( const unsigned int x, const unsigned int y, const unsigned int width, const unsigned int height )
{
QPainter PlotPainter ( &PlotPixmap );
PlotPainter.setPen ( FrameColor );
PlotPainter.drawRect ( x, y, width, height );
}
void CJpegHistoryGraph::text ( const unsigned int x, const unsigned int y, const QString& value )
{
QPainter PlotPainter ( &PlotPixmap );
PlotPainter.setPen ( TextColor );
// QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
2019-05-27 18:04:24 +02:00
PlotPainter.setFont ( QFont( axisFontFamily, static_cast<int> ( axisFontSize ), iAxisFontWeight ) );
2019-05-26 20:16:45 +02:00
PlotPainter.drawText ( QPoint ( x, y ), value );
}
void CJpegHistoryGraph::line ( const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const unsigned int strokeWidth )
{
QPainter PlotPainter ( &PlotPixmap );
PlotPainter.setPen ( QPen ( QBrush ( QColor ( GridColor ) ), strokeWidth ) );
PlotPainter.drawLine ( x1, y1, x2, y2 );
}
void CJpegHistoryGraph::point ( const unsigned int x, const unsigned int y, const unsigned int size, const QString& colour )
{
QPainter PlotPainter ( &PlotPixmap );
PlotPainter.setPen ( QPen ( QBrush( QColor ( colour ) ), size ) );
PlotPainter.drawPoint ( x, y );
}
2019-05-27 18:04:24 +02:00
2019-05-26 20:16:45 +02:00
/* SVG History Graph implementation *******************************************/
2020-03-22 20:24:30 +01:00
CSvgHistoryGraph::CSvgHistoryGraph ( const int iMaxDaysHistory ) :
2020-03-22 18:45:00 +01:00
AHistoryGraph ( iMaxDaysHistory ),
2020-03-21 19:57:18 +01:00
svgImage ( "" ),
2019-05-27 18:04:24 +02:00
svgStreamWriter ( &svgImage )
2019-05-26 20:16:45 +02:00
{
// set SVG veiwBox to correct size to ensure correct scaling
2019-05-27 18:04:24 +02:00
svgRootAttributes.append ( "viewBox",
QString ( "%1, %2, %3, %4" )
.arg ( canvasRectX )
.arg ( canvasRectY )
.arg ( canvasRectWidth )
.arg ( canvasRectHeight )
2019-05-26 20:16:45 +02:00
);
2019-05-27 18:04:24 +02:00
svgRootAttributes.append ( "xmlns", "http://www.w3.org/2000/svg" );
svgRootAttributes.append ( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
2019-05-26 20:16:45 +02:00
// QTextStream& tsConsoleStream = *( ( new ConsoleWriterFactory() )->get() );
// tsConsoleStream << "CSvgHistoryGraph" << endl; // on console
2019-05-26 20:16:45 +02:00
2019-05-27 18:04:24 +02:00
2019-05-26 20:16:45 +02:00
// Connections -------------------------------------------------------------
QObject::connect ( &TimerDailyUpdate, SIGNAL ( timeout() ),
this, SLOT ( OnTimerDailyUpdate() ) );
}
// Override Update to create the fresh SVG stream each time
void CSvgHistoryGraph::Update()
{
if ( bDoHistory )
{
// create SVG document
svgImage = "";
2019-05-27 18:04:24 +02:00
svgStreamWriter.setAutoFormatting ( true );
2019-05-26 20:16:45 +02:00
svgStreamWriter.writeStartDocument();
2019-05-27 18:04:24 +02:00
svgStreamWriter.writeStartElement ( "svg" );
svgStreamWriter.writeAttributes ( svgRootAttributes );
2019-05-26 20:16:45 +02:00
AHistoryGraph::Update();
}
}
void CSvgHistoryGraph::Save ( const QString sFileName )
{
svgStreamWriter.writeEndDocument();
2019-05-27 18:04:24 +02:00
QFile outf ( sFileName );
if ( !outf.open ( QFile::WriteOnly ) )
{
throw std::runtime_error ( ( sFileName + " could not be written. Aborting." ).toStdString() );
2019-05-26 20:16:45 +02:00
}
2019-05-27 18:04:24 +02:00
QTextStream out ( &outf );
2019-05-26 20:16:45 +02:00
out << svgImage << endl;
}
void CSvgHistoryGraph::rect ( const unsigned int x, const unsigned int y, const unsigned int width, const unsigned int height )
{
2019-05-27 18:04:24 +02:00
svgStreamWriter.writeEmptyElement ( "rect" );
svgStreamWriter.writeAttribute ( "x", QString ( "%1" ).arg ( x ) );
svgStreamWriter.writeAttribute ( "y", QString ( "%1" ).arg ( y ) );
svgStreamWriter.writeAttribute ( "width", QString ( "%1" ).arg ( width ) );
svgStreamWriter.writeAttribute ( "height", QString ( "%1" ).arg ( height ) );
svgStreamWriter.writeAttribute ( "stroke", FrameColor );
svgStreamWriter.writeAttribute ( "stroke-width", QString ( "1" ) );
svgStreamWriter.writeAttribute ( "style", QString ( "fill: none;" ) );
2019-05-26 20:16:45 +02:00
}
void CSvgHistoryGraph::text ( const unsigned int x, const unsigned int y, const QString& value )
{
2019-05-27 18:04:24 +02:00
svgStreamWriter.writeStartElement ( "text" );
svgStreamWriter.writeAttribute ( "x", QString ( "%1" ).arg ( x ) );
svgStreamWriter.writeAttribute ( "y", QString ( "%1" ).arg ( y ) );
svgStreamWriter.writeAttribute ( "stroke", TextColor );
svgStreamWriter.writeAttribute ( "font-family", axisFontFamily );
svgStreamWriter.writeAttribute ( "font-weight", axisFontWeight );
svgStreamWriter.writeAttribute ( "font-size", QString ( "%1" ).arg ( axisFontSize ) );
2019-05-26 20:16:45 +02:00
svgStreamWriter.writeCharacters( value );
svgStreamWriter.writeEndElement();
}
void CSvgHistoryGraph::line ( const unsigned int x1, const unsigned int y1, const unsigned int x2, const unsigned int y2, const unsigned int strokeWidth )
{
2019-05-27 18:04:24 +02:00
svgStreamWriter.writeEmptyElement ( "line" );
svgStreamWriter.writeAttribute ( "x1", QString ( "%1" ).arg ( x1 ) );
svgStreamWriter.writeAttribute ( "y1", QString ( "%1" ).arg ( y1 ) );
svgStreamWriter.writeAttribute ( "x2", QString ( "%1" ).arg ( x2 ) );
svgStreamWriter.writeAttribute ( "y2", QString ( "%1" ).arg ( y2 ) );
svgStreamWriter.writeAttribute ( "stroke", GridColor );
svgStreamWriter.writeAttribute ( "stroke-width", QString ( "%1" ).arg ( strokeWidth ) );
2019-05-26 20:16:45 +02:00
}
void CSvgHistoryGraph::point ( const unsigned int x, const unsigned int y, const unsigned int size, const QString& colour )
{
2019-05-27 18:04:24 +02:00
svgStreamWriter.writeEmptyElement ( "rect" );
svgStreamWriter.writeAttribute ( "x", QString ( "%1" ).arg ( x ) );
svgStreamWriter.writeAttribute ( "y", QString ( "%1" ).arg ( y ) );
svgStreamWriter.writeAttribute ( "width", QString ( "%1" ).arg ( size ) );
svgStreamWriter.writeAttribute ( "height", QString ( "%1" ).arg ( size ) );
svgStreamWriter.writeAttribute ( "stroke-opacity", "0" );
svgStreamWriter.writeAttribute ( "fill", colour );
2019-05-26 20:16:45 +02:00
}