Skip to content

Commit 761b804

Browse files
author
Martin Todorov
committed
removed comments
1 parent 74c409d commit 761b804

File tree

2 files changed

+8
-46
lines changed

2 files changed

+8
-46
lines changed

src/Altinn.Broker.Application/GenerateReport/GenerateDailySummaryReportHandler.cs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public async Task<OneOf<GenerateDailySummaryReportResponse, Error>> Process(
3131
{
3232
logger.LogInformation("Starting daily summary report generation with Altinn2Included={altinn2Included}", request.Altinn2Included);
3333

34-
// Get aggregated data directly from SQL (optimized with GROUP BY)
35-
// Note: Broker only supports Altinn3, so Altinn2Included is ignored but kept for API compatibility
3634
var aggregatedData = await fileTransferRepository.GetAggregatedDailySummaryData(cancellationToken);
3735
logger.LogInformation("Retrieved {count} aggregated records for daily summary report", aggregatedData.Count);
3836

@@ -42,11 +40,9 @@ public async Task<OneOf<GenerateDailySummaryReportResponse, Error>> Process(
4240
return StatisticsErrors.NoFileTransfersFound;
4341
}
4442

45-
// Enrich with service owner names and resource titles
4643
var summaryData = await EnrichAggregatedDataAsync(aggregatedData, cancellationToken);
4744
logger.LogInformation("Aggregated data into {count} daily summary records", summaryData.Count);
4845

49-
// Generate parquet file and upload to blob storage
5046
var (blobUrl, fileHash, fileSize) = await GenerateAndUploadParquetFile(summaryData, request.Altinn2Included, cancellationToken);
5147

5248
var response = new GenerateDailySummaryReportResponse
@@ -58,7 +54,7 @@ public async Task<OneOf<GenerateDailySummaryReportResponse, Error>> Process(
5854
Environment = hostEnvironment.EnvironmentName ?? "Unknown",
5955
FileSizeBytes = fileSize,
6056
FileHash = fileHash,
61-
Altinn2Included = false // Broker only supports Altinn3
57+
Altinn2Included = false
6258
};
6359

6460
logger.LogInformation("Successfully generated and uploaded daily summary report to blob storage: {blobUrl}", blobUrl);
@@ -75,7 +71,6 @@ private async Task<List<DailySummaryData>> EnrichAggregatedDataAsync(
7571
List<Core.Repositories.AggregatedDailySummaryData> aggregatedData,
7672
CancellationToken cancellationToken)
7773
{
78-
// Fetch service owner names in parallel
7974
var uniqueServiceOwnerIds = aggregatedData
8075
.Select(d => d.ServiceOwnerId)
8176
.Distinct()
@@ -96,7 +91,6 @@ private async Task<List<DailySummaryData>> EnrichAggregatedDataAsync(
9691
serviceOwnerNames[serviceOwnerId] = name;
9792
}
9893

99-
// Fetch resource titles in parallel
10094
var uniqueResourceIds = aggregatedData
10195
.Select(d => d.ResourceId)
10296
.Distinct()
@@ -117,7 +111,6 @@ private async Task<List<DailySummaryData>> EnrichAggregatedDataAsync(
117111
resourceTitles[resourceId] = title;
118112
}
119113

120-
// Convert to DailySummaryData with enrichment
121114
return aggregatedData.Select(d => new DailySummaryData
122115
{
123116
Date = d.Date,
@@ -138,8 +131,6 @@ private async Task<List<DailySummaryData>> EnrichAggregatedDataAsync(
138131

139132
private async Task<string> GetServiceOwnerIdAsync(FileTransferEntity fileTransfer, CancellationToken cancellationToken)
140133
{
141-
// Get service owner from database resource table (service_owner_id_fk -> service_owner.service_owner_id_pk)
142-
// This matches the mapping: serviceownerorgnr -> service_owner.service_owner_id_pk
143134
try
144135
{
145136
var resource = await resourceRepository.GetResource(fileTransfer.ResourceId, cancellationToken);
@@ -205,8 +196,6 @@ private async Task<string> GetResourceTitleAsync(string? resourceId, Cancellatio
205196

206197
try
207198
{
208-
// Get service owner name from Resource Registry (like correspondence does)
209-
// This returns the name from HasCompetentAuthority.Name (e.g., "Digitaliseringsdirektoratet", "NAV", etc.)
210199
var serviceOwnerName = await altinnResourceRepository.GetServiceOwnerNameOfResource(resourceId, cancellationToken);
211200
return serviceOwnerName ?? $"Unknown ({resourceId})";
212201
}
@@ -219,29 +208,23 @@ private async Task<string> GetResourceTitleAsync(string? resourceId, Cancellatio
219208

220209
private long CalculateDatabaseStorage(List<FileTransferEntity> fileTransfers)
221210
{
222-
// TODO: Calculate database storage based on file transfer metadata
223-
// For now, return 0 as placeholder
224211
return 0;
225212
}
226213

227214
private long CalculateAttachmentStorage(List<FileTransferEntity> fileTransfers)
228215
{
229-
// Sum up file transfer sizes
230216
return fileTransfers.Sum(ft => ft.FileTransferSize);
231217
}
232218

233219
private async Task<(string blobUrl, string fileHash, long fileSize)> GenerateAndUploadParquetFile(List<DailySummaryData> summaryData, bool altinn2Included, CancellationToken cancellationToken)
234220
{
235-
// Generate filename with timestamp as prefix and Altinn version indicator
236-
var altinnVersionIndicator = "A3"; // Broker only supports Altinn3
221+
var altinnVersionIndicator = "A3";
237222
var fileName = $"{DateTimeOffset.UtcNow:yyyyMMdd_HHmmss}_daily_summary_report_{altinnVersionIndicator}_{hostEnvironment.EnvironmentName}.parquet";
238223

239224
logger.LogInformation("Generating daily summary parquet file with {count} records for blob storage", summaryData.Count);
240225

241-
// Generate the parquet file as a stream
242226
var (parquetStream, fileHash, fileSize) = await GenerateParquetFileStream(summaryData, altinn2Included, cancellationToken);
243227

244-
// Upload to blob storage - need to add this method to storage service
245228
var blobUrl = await UploadReportFileToStorage(fileName, parquetStream, cancellationToken);
246229

247230
logger.LogInformation("Successfully generated and uploaded daily summary parquet file to blob storage: {blobUrl}", blobUrl);
@@ -251,8 +234,6 @@ private long CalculateAttachmentStorage(List<FileTransferEntity> fileTransfers)
251234

252235
private async Task<string> UploadReportFileToStorage(string fileName, Stream stream, CancellationToken cancellationToken)
253236
{
254-
// Use Azure Storage directly for reports (similar to correspondence implementation)
255-
// Reports are stored in a "reports" container in the default storage account
256237
try
257238
{
258239
var connectionString = reportStorageOptions.Value.ConnectionString;
@@ -264,12 +245,10 @@ private async Task<string> UploadReportFileToStorage(string fileName, Stream str
264245
var blobServiceClient = new Azure.Storage.Blobs.BlobServiceClient(connectionString);
265246
var blobContainerClient = blobServiceClient.GetBlobContainerClient("reports");
266247

267-
// Ensure the reports container exists
268248
await blobContainerClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
269249

270250
var blobClient = blobContainerClient.GetBlobClient(fileName);
271251

272-
// Upload the file
273252
await blobClient.UploadAsync(stream, overwrite: true, cancellationToken);
274253

275254
logger.LogInformation("Successfully uploaded report file to blob storage: {fileName}", fileName);
@@ -286,7 +265,6 @@ private async Task<string> UploadReportFileToStorage(string fileName, Stream str
286265
{
287266
logger.LogInformation("Generating daily summary parquet file with {count} records", summaryData.Count);
288267

289-
// Convert to parquet-friendly model
290268
var parquetData = summaryData.Select(d => new ParquetDailySummaryData
291269
{
292270
Date = d.Date.ToString("yyyy-MM-dd"),
@@ -304,17 +282,14 @@ private async Task<string> UploadReportFileToStorage(string fileName, Stream str
304282
AttachmentStorageBytes = d.AttachmentStorageBytes
305283
}).ToList();
306284

307-
// Create a memory stream for the parquet data
308285
var memoryStream = new MemoryStream();
309286

310-
// Write parquet data to memory stream
311287
await ParquetSerializer.SerializeAsync(parquetData, memoryStream, cancellationToken: cancellationToken);
312-
memoryStream.Position = 0; // Reset position for reading
288+
memoryStream.Position = 0;
313289

314-
// Calculate MD5 hash
315290
using var md5 = MD5.Create();
316291
var hash = Convert.ToBase64String(md5.ComputeHash(memoryStream.ToArray()));
317-
memoryStream.Position = 0; // Reset position for reading
292+
memoryStream.Position = 0;
318293

319294
logger.LogInformation("Successfully generated daily summary parquet file stream");
320295

@@ -329,7 +304,6 @@ public async Task<OneOf<GenerateAndDownloadDailySummaryReportResponse, Error>> P
329304

330305
try
331306
{
332-
// Get aggregated data directly from SQL (optimized with GROUP BY)
333307
var aggregatedData = await fileTransferRepository.GetAggregatedDailySummaryData(cancellationToken);
334308

335309
if (!aggregatedData.Any())
@@ -340,14 +314,11 @@ public async Task<OneOf<GenerateAndDownloadDailySummaryReportResponse, Error>> P
340314

341315
logger.LogInformation("Found {count} aggregated records for report generation", aggregatedData.Count);
342316

343-
// Enrich with service owner names and resource titles
344317
var summaryData = await EnrichAggregatedDataAsync(aggregatedData, cancellationToken);
345318

346-
// Generate the parquet file as a stream
347319
var (parquetStream, fileHash, fileSize) = await GenerateParquetFileStream(summaryData, request.Altinn2Included, cancellationToken);
348320

349-
// Generate filename
350-
var altinnVersionIndicator = "A3"; // Broker only supports Altinn3
321+
var altinnVersionIndicator = "A3";
351322
var fileName = $"{DateTimeOffset.UtcNow:yyyyMMdd_HHmmss}_daily_summary_report_{altinnVersionIndicator}_{hostEnvironment.EnvironmentName}.parquet";
352323

353324
var response = new GenerateAndDownloadDailySummaryReportResponse
@@ -360,7 +331,7 @@ public async Task<OneOf<GenerateAndDownloadDailySummaryReportResponse, Error>> P
360331
TotalFileTransferCount = summaryData.Sum(d => d.MessageCount),
361332
GeneratedAt = DateTimeOffset.UtcNow,
362333
Environment = hostEnvironment.EnvironmentName ?? "Unknown",
363-
Altinn2Included = false // Broker only supports Altinn3
334+
Altinn2Included = false
364335
};
365336

366337
logger.LogInformation("Successfully generated daily summary report for download with {serviceOwnerCount} service owners and {totalCount} file transfers",

src/Altinn.Broker.Persistence/Repositories/FileTransferRepository.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -802,13 +802,8 @@ FROM broker.file_transfer f
802802

803803
public async Task<List<AggregatedDailySummaryData>> GetAggregatedDailySummaryData(CancellationToken cancellationToken)
804804
{
805-
// Optimized query: Aggregate data directly in SQL using GROUP BY
806-
// Uses actor_file_transfer_status (history table) with CTE to get latest status per recipient
807-
// This matches the old query logic to ensure we get the same results
808-
// Includes file transfers with recipients AND file transfers without recipients
809805
const string query = @"
810806
WITH latest_recipient_status AS (
811-
-- Get the latest status per (file_transfer, recipient) pair
812807
SELECT DISTINCT ON (afs.file_transfer_id_fk, afs.actor_id_fk)
813808
afs.file_transfer_id_fk,
814809
afs.actor_id_fk,
@@ -826,17 +821,13 @@ FROM broker.actor_file_transfer_status afs
826821
COALESCE(f.resource_id, 'unknown') as resource_id,
827822
COALESCE(recipient.actor_external_id, 'unknown') as recipient_id,
828823
CASE
829-
-- Organization: Extract part after colon (if exists) and check if it's 9 digits
830-
-- Matches C# logic: WithoutPrefix() then IsOrganizationNumber() which accepts 9 digits
831824
WHEN recipient.actor_external_id IS NOT NULL
832825
AND COALESCE(SPLIT_PART(recipient.actor_external_id, ':', -1), recipient.actor_external_id) ~ '^\d{9}$' THEN 1
833-
-- Person: Extract part after colon (if exists) and check if it's 11 digits
834-
-- Note: C# also validates mod11 checksum, but for aggregation format check is acceptable
835826
WHEN recipient.actor_external_id IS NOT NULL
836827
AND COALESCE(SPLIT_PART(recipient.actor_external_id, ':', -1), recipient.actor_external_id) ~ '^\d{11}$' THEN 0
837828
ELSE 2
838829
END as recipient_type,
839-
1 as altinn_version, -- Altinn3 = 1 (Broker only supports Altinn3)
830+
1 as altinn_version,
840831
COUNT(*)::int as message_count,
841832
0::bigint as database_storage_bytes,
842833
COALESCE(SUM(f.file_transfer_size), 0)::bigint as attachment_storage_bytes
@@ -868,7 +859,7 @@ ORDER BY
868859
altinn_version";
869860

870861
await using var command = dataSource.CreateCommand(query);
871-
command.CommandTimeout = 600; // 10 minutes timeout (20x typical execution time of 30 seconds)
862+
command.CommandTimeout = 600;
872863

873864
return await commandExecutor.ExecuteWithRetry(async (ct) =>
874865
{

0 commit comments

Comments
 (0)