Skip to content

Commit

Permalink
misc: Fix typo sanitized_property_name
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Feb 14, 2024
1 parent 451b6b0 commit 9fd29cd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/services/events/stores/clickhouse/weighted_sum_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def breakdown_query

attr_reader :store

delegate :events, :charges_duration, :sanitized_propery_name, :sanitized_numeric_property, to: :store
delegate :events, :charges_duration, :sanitized_property_name, :sanitized_numeric_property, to: :store

def events_cte_sql
<<-SQL
Expand Down Expand Up @@ -116,7 +116,7 @@ def period_ratio_sql

def grouped_events_cte_sql(initial_values)
groups = store.grouped_by.map.with_index do |group, index|
"#{sanitized_propery_name(group)} AS g_#{index}"
"#{sanitized_property_name(group)} AS g_#{index}"
end

<<-SQL
Expand Down
14 changes: 7 additions & 7 deletions app/services/events/stores/clickhouse_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def last_event
end

def grouped_last_event
groups = grouped_by.map { |group| sanitized_propery_name(group) }
groups = grouped_by.map { |group| sanitized_property_name(group) }
group_names = groups.map.with_index { |_, index| "g_#{index}" }.join(', ')

cte_sql = events.group(DEDUPLICATION_GROUP)
Expand Down Expand Up @@ -89,7 +89,7 @@ def count

def grouped_count
groups = grouped_by.map.with_index do |group, index|
"#{sanitized_propery_name(group)} AS g_#{index}"
"#{sanitized_property_name(group)} AS g_#{index}"
end
group_names = groups.map.with_index { |_, index| "g_#{index}" }

Expand All @@ -114,7 +114,7 @@ def max
end

def grouped_max
groups = grouped_by.map { |group| sanitized_propery_name(group) }
groups = grouped_by.map { |group| sanitized_property_name(group) }
group_names = groups.map.with_index { |_, index| "g_#{index}" }.join(', ')

cte_sql = events.group(DEDUPLICATION_GROUP)
Expand Down Expand Up @@ -145,7 +145,7 @@ def last
end

def grouped_last
groups = grouped_by.map { |group| sanitized_propery_name(group) }
groups = grouped_by.map { |group| sanitized_property_name(group) }
group_names = groups.map.with_index { |_, index| "g_#{index}" }.join(', ')

cte_sql = events.group(DEDUPLICATION_GROUP)
Expand Down Expand Up @@ -185,7 +185,7 @@ def sum

def grouped_sum
groups = grouped_by.map.with_index do |group, index|
"#{sanitized_propery_name(group)} AS g_#{index}"
"#{sanitized_property_name(group)} AS g_#{index}"
end
group_names = groups.map.with_index { |_, index| "g_#{index}" }.join(', ')

Expand Down Expand Up @@ -230,7 +230,7 @@ def prorated_sum(period_duration:, persisted_duration: nil)

def grouped_prorated_sum(period_duration:, persisted_duration: nil)
groups = grouped_by.map.with_index do |group, index|
"#{sanitized_propery_name(group)} AS g_#{index}"
"#{sanitized_property_name(group)} AS g_#{index}"
end
group_names = groups.map.with_index { |_, index| "g_#{index}" }.join(', ')

Expand Down Expand Up @@ -369,7 +369,7 @@ def with_grouped_by_values(scope)
scope
end

def sanitized_propery_name(property = aggregation_property)
def sanitized_property_name(property = aggregation_property)
ActiveRecord::Base.sanitize_sql_for_conditions(
['events_raw.properties[?]', property],
)
Expand Down
4 changes: 2 additions & 2 deletions app/services/events/stores/postgres/unique_count_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def breakdown_query

attr_reader :store

delegate :events, :sanitized_propery_name, to: :store
delegate :events, :sanitized_property_name, to: :store

def events_cte_sql
# NOTE: Common table expression returning event's timestamp, property name and operation type.
Expand All @@ -72,7 +72,7 @@ def events_cte_sql
events
.select(
"timestamp, \
#{sanitized_propery_name} AS property, \
#{sanitized_property_name} AS property, \
COALESCE(events.properties->>'operation_type', 'add') AS operation_type",
).to_sql
})
Expand Down
8 changes: 4 additions & 4 deletions app/services/events/stores/postgres/weighted_sum_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def breakdown_query

attr_reader :store

delegate :events, :charges_duration, :sanitized_propery_name, to: :store
delegate :events, :charges_duration, :sanitized_property_name, to: :store

def events_cte_sql
<<-SQL
Expand All @@ -66,7 +66,7 @@ def events_cte_sql
UNION
(#{
events
.select("timestamp, (#{sanitized_propery_name})::numeric AS difference, events.created_at")
.select("timestamp, (#{sanitized_property_name})::numeric AS difference, events.created_at")
.to_sql
})
UNION
Expand Down Expand Up @@ -114,7 +114,7 @@ def period_ratio_sql

def grouped_events_cte_sql(initial_values)
groups = store.grouped_by.map.with_index do |group, index|
"#{sanitized_propery_name(group)} AS g_#{index}"
"#{sanitized_property_name(group)} AS g_#{index}"
end

