-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathRecommendationListCommand.cs
More file actions
80 lines (71 loc) · 3.12 KB
/
Copy pathRecommendationListCommand.cs
File metadata and controls
80 lines (71 loc) · 3.12 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.CommandLine;
using System.Text.Json.Nodes;
using Azure.Mcp.Core.Commands.Subscription;
using Azure.Mcp.Tools.ApplicationInsights.Options;
using Azure.Mcp.Tools.ApplicationInsights.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Extensions;
using Microsoft.Mcp.Core.Models.Command;
using Microsoft.Mcp.Core.Models.Option;
namespace Azure.Mcp.Tools.ApplicationInsights.Commands.Recommendation;
[CommandMetadata(
Id = "8d259f21-43b3-4962-bec8-de616b8b5f0d",
Name = "list",
Title = "List Application Insights Recommendations",
Description = """
List Application Insights Code Optimization Recommendations in a subscription or resource group.
Returns the code optimization recommendations based on the profiler data.
""",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class RecommendationListCommand(ILogger<RecommendationListCommand> logger, IApplicationInsightsService applicationInsightsService) : SubscriptionCommand<RecommendationListOptions>()
{
private readonly ILogger<RecommendationListCommand> _logger = logger;
private readonly IApplicationInsightsService _applicationInsightsService = applicationInsightsService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
// New explicit option registration pattern: add resource group as optional per-command
command.Options.Add(OptionDefinitions.Common.ResourceGroup.AsOptional());
}
protected override RecommendationListOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.ResourceGroup ??= parseResult.GetValueOrDefault<string>(OptionDefinitions.Common.ResourceGroup.Name);
return options;
}
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken)
{
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
var options = BindOptions(parseResult);
try
{
var insights = await _applicationInsightsService.GetProfilerInsightsAsync(
options.Subscription!,
options.ResourceGroup,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = insights?.Count() > 0 ?
ResponseResult.Create(new(insights), ApplicationInsightsJsonContext.Default.RecommendationListCommandResult) :
null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error listing Application Insights components for recommendations.");
HandleException(context, ex);
}
return context.Response;
}
internal record RecommendationListCommandResult(IEnumerable<JsonNode> Recommendations);
}