Skip to content

Commit

Permalink
Fix process history with telemetry (#841)
Browse files Browse the repository at this point in the history
* Fix process history with telemetry

* fix formatting
  • Loading branch information
ivarne committed Oct 17, 2024
1 parent ba02911 commit 12955fc
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Altinn.App.Api/Controllers/ProcessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ instance.Process.EndEvent is null
[HttpGet("history")]
[Authorize(Policy = AuthzConstants.POLICY_INSTANCE_READ)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult> GetProcessHistory(
public async Task<ActionResult<ProcessHistoryList>> GetProcessHistory(
[FromRoute] int instanceOwnerPartyId,
[FromRoute] Guid instanceGuid
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ partial class Telemetry
internal Activity? StartGetProcessDefinitionActivity() =>
ActivitySource.StartActivity("ProcessClient.GetProcessDefinition");

internal Activity? StartGetProcessHistoryActivity(string? instanceId, string? instanceOwnerPartyId)
internal Activity? StartGetProcessHistoryActivity(string? instanceGuid, string? instanceOwnerPartyId)
{
var activity = ActivitySource.StartActivity("ProcessClient.GetProcessHistory");
activity?.SetInstanceId(instanceId);
if (Guid.TryParse(instanceGuid, out Guid instanceId))
{
activity?.SetInstanceId(instanceId);
}

activity?.SetInstanceOwnerPartyId(instanceOwnerPartyId);
return activity;
}
Expand Down
39 changes: 39 additions & 0 deletions test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.Encodings.Web;
Expand All @@ -14,7 +15,9 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using Xunit.Abstractions;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Altinn.App.Api.Tests.Controllers;

Expand Down Expand Up @@ -409,6 +412,42 @@ public async Task RunNextWithAction_WhenActionIsNotAuthorized_ReturnsUnauthorize
nextResponse.Should().HaveStatusCode(HttpStatusCode.Forbidden);
}

[Fact]
public async Task ProcessHistory_ShouldReturnProcessHistory()
{
var start = "2024-10-16T10:33:54.935732Z";
var processList = new ProcessHistoryList()
{
ProcessHistory = [new() { ElementId = "Task_1", Started = DateTime.Parse(start).ToUniversalTime(), }],
};
SendAsync = message =>
{
ArgumentNullException.ThrowIfNull(message.RequestUri);
message
.RequestUri.PathAndQuery.Should()
.Be($"/storage/api/v1/instances/{InstanceOwnerPartyId}/{_instanceGuid}/process/history");

Check failure on line 428 in test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs

View workflow job for this annotation

GitHub Actions / release-nugets

The name '_instanceGuid' does not exist in the current context

Check failure on line 428 in test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs

View workflow job for this annotation

GitHub Actions / release-nugets

The name '_instanceGuid' does not exist in the current context
return Task.FromResult(
new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(processList)), // Api uses Newtonsoft.Json
}
);
};
HttpClient client = GetRootedClient(Org, App, 1337, InstanceOwnerPartyId);
string url = $"/{Org}/{App}/instances/{InstanceOwnerPartyId}/{_instanceGuid}/process/history";

Check failure on line 437 in test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs

View workflow job for this annotation

GitHub Actions / release-nugets

The name '_instanceGuid' does not exist in the current context

Check failure on line 437 in test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs

View workflow job for this annotation

GitHub Actions / release-nugets

The name '_instanceGuid' does not exist in the current context

HttpResponseMessage response = await client.GetAsync(url);

var content = await response.Content.ReadAsStringAsync();
OutputHelper.WriteLine(content);
response.Should().HaveStatusCode(HttpStatusCode.OK);
content
.Should()
.Be(
$$"""{"processHistory":[{"eventType":null,"elementId":"Task_1","occured":null,"started":"{{start}}","ended":null,"performedBy":null}]}"""
);
}

//TODO: replace this assertion with a proper one once fluentassertions has a json compare feature scheduled for v7 https://github.com/fluentassertions/fluentassertions/issues/2205
private static void CompareResult<T>(string expectedString, string actualString)
{
Expand Down
75 changes: 74 additions & 1 deletion test/Altinn.App.Api.Tests/OpenApi/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3465,7 +3465,34 @@
],
"responses": {
"200": {
"description": "OK"
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProcessHistoryList"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProcessHistoryList"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProcessHistoryList"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/ProcessHistoryList"
}
},
"text/xml": {
"schema": {
"$ref": "#/components/schemas/ProcessHistoryList"
}
}
}
}
}
}
Expand Down Expand Up @@ -6359,6 +6386,52 @@
},
"additionalProperties": false
},
"ProcessHistoryItem": {
"type": "object",
"properties": {
"eventType": {
"type": "string",
"nullable": true
},
"elementId": {
"type": "string",
"nullable": true
},
"occured": {
"type": "string",
"format": "date-time",
"nullable": true
},
"started": {
"type": "string",
"format": "date-time",
"nullable": true
},
"ended": {
"type": "string",
"format": "date-time",
"nullable": true
},
"performedBy": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
"ProcessHistoryList": {
"type": "object",
"properties": {
"processHistory": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ProcessHistoryItem"
},
"nullable": true
}
},
"additionalProperties": false
},
"ProcessNext": {
"type": "object",
"properties": {
Expand Down
50 changes: 50 additions & 0 deletions test/Altinn.App.Api.Tests/OpenApi/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,22 @@ paths:
responses:
'200':
description: OK
content:
text/plain:
schema:
$ref: '#/components/schemas/ProcessHistoryList'
application/json:
schema:
$ref: '#/components/schemas/ProcessHistoryList'
text/json:
schema:
$ref: '#/components/schemas/ProcessHistoryList'
application/xml:
schema:
$ref: '#/components/schemas/ProcessHistoryList'
text/xml:
schema:
$ref: '#/components/schemas/ProcessHistoryList'
'/{org}/{app}/api/v1/profile/user':
get:
tags:
Expand Down Expand Up @@ -4069,6 +4085,40 @@ components:
type: string
nullable: true
additionalProperties: false
ProcessHistoryItem:
type: object
properties:
eventType:
type: string
nullable: true
elementId:
type: string
nullable: true
occured:
type: string
format: date-time
nullable: true
started:
type: string
format: date-time
nullable: true
ended:
type: string
format: date-time
nullable: true
performedBy:
type: string
nullable: true
additionalProperties: false
ProcessHistoryList:
type: object
properties:
processHistory:
type: array
items:
$ref: '#/components/schemas/ProcessHistoryItem'
nullable: true
additionalProperties: false
ProcessNext:
type: object
properties:
Expand Down

0 comments on commit 12955fc

Please sign in to comment.