Skip to content

Commit

Permalink
[Connector API] Allow for partial updates in update last sync stats e…
Browse files Browse the repository at this point in the history
…ndpoint (#108955)
  • Loading branch information
jedrazb committed May 24, 2024
1 parent 2900e03 commit 0d404d9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ setup:
- match: { last_sync_error: "oh no error" }
- match: { last_access_control_sync_scheduled_at: "2023-05-25T12:30:00.000Z" }

---
"Update Connector Last Sync Stats - Supports different partial updates":
- do:
connector.last_sync:
connector_id: test-connector
body:
last_deleted_document_count: 43

- match: { result: updated }

- do:
connector.last_sync:
connector_id: test-connector
body:
last_indexed_document_count: 42

- match: { result: updated }


- do:
connector.get:
connector_id: test-connector

- match: { last_deleted_document_count: 43 }
- match: { last_indexed_document_count: 42 }


---
"Update Connector Last Sync Stats - Connector doesn't exist":
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,78 @@ public ConnectorSyncInfo(StreamInput in) throws IOException {
public static final ParseField LAST_SYNC_STATUS_FIELD = new ParseField("last_sync_status");
public static final ParseField LAST_SYNCED_FIELD = new ParseField("last_synced");

public String getLastAccessControlSyncError() {
return lastAccessControlSyncError;
}

public Instant getLastAccessControlSyncScheduledAt() {
return lastAccessControlSyncScheduledAt;
}

public ConnectorSyncStatus getLastAccessControlSyncStatus() {
return lastAccessControlSyncStatus;
}

public Long getLastDeletedDocumentCount() {
return lastDeletedDocumentCount;
}

public Instant getLastIncrementalSyncScheduledAt() {
return lastIncrementalSyncScheduledAt;
}

public Long getLastIndexedDocumentCount() {
return lastIndexedDocumentCount;
}

public String getLastSyncError() {
return lastSyncError;
}

public Instant getLastSyncScheduledAt() {
return lastSyncScheduledAt;
}

public ConnectorSyncStatus getLastSyncStatus() {
return lastSyncStatus;
}

public Instant getLastSynced() {
return lastSynced;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(LAST_ACCESS_CONTROL_SYNC_ERROR.getPreferredName(), lastAccessControlSyncError);
builder.field(LAST_ACCESS_CONTROL_SYNC_STATUS_FIELD.getPreferredName(), lastAccessControlSyncStatus);
builder.field(LAST_ACCESS_CONTROL_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastAccessControlSyncScheduledAt);
if (lastAccessControlSyncError != null) {
builder.field(LAST_ACCESS_CONTROL_SYNC_ERROR.getPreferredName(), lastAccessControlSyncError);
}
if (lastAccessControlSyncStatus != null) {
builder.field(LAST_ACCESS_CONTROL_SYNC_STATUS_FIELD.getPreferredName(), lastAccessControlSyncStatus);
}
if (lastAccessControlSyncScheduledAt != null) {
builder.field(LAST_ACCESS_CONTROL_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastAccessControlSyncScheduledAt);
}
if (lastDeletedDocumentCount != null) {
builder.field(LAST_DELETED_DOCUMENT_COUNT_FIELD.getPreferredName(), lastDeletedDocumentCount);
}
builder.field(LAST_INCREMENTAL_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastIncrementalSyncScheduledAt);
if (lastIncrementalSyncScheduledAt != null) {
builder.field(LAST_INCREMENTAL_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastIncrementalSyncScheduledAt);
}
if (lastIndexedDocumentCount != null) {
builder.field(LAST_INDEXED_DOCUMENT_COUNT_FIELD.getPreferredName(), lastIndexedDocumentCount);
}
builder.field(LAST_SYNC_ERROR_FIELD.getPreferredName(), lastSyncError);
builder.field(LAST_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastSyncScheduledAt);
builder.field(LAST_SYNC_STATUS_FIELD.getPreferredName(), lastSyncStatus);
builder.field(LAST_SYNCED_FIELD.getPreferredName(), lastSynced);
if (lastSyncError != null) {
builder.field(LAST_SYNC_ERROR_FIELD.getPreferredName(), lastSyncError);
}
if (lastSyncScheduledAt != null) {
builder.field(LAST_SYNC_SCHEDULED_AT_FIELD.getPreferredName(), lastSyncScheduledAt);
}
if (lastSyncStatus != null) {
builder.field(LAST_SYNC_STATUS_FIELD.getPreferredName(), lastSyncStatus);
}
if (lastSynced != null) {
builder.field(LAST_SYNCED_FIELD.getPreferredName(), lastSynced);
}
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,48 @@ public void testUpdateConnectorLastSyncStats() throws Exception {
assertThat(syncStats, equalTo(indexedConnector.getSyncInfo()));
}

public void testUpdateConnectorLastSyncStats_withPartialUpdate() throws Exception {
Connector connector = ConnectorTestUtils.getRandomConnector();
String connectorId = randomUUID();

ConnectorCreateActionResponse resp = awaitCreateConnector(connectorId, connector);
assertThat(resp.status(), anyOf(equalTo(RestStatus.CREATED), equalTo(RestStatus.OK)));

ConnectorSyncInfo syncStats = new ConnectorSyncInfo.Builder().setLastSyncError(randomAlphaOfLengthBetween(5, 10))
.setLastIndexedDocumentCount(randomLong())
.setLastDeletedDocumentCount(randomLong())
.build();

UpdateConnectorLastSyncStatsAction.Request lastSyncStats = new UpdateConnectorLastSyncStatsAction.Request(connectorId, syncStats);

DocWriteResponse updateResponse = awaitUpdateConnectorLastSyncStats(lastSyncStats);
assertThat(updateResponse.status(), equalTo(RestStatus.OK));

Connector indexedConnector = awaitGetConnector(connectorId);

// Check fields from the partial update of last sync stats
assertThat(syncStats.getLastSyncError(), equalTo(indexedConnector.getSyncInfo().getLastSyncError()));
assertThat(syncStats.getLastDeletedDocumentCount(), equalTo(indexedConnector.getSyncInfo().getLastDeletedDocumentCount()));
assertThat(syncStats.getLastIndexedDocumentCount(), equalTo(indexedConnector.getSyncInfo().getLastIndexedDocumentCount()));

ConnectorSyncInfo nextSyncStats = new ConnectorSyncInfo.Builder().setLastIndexedDocumentCount(randomLong()).build();

lastSyncStats = new UpdateConnectorLastSyncStatsAction.Request(connectorId, nextSyncStats);

updateResponse = awaitUpdateConnectorLastSyncStats(lastSyncStats);
assertThat(updateResponse.status(), equalTo(RestStatus.OK));

indexedConnector = awaitGetConnector(connectorId);

// Check fields from the partial update of last sync stats
assertThat(nextSyncStats.getLastIndexedDocumentCount(), equalTo(indexedConnector.getSyncInfo().getLastIndexedDocumentCount()));

// Check that other fields remained unchanged
assertThat(syncStats.getLastSyncError(), equalTo(indexedConnector.getSyncInfo().getLastSyncError()));
assertThat(syncStats.getLastDeletedDocumentCount(), equalTo(indexedConnector.getSyncInfo().getLastDeletedDocumentCount()));

}

public void testUpdateConnectorScheduling() throws Exception {
Connector connector = ConnectorTestUtils.getRandomConnector();
String connectorId = randomUUID();
Expand Down

0 comments on commit 0d404d9

Please sign in to comment.