This is the official documentation of the agent_ruby Ruby agent.
Value Charts display a single numerical value.
They can be added to a Dashboard using the addChart method on the agent object
include ForestAdminDatasourceToolkit::Components::Query
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
@create_agent.add_chart('monthlyRecuringRevenue') do |context, result_builder|
aggregation = Aggregation.new(operation: 'Sum', field: 'amount')
filter = Filter.new(condition_tree: Nodes::ConditionTreeLeaf.new('status', Operators::EQUAL, 'paid'))
result = context.datasource.get_collection('payment').aggregate(filter, aggregation)
result_builder.value(result[0]['value'])
end
Or to the "Analytics" Tab of a Collection using the addChart method on the collection object
include ForestAdminDatasourceToolkit::Components::Query
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
@create_agent.customize_collection('customer') do |collection|
collection.add_chart('monthlyRecuringRevenue') do |context, result_builder|
aggregation = Aggregation.new(operation: 'Count', field: 'id')
filter = Filter.new(
condition_tree: Nodes::ConditionTreeBranch.new(
'And',
[
Nodes::ConditionTreeLeaf.new('status', Operators::EQUAL, 'paid'),
Nodes::ConditionTreeLeaf.new('customer:id', Operators::EQUAL, context.get_record_id)
]
)
)
result = context.datasource.get_collection('payment').aggregate(filter, aggregation)
result_builder.value(result[0]['value'])
end
end
Optionally, an older value can be provided to the resultBuilder to display a growth percentage on the top right of the widget as in the following Chart display:
@create_agent.add_chart('appointments') do |_context, result_builder|
result_builder.value(784, 760)
end