Skip to content

Commit

Permalink
Merge pull request data-integrations#64 from cloudsufi/story/PLUGIN-1689
Browse files Browse the repository at this point in the history
ServiceNow Plugin : Make PAGE_SIZE configurable
  • Loading branch information
sgarg-CS authored Oct 31, 2023
2 parents 554fa90 + 6fab0c2 commit 7848498
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 29 deletions.
2 changes: 2 additions & 0 deletions docs/ServiceNow-batchsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ ignored if the Mode is set to `Reporting`.

**End Date**: The End date to be used to filter the data. The format must be `yyyy-MM-dd`.

**Page Size**: The number of records to fetch from ServiceNow. Default is 5000.

**Type of values**: The type of values to be returned. The type can be one of two values:

`Actual` - will fetch the actual values from the ServiceNow tables,
Expand Down
2 changes: 2 additions & 0 deletions docs/ServiceNowMultiSource-batchsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Properties

**End Date**: The End date to be used to filter the data. The format must be `yyyy-MM-dd`.

**Page Size**: The number of records to fetch from ServiceNow. Default is 5000.

**Type of values**: The type of values to be returned. The type can be one of two values:

`Actual` - will fetch the actual values from the ServiceNow tables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public class ServiceNowBaseSourceConfig extends ServiceNowBaseConfig {
"is set to `Table`.")
protected String tableNameField;

@Name(ServiceNowConstants.PROPERTY_PAGE_SIZE)
@Macro
@Nullable
@Description("The number of records to fetch from ServiceNow. Default is 5000.")
private Integer pageSize;

/**
* Constructor for ServiceNowSourceConfig object.
*
Expand All @@ -82,16 +88,18 @@ public class ServiceNowBaseSourceConfig extends ServiceNowBaseConfig {
* @param valueType The value type
* @param startDate The start date
* @param endDate The end date
* @param pageSize The page size
*/
public ServiceNowBaseSourceConfig(String referenceName, String clientId, String clientSecret, String restApiEndpoint,
String user, String password, String tableNameField, String valueType,
@Nullable String startDate, @Nullable String endDate) {
@Nullable String startDate, @Nullable String endDate, Integer pageSize) {
super(clientId, clientSecret, restApiEndpoint, user, password);
this.referenceName = referenceName;
this.tableNameField = tableNameField;
this.valueType = valueType;
this.startDate = startDate;
this.endDate = endDate;
this.pageSize = pageSize;
}

