Chia.NetStandard

This library Chia contains utils for internal Danpower Reporting and is used by serveral reports.


Keywords
Shared, Utils, Danpower
License
Apache-2.0
Install
Install-Package Chia.NetStandard -Version 0.6.6

Documentation

Chia - HelperFunctions for reporting

Build Status

Available Packages:

Library Version
Chia nuget - Chia
Chia.Client nuget - ChiaClient

Chia contains HelperFunctions for reporting. Chia contains some Azure Storage functions, logging features and some excel utils.

FileWriter

Initialize your FileWriter instant with initFileWriter. If you want to log to ApplicationInsight you have to create a new Application Insight resource in Azure and set your ApplicationInsights key.

open Chia
open Shared.Logging
open Shared.Config
open InitBuilder
let devStatus = getDevStatusFromEnv  /// Get your devStatus from you environment variable. For example pass in an environment variable in Fake --> '-e devStatus=Productive
let fileWriterConfig =
    initWriter {
        devStatus Development
        companyInitials "dp"
        projectName "TestChia"
        devOption (Azure "aiKey")

Infrastructure

Chia now can help you to set up your Azure infrastructure by using Chia on top of Farmer. You simply create a new FileWriter and then Chia will create a fresh Azure storageaccount in your preferred location. Chia will connect to your storage account and you don't need to add you storage account at all. Here is an example:

open Chia
open Infrastructure
open InitWriter
open Shared.Config
open Shared.Logging
open Farmer
let devStatus = Development
let fileWriterConfig =
    initWriter {
        devStatus Development
        companyInitials "dp"
        projectName "TestChia"
        devOption (Azure "aiKey")
let azAccount = azConnection fileWriterConfig Location.WestEurope

Know issues

Currently Chia doesn't support F# projects which are using TypeProviders. There gonna cause issues with Farmer.

Log

Once you set configured your initbuilder you can now log to Application Insight or just to a local file

There are three many log functions. logStarting, logFinished and logCritical.

The log function is using categories for clustering events in ApplicationInsights. This will help you to get the most out of the ApplicationInsight dashboard and LogAnalytics.

If you want to log a information that a process is starting you can use logStarting like this:

Log.logStarting("Starting to get Data",LocalServer,Get,AzureTable,fileWriterConfig)

If a process finished as expected use logFinished:

Log.logFinished("Finished receiving Data",LocalServer,Get,AzureTable,fileWriterConfig)

If a process crashed unexpected use can track the error message with logCritical:

try
    let trySomething = unsafe ()
with
| exn ->
    let msg = sprintf  "Your error message: %s" exn.Message
    Log.logCritical (msg,LocalService,LocalServer,Get,AzureTable,exn,fileWriterConfig)
    failwith msg

The logger uses following clusters.

Log.logStarting("UsefullMessage",Source,Operation,Destination,filewriterInfo)

Here are the implemented descriminated unions:

type Source =
| LocalService
| LocalServer
| AzureFunction
| AzureInfrastructure
| PiServer
| Client
| SPSCommunication

type Operation =
| Upload
| Download
| Insert
| Query
| Create
| Delete
| Calculation
| Post
| Get
| Restart
| Stop

type Destination =
| AzureTable
| QueueTable
| BlobTable
| SqlTable
| LocalStorage
| EventHub

CreateTable

Helper to create a Azure table:

First connect to your storage account:

open Chia.CreateTable
let connected =
    let connection = AzureConnection StorageAccount.storageConnString
    connection.Connect()

Now you can create your Azure table like this:

let azureTable = getTable "TableName" fileWriterConfigAzure connected

CreateBlob

Helper to create a Azure blobs:

First create your blob container:

open Chia.CreateBlob
let containerInfo = {   StorageConnString = StorageConnString = StorageAccount.storageConnString
                        ContainerName = "ContainerName"}
let myContainer = getContainer containerInfo

Now you can get a list of all you blobs in the container like this:

let blobItems = getBlobs myContainer

GetTableEntry

Helper to query a Azure tables:

With the GetTableEntry module you can easily query Azure Tables.

Get just on single tableValue like this:

let! value = getValue (request.PartKey, request.RowKey) azureTable

Get all values in a table by using a table mapper. First you have to define you mapper:

type Mapper = {
    PartKey : string
    RowKey : Ids.SortableRowKey
}

let mapper (entity : DynamicTableEntity) : Mapper =
    { PartyKey = entity.PartitionKey
      RowKey = SortableRowKey entity.RowKey

let! values = getValues mapper azureTable

You can also get all values by one rowKey like this:

let valuesByRowKey = getValuesByRowKey rowKey mapper azureTable

If you want to create more complex queries you can just parse in a TableQuery Filter.

First define your filter:

let filter partKey = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partKey)
let filteredValues partKey = getValuesWithFilter (filter partKey) azureTable

PostToQueue

You can use Chia to sent out a AzureQueue message like this:

open Chia.PostToQueue
open Chia.CreateTable


let connected =
    let connection = AzureConnection StorageAccount.storageConnString
    connection.Connect()

[<Literal>]
let SendMail = "sendmail-queue"

let sendQueue = getQueue connected SendMail fileWriterConfig

RedisCache

Helper to create or directly query a RedisCache:

To create or read a Redis values with a Redis Key you first have to create a Redis cache info:

let cacheInfo : RedisCache = {
    Cache = Redis.cache
    Key = key
    FileWriterInfo = fileWriterConfig }

To deserialze your Redis values to your pass in a System.Text.Json mapper. You also should pass in a task to receive your data. The function tries to find the cache in Redis. If there is no Redis cache it will create a new cache by executing you task. The following example shows how to receive a Plant array directly out of Redis or creates a new cache if theres no existing cache and returns the Plant array.

let! plants = tryGetCachedData JsonSerializer.Parse<Plant[]> cacheInfo getPlants

EventHub

You can use Chia to sent out a event to Azure Event Hubs like this:

open Chia.EventHubs

let eventHubClient = getEventHubClient "EventHubSASConnectionString"

type Data = int

let data = 100

do! pushEvent (eventHubClient,data,fileWriterConfigAzure)
do! pushSingleEvent (eventHubClient,data,fileWriterConfigAzure)

ExcelUtils

Mini Helper to start and ExcelApp using the EPPlus package:

Start your excel app like this:

let excelPackage = startExcelApp ()

Example for a bigger TableMapper

If you want to map your data from Azure to your specific domain types you could do it like this.

open Chia.CreateTable

type Address =
    { LocationId : Ids.LocationId
      PostalCode : string
      StreetNr : int option
      Additions : string option
      Street : string option
      Location : string option
      Country : string option
      FedState : string option }

let mapAddress (entity : DynamicTableEntity) : Address =
    { LocationId = LocationId (entity.PartitionKey |> int64)
      PostalCode = entity.RowKey
      StreetNr = getOptionalIntProperty "StreetNr" entity
      Street = getOptionalStringProperty "Street" entity
      Additions = getOptionalStringProperty "Additions" entity
      Location = getOptionalStringProperty "Location" entity
      Country = getOptionalStringProperty "Country" entity
      FedState = getOptionalStringProperty "FedState" entity }