<<-SQL
Expand All @@ -123,7 +123,7 @@ def grouped_events_cte_sql(initial_values)
UNION
(#{
events
.select("#{groups.join(', ')}, timestamp, (#{sanitized_propery_name})::numeric AS difference, events.created_at")
.select("#{groups.join(', ')}, timestamp, (#{sanitized_property_name})::numeric AS difference, events.created_at")
.to_sql
})
UNION
Expand Down
28 changes: 14 additions & 14 deletions app/services/events/stores/postgres_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def events(force_from: false)
end

def events_values(limit: nil, force_from: false)
field_name = sanitized_propery_name
field_name = sanitized_property_name
field_name = "(#{field_name})::numeric" if numeric_property

scope = events(force_from:)
Expand All @@ -47,7 +47,7 @@ def grouped_last_event
[
"DISTINCT ON (#{groups.join(', ')}) #{groups.join(', ')}",
'events.timestamp',
"(#{sanitized_propery_name})::numeric AS value",
"(#{sanitized_property_name})::numeric AS value",
].join(', '),
)
.to_sql
Expand All @@ -58,7 +58,7 @@ def grouped_last_event
def prorated_events_values(total_duration)
ratio_sql = duration_ratio_sql('events.timestamp', to_datetime, total_duration)

events.pluck(Arel.sql("(#{sanitized_propery_name})::numeric * (#{ratio_sql})::numeric"))
events.pluck(Arel.sql("(#{sanitized_property_name})::numeric * (#{ratio_sql})::numeric"))
end

def grouped_count
Expand Down Expand Up @@ -88,14 +88,14 @@ def unique_count_breakdown
end

def max
events.maximum("(#{sanitized_propery_name})::numeric")
events.maximum("(#{sanitized_property_name})::numeric")
end

def grouped_max
results = events
.reorder(nil)
.group(sanitized_grouped_by)
.maximum("(#{sanitized_propery_name})::numeric")
.maximum("(#{sanitized_property_name})::numeric")
.map { |group, value| [group, value].flatten }

prepare_grouped_result(results)
Expand All @@ -111,22 +111,22 @@ def grouped_last
sql = events
.reorder(Arel.sql((groups + ['events.timestamp DESC, created_at DESC']).join(', ')))
.select(
"DISTINCT ON (#{groups.join(', ')}) #{groups.join(', ')}, (#{sanitized_propery_name})::numeric AS value",
"DISTINCT ON (#{groups.join(', ')}) #{groups.join(', ')}, (#{sanitized_property_name})::numeric AS value",
)
.to_sql

prepare_grouped_result(Event.connection.select_all(sql).rows)
end

def sum
events.sum("(#{sanitized_propery_name})::numeric")
events.sum("(#{sanitized_property_name})::numeric")
end

def grouped_sum
results = events
.reorder(nil)
.group(sanitized_grouped_by)
.sum("(#{sanitized_propery_name})::numeric")
.sum("(#{sanitized_property_name})::numeric")
.map { |group, value| [group, value].flatten }

prepare_grouped_result(results)
Expand All @@ -141,7 +141,7 @@ def prorated_sum(period_duration:, persisted_duration: nil)

sql = <<-SQL
SUM(
(#{sanitized_propery_name})::numeric * (#{ratio})::numeric
(#{sanitized_property_name})::numeric * (#{ratio})::numeric
) AS sum_result
SQL

Expand All @@ -162,7 +162,7 @@ def grouped_prorated_sum(period_duration:, persisted_duration: nil)
sum_sql = <<-SQL
#{sanitized_grouped_by.join(', ')},
SUM(
(#{sanitized_propery_name})::numeric * (#{ratio})::numeric
(#{sanitized_property_name})::numeric * (#{ratio})::numeric
) AS sum_result
SQL

Expand All @@ -179,7 +179,7 @@ def sum_date_breakdown

events.group(Arel.sql("DATE(#{date_field})"))
.reorder(Arel.sql("DATE(#{date_field}) ASC"))
.pluck(Arel.sql("DATE(#{date_field}) AS date, SUM((#{sanitized_propery_name})::numeric)"))
.pluck(Arel.sql("DATE(#{date_field}) AS date, SUM((#{sanitized_property_name})::numeric)"))
.map do |row|
{ date: row.first.to_date, value: row.last }
end
Expand Down Expand Up @@ -268,7 +268,7 @@ def with_grouped_by_values(scope)
scope
end

def sanitized_propery_name(property = aggregation_property)
def sanitized_property_name(property = aggregation_property)
ActiveRecord::Base.sanitize_sql_for_conditions(
['events.properties->>?', property],
)
Expand All @@ -280,11 +280,11 @@ def presence_condition

def numeric_condition
# NOTE: ensure property value is a numeric value
"#{sanitized_propery_name} ~ '^-?\\d+(\\.\\d+)?$'"
"#{sanitized_property_name} ~ '^-?\\d+(\\.\\d+)?$'"
end

def sanitized_grouped_by
grouped_by.map { sanitized_propery_name(_1) }
grouped_by.map { sanitized_property_name(_1) }
end

# NOTE: Compute pro-rata of the duration in days between the datetimes over the duration of the billing period
Expand Down

0 comments on commit 9fd29cd

Please sign in to comment.