using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Unity.Cloud.UserReporting
{
///
/// Represents a user report.
///
public class UserReport : UserReportPreview
{
#region Nested Types
///
/// Provides sorting for metrics.
///
private class UserReportMetricSorter : IComparer
{
#region Methods
///
public int Compare(UserReportMetric x, UserReportMetric y)
{
return string.Compare(x.Name, y.Name, StringComparison.Ordinal);
}
#endregion
}
#endregion
#region Constructors
///
/// Creates a new instance of the class.
///
public UserReport()
{
this.AggregateMetrics = new List();
this.Attachments = new List();
this.ClientMetrics = new List();
this.DeviceMetadata = new List();
this.Events = new List();
this.Fields = new List();
this.Measures = new List();
this.Screenshots = new List();
}
#endregion
#region Properties
///
/// Gets or sets the attachments.
///
public List Attachments { get; set; }
///
/// Gets or sets the client metrics.
///
public List ClientMetrics { get; set; }
///
/// Gets or sets the device metadata.
///
public List DeviceMetadata { get; set; }
///
/// Gets or sets the events.
///
public List Events { get; set; }
///
/// Gets or sets the fields.
///
public List Fields { get; set; }
///
/// Gets or sets the measures.
///
public List Measures { get; set; }
///
/// Gets or sets the screenshots.
///
public List Screenshots { get; set; }
#endregion
#region Methods
///
/// Clones the user report.
///
/// The cloned user report.
public UserReport Clone()
{
UserReport userReport = new UserReport();
userReport.AggregateMetrics = this.AggregateMetrics != null ? this.AggregateMetrics.ToList() : null;
userReport.Attachments = this.Attachments != null ? this.Attachments.ToList() : null;
userReport.ClientMetrics = this.ClientMetrics != null ? this.ClientMetrics.ToList() : null;
userReport.ContentLength = this.ContentLength;
userReport.DeviceMetadata = this.DeviceMetadata != null ? this.DeviceMetadata.ToList() : null;
userReport.Dimensions = this.Dimensions.ToList();
userReport.Events = this.Events != null ? this.Events.ToList() : null;
userReport.ExpiresOn = this.ExpiresOn;
userReport.Fields = this.Fields != null ? this.Fields.ToList() : null;
userReport.Identifier = this.Identifier;
userReport.IPAddress = this.IPAddress;
userReport.Measures = this.Measures != null ? this.Measures.ToList() : null;
userReport.ProjectIdentifier = this.ProjectIdentifier;
userReport.ReceivedOn = this.ReceivedOn;
userReport.Screenshots = this.Screenshots != null ? this.Screenshots.ToList() : null;
userReport.Summary = this.Summary;
userReport.Thumbnail = this.Thumbnail;
return userReport;
}
///
/// Completes the user report. This is called by the client and only needs to be called when constructing a user report manually.
///
public void Complete()
{
// Thumbnail
if (this.Screenshots.Count > 0)
{
this.Thumbnail = this.Screenshots[this.Screenshots.Count - 1];
}
// Aggregate Metrics
Dictionary aggregateMetrics = new Dictionary();
foreach (UserReportMeasure measure in this.Measures)
{
foreach (UserReportMetric metric in measure.Metrics)
{
if (!aggregateMetrics.ContainsKey(metric.Name))
{
UserReportMetric userReportMetric = new UserReportMetric();
userReportMetric.Name = metric.Name;
aggregateMetrics.Add(metric.Name, userReportMetric);
}
UserReportMetric aggregateMetric = aggregateMetrics[metric.Name];
aggregateMetric.Sample(metric.Average);
aggregateMetrics[metric.Name] = aggregateMetric;
}
}
if (this.AggregateMetrics == null)
{
this.AggregateMetrics = new List();
}
foreach (KeyValuePair kvp in aggregateMetrics)
{
this.AggregateMetrics.Add(kvp.Value);
}
this.AggregateMetrics.Sort(new UserReportMetricSorter());
}
///
/// Fixes the user report by replace null lists with empty lists.
///
public void Fix()
{
this.AggregateMetrics = this.AggregateMetrics ?? new List();
this.Attachments = this.Attachments ?? new List();
this.ClientMetrics = this.ClientMetrics ?? new List();
this.DeviceMetadata = this.DeviceMetadata ?? new List();
this.Dimensions = this.Dimensions ?? new List();
this.Events = this.Events ?? new List();
this.Fields = this.Fields ?? new List();
this.Measures = this.Measures ?? new List();
this.Screenshots = this.Screenshots ?? new List();
}
///
/// Gets the dimension string for the dimensions associated with this user report.
///
///
public string GetDimensionsString()
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < this.Dimensions.Count; i++)
{
UserReportNamedValue dimension = this.Dimensions[i];
stringBuilder.Append(dimension.Name);
stringBuilder.Append(": ");
stringBuilder.Append(dimension.Value);
if (i != this.Dimensions.Count - 1)
{
stringBuilder.Append(", ");
}
}
return stringBuilder.ToString();
}
///
/// Removes screenshots above a certain size from the user report.
///
/// The maximum width.
/// The maximum height.
/// The total bytes allowed by screenshots.
/// The number of screenshots to ignoreCount.
public void RemoveScreenshots(int maximumWidth, int maximumHeight, int totalBytes, int ignoreCount)
{
int byteCount = 0;
for (int i = this.Screenshots.Count; i > 0; i--)
{
if (i < ignoreCount)
{
continue;
}
UserReportScreenshot screenshot = this.Screenshots[i];
byteCount += screenshot.DataBase64.Length;
if (byteCount > totalBytes)
{
break;
}
if (screenshot.Width > maximumWidth || screenshot.Height > maximumHeight)
{
this.Screenshots.RemoveAt(i);
}
}
}
///
/// Casts the user report to a user report preview.
///
/// The user report preview.
public UserReportPreview ToPreview()
{
UserReportPreview userReportPreview = new UserReportPreview();
userReportPreview.AggregateMetrics = this.AggregateMetrics != null ? this.AggregateMetrics.ToList() : null;
userReportPreview.ContentLength = this.ContentLength;
userReportPreview.Dimensions = this.Dimensions != null ? this.Dimensions.ToList() : null;
userReportPreview.ExpiresOn = this.ExpiresOn;
userReportPreview.Identifier = this.Identifier;
userReportPreview.IPAddress = this.IPAddress;
userReportPreview.ProjectIdentifier = this.ProjectIdentifier;
userReportPreview.ReceivedOn = this.ReceivedOn;
userReportPreview.Summary = this.Summary;
userReportPreview.Thumbnail = this.Thumbnail;
return userReportPreview;
}
#endregion
}
}