public String getReferenceName() {
Expand All @@ -112,6 +120,10 @@ public String getTableNameField() {
return Strings.isNullOrEmpty(tableNameField) ? ServiceNowConstants.TABLE_NAME_FIELD_DEFAULT : tableNameField;
}

public Integer getPageSize() {
return pageSize == null ? ServiceNowConstants.PAGE_SIZE : pageSize;
}

/**
* Validates {@link ServiceNowBaseSourceConfig} instance.
*/
Expand All @@ -120,6 +132,7 @@ public void validate(FailureCollector collector) {
IdUtils.validateReferenceName(referenceName, collector);
validateValueType(collector);
validateDateRange(collector);
validatePageSize(collector);
}

/**
Expand Down Expand Up @@ -208,6 +221,18 @@ private void validateDateRange(FailureCollector collector) {
}
}

private void validatePageSize(FailureCollector collector) {
if (containsMacro(ServiceNowConstants.PROPERTY_PAGE_SIZE)) {
return;
}

if (getPageSize() <= 0 || getPageSize() > 10000) {
collector.addFailure("Invalid Page Size.", "Page Size must be greater than 0 and " +
"less than or equal to 10000.")
.withConfigProperty(ServiceNowConstants.PROPERTY_PAGE_SIZE);
}
}

public boolean shouldGetSchema() {
return !containsMacro(ServiceNowConstants.PROPERTY_QUERY_MODE)
&& !containsMacro(ServiceNowConstants.PROPERTY_APPLICATION_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,28 @@ private static ServiceNowTableInfo getTableMetaData(String tableName, ServiceNow
@Override
public List<InputSplit> getSplits(JobContext jobContext) {
ServiceNowJobConfiguration jobConfig = new ServiceNowJobConfiguration(jobContext.getConfiguration());

int pageSize = jobConfig.getPluginConf().getPageSize().intValue();
List<ServiceNowTableInfo> tableInfos = jobConfig.getTableInfos();
List<InputSplit> resultSplits = new ArrayList<>();

for (ServiceNowTableInfo tableInfo : tableInfos) {
String tableName = tableInfo.getTableName();
int totalRecords = tableInfo.getRecordCount();
if (totalRecords <= ServiceNowConstants.PAGE_SIZE) {
if (totalRecords <= pageSize) {
// add single split for table and continue
resultSplits.add(new ServiceNowInputSplit(tableName, 0));
continue;
}

int pages = (tableInfo.getRecordCount() / ServiceNowConstants.PAGE_SIZE);
if (tableInfo.getRecordCount() % ServiceNowConstants.PAGE_SIZE > 0) {
int pages = (tableInfo.getRecordCount() / pageSize);
if (tableInfo.getRecordCount() % pageSize > 0) {
pages++;
}
int offset = 0;

for (int page = 1; page <= pages; page++) {
resultSplits.add(new ServiceNowInputSplit(tableName, offset));
offset += ServiceNowConstants.PAGE_SIZE;
offset += pageSize;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@ public static Set<String> getList(String value) {
@Override
public List<InputSplit> getSplits(JobContext jobContext) throws IOException, InterruptedException {
ServiceNowJobConfiguration jobConfig = new ServiceNowJobConfiguration(jobContext.getConfiguration());

int pageSize = jobConfig.getPluginConf().getPageSize().intValue();
List<ServiceNowTableInfo> tableInfos = jobConfig.getTableInfos();
List<InputSplit> resultSplits = new ArrayList<>();

for (ServiceNowTableInfo tableInfo : tableInfos) {
String tableName = tableInfo.getTableName();
int totalRecords = tableInfo.getRecordCount();

int pages = (totalRecords / ServiceNowConstants.PAGE_SIZE);
if (totalRecords % ServiceNowConstants.PAGE_SIZE > 0) {
int pages = (totalRecords / pageSize);
if (totalRecords % pageSize > 0) {
pages++;
}
int offset = 0;

for (int page = 1; page <= pages; page++) {
resultSplits.add(new ServiceNowInputSplit(tableName, offset));
offset += ServiceNowConstants.PAGE_SIZE;
offset += pageSize;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void fetchData() throws IOException {
results = restApi.fetchTableRecordsRetryableMode(tableName, multiSourcePluginConf.getValueType(),
multiSourcePluginConf.getStartDate(),
multiSourcePluginConf.getEndDate(), split.getOffset(),
ServiceNowConstants.PAGE_SIZE);
multiSourcePluginConf.getPageSize());

iterator = results.iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public class ServiceNowMultiSourceConfig extends ServiceNowBaseSourceConfig {
* @param valueType The value type
* @param startDate The start date
* @param endDate The end date
* @param pageSize The page size
*/
public ServiceNowMultiSourceConfig(String referenceName, String clientId, String clientSecret, String restApiEndpoint,
String user, String password, String tableNameField, String valueType,
@Nullable String startDate, @Nullable String endDate, String tableNames) {
@Nullable String startDate, @Nullable String endDate, Integer pageSize,
String tableNames) {
super(referenceName, clientId, clientSecret, restApiEndpoint, user, password, tableNameField, valueType, startDate,
endDate);
endDate, pageSize);
this.tableNames = tableNames;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void fetchData() throws IOException {
// Get the table data
results = restApi.fetchTableRecordsRetryableMode(tableName, pluginConf.getValueType(), pluginConf.getStartDate(),
pluginConf.getEndDate(), split.getOffset(),
ServiceNowConstants.PAGE_SIZE);
pluginConf.getPageSize());
LOG.debug("Results size={}", results.size());

iterator = results.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class ServiceNowSourceConfig extends ServiceNowBaseSourceConfig {
"will be ignored if the Mode is set to `Reporting`. The ServiceNow table can be browsed using browse button.")
private String tableName;


/**
* Constructor for ServiceNowSourceConfig object.
*
Expand All @@ -74,13 +73,15 @@ public class ServiceNowSourceConfig extends ServiceNowBaseSourceConfig {
* @param valueType The value type
* @param startDate The start date
* @param endDate The end date
* @param pageSize The page size
*/
public ServiceNowSourceConfig(String referenceName, String queryMode, @Nullable String applicationName,
@Nullable String tableNameField, @Nullable String tableName, String clientId,
String clientSecret, String restApiEndpoint, String user, String password,
String valueType, @Nullable String startDate, @Nullable String endDate) {
String valueType, @Nullable String startDate, @Nullable String endDate,
Integer pageSize) {
super(referenceName, clientId, clientSecret, restApiEndpoint, user, password, tableNameField, valueType, startDate,
endDate);
endDate, pageSize);
this.referenceName = referenceName;
this.queryMode = queryMode;
this.applicationName = applicationName;
Expand Down Expand Up @@ -152,7 +153,7 @@ public SourceApplication getApplicationName() {
public String getTableName() {
return tableName;
}

/**
* Validates {@link ServiceNowSourceConfig} instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public interface ServiceNowConstants {
*/
String PROPERTY_OPERATION = "operation";

/**
* Configuration property name used to specify the page size.
*/
String PROPERTY_PAGE_SIZE = "pageSize";

/**
* Configuration property name used to get the schema.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public void testFetchDataOnInvalidTable() throws IOException, OAuthProblemExcept
.setValueType("Actual")
.setStartDate("2021-01-01")
.setEndDate("2022-02-18")
.setPageSize(10)
.setTableNameField("tablename")
.buildMultiSource();

Expand All @@ -186,7 +187,7 @@ public void testFetchDataOnInvalidTable() throws IOException, OAuthProblemExcept
restApi.fetchTableRecords(tableName, serviceNowMultiSourceConfig.getValueType(),
serviceNowMultiSourceConfig.getStartDate(), serviceNowMultiSourceConfig.getEndDate(),
split.getOffset(),
ServiceNowConstants.PAGE_SIZE);
serviceNowMultiSourceConfig.getPageSize());

ServiceNowTableDataResponse response = new ServiceNowTableDataResponse();
response.setResult(results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ServiceNowMultiSourceConfigTest {
public void testConstructor() {
serviceNowMultiSourceConfig = new ServiceNowMultiSourceConfig("referenceName", "client_id",
"client_secret", "https://example.com", "user", "password",
"tablename", "Actual", "2021-12-30", "2021-12-31",
"tablename", "Actual", "2021-12-30", "2021-12-31", 10,
"sys_user");
Assert.assertEquals("sys_user", serviceNowMultiSourceConfig.getTableNames());
Assert.assertEquals("Actual", serviceNowMultiSourceConfig.getValueType().getValueType());
Expand Down Expand Up @@ -426,7 +426,8 @@ public void testValidateTableNames() {
ServiceNowMultiSourceConfig serviceNowMultiSourceConfig = new ServiceNowMultiSourceConfig(
"Reference Name", "42", "client_secret",
"https://config.us-east-2.amazonaws.com", "User", "password",
"tablename", "Actual", "2020-03-01", "2020-03-01", ",");
"tablename", "Actual", "2020-03-01", "2020-03-01", 10,
",");
serviceNowMultiSourceConfig.validateTableNames(new MockFailureCollector("Stage Name"));
Assert.assertEquals("42", serviceNowMultiSourceConfig.getConnection().getClientId());
Assert.assertEquals("tablename", serviceNowMultiSourceConfig.tableNameField);
Expand All @@ -446,7 +447,7 @@ public void testValidateTableNames2() {
ServiceNowMultiSourceConfig serviceNowMultiSourceConfig = new ServiceNowMultiSourceConfig(
"Reference Name", "Table Name Field", "42", "Client Secret",
"https://config.us-east-2.amazonaws.com", "User", "password", "42",
"2020-03-01", "2020-03-01", "");
"2020-03-01", "2020-03-01", 10, "");
MockFailureCollector mockFailureCollector = new MockFailureCollector("Stage Name");
serviceNowMultiSourceConfig.validateTableNames(mockFailureCollector);
List<ValidationFailure> validationFailures = mockFailureCollector.getValidationFailures();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void initializeTests() {
.setValueType("Actual")
.setStartDate("2021-12-30")
.setEndDate("2021-12-31")
.setPageSize(10)
.setTableNameField("tablename")
.build();

Expand All @@ -98,8 +99,9 @@ public void testConstructor2() throws IOException {
"https://ven05127." +
"service-now.com/", "User",
"password",
"Actual", "2021-12-30",
"2021-12-31");
"Actual",
"2021-12-30",
"2021-12-31", 10);

serviceNowRecordReader.close();
Assert.assertEquals(0, serviceNowRecordReader.pos);
Expand Down Expand Up @@ -182,7 +184,7 @@ public void testFetchData() throws Exception {
Mockito.when(restApi.fetchTableRecordsRetryableMode(tableName, serviceNowSourceConfig.getValueType(),
serviceNowSourceConfig.getStartDate(), serviceNowSourceConfig.
getEndDate(), split.getOffset(),
ServiceNowConstants.PAGE_SIZE)).thenReturn(results);
serviceNowSourceConfig.getPageSize())).thenReturn(results);
Mockito.when(restApi.fetchTableSchema(tableName))
.thenReturn(Schema.recordOf(Schema.Field.of("calendar_integration", Schema.of(Schema.Type.STRING))));
serviceNowRecordReader.initialize(split, null);
Expand Down Expand Up @@ -236,7 +238,7 @@ public void testFetchDataReportingMode() throws Exception {
Mockito.when(restApi.fetchTableRecordsRetryableMode(tableName, serviceNowSourceConfig.getValueType(),
serviceNowSourceConfig.getStartDate(),
serviceNowSourceConfig.getEndDate(), split.getOffset(),
ServiceNowConstants.PAGE_SIZE)).thenReturn(results);
serviceNowSourceConfig.getPageSize())).thenReturn(results);
Mockito.when(restApi.fetchTableSchema(tableName))
.thenReturn(Schema.recordOf(Schema.Field.of("calendar_integration", Schema.of(Schema.Type.STRING))));
serviceNowRecordReader.initialize(split, null);
Expand Down Expand Up @@ -269,7 +271,7 @@ public void testFetchDataOnInvalidTable() throws Exception {
Mockito.when(restApi.fetchTableRecords(tableName, serviceNowSourceConfig.getValueType(),
serviceNowSourceConfig.getStartDate(), serviceNowSourceConfig.getEndDate(),
split.getOffset(),
ServiceNowConstants.PAGE_SIZE)).thenReturn(results);
serviceNowSourceConfig.getPageSize())).thenReturn(results);
ServiceNowTableDataResponse response = new ServiceNowTableDataResponse();
response.setResult(results);
Mockito.when(restApi.fetchTableSchema(tableName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ The name of the ServiceNow table(s) from which data to be fetched.
private String valueType = "Actual";
private String startDate = "";
private String endDate = "";
private Integer pageSize = 5000;


public ConfigBuilder setReferenceName(String referenceName) {
this.referenceName = referenceName;
Expand Down Expand Up @@ -129,14 +131,19 @@ public ConfigBuilder setEndDate(String endDate) {
return this;
}

public ConfigBuilder setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}

public ServiceNowSourceConfig build() {
return new ServiceNowSourceConfig(referenceName, queryMode, applicationName, tableNameField, tableName,
clientId, clientSecret, restApiEndpoint, user, password, valueType, startDate, endDate);
clientId, clientSecret, restApiEndpoint, user, password, valueType, startDate, endDate, pageSize);
}

public ServiceNowMultiSourceConfig buildMultiSource() {
return new ServiceNowMultiSourceConfig(referenceName, clientId, clientSecret, restApiEndpoint, user, password,
tableNameField, valueType, startDate, endDate, tableNames);
tableNameField, valueType, startDate, endDate, pageSize, tableNames);
}


Expand Down
9 changes: 9 additions & 0 deletions widgets/ServiceNow-batchsource.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@
"widget-attributes" : {
"placeholder": "End date to be used to filter the data"
}
},
{
"widget-type": "number",
"label": "Page Size",
"name": "pageSize",
"widget-attributes" : {
"placeholder": "Number of records to fetch from ServiceNow",
"default": 5000
}
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions widgets/ServiceNowMultiSource-batchsource.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@
"placeholder": "End date to be used to filter the data"
}
},
{
"widget-type": "number",
"label": "Page Size",
"name": "pageSize",
"widget-attributes" : {
"placeholder": "Number of records to fetch from ServiceNow",
"default": 5000
}
},
{
"widget-type": "textbox",
"label": "Table Name Field",
Expand Down

0 comments on commit 7848498

Please sign in to comment.