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 #272 from dcanar9/master
Browse files Browse the repository at this point in the history
Removed if statement that prevented from checking if a widget already exists
  • Loading branch information
nameisaravind authored May 5, 2022
2 parents 51febdb + 393cb38 commit 6e20713
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 11 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.34</version>
<version>3.4.35</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 @@ -481,4 +481,11 @@ public ResponseEntity<List<Dashboard>> myDashboardByTitlePage(@RequestParam(valu
.headers(paginationHeaderUtility.buildPaginationHeaders(pageDashboardItems))
.body(pageDashboardItems.getContent());
}

@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);
return ResponseEntity.ok().body(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,12 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)

component = dashboardService.associateCollectorToComponent(dashboard.getApplication().getComponents().get(0).getId(), widgetRequest.getCollectorItemIds(),component,true);
Widget newWidget = widgetRequest.widget();
if (isUpdate) {
Widget oldWidget = existingWidgets.get(newWidget.getName());
if (oldWidget == null) {
dashboardService.addWidget(dashboard, newWidget);
} else {
Widget widget = widgetRequest.updateWidget(dashboardService.getWidget(dashboard, oldWidget.getId()));
dashboardService.updateWidget(dashboard, widget);
}
} else {
Widget oldWidget = existingWidgets.get(newWidget.getName());
if (oldWidget == null) {
dashboardService.addWidget(dashboard, newWidget);
} else {
Widget widget = widgetRequest.updateWidget(dashboardService.getWidget(dashboard, oldWidget.getId()));
dashboardService.updateWidget(dashboard, widget);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public interface DashboardService {
Dashboard updateScoreSettings(ObjectId dashboardId, boolean scoreEnabled, ScoreDisplayType scoreDisplay);

Iterable<Dashboard> allTemplate(String template);

String removeWidgetDuplicates(String title, boolean dryRun);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,53 @@ public Dashboard updateScoreSettings(ObjectId dashboardId, boolean scoreEnabled,
return savedDashboard;
}

@Override
public String removeWidgetDuplicates(String title, boolean dryRun) {

List<Dashboard> dashboards;
if (StringUtils.isEmpty(title)){
dashboards = (List<Dashboard>) all();
}
else{
dashboards = dashboardRepository.findByTitle(title);
if (CollectionUtils.isEmpty(dashboards)){return "No dashboards with that title";}
}

for (Dashboard dashboard : dashboards) {
List<Widget> nonDuplicates = new ArrayList<Widget>();
List<Widget> dashWidgets = dashboard.getWidgets();

// loop through existing widgets, if the widget is not in the nonDuplicates list add it
for (Widget widget : dashWidgets) {
if (nonDuplicates.stream().noneMatch(w -> w.getName().equalsIgnoreCase(widget.getName()))) {
nonDuplicates.add(widget);
}
}

if(!dryRun){
dashboard.setWidgets(nonDuplicates);
dashboard.setUpdatedAt(System.currentTimeMillis());
dashboardRepository.save(dashboard);
}

// Logs number of original widgets along with contents of new widget array
LOG.info("Removing duplicates from dashboard " + dashboard.getTitle() + ": " + dashWidgets.size() +
" 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ public void updateScoreSettings() throws Exception {
.andExpect(status().isOk());
}


private DashboardRequestTitle makeDashboardRequestTitle(String title) {
DashboardRequestTitle request = new DashboardRequestTitle();
request.setTitle(title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.capitalone.dashboard.model.CollectorType;
import com.capitalone.dashboard.model.Owner;
import com.capitalone.dashboard.model.AuthType;
import com.capitalone.dashboard.model.Widget;
import com.capitalone.dashboard.repository.CollectorItemRepository;
import com.capitalone.dashboard.repository.DashboardRepository;
import com.capitalone.dashboard.repository.UserInfoRepository;
Expand Down Expand Up @@ -301,6 +302,36 @@ public void remoteCreateBuild() throws HygieiaException, IOException {
assertEquals(1, component.getCollectorItems().get(CollectorType.Build).size());
assertEquals(2, dashboard.get(0).getOwners().size());
}

@Test
public void remoteCreateWithoutDuplicateWidgets() throws IOException, HygieiaException {

for(int i = 0; i < 2; i++){
// Creating codeRepoEntry to add to the request
List<DashboardRemoteRequest.CodeRepoEntry> entries = new ArrayList<>();
DashboardRemoteRequest.CodeRepoEntry entry = new DashboardRemoteRequest.CodeRepoEntry();
entry.setToolName("GitHub");
Map options = new HashMap();
if (i==0) {
options.put("url", "http://git.test.com/capone/better.git");
options.put("branch", "master");
}else{
options.put("url", "http://git.test.com/captwo/best.git");
options.put("branch", "main");
}
entry.setOptions(options);
entries.add(entry);

// create remote request with repo and call remote create
DashboardRemoteRequest request = getRemoteRequest("./dashboardRemoteRequests/Remote-Request-Base.json");
request.getMetaData().setTitle("dupWidgetTest");
request.setCodeRepoEntries(entries);
Dashboard dash = dashboardRemoteService.remoteCreate(request, false);
assertEquals(1, dash.getWidgets().size());
assertEquals("repo", dash.getWidgets().get(0).getName());
}
}

@Test
public void remoteUpdateNonExisting() throws IOException {
DashboardRemoteRequest request = getRemoteRequest("./dashboardRemoteRequests/Remote-Request-Base.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,25 @@ public void compareMapsTest() {
Assert.assertTrue(result);
}

@Test
public void removeWidgetDuplicates(){
Dashboard dashboard = makeTeamDashboard("template", "title", "appName", "Test",configItemBusServName,configItemBusAppName, "comp1");
Widget widget = new Widget();
Widget widget2 = new Widget();

widget.setName("build");
dashboard.getWidgets().add(widget);
dashboard.getWidgets().add(widget);
widget2.setName("repo");
dashboard.getWidgets().add(widget2);
dashboard.getWidgets().add(widget2);
assertThat(dashboard.getWidgets().size(), is(4));

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

private Dashboard makeTeamDashboard(String template, String title, String appName, String owner,String configItemBusServName,String configItemBusAppName, String... compNames) {
Application app = new Application(appName);
for (String compName : compNames) {
Expand Down

0 comments on commit 6e20713

Please sign in to comment.