Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsdatetimestatisticalsummary.h 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 : : 16 : : #ifndef QGSDATETIMESTATISTICALSUMMARY_H 17 : : #define QGSDATETIMESTATISTICALSUMMARY_H 18 : : 19 : : #include "qgis_core.h" 20 : : #include "qgis_sip.h" 21 : : #include "qgsinterval.h" 22 : : #include <QSet> 23 : : #include <QDateTime> 24 : : #include <QVariantList> 25 : : 26 : : /*************************************************************************** 27 : : * This class is considered CRITICAL and any change MUST be accompanied with 28 : : * full unit tests in test_qgsdatetimestatisticalsummary.py. 29 : : * See details in QEP #17 30 : : ****************************************************************************/ 31 : : 32 : : /** 33 : : * \ingroup core 34 : : * \class QgsDateTimeStatisticalSummary 35 : : * \brief Calculator for summary statistics and aggregates for a list of datetimes. 36 : : * 37 : : * Statistics are calculated by calling calculate() and passing a list of datetimes. The 38 : : * individual statistics can then be retrieved using the associated methods. Note that not all statistics 39 : : * are calculated by default. Statistics which require slower computations are only calculated by 40 : : * specifying the statistic in the constructor or via setStatistics(). 41 : : * 42 : : * \since QGIS 2.16 43 : : */ 44 : : 45 : 0 : class CORE_EXPORT QgsDateTimeStatisticalSummary 46 : : { 47 : : public: 48 : : 49 : : //! Enumeration of flags that specify statistics to be calculated 50 : : enum Statistic 51 : : { 52 : : Count = 1, //!< Count 53 : : CountDistinct = 2, //!< Number of distinct datetime values 54 : : CountMissing = 4, //!< Number of missing (null) values 55 : : Min = 8, //!< Minimum (earliest) datetime value 56 : : Max = 16, //!< Maximum (latest) datetime value 57 : : Range = 32, //!< Interval between earliest and latest datetime value 58 : : All = Count | CountDistinct | CountMissing | Min | Max | Range, //!< All statistics 59 : : }; 60 : : Q_DECLARE_FLAGS( Statistics, Statistic ) 61 : : 62 : : /** 63 : : * Constructor for QgsDateTimeStatisticalSummary 64 : : * \param stats flags for statistics to calculate 65 : : */ 66 : : QgsDateTimeStatisticalSummary( QgsDateTimeStatisticalSummary::Statistics stats = All ); 67 : : 68 : : /** 69 : : * Returns flags which specify which statistics will be calculated. Some statistics 70 : : * are always calculated (e.g., count). 71 : : * \see setStatistics 72 : : */ 73 : : Statistics statistics() const { return mStatistics; } 74 : : 75 : : /** 76 : : * Sets flags which specify which statistics will be calculated. Some statistics 77 : : * are always calculated (e.g., count). 78 : : * \param stats flags for statistics to calculate 79 : : * \see statistics 80 : : */ 81 : : void setStatistics( Statistics stats ) { mStatistics = stats; } 82 : : 83 : : /** 84 : : * Resets the calculated values 85 : : */ 86 : : void reset(); 87 : : 88 : : /** 89 : : * Calculates summary statistics for a list of variants. Any non-datetime variants will be 90 : : * ignored. 91 : : * \param values list of variants 92 : : * \see addValue() 93 : : */ 94 : : void calculate( const QVariantList &values ); 95 : : 96 : : /** 97 : : * Adds a single datetime to the statistics calculation. Calling this method 98 : : * allows datetimes to be added to the calculation one at a time. For large 99 : : * quantities of dates this may be more efficient then first adding all the 100 : : * variants to a list and calling calculate(). 101 : : * \param value datetime to add. Any non-datetime variants will be ignored. 102 : : * \note call reset() before adding the first datetime using this method 103 : : * to clear the results from any previous calculations 104 : : * \note finalize() must be called after adding the final value and before 105 : : * retrieving calculated statistics. 106 : : * \see calculate() 107 : : * \see finalize() 108 : : */ 109 : : void addValue( const QVariant &value ); 110 : : 111 : : /** 112 : : * Must be called after adding all datetimes with addValue() and before retrieving 113 : : * any calculated datetime statistics. 114 : : * \see addValue() 115 : : */ 116 : : void finalize(); 117 : : 118 : : /** 119 : : * Returns the value of a specified statistic 120 : : * \param stat statistic to return 121 : : * \returns calculated value of statistic 122 : : */ 123 : : QVariant statistic( QgsDateTimeStatisticalSummary::Statistic stat ) const; 124 : : 125 : : /** 126 : : * Returns the calculated count of values. 127 : : */ 128 : : int count() const { return mCount; } 129 : : 130 : : /** 131 : : * Returns the number of distinct datetime values. 132 : : */ 133 : : int countDistinct() const { return mValues.count(); } 134 : : 135 : : /** 136 : : * Returns the set of distinct datetime values. 137 : : */ 138 : : QSet< QDateTime > distinctValues() const { return mValues; } 139 : : 140 : : /** 141 : : * Returns the number of missing (null) datetime values. 142 : : */ 143 : : int countMissing() const { return mCountMissing; } 144 : : 145 : : /** 146 : : * Returns the minimum (earliest) non-null datetime value. 147 : : */ 148 : : QDateTime min() const { return mMin; } 149 : : 150 : : /** 151 : : * Returns the maximum (latest) non-null datetime value. 152 : : */ 153 : : QDateTime max() const { return mMax; } 154 : : 155 : : /** 156 : : * Returns the range (interval between earliest and latest non-null datetime values). 157 : : */ 158 : : QgsInterval range() const { return mMax - mMin; } 159 : : 160 : : /** 161 : : * Returns the friendly display name for a statistic 162 : : * \param statistic statistic to return name for 163 : : */ 164 : : static QString displayName( QgsDateTimeStatisticalSummary::Statistic statistic ); 165 : : 166 : : private: 167 : : 168 : : Statistics mStatistics; 169 : : 170 : : int mCount; 171 : : QSet< QDateTime > mValues; 172 : : int mCountMissing; 173 : : QDateTime mMin; 174 : : QDateTime mMax; 175 : : bool mIsTimes; 176 : : 177 : : void testDateTime( const QDateTime &dateTime, bool isNull ); 178 : : }; 179 : : 180 : : Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDateTimeStatisticalSummary::Statistics ) 181 : : 182 : : #endif // QGSDATETIMESTATISTICALSUMMARY_H