@@ -54,21 +54,28 @@ func (db *storageUsageDB) GetDaily(ctx context.Context, satelliteID storj.NodeID
5454
5555	// hour_interval = current row interval_end_time - previous row interval_end_time 
5656	// Rows with 0-hour difference are assumed to be 24 hours. 
57- 	query  :=  `SELECT satellite_id, 
58- 					at_rest_total, 
57+ 	query  :=  `SELECT su1. satellite_id, 
58+ 					su1. at_rest_total, 
5959					COALESCE( 
60- 						( 
61- 							CAST(strftime('%s', interval_end_time) AS NUMERIC) 
62- 							- 
63- 							CAST(strftime('%s', LAG(interval_end_time) OVER (PARTITION BY satellite_id ORDER BY interval_end_time)) AS NUMERIC) 
64- 						) / 3600, 
65- 						24 
60+ 					    ( 
61+ 					        CAST(strftime('%s', su1.interval_end_time) AS NUMERIC) 
62+ 					        - 
63+ 					        CAST(strftime('%s', ( 
64+ 					            SELECT interval_end_time 
65+ 					            FROM storage_usage 
66+ 					            WHERE satellite_id = su1.satellite_id 
67+ 					            AND timestamp < su1.timestamp 
68+ 					            ORDER BY timestamp DESC 
69+ 					            LIMIT 1 
70+ 					        )) AS NUMERIC) 
71+ 					    ) / 3600, 
72+ 					    24 
6673					) AS hour_interval, 
67- 					timestamp 
68- 				FROM storage_usage 
69- 				WHERE satellite_id = ? 
70- 				AND ? <= timestamp AND timestamp <= ? 
71- 				ORDER BY timestamp` 
74+ 					su1. timestamp 
75+ 				FROM storage_usage su1  
76+ 				WHERE su1. satellite_id = ? 
77+ 				AND ? <= su1. timestamp AND su1. timestamp <= ? 
78+ 				ORDER BY su1. timestamp ASC ` 
7279
7380	rows , err  :=  db .QueryContext (ctx , query , satelliteID , from .UTC (), to .UTC ())
7481	if  err  !=  nil  {
@@ -106,22 +113,30 @@ func (db *storageUsageDB) GetDailyTotal(ctx context.Context, from, to time.Time)
106113
107114	// hour_interval = current row interval_end_time - previous row interval_end_time 
108115	// Rows with 0-hour difference are assumed to be 24 hours. 
109- 	query  :=  `SELECT SUM(usages .at_rest_total), SUM(usages .hour_interval), usages .timestamp 
116+ 	query  :=  `SELECT SUM(su3 .at_rest_total), SUM(su3 .hour_interval), su3 .timestamp 
110117				FROM ( 
111- 					SELECT at_rest_total, timestamp, 
112- 							COALESCE( 
113- 								( 
114- 									CAST(strftime('%s', interval_end_time) AS NUMERIC) 
115- 									- 
116- 									CAST(strftime('%s', LAG(interval_end_time) OVER (PARTITION BY satellite_id ORDER BY interval_end_time)) AS NUMERIC) 
117- 								) / 3600, 
118- 								24 
119- 							) AS hour_interval 
120- 					FROM storage_usage 
121- 					WHERE ? <= timestamp AND timestamp <= ? 
122- 				) as usages 
123- 				GROUP BY usages.timestamp 
124- 				ORDER BY usages.timestamp` 
118+ 					SELECT su1.at_rest_total, 
119+ 						COALESCE( 
120+ 						    ( 
121+ 						        CAST(strftime('%s', su1.interval_end_time) AS NUMERIC) 
122+ 						        - 
123+ 						        CAST(strftime('%s', ( 
124+ 						            SELECT interval_end_time 
125+ 						            FROM storage_usage su2 
126+ 						            WHERE su2.satellite_id = su1.satellite_id 
127+ 						            AND su2.timestamp < su1.timestamp 
128+ 						            ORDER BY su2.timestamp DESC 
129+ 						            LIMIT 1 
130+ 						        )) AS NUMERIC) 
131+ 						    ) / 3600, 
132+ 						    24 
133+ 						) AS hour_interval, 
134+ 						su1.timestamp 
135+ 					FROM storage_usage su1 
136+ 					WHERE ? <= su1.timestamp AND su1.timestamp <= ? 
137+ 				) as su3 
138+ 				GROUP BY su3.timestamp 
139+ 				ORDER BY su3.timestamp ASC` 
125140
126141	rows , err  :=  db .QueryContext (ctx , query , from .UTC (), to .UTC ())
127142	if  err  !=  nil  {
@@ -157,23 +172,30 @@ func (db *storageUsageDB) Summary(ctx context.Context, from, to time.Time) (_, _
157172	defer  mon .Task ()(& ctx , from , to )(& err )
158173	var  summary , averageUsageInBytes  sql.NullFloat64 
159174
160- 	query  :=  `SELECT SUM(usages .at_rest_total), AVG(usages .at_rest_total_bytes) 
175+ 	query  :=  `SELECT SUM(su3 .at_rest_total), AVG(su3 .at_rest_total_bytes) 
161176				FROM ( 
162177					SELECT 
163178						at_rest_total, 
164179						at_rest_total / ( 
165180							COALESCE( 
166- 								( 
167- 									CAST(strftime('%s', interval_end_time) AS NUMERIC) 
168- 									- 
169- 									CAST(strftime('%s', LAG(interval_end_time) OVER (PARTITION BY satellite_id ORDER BY interval_end_time)) AS NUMERIC) 
170- 								) / 3600, 
171- 								24 
172- 							)  
181+ 						    	( 
182+ 						    	    CAST(strftime('%s', su1.interval_end_time) AS NUMERIC) 
183+ 						    	    - 
184+ 						    	    CAST(strftime('%s', ( 
185+ 						    	        SELECT interval_end_time 
186+ 						    	        FROM storage_usage su2 
187+ 						    	        WHERE su2.satellite_id = su1.satellite_id 
188+ 						    	        AND su2.timestamp < su1.timestamp 
189+ 						    	        ORDER BY su2.timestamp DESC 
190+ 						    	        LIMIT 1 
191+ 						    	    )) AS NUMERIC) 
192+ 						    	) / 3600, 
193+ 						    	24 
194+ 							) 
173195						) AS at_rest_total_bytes 
174- 					FROM storage_usage 
196+ 					FROM storage_usage su1  
175197					WHERE ? <= timestamp AND timestamp <= ? 
176- 				) as usages ` 
198+ 				) as su3 ` 
177199
178200	err  =  db .QueryRowContext (ctx , query , from .UTC (), to .UTC ()).Scan (& summary , & averageUsageInBytes )
179201	return  summary .Float64 , averageUsageInBytes .Float64 , err 
@@ -184,24 +206,31 @@ func (db *storageUsageDB) SatelliteSummary(ctx context.Context, satelliteID stor
184206	defer  mon .Task ()(& ctx , satelliteID , from , to )(& err )
185207	var  summary , averageUsageInBytes  sql.NullFloat64 
186208
187- 	query  :=  `SELECT SUM(usages .at_rest_total), AVG(usages .at_rest_total_bytes) 
209+ 	query  :=  `SELECT SUM(su3 .at_rest_total), AVG(su3 .at_rest_total_bytes) 
188210				FROM ( 
189211					SELECT 
190212						at_rest_total, 
191213						at_rest_total / ( 
192214							COALESCE( 
193- 								( 
194- 									CAST(strftime('%s', interval_end_time) AS NUMERIC) 
195- 									- 
196- 									CAST(strftime('%s', LAG(interval_end_time) OVER (PARTITION BY satellite_id ORDER BY interval_end_time)) AS NUMERIC) 
197- 								) / 3600, 
198- 								24 
199- 							)  
215+ 						    	( 
216+ 						    	    CAST(strftime('%s', su1.interval_end_time) AS NUMERIC) 
217+ 						    	    - 
218+ 						    	    CAST(strftime('%s', ( 
219+ 						    	        SELECT interval_end_time 
220+ 						    	        FROM storage_usage su2 
221+ 						    	        WHERE su2.satellite_id = su1.satellite_id 
222+ 						    	        AND su2.timestamp < su1.timestamp 
223+ 						    	        ORDER BY su2.timestamp DESC 
224+ 						    	        LIMIT 1 
225+ 						    	    )) AS NUMERIC) 
226+ 						    	) / 3600, 
227+ 						    	24 
228+ 							) 
200229						) AS at_rest_total_bytes 
201- 					FROM storage_usage 
230+ 					FROM storage_usage su1  
202231					WHERE satellite_id = ? 
203232					AND ? <= timestamp AND timestamp <= ? 
204- 				) as usages ` 
233+ 				) as su3 ` 
205234
206235	err  =  db .QueryRowContext (ctx , query , satelliteID , from .UTC (), to .UTC ()).Scan (& summary , & averageUsageInBytes )
207236	return  summary .Float64 , averageUsageInBytes .Float64 , err 
0 commit comments