77
88use Illuminate \Database \Eloquent \Builder ;
99use Illuminate \Support \Facades \DB ;
10+ use Javaabu \Stats \Enums \TimeSeriesModes ;
1011use Javaabu \Stats \TimeSeriesStats ;
1112
1213abstract class AggregateStatsRepository extends AbstractTimeSeriesStatsRepository
@@ -21,6 +22,11 @@ public abstract function getTable(): string;
2122 */
2223 public abstract function getAggregateSql (): string ;
2324
25+ public function getDateSqlForTimeMode (TimeSeriesModes $ mode ): string
26+ {
27+ return $ mode ->getSql ($ this ->getDateField ());
28+ }
29+
2430 /**
2531 * Get the date field name for the repository
2632 */
@@ -44,8 +50,10 @@ public function getDateField(): string
4450 */
4551 public function hour (): Builder
4652 {
53+ $ this ->setCurrentMode (TimeSeriesModes::HOUR );
54+
4755 return $ this ->filteredQuery ()
48- ->select (DB ::raw ($ this ->getAggregateSql ().", DATE_FORMAT( " . $ this ->getDateField (). " , '%Y-%m-%d %H:00') as hour " ))
56+ ->select (DB ::raw ($ this ->getAggregateSql ().", " . $ this ->getDateSqlForTimeMode (TimeSeriesModes:: HOUR ) . " as hour " ))
4957 ->groupBy ('hour ' )
5058 ->orderBy ('hour ' , 'ASC ' );
5159 }
@@ -57,8 +65,10 @@ public function hour(): Builder
5765 */
5866 public function day (): Builder
5967 {
68+ $ this ->setCurrentMode (TimeSeriesModes::DAY );
69+
6070 return $ this ->filteredQuery ()
61- ->select (DB ::raw ($ this ->getAggregateSql ().", DATE( " . $ this ->getDateField (). " ) as day " ))
71+ ->select (DB ::raw ($ this ->getAggregateSql ().", " . $ this ->getDateSqlForTimeMode (TimeSeriesModes:: DAY ) . " as day " ))
6272 ->groupBy ('day ' )
6373 ->orderBy ('day ' , 'ASC ' );
6474 }
@@ -70,11 +80,11 @@ public function day(): Builder
7080 */
7181 public function week (): Builder
7282 {
73- $ week_mode = TimeSeriesStats:: weekMode ( );
83+ $ this -> setCurrentMode (TimeSeriesModes:: WEEK );
7484
7585 return $ this ->filteredQuery ()
7686 //->select(DB::raw($this->getAggregateSql().", DATE_FORMAT(".$this->getDateField().", '%X, %V') as week"))
77- ->select (DB ::raw ($ this ->getAggregateSql ().", YEARWEEK( " . $ this ->getDateField (). " , $ week_mode ) as week " ))
87+ ->select (DB ::raw ($ this ->getAggregateSql ().", " . $ this ->getDateSqlForTimeMode (TimeSeriesModes:: WEEK ) . " as week " ))
7888 ->groupBy ('week ' )
7989 ->orderBy ('week ' , 'ASC ' );
8090 }
@@ -86,8 +96,10 @@ public function week(): Builder
8696 */
8797 public function month (): Builder
8898 {
99+ $ this ->setCurrentMode (TimeSeriesModes::MONTH );
100+
89101 return $ this ->filteredQuery ()
90- ->select (DB ::raw ($ this ->getAggregateSql ().", DATE_FORMAT( " . $ this ->getDateField (). " , '%Y, %m') as month " ))
102+ ->select (DB ::raw ($ this ->getAggregateSql ().", " . $ this ->getDateSqlForTimeMode (TimeSeriesModes:: MONTH ) . " as month " ))
91103 ->groupBy ('month ' )
92104 ->orderBy ('month ' , 'ASC ' );
93105 }
@@ -99,8 +111,10 @@ public function month(): Builder
99111 */
100112 public function year (): Builder
101113 {
114+ $ this ->setCurrentMode (TimeSeriesModes::YEAR );
115+
102116 return $ this ->filteredQuery ()
103- ->select (DB ::raw ($ this ->getAggregateSql ().", YEAR( " . $ this ->getDateField (). " ) as year " ))
117+ ->select (DB ::raw ($ this ->getAggregateSql ().", " . $ this ->getDateSqlForTimeMode (TimeSeriesModes:: YEAR ) . " as year " ))
104118 ->groupBy ('year ' )
105119 ->orderBy ('year ' , 'ASC ' );
106120 }
@@ -110,6 +124,8 @@ public function year(): Builder
110124 */
111125 public function total (): float |int
112126 {
127+ $ this ->setCurrentMode (null );
128+
113129 $ total = $ this ->filteredQuery ()
114130 ->select (DB ::raw ($ this ->getAggregateSql ()))
115131 ->value ($ this ->getAggregateFieldName ());
0 commit comments