Business events end-to-end use case

This use case shows how your data can be captured, how a processing rule is set, and how this data can be further analyzed and visualized.

Scenario

EasyTrade is a simulated trading application with the following use cases:

  • Money deposits and withdrawals
  • Buy-assets and sell-assets transactions
  • Basic dashboards for your trading activities

In this scenario, we suppose you need to calculate the total value of deposits placed on each account, the total and maximum dollar trading volume per account on EasyTrade, and the total value of buy-asset transactions per account.

Before you begin

You need to determine:

  • The processing logic and rules
  • The data fields needed

Because you need to calculate the dollar trading volume, your processing rule should multiply the price by the amount and add the result as a new field to the event:

Dollar Trading Volume = Price * Amount

You also need to define the data items of the buy-asset request. For example:

{
"accountId":6,
"amount":10,
"instrumentId":1,
"price":157.025
}

Create a capture rule

  1. Go to Settings > Business Analytics > Business Event Sources > OneAgent.

  2. Select Add new capture rule and set Rule name to:

    easyTrade- /broker/api/trade/BuyAssets

  3. Select Add trigger and, in the Summary section, set the following:

    • Data source: select Request - Path
    • Operator: select starts with
    • Value: enter or paste /broker/api/trade/BuyAssets
  4. Define the Event provider:

    • Data source: Fixed value
    • Fixed value: www.easytrade.com
  5. Define the Event type:

    • Data source: Fixed value
    • Fixed value: com.easytrade.buy-assets

      If you need to add more event types, select Event type separately for each event type. In this case, the event types will also be com.easytrade.sell-assets and com.easytrade.deposit-money.

  6. Define the Event category:

    • Data source: Fixed value
    • Fixed value: optional; you can leave it blank in this case
  7. In the Event data section, you need to add four data fields.

    For each data field:

    1. Select Add data field.

    2. Set the Field name, Source, and Path values according to the following table:

      Field name

      Source

      Path

      accountId

      Request-Body

      accountId

      amount

      Request-Body

      amount

      instrumentId

      Request-Body

      instrumentID

      price

      Request-Body

      price

  8. Select Save changes.

Create a data processing rule

To create your data processing rule

  1. Go to Settings > Business Analytics > Ingest Pipeline > Processing.

  2. Select Add rule and name your rule as EasyTrade add sum.

  3. Set your rule's Matcher to the following matcher-specific DQL query:

    matchesValue(event.provider, "www.easytrade.com")

    • If you needed to add only one event type (for example, com.easytrade.buy-assets), the matcher would be:
      matchesValue(event.type, "com.easytrade.buy-assets")
    • For two event types within the same event provider, the matcher would be:
      matchesValue(event.type, "com.easytrade.buy-assets") or matchesValue(event.type, "com.easytrade.sell-assets")
    • In this use case, however, you need to take all event types under the EasyTrade event provider, so it is sufficient to use:
      matchesValue(event.provider, "www.easytrade.com")
  4. In the Transformation fields section, add price and amount.

    1. Select Add item, set Type to double, and set Name to price
    2. Select Add item again, set Type to double, and set Name to amount
  5. Set Processor definition to FIELDS_ADD(trading_volume:price*amount)

  6. Select Save changes.

Add business event metric

To add your business event metric

  1. Go to Settings > Business Analytics > Metric extraction.

  2. Select Add business event metric and name your metric by adding a Key as bizevents.EasyTrade.TradingVolume

  3. Add a Matcher to your rule by pasting your matcher-specific DQL query:

    matchesValue(event.type, "com.easyTrade.buy-assets")
  4. Choose the Measure on which your metric will be based. In the Attribute value field, add trading_volume.

  5. Select Add dimension. For example, you can define a host as Host-1.

  6. Select Save changes.

To display your metrics

  1. Go to Data Explorer.

  2. Find your metric in the search window, select Run query, and display your results.

    You can also:

    • Pin the metric to a classic dashboard. For details, see Pin tiles to your dashboard.
    • Export your data to a CSV file.
    • Share a link.
    • Copy the request.

To create an alert with your metric

  1. Go to Settings > Anomaly detection > Metric events.
  2. Select Add metric event and create an event based on your Key, bizevents.EasyTrade.TradingVolume.

Choose the retention period

If you need to store your data for one year, for example, for tax purposes

  1. Go to Settings > Business Analytics > Ingest Pipeline > Bucket assignment.

  2. Select Add rule.

  3. Set Rule name to Bucket_rule.

  4. Set Bucket to Business events (1 year).

  5. Set your rule's Matcher to the following matcher-specific DQL query:

    matchesValue(event.provider, "www.easytrade.com")

    • If you needed to add only one event type (for example, com.easytrade.buy-assets), the matcher would be:

      matchesValue(event.type, "com.easytrade.buy-assets")
    • For two event types within the same event provider, the matcher would be:

      matchesValue(event.type, "com.easytrade.buy-assets") or matchesValue(event.type, "com.easytrade.sell-assets")
    • In this use case, however, you need to take all event types under the EasyTrade event provider, so it is sufficient just to use:

      matchesValue(event.provider, "www.easytrade.com")
  6. Select Save changes.

Test your results

To see if your data was captured correctly

  1. Go to Explore Business Events.

  2. Copy and paste the following query into the edit box:

    fetch bizevents
    | filter event.provider == "www.easytrade.com"
    | filter event.type == "com.easytrade.buy-assets" OR event.type == "com.easytrade.deposit-money" OR event.type == "com.easytrade.sell-assets"
    | sort timestamp, direction:"descending"
  3. Select Run query.

You should get results something like the following:

results of the DQL test query

Analyze your data with DQL

At this point, you can start building your DQL queries to analyze the data loaded, define metrics, and create charts and dashboards.

You need to calculate:

  • The total value of deposits per account in the last 30 days
  • The total value of buy-asset transactions per account in the last 30 days
  • The total and maximum dollar trading volume per account in the last 30 days

To analyze your data using DQL

  1. Go to Explore Business Events.

  2. Build and run your query:

    fetch bizevents, from:now()-30d, to:now()
    | filter event.provider == "www.easytrade.com" and (event.type == "com.easytrade.buy-assets" OR event.type == "com.easytrade.deposit-money" OR event.type == "com.easytrade.sell-assets")
    | fieldsAdd moneyTransfered = if(event.type == "com.easytrade.deposit-money", toDouble(amount)),
    buyAssets = if(event.type == "com.easytrade.buy-assets", toDouble(amount)),
    trading_volume= if(event.type == "com.easytrade.sell-assets",(amount*price))
    | summarize sum(moneyTransfered), sum(buyAssets), sum(trading_volume), max(trading_volume), by:accountId

Results table:

accountid

max(trading_volume)

sum(buyAssets)

sum(moneyTransfered)

sum(trading_volume)

6

19924.3187495

609491

1340160.809059129

526623.1340707905

7

47827.65870614

593648

1287203.0690401047

922284.0555854205

8

16718.08125

615123

1288892.9309932007

550042.8770441795

Display your results

Bar chart

Bar chart illustrating sum, max volume in business nalytics

Line chart

Line chart illustrating sum, trading volume in business analytics

Dashboard tile

To pin your results to a dashboard, select Actions > Pin to dashboard. You can then select an existing dashboard or create a new one, and name the tile you want to display.