Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #274 from dcanar9/master
Browse files Browse the repository at this point in the history
Widget Cleanup Fix
  • Loading branch information
nameisaravind authored May 10, 2022
2 parents 6e20713 + e0a34f6 commit 434f545
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>api</artifactId>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<version>3.4.35</version>
<version>3.4.36</version>
<description>Hygieia Rest API Layer</description>
<url>https://github.com/Hygieia/api</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public ResponseEntity<List<Dashboard>> myDashboardByTitlePage(@RequestParam(valu
@RequestMapping(value = "/dashboard/removeWidgetDuplicates", method = DELETE)
public ResponseEntity<String> removeWidgetDuplicates(@RequestParam(value="title", required = false)String title,
@RequestParam(value="dryRun", required = true) boolean dryRun){
String message = dashboardService.removeWidgetDuplicates(title, dryRun);
String message = dashboardService.removeWidgetDuplicatesHelper(title, dryRun);
return ResponseEntity.ok().body(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ public interface DashboardService {

Iterable<Dashboard> allTemplate(String template);

String removeWidgetDuplicates(String title, boolean dryRun);
void removeWidgetDuplicates(List<Dashboard> dashboards, boolean dryRun);

String removeWidgetDuplicatesHelper(String title, boolean dryRun);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -1006,16 +1007,44 @@ public Dashboard updateScoreSettings(ObjectId dashboardId, boolean scoreEnabled,
}

@Override
public String removeWidgetDuplicates(String title, boolean dryRun) {
public String removeWidgetDuplicatesHelper(String title, boolean dryRun){
// get page and clean until there are no more pages
if(StringUtils.isEmpty(title)){
Pageable pageable = new PageRequest(0, settings.getBatchSize());
Page<Dashboard> page = findDashboardsByPage("", pageable);

List<Dashboard> dashboards;
if (StringUtils.isEmpty(title)){
dashboards = (List<Dashboard>) all();
while(page.hasNext()){
pageable = pageable.next();
removeWidgetDuplicates(page.getContent(), dryRun);
page = findDashboardsByPage("", pageable);
}
}
else{
dashboards = dashboardRepository.findByTitle(title);
List<Dashboard> dashboards = dashboardRepository.findByTitle(title);
if (CollectionUtils.isEmpty(dashboards)){return "No dashboards with that title";}
removeWidgetDuplicates(dashboards, dryRun);
}

// messages upon success
if(StringUtils.isEmpty(title)){
if(dryRun){
return "DRY_RUN: All Dashboard widgets cleaned";
} else{
return "All Dashboard widgets cleaned";
}
}
else {
if(dryRun){
return "DRY_RUN: Cleaned widgets for dashboard " + title;
} else{
return "Cleaned widgets for dashboard " + title;
}
}

}

@Override
public void removeWidgetDuplicates(List<Dashboard> dashboards, boolean dryRun) {

for (Dashboard dashboard : dashboards) {
List<Widget> nonDuplicates = new ArrayList<Widget>();
Expand All @@ -1039,19 +1068,6 @@ public String removeWidgetDuplicates(String title, boolean dryRun) {
" widgets simplified to " + nonDuplicates.stream().map(Widget::getName).collect(Collectors.toList()));
}

if(StringUtils.isEmpty(title)){
if(dryRun){
return "DRY_RUN: All Dashboard widgets cleaned";
} else{
return "All Dashboard widgets cleaned";
}
}
else {
if(dryRun){
return "DRY_RUN: Cleaned widgets for dashboard " + title;
} else{
return "Cleaned widgets for dashboard " + title;
}
}

}
}
11 changes: 11 additions & 0 deletions src/main/java/com/capitalone/dashboard/settings/ApiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public class ApiSettings {
@Value("${optimizeUserCallsToGithub:true}")
private boolean optimizeUserCallsToGithub;

@Value("${batchSize:500}")
private int batchSize;

private String hygieia_ui_url="";

public Map<String, String> getFunctional() {
Expand Down Expand Up @@ -267,4 +270,12 @@ public boolean isOptimizeUserCallsToGithub() {
public void setOptimizeUserCallsToGithub(boolean optimizeUserCallsToGithub) {
this.optimizeUserCallsToGithub = optimizeUserCallsToGithub;
}

public int getBatchSize() {
return batchSize;
}

public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ public void removeWidgetDuplicates(){
assertThat(dashboard.getWidgets().size(), is(4));

when(dashboardRepository.findByTitle(dashboard.getTitle())).thenReturn(Collections.singletonList(dashboard));
dashboardService.removeWidgetDuplicates(dashboard.getTitle(), false);
dashboardService.removeWidgetDuplicatesHelper(dashboard.getTitle(), false);
assertThat(dashboard.getWidgets().size(), is(2));
}

Expand Down

0 comments on commit 434f545

Please sign in to comment.