Skip to content

Commit

Permalink
Merge pull request #1467 from NASA-AMMOS/feat/pending-hasura-events
Browse files Browse the repository at this point in the history
Include Pending Events in Event Log
  • Loading branch information
Mythicaeda authored Jun 27, 2024
2 parents 2b8a594 + 899da1b commit 0da0b93
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,45 +119,33 @@ delete_permissions:

event_triggers:
- definition:
enable_manual: false
enable_manual: true
insert:
columns: "*"
update:
columns:
- id
- revision
- jar_id
- mission
- name
- version
- owner
name: refreshActivityTypes
retry_conf:
interval_sec: 10
num_retries: 0
timeout_sec: 300
webhook: "{{AERIE_MERLIN_URL}}/refreshActivityTypes"
- definition:
enable_manual: false
enable_manual: true
insert:
columns: "*"
update:
columns:
- id
- revision
- jar_id
- mission
- name
- version
- owner
- jar_id
name: refreshModelParameters
retry_conf:
interval_sec: 10
num_retries: 0
timeout_sec: 300
webhook: "{{AERIE_MERLIN_URL}}/refreshModelParameters"
- definition:
enable_manual: false
enable_manual: true
insert:
columns: "*"
update:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
drop view hasura.refresh_resource_type_logs;
drop view hasura.refresh_model_parameter_logs;
drop view hasura.refresh_activity_type_logs;
drop function hasura.get_event_logs(_trigger_name text);

create function hasura.get_event_logs(_trigger_name text)
returns table (
model_id int,
model_name text,
model_version text,
triggering_user text,
delivered boolean,
success boolean,
tries int,
created_at timestamp,
next_retry_at timestamp,
status int,
error json,
error_message text,
error_type text
)
stable
security invoker
language plpgsql as $$
begin
return query (
select
(el.payload->'data'->'new'->>'id')::int as model_id,
el.payload->'data'->'new'->>'name' as model_name,
el.payload->'data'->'new'->>'version' as model_version,
el.payload->'session_variables'->>'x-hasura-user-id' as triggering_user,
el.delivered,
eil.status is not distinct from 200 as success, -- is not distinct from to catch `null`
el.tries,
el.created_at,
el.next_retry_at,
eil.status,
eil.response -> 'data'-> 'message' as error,
eil.response -> 'data'-> 'message'->>'message' as error_message,
eil.response -> 'data'-> 'message'->>'type' as error_type
from hdb_catalog.event_log el
join hdb_catalog.event_invocation_logs eil on el.id = eil.event_id
where trigger_name = _trigger_name);
end;
$$;
comment on function hasura.get_event_logs(_trigger_name text) is e''
'Get the logs for every run of a Hasura event with the specified trigger name.';

create view hasura.refresh_activity_type_logs as
select * from hasura.get_event_logs('refreshActivityTypes');
comment on view hasura.refresh_activity_type_logs is e''
'View containing logs for every run of the Hasura event `refreshActivityTypes`.';

create view hasura.refresh_model_parameter_logs as
select * from hasura.get_event_logs('refreshModelParameters');
comment on view hasura.refresh_model_parameter_logs is e''
'View containing logs for every run of the Hasura event `refreshModelParameters`.';

create view hasura.refresh_resource_type_logs as
select * from hasura.get_event_logs('refreshResourceTypes');
comment on view hasura.refresh_resource_type_logs is e''
'View containing logs for every run of the Hasura event `refreshResourceTypes`.';


call migrations.mark_migration_rolled_back('7')
68 changes: 68 additions & 0 deletions deployment/hasura/migrations/Aerie/7_pending_hasura_events/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
drop view hasura.refresh_resource_type_logs;
drop view hasura.refresh_model_parameter_logs;
drop view hasura.refresh_activity_type_logs;
drop function hasura.get_event_logs(_trigger_name text);

create function hasura.get_event_logs(_trigger_name text)
returns table (
model_id int,
model_name text,
model_version text,
triggering_user text,
pending boolean,
delivered boolean,
success boolean,
tries int,
created_at timestamp,
next_retry_at timestamp,
status int,
error json,
error_message text,
error_type text
)
stable
security invoker
language plpgsql as $$
begin
return query (
select
(el.payload->'data'->'new'->>'id')::int as model_id,
el.payload->'data'->'new'->>'name' as model_name,
el.payload->'data'->'new'->>'version' as model_version,
el.payload->'session_variables'->>'x-hasura-user-id' as triggering_user,
eil.id is null as pending,
el.delivered,
eil.status is not distinct from 200 as success, -- is not distinct from to catch `null`
el.tries,
el.created_at,
el.next_retry_at,
eil.status,
eil.response -> 'data'-> 'message' as error,
-- Javalin uses "title" as it's "message" field
coalesce(eil.response -> 'data'-> 'message'->> 'message',
eil.response -> 'data'-> 'message'->> 'title') as error_message,
eil.response -> 'data'-> 'message'->>'type' as error_type
from hdb_catalog.event_log el
left join hdb_catalog.event_invocation_logs eil on el.id = eil.event_id
where trigger_name = _trigger_name);
end;
$$;
comment on function hasura.get_event_logs(_trigger_name text) is e''
'Get the logs for every run of a Hasura event with the specified trigger name.';

