Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #38 from imranmomin/develop
Browse files Browse the repository at this point in the history
3.0.1
  • Loading branch information
imranmomin authored Feb 15, 2019
2 parents 491c0bf + e9ccc7c commit f96bca8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
11 changes: 9 additions & 2 deletions Hangfire.AzureDocumentDB/DocumentDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ public override List<string> GetRangeFromSet(string key, int startingFrom, int e
{
if (key == null) throw new ArgumentNullException(nameof(key));

FeedOptions feedOptions = new FeedOptions { MaxItemCount = endingAt + 1 };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = endingAt + 1
};

endingAt += 1 - startingFrom;

return Storage.Client.CreateDocumentQuery<Set>(Storage.CollectionUri, feedOptions)
Expand Down Expand Up @@ -486,7 +490,10 @@ public override List<string> GetRangeFromList(string key, int startingFrom, int
{
if (key == null) throw new ArgumentNullException(nameof(key));

FeedOptions feedOptions = new FeedOptions { MaxItemCount = endingAt + 1 };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = endingAt + 1
};
endingAt += 1 - startingFrom;

return Storage.Client.CreateDocumentQuery<List>(Storage.CollectionUri, feedOptions)
Expand Down
83 changes: 46 additions & 37 deletions Hangfire.AzureDocumentDB/DocumentDbMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Azure.Documents;
using Hangfire.Storage.Monitoring;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.SystemFunctions;

using Hangfire.Azure.Queue;
using Hangfire.Azure.Helper;
Expand Down Expand Up @@ -117,20 +116,13 @@ public StatisticsDto GetStatistics()
{
Dictionary<string, long> results = new Dictionary<string, long>();

FeedOptions feedOptions = new FeedOptions
// get counts of jobs on state
string[] keys = { EnqueuedState.StateName, FailedState.StateName, ProcessingState.StateName, ScheduledState.StateName };
foreach (string state in keys)
{
MaxItemCount = 1000,
};

// get counts of jobs group-by on state
Dictionary<string, long> states = storage.Client.CreateDocumentQuery<Documents.Job>(storage.CollectionUri, feedOptions)
.Where(j => j.DocumentType == DocumentTypes.Job && j.StateName.IsDefined())
.Select(j => j.StateName)
.ToQueryResult()
.GroupBy(j => j)
.ToDictionary(g => g.Key, g => g.LongCount());

results = results.Concat(states).ToDictionary(k => k.Key, v => v.Value);
long states = GetNumberOfJobsByStateName(state);
results.Add(state.ToLower(), states);
}

// get counts of servers
SqlQuerySpec sql = new SqlQuerySpec
Expand All @@ -146,17 +138,28 @@ public StatisticsDto GetStatistics()
.ToQueryResult()
.FirstOrDefault();

results.Add("Servers", servers);
results.Add("servers", servers);

// get sum of stats:succeeded counters raw / aggregate
Dictionary<string, long> counters = storage.Client.CreateDocumentQuery<Counter>(storage.CollectionUri, feedOptions)
.Where(c => c.DocumentType == DocumentTypes.Counter && (c.Key == "stats:succeeded" || c.Key == "stats:deleted"))
.Select(c => new { c.Key, c.Value })
.ToQueryResult()
.GroupBy(c => c.Key)
.ToDictionary(g => g.Key, g => (long)g.Sum(c => c.Value));
// get sum of stats:succeeded / stats:deleted counters
keys = new[] { "stats:succeeded", "stats:deleted" };
foreach (string key in keys)
{
sql = new SqlQuerySpec
{
QueryText = "SELECT TOP 1 VALUE SUM(doc['value']) FROM doc WHERE doc.type = @type AND doc.key = @key",
Parameters = new SqlParameterCollection
{
new SqlParameter("@key", key),
new SqlParameter("@type", DocumentTypes.Counter)
}
};

long counters = storage.Client.CreateDocumentQuery<long>(storage.CollectionUri, sql)
.ToQueryResult()
.FirstOrDefault();

results = results.Concat(counters).ToDictionary(k => k.Key, v => v.Value);
results.Add(key, counters);
}

sql = new SqlQuerySpec
{
Expand All @@ -168,25 +171,25 @@ public StatisticsDto GetStatistics()
}
};

long count = storage.Client.CreateDocumentQuery<long>(storage.CollectionUri, sql)
long jobs = storage.Client.CreateDocumentQuery<long>(storage.CollectionUri, sql)
.ToQueryResult()
.FirstOrDefault();

results.Add("recurring-jobs", count);
results.Add("recurring-jobs", jobs);

long GetValueOrDefault(string key) => results.Where(r => r.Key == key).Select(r => r.Value).SingleOrDefault();
long getValueOrDefault(string key) => results.TryGetValue(key, out long value) ? value : default(long);

// ReSharper disable once UseObjectOrCollectionInitializer
cacheStatisticsDto = new StatisticsDto
{
Enqueued = GetValueOrDefault("Enqueued"),
Failed = GetValueOrDefault("Failed"),
Processing = GetValueOrDefault("Processing"),
Scheduled = GetValueOrDefault("Scheduled"),
Succeeded = GetValueOrDefault("stats:succeeded"),
Deleted = GetValueOrDefault("stats:deleted"),
Recurring = GetValueOrDefault("recurring-jobs"),
Servers = GetValueOrDefault("Servers")
Enqueued = getValueOrDefault("enqueued"),
Failed = getValueOrDefault("failed"),
Processing = getValueOrDefault("processing"),
Scheduled = getValueOrDefault("scheduled"),
Succeeded = getValueOrDefault("stats:succeeded"),
Deleted = getValueOrDefault("stats:deleted"),
Recurring = getValueOrDefault("recurring-jobs"),
Servers = getValueOrDefault("servers"),
};

cacheStatisticsDto.Queues = storage.QueueProviders
Expand Down Expand Up @@ -290,7 +293,10 @@ public JobList<DeletedJobDto> DeletedJobs(int from, int count)
private JobList<T> GetJobsOnState<T>(string stateName, int from, int count, Func<State, Common.Job, T> selector)
{
List<KeyValuePair<string, T>> jobs = new List<KeyValuePair<string, T>>();
FeedOptions feedOptions = new FeedOptions { MaxItemCount = from + count };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = from + count
};

List<Documents.Job> filterJobs = storage.Client.CreateDocumentQuery<Documents.Job>(storage.CollectionUri, feedOptions)
.Where(j => j.DocumentType == DocumentTypes.Job && j.StateName == stateName)
Expand Down Expand Up @@ -334,7 +340,10 @@ private JobList<T> GetJobsOnQueue<T>(string queryText, string queue, int from, i
}
};

