fonsecamar / cosmos-trading-demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cosmos DB NoSQL API - Trading Demo

Introduction

This repository provides a code sample in .NET on how to use some Azure Cosmos DB features integrated with Azure Funcions.

Requirements to deploy

Setup shell was tested on WSL2 (Ubuntu 22.04.2 LTS)

Setup environment

The setup will provision and configure all the resources required.

  • Sign in with Azure CLI

    az login
  • Clone the repo

    git clone https://github.com/fonsecamar/cosmos-trading-demo.git
    cd cosmos-trading-demo/deploy/
  • Run setup.sh with the appropriete parameters. Keep the API's URIs prompted when completed.

Provide a non-existent resource group name. Setup will provision.

```bash
#SAMPLE
#./setup.sh 00000000-0000-0000-0000-000000000000 rg-my-demo SouthCentralUS myrandomsuffix

./setup.sh <subscription id> <resource group> <location> <resources suffix>
```

Setup has some pause stages. Hit enter to continue when prompted.

It takes around 3min to provision and configure resoures.

Resources created:

  • Resource group
  • Azure Blob Storage (ADLS Gen2)
  • Azure Cosmos DB account (1 database with 1000 RUs autoscale shared with 5 collections) with Analytical Store enabled
  • Azure Event Hub standard
  • Azure Steam Analytics job
  • Azure Functions Consumption Plan
  • Azure Application Insights

Running the sample

You can call Function APIs from Azure Portal or your favorite tool.

  1. Start Azure Stream Analytics job

  2. Run markerdata generator

    cd ../src/marketdata-generator
    dotnet run
  3. Check Cosmos DB marketdata container (updated every 15 second by Azure Stream Analytics job).

  4. Call GetStockPrice function

    #Setting variables
    SUFFIX=<your suffix>
    
    # Returns Stock Price by symbol
    curl --request GET "https://functiondemo$SUFFIX.azurewebsites.net/api/stock/MSFT"
  5. Call CreateOrder function

    # Creates an Order
    curl --request POST "https://functiondemo$SUFFIX.azurewebsites.net/api/orders/create" \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "customerId": 99999999,
        "quantity": 1000,
        "symbol": "MSFT",
        "price": 300,
        "action": "buy"
    }'
  6. Call GetOrder function (use orderId from the previous response)

    # Returns Order by orderId
    curl --request GET "https://functiondemo$SUFFIX.azurewebsites.net/api/orders/{orderId}"
  7. Call GetExecutions function (use the same orderId)

    -- Returns Order Executions by orderId
    curl --request GET "https://functiondemo$SUFFIX.azurewebsites.net/api/orders/execution/{orderId}"
  8. Call GetCustomerPortfolio function (use customerId provided on step 1)

    -- Returns Customer Portfolio by customerId
    curl --request GET "https://functiondemo$SUFFIX.azurewebsites.net/api/customerPortfolio/{customerId}"

Clean Up

  1. CTRL + C to stop marketdata generator
  2. Delete the Resource Group to destroy all resources

How to Contribute

If you find any errors or have suggestions for changes, please be part of this project!

  1. Create your branch: git checkout -b my-new-feature
  2. Add your changes: git add .
  3. Commit your changes: git commit -m '<message>'
  4. Push your branch to Github: git push origin my-new-feature
  5. Create a new Pull Request 😄

About

License:MIT License


Languages

Language:C# 61.9%Language:Bicep 32.5%Language:Shell 5.6%