-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAzureTable.fs
More file actions
66 lines (52 loc) · 2.25 KB
/
AzureTable.fs
File metadata and controls
66 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// This modules contains helper functions to retrieve log events from Azure Tables
module Microsoft.FSharpLu.Azure.Table
open System
open Microsoft.FSharpLu.Async
open Microsoft.FSharpLu
open Microsoft.Azure.Cosmos
// Azure tables are totally Type Provider material since it is possible to query table schema
type AzureTableEntry = System.Collections.Generic.IDictionary<string,Table.EntityProperty>
type Table.EntityProperty with
member this.AsInt32Opt
with get() =
Option.ofNullable this.Int32Value
member this.AsInt64Opt
with get() =
Option.ofNullable this.Int64Value
member this.AsDateTimeOpt
with get() =
Option.ofNullable this.DateTime
member this.AsString
with get() =
this.StringValue
/// Retrieve all entries from Azure table where PartitionKey is derived from DateTime
let queryTableEntries (credentials: Table.StorageCredentials, uri: Table.StorageUri) (tableName: String, continuationToken: Table.TableContinuationToken option) (query: Table.TableQuery) =
async {
let tableClient = Table.CloudTableClient(uri, credentials)
let table = tableClient.GetTableReference(tableName)
let! exists = table.ExistsAsync().AsAsync
if exists then
let! xs =
let ct = Option.toObj continuationToken
table.ExecuteQuerySegmentedAsync(query, ct).AsAsync
let ct = Option.ofObj(xs.ContinuationToken)
return Some(ct, xs.Results)
else
return None
}
/// Retrieve entries from Azure table, and apply 'convert' function to every table entry
let getEvents
(name: string)
(convert: AzureTableEntry-> 'a)
(continutationToken, credentials: Table.StorageCredentials, uri: Table.StorageUri)
(query: Table.TableQuery) =
let convertTableEntries (xs: Table.DynamicTableEntity seq) (f: AzureTableEntry -> 'a) =
[
for x in xs do
if not <| obj.ReferenceEquals(null, x) then
yield f x.Properties
]
async{
let! entries = queryTableEntries (credentials, uri) (name, continutationToken) query
return entries |> Option.map (fun (ct,xs) -> ct, convertTableEntries xs convert)
}