Skip to content

Commit

Permalink
add lastOpenAt in Query (#2878)
Browse files Browse the repository at this point in the history
  • Loading branch information
YannanGao-gs authored Jun 12, 2024
1 parent e69054f commit 678c0fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -298,7 +299,7 @@ else if (count == 0)
return matchingQueries;
}

public Query getQuery(String queryId)
public Query getQuery(String queryId) throws JsonProcessingException
{
List<Query> matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", queryId)), this::documentToQuery).toList();
if (matchingQueries.size() > 1)
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Query
public String content;
public Long lastUpdatedAt;
public Long createdAt;
public Long lastOpenAt;

public List<TaggedValue> taggedValues;
public List<StereotypePtr> stereotypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,18 +576,24 @@ 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());
Assert.assertEquals("booleanParam1", query4.defaultParameterValues.get(0).name);
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);
}


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 678c0fd

Please sign in to comment.