Skip to content

Commit

Permalink
Merge pull request #23 from liuhaoyang/dev
Browse files Browse the repository at this point in the history
Fix InMemory-Storage Query
  • Loading branch information
liuhaoyang authored Feb 12, 2018
2 parents 1529468 + c62db83 commit 2271ea0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>0.0.6</VersionPrefix>
<VersionPrefix>0.0.7</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Version>$(VersionPrefix)$(VersionSuffix)</Version>
<AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Butterfly.Core/Storage/Query/TimeRangeQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public virtual void Ensure()
{
if (FinishTimestamp == null)
{
FinishTimestamp = DateTimeOffset.UtcNow.AddSeconds(-15);
FinishTimestamp = DateTimeOffset.UtcNow;
}
if (StartTimestamp == null)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Butterfly.Elasticsearch/ElasticsearchSpanQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ public async Task<IEnumerable<TraceHistogram>> GetTraceHistogram(TraceQuery trac

var timeSpan = traceQuery.FinishTimestamp.Value - traceQuery.StartTimestamp.Value;

var interval = timeSpan.TotalMinutes > 100 ? ((int)timeSpan.TotalMinutes / 100) + "m" : ((int)timeSpan.TotalSeconds / 100) + "s";

var histogramAggregationsResult = await _elasticClient.SearchAsync<Span>(s => s.Index(index).Size(0).Query(query).
Aggregations(a =>
a.DateHistogram("data_histogram_startTimestamp", d => d.Field(f => f.StartTimestamp).Interval(new Time(interval)).Format("yyyy-MM-dd HH:mm:ss").
a.DateHistogram("data_histogram_startTimestamp", d => d.Field(f => f.StartTimestamp).Interval(DateInterval.Minute).Format("yyyy-MM-dd HH:mm:ss").
Aggregations(sub => sub.Cardinality("cardinality_traceId", c => c.Field(f => f.TraceId))))));

var histogramAggregations = histogramAggregationsResult.Aggregations.FirstOrDefault().Value as BucketAggregate;
Expand Down
29 changes: 23 additions & 6 deletions src/Butterfly.EntityFrameworkCore/InMemorySpanQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public Task<IEnumerable<Trace>> GetTraces(TraceQuery traceQuery)

var queryGroup = query.ToList().GroupBy(x => x.TraceId).Take(traceQuery.Limit).ToList();

return Task.FromResult<IEnumerable<Trace>>(queryGroup.Select(x => new Trace() {TraceId = x.Key, Spans = _mapper.Map<List<Span>>(x.ToList())}).ToList());
return Task.FromResult<IEnumerable<Trace>>(queryGroup.Select(x => new Trace() { TraceId = x.Key, Spans = _mapper.Map<List<Span>>(x.ToList()) }).ToList());
}

public Task<IEnumerable<Span>> GetSpanDependencies(DependencyQuery dependencyQuery)
{
var query = _dbContext.Spans.AsNoTracking().Include(x => x.References).Include(x => x.Tags).AsQueryable();

if (dependencyQuery.StartTimestamp != null)
{
query = query.Where(x => x.StartTimestamp >= dependencyQuery.StartTimestamp);
Expand All @@ -102,7 +102,7 @@ private IEnumerable<Tag> BuildQueryTags(TraceQuery traceQuery)
{
if (!string.IsNullOrEmpty(traceQuery.ServiceName))
{
yield return new Tag {Key = QueryConstants.Service, Value = traceQuery.ServiceName};
yield return new Tag { Key = QueryConstants.Service, Value = traceQuery.ServiceName };
}

if (!string.IsNullOrEmpty(traceQuery.Tags))
Expand All @@ -113,15 +113,32 @@ private IEnumerable<Tag> BuildQueryTags(TraceQuery traceQuery)
var pair = tag.Split('=');
if (pair.Length == 2)
{
yield return new Tag {Key = pair[0], Value = pair[1]};
yield return new Tag { Key = pair[0], Value = pair[1] };
}
}
}
}

public Task<IEnumerable<TraceHistogram>> GetTraceHistogram(TraceQuery traceQuery)
public async Task<IEnumerable<TraceHistogram>> GetTraceHistogram(TraceQuery traceQuery)
{
throw new NotImplementedException();
traceQuery.Ensure();

var query = _dbContext.Spans.AsQueryable();

if (traceQuery.StartTimestamp != null)
{
query = query.Where(x => x.StartTimestamp >= traceQuery.StartTimestamp);
}

if (traceQuery.FinishTimestamp != null)
{
query = query.Where(x => x.FinishTimestamp <= traceQuery.FinishTimestamp);
}

var queryGroup = query.ToList().GroupBy(x => x.TraceId).ToList();

var histogram = queryGroup.GroupBy(x => x.Min(s => s.StartTimestamp).ToString("yyyy-MM-dd HH:mm")).Select(x => new TraceHistogram { Count = x.Count(), Time = DateTimeOffset.Parse(x.Key) });
return histogram.ToList();
}
}
}

0 comments on commit 2271ea0

Please sign in to comment.