FeedOptions feedOptions = new FeedOptions { MaxItemCount = from + count };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = from + count
};

List<Documents.Queue> queues = storage.Client.CreateDocumentQuery<Documents.Queue>(storage.CollectionUri, sql, feedOptions)
.ToQueryResult()
Expand Down Expand Up @@ -402,7 +411,7 @@ private long GetNumberOfJobsByStateName(string state)
{
SqlQuerySpec sql = new SqlQuerySpec
{
QueryText = "SELECT TOP 1 VALUE COUNT(1) FROM doc WHERE doc.type = @type AND doc.state_name = @state",
QueryText = "SELECT TOP 1 VALUE COUNT(1) FROM doc WHERE doc.type = @type AND IS_DEFINED(doc.state_name) AND doc.state_name = @state",
Parameters = new SqlParameterCollection
{
new SqlParameter("@state", state),
Expand Down
10 changes: 8 additions & 2 deletions Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public int GetEnqueuedCount(string queue)

public IEnumerable<string> GetEnqueuedJobIds(string queue, int from, int perPage)
{
FeedOptions feedOptions = new FeedOptions { MaxItemCount = from + perPage };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = from + perPage
};

return storage.Client.CreateDocumentQuery<Documents.Queue>(storage.CollectionUri, feedOptions)
.Where(q => q.DocumentType == DocumentTypes.Queue && q.Name == queue && q.FetchedAt.IsDefined() == false)
Expand All @@ -77,7 +80,10 @@ public IEnumerable<string> GetEnqueuedJobIds(string queue, int from, int perPage

public IEnumerable<string> GetFetchedJobIds(string queue, int from, int perPage)
{
FeedOptions feedOptions = new FeedOptions { MaxItemCount = from + perPage };
FeedOptions feedOptions = new FeedOptions {
EnableCrossPartitionQuery = true,
MaxItemCount = from + perPage
};

return storage.Client.CreateDocumentQuery<Documents.Queue>(storage.CollectionUri, feedOptions)
.Where(q => q.DocumentType == DocumentTypes.Queue && q.Name == queue && q.FetchedAt.IsDefined())
Expand Down

0 comments on commit f96bca8

Please sign in to comment.