|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Globalization; |
| 7 | +using System.IO; |
7 | 8 | using System.Linq; |
8 | 9 | using System.Text; |
9 | 10 | using System.Threading; |
10 | 11 | using System.Threading.Tasks; |
11 | 12 | using Azure.Storage.Blobs; |
12 | 13 | using Azure.Storage.Blobs.Models; |
13 | 14 | using Azure.Storage.Blobs.Specialized; |
14 | | -using Microsoft.Azure.WebJobs.Host.Timers; |
15 | 15 | using Microsoft.Extensions.Logging; |
16 | 16 |
|
17 | 17 | namespace Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Listeners |
@@ -77,6 +77,72 @@ public async Task<IEnumerable<BlobWithContainer<BlobBaseClient>>> GetRecentBlobW |
77 | 77 | return blobs; |
78 | 78 | } |
79 | 79 |
|
| 80 | + public async Task<StorageAnalyticsLogEntry> GetFirstLogEntryWithWritesAsync(string containerName, CancellationToken cancellationToken, int hoursWindow = DefaultScanHoursWindow) |
| 81 | + { |
| 82 | + if (hoursWindow <= 0) |
| 83 | + { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + DateTime hourCursor = DateTime.UtcNow; |
| 88 | + BlobContainerClient containerClient = _blobClient.GetBlobContainerClient(LogContainer); |
| 89 | + |
| 90 | + for (int hourIndex = 0; hourIndex < hoursWindow; hourIndex++) |
| 91 | + { |
| 92 | + cancellationToken.ThrowIfCancellationRequested(); |
| 93 | + |
| 94 | + string prefix = GetSearchPrefix("blob", hourCursor, hourCursor); |
| 95 | + |
| 96 | + await foreach (BlobItem blob in containerClient |
| 97 | + .GetBlobsAsync(traits: BlobTraits.Metadata, prefix: prefix, states: BlobStates.None, cancellationToken: cancellationToken) |
| 98 | + .ConfigureAwait(false)) |
| 99 | + { |
| 100 | + cancellationToken.ThrowIfCancellationRequested(); |
| 101 | + |
| 102 | + if (blob.Metadata is not null && |
| 103 | + blob.Metadata.TryGetValue(LogType, out string logType) && |
| 104 | + !string.IsNullOrEmpty(logType) && |
| 105 | + logType.IndexOf("write", StringComparison.OrdinalIgnoreCase) >= 0) |
| 106 | + { |
| 107 | + BlobClient logBlobClient = containerClient.GetBlobClient(blob.Name); |
| 108 | + |
| 109 | + using (Stream stream = await logBlobClient.OpenReadAsync(options: default, cancellationToken: cancellationToken).ConfigureAwait(false)) |
| 110 | + { |
| 111 | + using (StreamReader reader = new StreamReader(stream)) |
| 112 | + { |
| 113 | + int lineNumber = 0; |
| 114 | + while (!reader.EndOfStream) |
| 115 | + { |
| 116 | + cancellationToken.ThrowIfCancellationRequested(); |
| 117 | + string line = await reader.ReadLineAsync().ConfigureAwait(false); |
| 118 | + lineNumber++; |
| 119 | + |
| 120 | + if (line != null) |
| 121 | + { |
| 122 | + var entry = _parser.ParseLine(line, logBlobClient.Name, lineNumber.ToString(CultureInfo.InvariantCulture)); |
| 123 | + if (entry != null && entry.IsBlobWrite) |
| 124 | + { |
| 125 | + var path = entry.ToBlobPath(); |
| 126 | + if (path != null && |
| 127 | + string.Equals(path.ContainerName, containerName, StringComparison.OrdinalIgnoreCase)) |
| 128 | + { |
| 129 | + // If we found a valid write entry, we can stop searching. |
| 130 | + return entry; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + hourCursor = hourCursor.AddHours(-1); |
| 141 | + } |
| 142 | + |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
80 | 146 | internal static IEnumerable<BlobPath> GetPathsForValidBlobWrites(IEnumerable<StorageAnalyticsLogEntry> entries) |
81 | 147 | { |
82 | 148 | IEnumerable<BlobPath> parsedBlobPaths = from entry in entries |
|
0 commit comments