private void CreateTimedCountItems(long maxCount) { _heightMultiplier = BarMaxHeight / maxCount; string timeLineFormat = GroupTimeRange.TimeLineFormat(); var timedCountItemList = new List <TimedCountItem>(); int k = 0; foreach (var counter in TimedCountList) { // Time line is only shown on items of first, last-3, and optional other 2 items. bool isTimeLineVisible = (k == 0 || k == TimedCountList.Count - 3 || k == TimedCountList.Count / 3 || k == TimedCountList.Count * 2 / 3); DateTime startTime = (DateTime)counter.StartTime; string timeLine = isTimeLineVisible ? startTime.ToString(timeLineFormat) : null; timedCountItemList.Add( new TimedCountItem( counter, timeLine, _heightMultiplier, GroupTimeRange.TimeCountDuration())); ++k; } _timedCountItemsControl.ItemsSource = timedCountItemList; }
private static string GetDateFormat(GroupTimeRange timeRange) { return(timeRange switch { GroupTimeRange.Day => "%Y-%m-%d", GroupTimeRange.Week => "%V/%Y", GroupTimeRange.Month => "%Y-%m", GroupTimeRange.Year => "%Y", _ => throw new NotSupportedException($"{timeRange} is not supported."), });
/// <summary>Serves as the default hash function. </summary> /// <returns>A hash code for the current object.</returns> public override int GetHashCode() { int hashCode = Caption.GetHashCode(); hashCode *= 31; hashCode += TimedCountDuration.GetHashCode(); hashCode *= 31; hashCode += EventTimeRange.GetHashCode(); hashCode *= 31; hashCode += GroupTimeRange.GetHashCode(); return(hashCode); }
public static BsonDocument DateToString(string path, GroupTimeRange timeRange) { string dateFormat = GetDateFormat(timeRange); return(new BsonDocument { { "$dateToString", new BsonDocument { { "format", dateFormat }, { "date", $"${path}" }, { "timezone", TimeZone } } } }); }
/// <summary>Determines whether the specified object is equal to the current object.</summary> /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns> /// <param name="obj">The object to compare with the current object. </param> public override bool Equals(object obj) { if (obj == this) { return(true); } else if (!(obj is TimeRangeItem)) { return(false); } else { var other = (TimeRangeItem)obj; return(Caption.Equals(other.Caption) && TimedCountDuration.Equals(other.TimedCountDuration) && EventTimeRange.Equals(other.EventTimeRange) && GroupTimeRange.Equals(other.GroupTimeRange)); } }
public static BsonDocument GroupByDate(string path, GroupTimeRange timeRange, params BsonElement[] props) { string dateFormat = GetDateFormat(timeRange); var doc = new BsonDocument { { "_id", new BsonDocument { { "$dateToString", new BsonDocument { { "format", dateFormat }, { "date", path }, { "timezone", TimeZone } } } } } }; doc.AddRange(props); return(doc); }
public async Task <List <GroupResult> > AggregateCountAsync(string author, GroupTimeRange groupTimeRange, FilterSettings filterSettings) { /* * { "$match" : { "Author" : "test" } }, * { "$group" : { "_id" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "$Created", "timezone" : "Europe/Berlin" } }, "count" : { "$sum" : 1 } } }, * { "$project" : { "_id" : 0, "Key" : "$_id", "Value" : "$count" } }, * { "$sort" : { "Key" : 1 } } */ var projectStage = new BsonDocument { { "_id", 0 }, { "Key", "$_id" }, { "Value", "$count" } }; var watch = Stopwatch.StartNew(); var aggregateResult = this.documentCollection.Aggregate() .Match(this.BuildMatchStage(author, filterSettings)) .Group(MongoOp.GroupByDate("$Created", groupTimeRange, MongoOp.Count("count"))) .Project(projectStage) .Sort(MongoOp.SortBy("Key", asc: true)); var bsonResult = await aggregateResult.ToListAsync(); var result = bsonResult.Select(doc => BsonSerializer.Deserialize <GroupResult>(doc)) .ToList(); watch.Stop(); if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug("Explore took {ms}ms.", watch.ElapsedMilliseconds); } return(result); }
public async Task <List <ValuesResult> > AggregateValuesAsync(string author, GroupTimeRange groupTimeRange, Aggregate aggregation, FilterSettings filterSettings) { /* * db.getCollection('documents').aggregate([ * { "$match" : { "Author" : "test" } }, * { "$project" : { "date" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "$Created", "timezone" : "Europe/Berlin" } }, "values" : { "$objectToArray" : "$Values" } } }, * { "$unwind" : "$values" }, * { "$group" : { "_id" : { "date" : "$date", "key" : "$values.k" }, "value" : { "$sum" : "$values.v" } } }, * { "$group" : { "_id" : "$_id.key", "values" : { "$push" : { "$arrayToObject" : [[{ "k" : "$_id.date", "v" : "$value" }]] } } } }, * { "$project": { "key": "$_id", "values": 1, "_id": 0 } } * ]) * */ var projectStage = new BsonDocument { { "date", MongoOp.DateToString("Created", groupTimeRange) }, { "values", new BsonDocument { { "$objectToArray", "$Values" } } } }; var aggregationOp = aggregation switch { Aggregate.Sum => "$sum", Aggregate.Average => "$avg", _ => throw new InvalidOperationException($"{aggregation} not supported") }; var groupByDateAndKey = new BsonDocument { { "_id", new BsonDocument { { "date", "$date" }, { "key", "$values.k" } } }, { "value", new BsonDocument { { aggregationOp, "$values.v" } } } }; var groupByDateOnly = new BsonDocument { { "_id", "$_id.key" }, { "Values", new BsonDocument { { "$push", new BsonDocument { { "$arrayToObject", new BsonArray { new BsonArray { new BsonDocument { { "k", "$_id.date" }, { "v", "$value" } } } } } } } } } }; var jssson = groupByDateOnly.ToJson(); var endProjectStage = new BsonDocument { { "Key", "$_id" }, { "Values", 1 }, { "_id", 0 }, }; var watch = Stopwatch.StartNew(); var aggregateResult = this.documentCollection.Aggregate() .Match(this.BuildMatchStage(author, filterSettings)) .Project(projectStage) .Unwind("values") .Group(groupByDateAndKey) .Group(groupByDateOnly) .Project(endProjectStage); var bsonResult = await aggregateResult.ToListAsync(); var result = bsonResult.Select(doc => BsonSerializer.Deserialize <ValuesResult>(doc)) .ToList(); watch.Stop(); if (this.logger.IsEnabled(LogLevel.Debug)) { this.logger.LogDebug("Explore took {ms}ms.", watch.ElapsedMilliseconds); } return(result); }