-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportingApi.cs
115 lines (102 loc) · 4.89 KB
/
ReportingApi.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
114
115
using appsvc_function_dev_cm_stats_dotnet001.Configuration;
using Google.Analytics.Data.V1Beta;
using Google.Protobuf.Collections;
//using Grpc.Core.Logging;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace appsvc_function_dev_cm_stats_dotnet001
{
public class ReportingApi
{
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly BetaAnalyticsDataClient analyticsDataClient;
/// <summary>
/// Intializes and returns Analytics Reporting Service Instance using the parameters stored in key file
/// </summary>
/// <returns>AnalyticsReportingService</returns>
private BetaAnalyticsDataClient GetAnalyticsClient()
{
return new BetaAnalyticsDataClientBuilder
{
CredentialsPath = ConfigurationManager.AppSettings["KeyFileName"]
}.Build();
}
public ReportingApi()
{
this.analyticsDataClient = GetAnalyticsClient();
}
/// <summary>
/// Create date range based on entries in configuration
/// </summary>
/// <param name="config"></param>
/// <returns>DateRange</returns>
private DateRange GetDateRangeFromConfiguration(ReportConfiguration config)
{
var strStartDateFromConfig = config.DateConfiguration.StartDate;
var strEndDateFromConfig = config.DateConfiguration.EndDate;
var strNumberOfDaysFromConfig = config.DateConfiguration.NumberOfDays;
DateTime.TryParse(strStartDateFromConfig, out DateTime reportStartDate);
DateTime.TryParse(strEndDateFromConfig, out DateTime reportEndDate);
int.TryParse(strNumberOfDaysFromConfig, out int numberOfDays);
//Set start and end date for report using number of days
var startDate = DateTime.Now.AddDays(-numberOfDays);
var endDate = numberOfDays == 0 ? DateTime.Now : DateTime.Now.AddDays(-1);
//Use start and end date from config if specified else keep the existing values
if (reportStartDate != DateTime.MinValue && reportEndDate != DateTime.MinValue &&
reportStartDate <= reportEndDate)
{
startDate = reportStartDate;
endDate = reportEndDate;
}
return new DateRange
{
StartDate = startDate.ToString("yyyy-MM-dd"),
EndDate = endDate.ToString("yyyy-MM-dd")
};
}
/// <summary>
/// Get all reports configured in App.config
/// </summary>
/// <returns></returns>
public async Task GenerateReport(string propertyId, ILogger logger)
{
var reportResponse = new RunReportResponse();
try
{
logger.LogInformation("Processing Property Id: " + propertyId);
var config = ReportConfiguration.GetConfig();
foreach (var item in config.Reports)
{
if (item is Report report)
{
var stopwatch = new Stopwatch();
logger.LogInformation("Started fetching report: " + report.Name);
// Create the Metrics and dimensions object based on configuration.
var metrics = new RepeatedField<Metric> { report.Metrics.Split(',').Select(m => new Metric { Name = m }) };
var dimensions = new RepeatedField<Dimension> { report.Dimensions.Split(',').Select(d => new Dimension { Name = d }) };
var reportRequest = new RunReportRequest
{
Property = "properties/" + propertyId,
DateRanges = { GetDateRangeFromConfiguration(config) },
Metrics = { metrics },
Dimensions = { dimensions }
};
stopwatch.Start();
reportResponse = analyticsDataClient.RunReport(reportRequest);
stopwatch.Stop();
logger.LogInformation("Finished fetching report: " + report.Name);
logger.LogInformation(string.Format("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed));
//new ReportingService().SaveReportToDisk(report.Name, propertyId, reportResponse, logger);
await new ReportingService().SaveReportToStorageContainerAsync(report.Name, propertyId, reportResponse, logger);
}
}
}
catch (Exception ex)
{
logger.LogError("Error in fetching reports: " + ex);
}
}
}
}