@@ -164,23 +164,26 @@ defmodule Batches do
164164 end
165165 end
166166
167- def get_last_24h_verified_proof_stats ( ) do
168- minutes_in_a_day = 1440
169- threshold_datetime = DateTime . utc_now ( ) |> DateTime . add ( - 1 * minutes_in_a_day , :minute ) # Last 24 hours
167+ # The following query was built by reading:
168+ # - https://hexdocs.pm/ecto/Ecto.Query.html#module-fragments
169+ # - https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
170+ # - https://stackoverflow.com/questions/43288914/how-to-use-date-trunc-with-timezone-in-ecto-fragment-statement
171+ # - https://glennjon.es/2016/09/24/elixir-ecto-how-to-group-and-count-records-by-week.html
172+ def get_daily_verified_batches_summary ( ) do
173+ submission_constant_cost = Utils . constant_batch_submission_gas_cost ( )
174+ additional_cost_per_proof = Utils . additional_batch_submission_gas_cost_per_proof ( )
175+
176+ query = Batches
177+ |> where ( [ b ] , b . is_verified == true )
178+ |> group_by ( [ b ] , fragment ( "DATE_TRUNC('day', ?)" , b . response_timestamp ) )
179+ |> select ( [ b ] , % {
180+ date: fragment ( "DATE_TRUNC('day', ?)::date" , b . response_timestamp ) ,
181+ amount_of_proofs: sum ( b . amount_of_proofs ) ,
182+ gas_cost: sum ( fragment ( "? + ? * ?" , ^ submission_constant_cost , ^ additional_cost_per_proof , b . amount_of_proofs ) )
183+ } )
184+ |> order_by ( [ b ] , fragment ( "DATE_TRUNC('day', ?)" , b . response_timestamp ) )
170185
171- query = from ( b in Batches ,
172- where: b . is_verified == true and b . submission_timestamp > ^ threshold_datetime ,
173- select: { sum ( b . amount_of_proofs ) , avg ( b . fee_per_proof ) } )
174-
175- { amount_of_proofs , avg_fee_per_proof } = case Explorer.Repo . one ( query ) do
176- nil -> { 0 , 0.0 }
177- result -> result
178- end
179-
180- % {
181- amount_of_proofs: amount_of_proofs ,
182- avg_fee_per_proof: avg_fee_per_proof
183- }
186+ Explorer.Repo . all ( query )
184187 end
185188
186189 def insert_or_update ( batch_changeset , proofs ) do
0 commit comments