-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(SqlExpression) - Fix batch endpoint (#2921)
## Description * bump Lago-expression gem. * Create new service Events::CalculateExpressionService. * Use new service in both Batch and regular event creation.
- Loading branch information
Showing
7 changed files
with
93 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Events | ||
class CalculateExpressionService < BaseService | ||
def initialize(organization:, event:) | ||
@organization = organization | ||
@event = event | ||
super | ||
end | ||
|
||
def call | ||
result.event = event | ||
|
||
field_name, expression = BillableMetrics::ExpressionCacheService.call(organization.id, event.code) do | ||
bm = organization.billable_metrics.with_expression.find_by(code: event.code) | ||
[bm&.field_name, bm&.expression] | ||
end | ||
return result if expression.blank? | ||
|
||
evaluation_event = Lago::Event.new(event.code, event.timestamp.to_i, event.properties) | ||
|
||
# The expression can always be parsed, otherwise it would not be saved. | ||
value = Lago::ExpressionParser.parse(expression).evaluate(evaluation_event) | ||
event.properties[field_name] = value | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :event | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Events::CalculateExpressionService, type: :service do | ||
describe '#call' do | ||
subject(:service_call) { described_class.new(organization: organization, event: event).call } | ||
|
||
let(:organization) { create(:organization) } | ||
let(:event) { create(:event, organization: organization, timestamp: Time.current.to_i, code: code, properties: properties) } | ||
let(:code) { 'test_code' } | ||
let(:properties) { {'left' => '1', 'right' => '2'} } | ||
let(:expression) { nil } | ||
let(:field_name) { 'result' } | ||
let(:billable_metric) { create(:billable_metric, organization: organization, code: code, field_name: field_name, expression: expression) } | ||
|
||
before do | ||
billable_metric | ||
end | ||
|
||
context 'when there is no expression configured' do | ||
it 'does not modify the event properties' do | ||
expect(service_call).to be_success | ||
expect(service_call.event.properties).to eq(properties) | ||
end | ||
end | ||
|
||
context 'when an expression is configured for the billable metric' do | ||
let(:expression) { 'event.properties.left + event.properties.right' } | ||
|
||
it 'evaluates the expression and updates the event properties' do | ||
expect(service_call).to be_success | ||
expect(service_call.event.properties[field_name]).to eq(3) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters