QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  if ( value.type() == QVariant::DateTime )
62  {
63  testDateTime( value.toDateTime() );
64  }
65  else if ( value.type() == QVariant::Date )
66  {
67  QDate date = value.toDate();
68  testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
69  : QDateTime() );
70  }
71  else if ( value.type() == QVariant::Time )
72  {
73  mIsTimes = true;
74  QTime time = value.toTime();
75  testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
76  : QDateTime() );
77  }
78  else //not a date
79  {
80  mCountMissing++;
81  mCount++;
82  }
83  // QTime?
84 }
85 
87 {
88  //nothing to do for now - this method has been added for forward compatibility
89  //if statistics are implemented which require a post-calculation step
90 }
91 
92 void QgsDateTimeStatisticalSummary::testDateTime( const QDateTime &dateTime )
93 {
94  mCount++;
95 
96  if ( !dateTime.isValid() )
97  mCountMissing++;
98 
99  if ( mStatistics & CountDistinct )
100  {
101  mValues << dateTime;
102  }
103  if ( mStatistics & Min || mStatistics & Range )
104  {
105  if ( mMin.isValid() && dateTime.isValid() )
106  {
107  mMin = std::min( mMin, dateTime );
108  }
109  else if ( !mMin.isValid() && dateTime.isValid() )
110  {
111  mMin = dateTime;
112  }
113  }
114  if ( mStatistics & Max || mStatistics & Range )
115  {
116  if ( mMax.isValid() && dateTime.isValid() )
117  {
118  mMax = std::max( mMax, dateTime );
119  }
120  else if ( !mMax.isValid() && dateTime.isValid() )
121  {
122  mMax = dateTime;
123  }
124  }
125 }
126 
128 {
129  switch ( stat )
130  {
131  case Count:
132  return mCount;
133  case CountDistinct:
134  return mValues.count();
135  case CountMissing:
136  return mCountMissing;
137  case Min:
138  return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
139  case Max:
140  return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
141  case Range:
142  return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
143  case All:
144  return 0;
145  }
146  return 0;
147 }
148 
150 {
151  switch ( statistic )
152  {
153  case Count:
154  return QObject::tr( "Count" );
155  case CountDistinct:
156  return QObject::tr( "Count (distinct)" );
157  case CountMissing:
158  return QObject::tr( "Count (missing)" );
159  case Min:
160  return QObject::tr( "Minimum (earliest)" );
161  case Max:
162  return QObject::tr( "Maximum (latest)" );
163  case Range:
164  return QObject::tr( "Range (interval)" );
165  case All:
166  return QString();
167  }
168  return QString();
169 }
170 
QgsDateTimeStatisticalSummary::displayName
static QString displayName(QgsDateTimeStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
Definition: qgsdatetimestatisticalsummary.cpp:149
QgsDateTimeStatisticalSummary::reset
void reset()
Resets the calculated values.
Definition: qgsdatetimestatisticalsummary.cpp:37
QgsDateTimeStatisticalSummary::CountMissing
@ CountMissing
Number of missing (null) values.
Definition: qgsdatetimestatisticalsummary.h:54
QgsDateTimeStatisticalSummary::All
@ All
All statistics.
Definition: qgsdatetimestatisticalsummary.h:58
QgsDateTimeStatisticalSummary::statistic
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
Definition: qgsdatetimestatisticalsummary.cpp:127
QgsDateTimeStatisticalSummary::calculate
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
Definition: qgsdatetimestatisticalsummary.cpp:47
qgsdatetimestatisticalsummary.h
QgsDateTimeStatisticalSummary::QgsDateTimeStatisticalSummary
QgsDateTimeStatisticalSummary(QgsDateTimeStatisticalSummary::Statistics stats=All)
Constructor for QgsDateTimeStatisticalSummary.
Definition: qgsdatetimestatisticalsummary.cpp:31
QgsDateTimeStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsdatetimestatisticalsummary.h:51
QgsDateTimeStatisticalSummary::Max
@ Max
Maximum (latest) datetime value.
Definition: qgsdatetimestatisticalsummary.h:56
QgsDateTimeStatisticalSummary::finalize
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
Definition: qgsdatetimestatisticalsummary.cpp:86
QgsDateTimeStatisticalSummary::Count
@ Count
Count.
Definition: qgsdatetimestatisticalsummary.h:52
QgsDateTimeStatisticalSummary::Min
@ Min
Minimum (earliest) datetime value.
Definition: qgsdatetimestatisticalsummary.h:55
QgsDateTimeStatisticalSummary::CountDistinct
@ CountDistinct
Number of distinct datetime values.
Definition: qgsdatetimestatisticalsummary.h:53
QgsDateTimeStatisticalSummary::Range
@ Range
Interval between earliest and latest datetime value.
Definition: qgsdatetimestatisticalsummary.h:57
QgsDateTimeStatisticalSummary::addValue
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
Definition: qgsdatetimestatisticalsummary.cpp:59