QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsdatetimestatisticalsummary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatetimestatisticalsummary.cpp
3  ---------------------------------
4  Date : May 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include <QString>
18 #include <QDateTime>
19 #include <QStringList>
20 #include <QObject>
21 #include <QVariant>
22 #include <QVariantList>
23 #include <limits>
24 
25 /***************************************************************************
26  * This class is considered CRITICAL and any change MUST be accompanied with
27  * full unit tests in test_qgsdatetimestatisticalsummary.py.
28  * See details in QEP #17
29  ****************************************************************************/
30 
31 QgsDateTimeStatisticalSummary::QgsDateTimeStatisticalSummary( QgsDateTimeStatisticalSummary::Statistics stats )
32  : mStatistics( stats )
33 {
34  reset();
35 }
36 
38 {
39  mCount = 0;
40  mValues.clear();
41  mCountMissing = 0;
42  mMin = QDateTime();
43  mMax = QDateTime();
44  mIsTimes = false;
45 }
46 
47 void QgsDateTimeStatisticalSummary::calculate( const QVariantList &values )
48 {
49  reset();
50 
51  const auto constValues = values;
52  for ( const QVariant &variant : constValues )
53  {
54  addValue( variant );
55  }
56  finalize();
57 }
58 
59 void QgsDateTimeStatisticalSummary::addValue( const QVariant &value )
60 {
61 
62  if ( value.type() == QVariant::DateTime )
63  {
64  testDateTime( value.toDateTime(), value.isNull() );
65  }
66  else if ( value.type() == QVariant::Date )
67  {
68  const QDate date = value.toDate();
69  testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
70  : QDateTime(), value.isNull() );
71  }
72  else if ( value.type() == QVariant::Time )
73  {
74  mIsTimes = true;
75  const QTime time = value.toTime();
76  testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
77  : QDateTime(), value.isNull() );
78  }
79  else //not a date
80  {
81  mCountMissing++;
82  mCount++;
83  }
84 
85  // QTime?
86 }
87 
89 {
90  //nothing to do for now - this method has been added for forward compatibility
91  //if statistics are implemented which require a post-calculation step
92 }
93 
94 void QgsDateTimeStatisticalSummary::testDateTime( const QDateTime &dateTime, bool isNull )
95 {
96  mCount++;
97 
98  if ( !dateTime.isValid() || isNull )
99  mCountMissing++;
100 
101  if ( mStatistics & CountDistinct )
102  {
103  mValues << dateTime;
104  }
105  if ( mStatistics & Min || mStatistics & Range )
106  {
107  if ( mMin.isValid() && dateTime.isValid() )
108  {
109  mMin = std::min( mMin, dateTime );
110  }
111  else if ( !mMin.isValid() && dateTime.isValid() )
112  {
113  mMin = dateTime;
114  }
115  }
116  if ( mStatistics & Max || mStatistics & Range )
117  {
118  if ( mMax.isValid() && dateTime.isValid() )
119  {
120  mMax = std::max( mMax, dateTime );
121  }
122  else if ( !mMax.isValid() && dateTime.isValid() )
123  {
124  mMax = dateTime;
125  }
126  }
127 }
128 
130 {
131  switch ( stat )
132  {
133  case Count:
134  return mCount;
135  case CountDistinct:
136  return mValues.count();
137  case CountMissing:
138  return mCountMissing;
139  case Min:
140  return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
141  case Max:
142  return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
143  case Range:
144  return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
145  case All:
146  return 0;
147  }
148  return 0;
149 }
150 
152 {
153  switch ( statistic )
154  {
155  case Count:
156  return QObject::tr( "Count" );
157  case CountDistinct:
158  return QObject::tr( "Count (distinct)" );
159  case CountMissing:
160  return QObject::tr( "Count (missing)" );
161  case Min:
162  return QObject::tr( "Minimum (earliest)" );
163  case Max:
164  return QObject::tr( "Maximum (latest)" );
165  case Range:
166  return QObject::tr( "Range (interval)" );
167  case All:
168  return QString();
169  }
170  return QString();
171 }
172 
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
QgsDateTimeStatisticalSummary(QgsDateTimeStatisticalSummary::Statistics stats=All)
Constructor for QgsDateTimeStatisticalSummary.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
void reset()
Resets the calculated values.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Min
Minimum (earliest) datetime value.
@ Max
Maximum (latest) datetime value.
@ CountDistinct
Number of distinct datetime values.
@ Range
Interval between earliest and latest datetime value.
@ CountMissing
Number of missing (null) values.
static QString displayName(QgsDateTimeStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.