-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryStatus.cs
More file actions
170 lines (139 loc) · 6.17 KB
/
QueryStatus.cs
File metadata and controls
170 lines (139 loc) · 6.17 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#region License
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2025 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
using System.Text.Json;
using Couchbase.AnalyticsClient.Internal;
using Couchbase.AnalyticsClient.Query;
namespace Couchbase.AnalyticsClient.Async;
/// <summary>
/// Represents the status of a server-side asynchronous query.
/// Obtained from <see cref="QueryHandle.FetchStatusAsync"/>.
/// </summary>
public class QueryStatus
{
private readonly string? _resultHandlePath;
private readonly string _requestId;
private readonly JsonElement _root;
private readonly IAnalyticsService _analyticsService;
internal string? Status { get; }
internal AsyncQueryMetrics? Metrics { get; }
internal int? ResultCount { get; }
internal bool? ResultSetOrdered { get; }
internal string? CreatedAt { get; }
internal int PartitionCount { get; }
/// <summary>
/// Returns <c>true</c> if and only if the server response provides a handle
/// for the SDK to retrieve the query results. Otherwise returns <c>false</c>.
/// This property does not perform network calls.
/// </summary>
public bool ResultsReady { get; }
internal QueryStatus(string requestId, JsonElement root, IAnalyticsService analyticsService)
{
_requestId = requestId ?? throw new ArgumentNullException(nameof(requestId));
_analyticsService = analyticsService ?? throw new ArgumentNullException(nameof(analyticsService));
if (root.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null)
{
throw new ArgumentException("The JSON response element must not be empty or null.", nameof(root));
}
_root = root;
// ── Core fields ──
Status = root.TryGetProperty("status", out var statusProp) ? statusProp.GetString() : null;
if (root.TryGetProperty("metrics", out var metricsElement))
{
Metrics = JsonSerializer.Deserialize<AsyncQueryMetrics>(metricsElement.GetRawText());
}
// ResultsReady is true when the server provides a result handle path
_resultHandlePath = root.TryGetProperty("handle", out var handleProp) ? handleProp.GetString() : null;
ResultsReady = !string.IsNullOrWhiteSpace(_resultHandlePath);
// ── Optional diagnostic fields (may be absent depending on server version or query state) ──
if (root.TryGetProperty("resultCount", out var resultCountProp) && resultCountProp.TryGetInt32(out var resultCount))
{
ResultCount = resultCount;
}
if (root.TryGetProperty("resultSetOrdered", out var orderedProp) && orderedProp.ValueKind is JsonValueKind.True or JsonValueKind.False)
{
ResultSetOrdered = orderedProp.GetBoolean();
}
CreatedAt = root.TryGetProperty("createdAt", out var createdProp) ? createdProp.GetString() : null;
if (root.TryGetProperty("partitions", out var partProp) && partProp.ValueKind == JsonValueKind.Array)
{
PartitionCount = partProp.GetArrayLength();
}
}
/// <summary>
/// Returns a new <see cref="QueryResultHandle"/> instance.
/// This method does not perform network calls.
/// </summary>
/// <returns>A <see cref="QueryResultHandle"/> that can be used to fetch or discard results.</returns>
/// <exception cref="InvalidOperationException">Thrown if results are not yet ready.</exception>
public QueryResultHandle ResultHandle()
{
if (!ResultsReady)
{
throw new InvalidOperationException(
$"Results are not ready. Current status: {Status ?? "unknown"}. " +
"Poll again with FetchStatusAsync() until ResultsReady is true.");
}
return new QueryResultHandle(_resultHandlePath!, _requestId, _root, _analyticsService);
}
/// <inheritdoc />
public override string ToString()
{
var parts = new List<string>
{
$"Status={Status ?? "unknown"}",
$"ResultsReady={ResultsReady}"
};
if (ResultCount.HasValue)
{
parts.Add($"ResultCount={ResultCount}");
}
if (ResultSetOrdered.HasValue)
{
parts.Add($"ResultSetOrdered={ResultSetOrdered}");
}
if (PartitionCount > 0)
{
parts.Add($"Partitions={PartitionCount}");
}
if (CreatedAt is not null)
{
parts.Add($"CreatedAt={CreatedAt}");
}
if (Metrics is not null)
{
var metricParts = new List<string>();
if (Metrics.ElapsedTime.HasValue)
metricParts.Add($"ElapsedTime={Metrics.ElapsedTime.Value.TotalMilliseconds:F1}ms");
if (Metrics.ExecutionTime.HasValue)
metricParts.Add($"ExecutionTime={Metrics.ExecutionTime.Value.TotalMilliseconds:F1}ms");
if (Metrics.CompileTime.HasValue)
metricParts.Add($"CompileTime={Metrics.CompileTime.Value.TotalMilliseconds:F1}ms");
if (Metrics.QueueWaitTime.HasValue)
metricParts.Add($"QueueWaitTime={Metrics.QueueWaitTime.Value.TotalMilliseconds:F1}ms");
if (Metrics.ProcessedObjects.HasValue)
metricParts.Add($"ProcessedObjects={Metrics.ProcessedObjects}");
parts.Add(metricParts.Count > 0
? $"Metrics={{{string.Join(", ", metricParts)}}}"
: "Metrics={none}");
}
return $"QueryStatus [{string.Join(", ", parts)}]";
}
}