diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java index 08828556405..ee565316df8 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java @@ -23,6 +23,7 @@ import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.Field; import com.mongodb.client.model.Filters; +import com.mongodb.client.model.Updates; import com.mongodb.client.model.Projections; import com.mongodb.client.model.Sorts; import org.bson.Document; @@ -298,7 +299,7 @@ else if (count == 0) return matchingQueries; } - public Query getQuery(String queryId) + public Query getQuery(String queryId) throws JsonProcessingException { List matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", queryId)), this::documentToQuery).toList(); if (matchingQueries.size() > 1) @@ -309,7 +310,13 @@ else if (matchingQueries.size() == 0) { throw new ApplicationQueryException("Can't find query with ID '" + queryId + "'", Response.Status.NOT_FOUND); } - return matchingQueries.get(0); + Query query = matchingQueries.get(0); + query.lastOpenAt = Instant.now().toEpochMilli(); + this.getQueryCollection().updateOne( + Filters.eq("id", queryId), + Updates.set("lastOpenAt", Instant.now().toEpochMilli()) + ); + return query; } public QueryStoreStats getQueryStoreStats() throws JsonProcessingException @@ -339,6 +346,7 @@ public Query createQuery(Query query, String currentUser) throws JsonProcessingE } query.createdAt = Instant.now().toEpochMilli(); query.lastUpdatedAt = query.createdAt; + query.lastOpenAt = query.createdAt; this.getQueryCollection().insertOne(queryToDocument(query)); QueryEvent createdEvent = createEvent(query.id, QueryEvent.QueryEventType.CREATED); createdEvent.timestamp = query.createdAt; @@ -374,6 +382,7 @@ else if (matchingQueries.size() == 0) query.owner = currentUser; query.createdAt = currentQuery.createdAt; query.lastUpdatedAt = Instant.now().toEpochMilli(); + query.lastOpenAt = Instant.now().toEpochMilli(); query.originalVersionId = currentQuery.originalVersionId; this.getQueryCollection().findOneAndReplace(Filters.eq("id", queryId), queryToDocument(query)); QueryEvent updatedEvent = createEvent(query.id, QueryEvent.QueryEventType.UPDATED); @@ -411,6 +420,7 @@ public Query patchQuery(String queryId, Query updatedQuery, String currentUser) } currentQuery.owner = currentUser; currentQuery.lastUpdatedAt = Instant.now().toEpochMilli(); + currentQuery.lastOpenAt = Instant.now().toEpochMilli(); this.getQueryCollection().findOneAndReplace(Filters.eq("id", queryId), queryToDocument(currentQuery)); QueryEvent updatedEvent = createEvent(queryId, QueryEvent.QueryEventType.UPDATED); updatedEvent.timestamp = currentQuery.lastUpdatedAt; diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java index ac80f3f2f89..2e2c0e50fae 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java @@ -39,6 +39,7 @@ public class Query public String content; public Long lastUpdatedAt; public Long createdAt; + public Long lastOpenAt; public List taggedValues; public List stereotypes; diff --git a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java index 2eabf5c4c79..205bfebf51a 100644 --- a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java +++ b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java @@ -576,11 +576,15 @@ public void testGetQueriesWithParameterValues() throws Exception Query query1 = queryStoreManager.getQuery("1"); Assert.assertEquals(0, query1.defaultParameterValues.size()); + Assert.assertNotNull(query1.lastOpenAt); + Assert.assertNotEquals(query1.lastOpenAt, query1.createdAt); Query query2 = queryStoreManager.getQuery("2"); Assert.assertEquals(1, query2.defaultParameterValues.size()); Assert.assertEquals("booleanParam1", query2.defaultParameterValues.get(0).name); Assert.assertEquals("true", query2.defaultParameterValues.get(0).content); + Assert.assertNotNull(query2.lastOpenAt); + Assert.assertNotEquals(query2.lastOpenAt, query2.createdAt); Query query4 = queryStoreManager.getQuery("4"); Assert.assertEquals(3, query4.defaultParameterValues.size()); @@ -588,6 +592,8 @@ public void testGetQueriesWithParameterValues() throws Exception Assert.assertEquals("stringParam2", query4.defaultParameterValues.get(1).name); Assert.assertEquals("myListParam3", query4.defaultParameterValues.get(2).name); Assert.assertEquals("['d','a']", query4.defaultParameterValues.get(2).content); + Assert.assertNotNull(query4.lastOpenAt); + Assert.assertNotEquals(query4.lastOpenAt, query4.createdAt); } @@ -735,7 +741,7 @@ public void testCreateSimpleQuery() throws Exception Assert.assertEquals(0, createdQuery.taggedValues.size()); Assert.assertNotNull(createdQuery.createdAt); Assert.assertEquals(createdQuery.createdAt, createdQuery.lastUpdatedAt); - + Assert.assertEquals(createdQuery.lastOpenAt, createdQuery.lastUpdatedAt); } @Test