|
8 | 8 | using System.Threading.Tasks; |
9 | 9 | using Azure.Identity; |
10 | 10 | using Azure.Storage.Files.DataLake; |
| 11 | +using Microsoft.Extensions.AI.Evaluation.Console.Telemetry; |
11 | 12 | using Microsoft.Extensions.AI.Evaluation.Console.Utilities; |
12 | 13 | using Microsoft.Extensions.AI.Evaluation.Reporting; |
13 | 14 | using Microsoft.Extensions.AI.Evaluation.Reporting.Storage; |
14 | 15 | using Microsoft.Extensions.Logging; |
| 16 | +using static Microsoft.Extensions.AI.Evaluation.Console.Telemetry.TelemetryConstants; |
15 | 17 |
|
16 | 18 | namespace Microsoft.Extensions.AI.Evaluation.Console.Commands; |
17 | 19 |
|
18 | | -internal sealed class CleanResultsCommand(ILogger logger) |
| 20 | +internal sealed class CleanResultsCommand(ILogger logger, TelemetryHelper telemetryHelper) |
19 | 21 | { |
20 | 22 | internal async Task<int> InvokeAsync( |
21 | 23 | DirectoryInfo? storageRootDir, |
22 | 24 | Uri? endpointUri, |
23 | 25 | int lastN, |
24 | 26 | CancellationToken cancellationToken = default) |
25 | 27 | { |
26 | | - IEvaluationResultStore resultStore; |
27 | | - |
28 | | - if (storageRootDir is not null) |
29 | | - { |
30 | | - string storageRootPath = storageRootDir.FullName; |
31 | | - logger.LogInformation("Storage root path: {storageRootPath}", storageRootPath); |
| 28 | + var telemetryProperties = |
| 29 | + new Dictionary<string, string> |
| 30 | + { |
| 31 | + [PropertyNames.LastN] = lastN.ToTelemetryPropertyValue() |
| 32 | + }; |
32 | 33 |
|
33 | | - resultStore = new DiskBasedResultStore(storageRootPath); |
34 | | - } |
35 | | - else if (endpointUri is not null) |
36 | | - { |
37 | | - logger.LogInformation("Azure Storage endpoint: {endpointUri}", endpointUri); |
| 34 | + await logger.ExecuteWithCatchAsync( |
| 35 | + operation: () => |
| 36 | + telemetryHelper.ReportOperationAsync( |
| 37 | + operationName: EventNames.CleanResultsCommand, |
| 38 | + operation: async ValueTask () => |
| 39 | + { |
| 40 | + IEvaluationResultStore resultStore; |
38 | 41 |
|
39 | | - var fsClient = new DataLakeDirectoryClient(endpointUri, new DefaultAzureCredential()); |
40 | | - resultStore = new AzureStorageResultStore(fsClient); |
41 | | - } |
42 | | - else |
43 | | - { |
44 | | - throw new InvalidOperationException("Either --path or --endpoint must be specified"); |
45 | | - } |
| 42 | + if (storageRootDir is not null) |
| 43 | + { |
| 44 | + string storageRootPath = storageRootDir.FullName; |
| 45 | + logger.LogInformation("Storage root path: {storageRootPath}", storageRootPath); |
46 | 46 |
|
47 | | - await logger.ExecuteWithCatchAsync( |
48 | | - async ValueTask () => |
49 | | - { |
50 | | - if (lastN is 0) |
51 | | - { |
52 | | - logger.LogInformation("Deleting all results..."); |
| 47 | + resultStore = new DiskBasedResultStore(storageRootPath); |
53 | 48 |
|
54 | | - await resultStore.DeleteResultsAsync(cancellationToken: cancellationToken).ConfigureAwait(false); |
55 | | - } |
56 | | - else |
57 | | - { |
58 | | - logger.LogInformation("Deleting all results except the {lastN} most recent ones...", lastN); |
| 49 | + telemetryProperties[PropertyNames.StorageType] = PropertyValues.StorageTypeDisk; |
| 50 | + } |
| 51 | + else if (endpointUri is not null) |
| 52 | + { |
| 53 | + logger.LogInformation("Azure Storage endpoint: {endpointUri}", endpointUri); |
59 | 54 |
|
60 | | - HashSet<string> toPreserve = []; |
| 55 | + var fsClient = new DataLakeDirectoryClient(endpointUri, new DefaultAzureCredential()); |
| 56 | + resultStore = new AzureStorageResultStore(fsClient); |
61 | 57 |
|
62 | | - await foreach (string executionName in |
63 | | - resultStore.GetLatestExecutionNamesAsync(lastN, cancellationToken).ConfigureAwait(false)) |
64 | | - { |
65 | | - _ = toPreserve.Add(executionName); |
66 | | - } |
| 58 | + telemetryProperties[PropertyNames.StorageType] = PropertyValues.StorageTypeAzure; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + throw new InvalidOperationException("Either --path or --endpoint must be specified"); |
| 63 | + } |
67 | 64 |
|
68 | | - await foreach (string executionName in |
69 | | - resultStore.GetLatestExecutionNamesAsync( |
70 | | - cancellationToken: cancellationToken).ConfigureAwait(false)) |
71 | | - { |
72 | | - if (!toPreserve.Contains(executionName)) |
| 65 | + if (lastN is 0) |
73 | 66 | { |
| 67 | + logger.LogInformation("Deleting all results..."); |
| 68 | + |
74 | 69 | await resultStore.DeleteResultsAsync( |
75 | | - executionName, |
76 | 70 | cancellationToken: cancellationToken).ConfigureAwait(false); |
77 | 71 | } |
78 | | - } |
79 | | - } |
80 | | - }).ConfigureAwait(false); |
| 72 | + else |
| 73 | + { |
| 74 | + logger.LogInformation( |
| 75 | + "Deleting all results except the {lastN} most recent ones...", |
| 76 | + lastN); |
| 77 | + |
| 78 | + HashSet<string> toPreserve = []; |
| 79 | + |
| 80 | + await foreach (string executionName in |
| 81 | + resultStore.GetLatestExecutionNamesAsync( |
| 82 | + lastN, |
| 83 | + cancellationToken).ConfigureAwait(false)) |
| 84 | + { |
| 85 | + _ = toPreserve.Add(executionName); |
| 86 | + } |
| 87 | + |
| 88 | + await foreach (string executionName in |
| 89 | + resultStore.GetLatestExecutionNamesAsync( |
| 90 | + cancellationToken: cancellationToken).ConfigureAwait(false)) |
| 91 | + { |
| 92 | + if (!toPreserve.Contains(executionName)) |
| 93 | + { |
| 94 | + await resultStore.DeleteResultsAsync( |
| 95 | + executionName, |
| 96 | + cancellationToken: cancellationToken).ConfigureAwait(false); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + properties: telemetryProperties, |
| 102 | + logger: logger)).ConfigureAwait(false); |
81 | 103 |
|
82 | 104 | return 0; |
83 | 105 | } |
|
0 commit comments