create view hasura.refresh_activity_type_logs as
select * from hasura.get_event_logs('refreshActivityTypes');
comment on view hasura.refresh_activity_type_logs is e''
'View containing logs for every run of the Hasura event `refreshActivityTypes`.';

create view hasura.refresh_model_parameter_logs as
select * from hasura.get_event_logs('refreshModelParameters');
comment on view hasura.refresh_model_parameter_logs is e''
'View containing logs for every run of the Hasura event `refreshModelParameters`.';

create view hasura.refresh_resource_type_logs as
select * from hasura.get_event_logs('refreshResourceTypes');
comment on view hasura.refresh_resource_type_logs is e''
'View containing logs for every run of the Hasura event `refreshResourceTypes`.';

call migrations.mark_migration_applied('7')
1 change: 1 addition & 0 deletions deployment/postgres-init-db/sql/applied_migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ call migrations.mark_migration_applied('3');
call migrations.mark_migration_applied('4');
call migrations.mark_migration_applied('5');
call migrations.mark_migration_applied('6');
call migrations.mark_migration_applied('7');
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ returns table (
model_name text,
model_version text,
triggering_user text,
pending boolean,
delivered boolean,
success boolean,
tries int,
Expand All @@ -24,17 +25,20 @@ begin
el.payload->'data'->'new'->>'name' as model_name,
el.payload->'data'->'new'->>'version' as model_version,
el.payload->'session_variables'->>'x-hasura-user-id' as triggering_user,
eil.id is null as pending,
el.delivered,
eil.status is not distinct from 200 as success, -- is not distinct from to catch `null`
el.tries,
el.created_at,
el.next_retry_at,
eil.status,
eil.response -> 'data'-> 'message' as error,
eil.response -> 'data'-> 'message'->>'message' as error_message,
-- Javalin uses "title" as it's "message" field
coalesce(eil.response -> 'data'-> 'message'->> 'message',
eil.response -> 'data'-> 'message'->> 'title') as error_message,
eil.response -> 'data'-> 'message'->>'type' as error_type
from hdb_catalog.event_log el
join hdb_catalog.event_invocation_logs eil on el.id = eil.event_id
left join hdb_catalog.event_invocation_logs eil on el.id = eil.event_id
where trigger_name = _trigger_name);
end;
$$;
Expand All @@ -55,6 +59,3 @@ create view hasura.refresh_resource_type_logs as
select * from hasura.get_event_logs('refreshResourceTypes');
comment on view hasura.refresh_resource_type_logs is e''
'View containing logs for every run of the Hasura event `refreshResourceTypes`.';



Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static gov.nasa.jpl.aerie.e2e.types.ValueSchema.*;
import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -377,14 +378,17 @@ void hasuraEventLogsAreAccessible() throws IOException {
// Check Activity Type Refresh Event Logs
final var activityTypeRefreshLogs = modelLogs.refreshActivityTypesLogs();
assertEquals(1, activityTypeRefreshLogs.size());
final var activityTypeLog = activityTypeRefreshLogs.get(0);
final var activityTypeLog = activityTypeRefreshLogs.getFirst();

assertEquals("Aerie Legacy", activityTypeLog.triggeringUser());

assertFalse(activityTypeLog.pending());
assertTrue(activityTypeLog.delivered());
assertTrue(activityTypeLog.success());
assertEquals(1, activityTypeLog.tries());
assertEquals(200, activityTypeLog.status());

assertTrue(activityTypeLog.status().isPresent());
assertEquals(200, activityTypeLog.status().get());

assertTrue(activityTypeLog.error().isEmpty());
assertTrue(activityTypeLog.errorMessage().isEmpty());
Expand All @@ -393,14 +397,17 @@ void hasuraEventLogsAreAccessible() throws IOException {
// Check Model Parameter Refresh Event Logs
final var modelParamRefreshLogs = modelLogs.refreshModelParamsLogs();
assertEquals(1, modelParamRefreshLogs.size());
final var modelParamLog = modelParamRefreshLogs.get(0);
final var modelParamLog = modelParamRefreshLogs.getFirst();

assertEquals("Aerie Legacy", modelParamLog.triggeringUser());

assertFalse(modelParamLog.pending());
assertTrue(modelParamLog.delivered());
assertTrue(modelParamLog.success());
assertEquals(1, modelParamLog.tries());
assertEquals(200, modelParamLog.status());

assertTrue(modelParamLog.status().isPresent());
assertEquals(200, modelParamLog.status().get());

assertTrue(modelParamLog.error().isEmpty());
assertTrue(modelParamLog.errorMessage().isEmpty());
Expand All @@ -409,14 +416,17 @@ void hasuraEventLogsAreAccessible() throws IOException {
// Check Resource Type Refresh Event Logs
final var resourceTypeRefreshLogs = modelLogs.refreshResourceTypesLogs();
assertEquals(1, resourceTypeRefreshLogs.size());
final var resourceTypeLog = resourceTypeRefreshLogs.get(0);
final var resourceTypeLog = resourceTypeRefreshLogs.getFirst();

assertEquals("Aerie Legacy", resourceTypeLog.triggeringUser());

assertFalse(resourceTypeLog.pending());
assertTrue(resourceTypeLog.delivered());
assertTrue(resourceTypeLog.success());
assertEquals(1, resourceTypeLog.tries());
assertEquals(200, resourceTypeLog.status());

assertTrue(resourceTypeLog.status().isPresent());
assertEquals(200, resourceTypeLog.status().get());

assertTrue(resourceTypeLog.error().isEmpty());
assertTrue(resourceTypeLog.errorMessage().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ public record ModelEventLogs(
) {
public record EventLog(
String triggeringUser,
boolean pending,
boolean delivered,
boolean success,
int tries,
String createdAt,
int status,
Optional<Integer> status,
Optional<JsonObject> error,
Optional<String> errorMessage,
Optional<String> errorType
)
{
public static EventLog fromJSON(JsonObject json) {
final Optional<Integer> status = json.isNull("status") ?
Optional.empty() : Optional.of(json.getInt("status"));
final Optional<JsonObject> error = json.isNull("error") ?
Optional.empty() : Optional.of(json.getJsonObject("error"));
final Optional<String> errorMsg = json.isNull("error_message") ?
Expand All @@ -34,11 +37,12 @@ public static EventLog fromJSON(JsonObject json) {

return new EventLog(
json.getString("triggering_user"),
json.getBoolean("pending"),
json.getBoolean("delivered"),
json.getBoolean("success"),
json.getInt("tries"),
json.getString("created_at"),
json.getInt("status"),
status,
error,
errorMsg,
errorType);
Expand Down
3 changes: 3 additions & 0 deletions e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/utils/GQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ query getModelLogs($modelId: Int!) {
version
refresh_activity_type_logs(order_by: {created_at: desc}) {
triggering_user
pending
delivered
success
tries
Expand All @@ -284,6 +285,7 @@ query getModelLogs($modelId: Int!) {
}
refresh_model_parameter_logs(order_by: {created_at: desc}) {
triggering_user
pending
delivered
success
tries
Expand All @@ -295,6 +297,7 @@ query getModelLogs($modelId: Int!) {
}
refresh_resource_type_logs(order_by: {created_at: desc}) {
triggering_user
pending
delivered
success
tries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,19 @@ public ModelEventLogs awaitModelEventLogs(int modelId, int timeout) throws IOExc
final var variables = Json.createObjectBuilder().add("modelId", modelId).build();

for(int i = 0; i < timeout; ++i){
final var logs = makeRequest(GQL.GET_MODEL_EVENT_LOGS, variables).getJsonObject("mission_model");
if(logs.getJsonArray("refresh_activity_type_logs").isEmpty()
|| logs.getJsonArray("refresh_model_parameter_logs").isEmpty()
|| logs.getJsonArray("refresh_resource_type_logs").isEmpty()) {
final var logs = ModelEventLogs.fromJSON(makeRequest(GQL.GET_MODEL_EVENT_LOGS, variables)
.getJsonObject("mission_model"));

if(logs.refreshActivityTypesLogs().getLast().pending() ||
logs.refreshModelParamsLogs().getLast().pending() ||
logs.refreshResourceTypesLogs().getLast().pending()) {
try {
Thread.sleep(1000); // 1s
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} else {
return ModelEventLogs.fromJSON(logs);
return logs;
}
}
throw new TimeoutError("One or more mission model Hausra events did not return after " + timeout + " seconds");
Expand Down
Loading

0 comments on commit 0da0b93

Please sign in to comment.