-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
113 lines (102 loc) · 4.59 KB
/
Program.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using CogniteSdk;
using dotenv.net;
using Microsoft.Identity.Client;
namespace minimal_dotnet_sdk_example
{
public static class Program
{
private static async Task FetchData()
{
// read authentication variables from a .env file
DotEnv.Load();
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var cluster = Environment.GetEnvironmentVariable("CDF_CLUSTER");
var project = Environment.GetEnvironmentVariable("CDF_PROJECT");
// fetch a valid token from the Azure Active directory
var scopes = new List<string> {$"https://{cluster}.cognitedata.com/.default"};
var app = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.WithRedirectUri("http://localhost")
.Build();
var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
var accessToken = result?.AccessToken;
// instantiate a Cognite SDK client
var httpClient = new HttpClient();
var client = Client.Builder.Create(httpClient)
.SetAppId("testNotebook")
.AddHeader("Authorization", $"Bearer {accessToken}")
.SetProject(project)
.SetBaseUrl(new Uri($"https://{cluster}.cognitedata.com"))
.Build();
// find some time series with unit "barg" in the project we have authenticated to
var resTs = await client.TimeSeries.ListAsync(
new TimeSeriesQuery
{
Filter = new TimeSeriesFilter {Unit = "barg"},
Limit = 2
}
);
var tsList = resTs.Items;
var tsExternalId = tsList.Last().ExternalId;
// check the timestamp for the latest datapoint for the selected time series
var resLatestDps = await client.DataPoints.LatestAsync(new DataPointsLatestQuery
{
Items = new List<IdentityWithBefore> {new IdentityWithBefore(externalId: tsExternalId, before: "now")}
});
var latestTimeStamp = resLatestDps.First().DataPoints?.First().Timestamp;
if (latestTimeStamp != null)
{
var latestTimeStampString = DateTimeOffset.FromUnixTimeMilliseconds(latestTimeStamp.Value).DateTime;
Console.WriteLine(
$"Latest data point found at {latestTimeStampString} for time series {tsExternalId}\n"
);
}
// fetch data points from the past 7 days from one of the time series listed above
var resDps = await client.DataPoints.ListAsync(new DataPointsQuery
{
Start = "7d-ago",
End = "now",
Items = new List<DataPointsQueryItem>
{
new DataPointsQueryItem
{
ExternalId = tsExternalId,
Aggregates = new List<string> {"average"},
Granularity = "1d",
Limit = 10_000
}
}
});
var ts = resDps.Items[0];
// get only the timestamp and the desired aggregate from the response
// convert the timestamp from ms since epoch to DateTime
var dps = ts.AggregateDatapoints?.Datapoints?.Select(
dp => new
{
DateTimeOffset.FromUnixTimeMilliseconds(dp.Timestamp).DateTime,
dp.Average
});
if (dps == null)
{
throw new Exception("No data points were located in the selected time window.");
}
// print each datapoint in a new line
Console.WriteLine($"Data fetched from time series {tsExternalId}:");
foreach (var dp in dps)
Console.WriteLine(dp);
// save the data points to a csv
using (var writer = new StreamWriter("output.csv"))
{
writer.WriteLine("DateTime,Average");
foreach (var dp in dps)
writer.WriteLine($"{dp.DateTime},{dp.Average}");
}
}
private static async Task Main()
{
await FetchData();
}
}
}