Skip to content

Commit

Permalink
Merge pull request #2148 from reportportal/EPMRPP-98548
Browse files Browse the repository at this point in the history
EPMRPP-98548 || Add a new endpoint to get dashboard widgets configura…
  • Loading branch information
pbortnik authored Jan 20, 2025
2 parents f7bdf63 + 286d979 commit b6223c1
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.model.dashboard.DashboardConfigResource;
import com.epam.ta.reportportal.model.dashboard.DashboardResource;
import org.springframework.data.domain.Pageable;

Expand All @@ -28,25 +29,36 @@
*/
public interface GetDashboardHandler {

/**
* Get dashboard resource by provided id
*
* @param id Provided id
* @param projectDetails Project detail
* @return {@link DashboardResource}
*/
DashboardResource getDashboard(Long id, ReportPortalUser.ProjectDetails projectDetails);
/**
* Get dashboard resource by provided id
*
* @param id Provided id
* @param projectDetails Project detail
* @return {@link DashboardResource}
*/
DashboardResource getDashboard(Long id, ReportPortalUser.ProjectDetails projectDetails);

/**
* Get permitted projects for concrete user for concrete project
*
* @param projectDetails Project details
* @param user User
* @param pageable Page Details
* @param filter {@link Filter}
* @return Page of permitted dashboard resources
*/
Iterable<DashboardResource> getDashboards(ReportPortalUser.ProjectDetails projectDetails, Pageable pageable, Filter filter,
ReportPortalUser user);
/**
* Get permitted projects for concrete user for concrete project
*
* @param projectDetails Project details
* @param user User
* @param pageable Page Details
* @param filter {@link Filter}
* @return Page of permitted dashboard resources
*/
Iterable<DashboardResource> getDashboards(ReportPortalUser.ProjectDetails projectDetails,
Pageable pageable, Filter filter,
ReportPortalUser user);

/**
* Get Dashboard configuration including its widgets and filters if any
*
* @param id Dashboard id
* @param projectDetails Project details
* @return Dashboard configuration
*/
DashboardConfigResource getDashboardConfig(Long id,
ReportPortalUser.ProjectDetails projectDetails);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,61 @@

package com.epam.ta.reportportal.core.dashboard.impl;

import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.commons.ReportPortalUser.ProjectDetails;
import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.commons.querygen.ProjectFilter;
import com.epam.ta.reportportal.core.dashboard.GetDashboardHandler;
import com.epam.ta.reportportal.core.widget.WidgetConfigurationService;
import com.epam.ta.reportportal.dao.DashboardRepository;
import com.epam.ta.reportportal.entity.dashboard.Dashboard;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.model.dashboard.DashboardConfigResource;
import com.epam.ta.reportportal.model.dashboard.DashboardResource;
import com.epam.ta.reportportal.ws.converter.PagedResourcesAssembler;
import com.epam.ta.reportportal.ws.converter.converters.DashboardConverter;
import com.epam.reportportal.rules.exception.ErrorType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
* @author Pavel Bortnik
*/
@Service
@RequiredArgsConstructor
public class GetDashboardHandlerImpl implements GetDashboardHandler {

private DashboardRepository dashboardRepository;
private final DashboardRepository dashboardRepository;
private final WidgetConfigurationService widgetConfigurationService;

@Override
public Iterable<DashboardResource> getDashboards(ReportPortalUser.ProjectDetails projectDetails,
Pageable pageable, Filter filter, ReportPortalUser user) {
var dashboards = dashboardRepository.findByFilter(
ProjectFilter.of(filter, projectDetails.getProjectId()), pageable);
return PagedResourcesAssembler.pageConverter(DashboardConverter.TO_RESOURCE).apply(dashboards);
}

@Autowired
public void setDashboardRepository(DashboardRepository dashboardRepository) {
this.dashboardRepository = dashboardRepository;
}
@Override
public DashboardResource getDashboard(Long id, ReportPortalUser.ProjectDetails projectDetails) {
var dashboard = getDashboardById(id, projectDetails);
return DashboardConverter.TO_RESOURCE.apply(dashboard);
}

@Override
public Iterable<DashboardResource> getDashboards(ReportPortalUser.ProjectDetails projectDetails, Pageable pageable, Filter filter,
ReportPortalUser user) {
final Page<Dashboard> dashboards = dashboardRepository.findByFilter(ProjectFilter.of(filter, projectDetails.getProjectId()),
pageable
);
return PagedResourcesAssembler.pageConverter(DashboardConverter.TO_RESOURCE).apply(dashboards);
}
@Override
public DashboardConfigResource getDashboardConfig(Long id,
ReportPortalUser.ProjectDetails projectDetails) {
var dashboard = getDashboardById(id, projectDetails);
var widgetsConfiguration = widgetConfigurationService.getWidgetsConfiguration(
dashboard, projectDetails);
return DashboardConfigResource.builder().widgets(widgetsConfiguration).build();
}

@Override
public DashboardResource getDashboard(Long id, ReportPortalUser.ProjectDetails projectDetails) {
final Dashboard dashboard = dashboardRepository.findByIdAndProjectId(id, projectDetails.getProjectId())
.orElseThrow(() -> new ReportPortalException(ErrorType.DASHBOARD_NOT_FOUND_IN_PROJECT,
id,
projectDetails.getProjectName()
));
return DashboardConverter.TO_RESOURCE.apply(dashboard);
}
private Dashboard getDashboardById(Long id, ProjectDetails projectDetails) {
return dashboardRepository.findByIdAndProjectId(id, projectDetails.getProjectId())
.orElseThrow(() -> new ReportPortalException(ErrorType.DASHBOARD_NOT_FOUND_IN_PROJECT, id,
projectDetails.getProjectName()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2025 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.epam.ta.reportportal.core.widget;

import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser.ProjectDetails;
import com.epam.ta.reportportal.dao.WidgetRepository;
import com.epam.ta.reportportal.entity.dashboard.Dashboard;
import com.epam.ta.reportportal.model.dashboard.DashboardResource.WidgetObjectModel;
import com.epam.ta.reportportal.model.widget.WidgetConfigResource;
import com.epam.ta.reportportal.ws.converter.converters.WidgetConverter;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* @author <a href="mailto:[email protected]">Pavel Bortnik</a>
*/
@Service
@RequiredArgsConstructor
public class WidgetConfigurationService {

private final WidgetRepository widgetRepository;

public List<WidgetConfigResource> getWidgetsConfiguration(Dashboard dashboard,
ProjectDetails projectDetails) {
return dashboard.getDashboardWidgets().stream().map(WidgetConverter.TO_OBJECT_MODEL)
.map(widget -> getWidgetConfig(widget, projectDetails)).collect(Collectors.toList());
}

private WidgetConfigResource getWidgetConfig(WidgetObjectModel widgetObject,
ProjectDetails projectDetails) {
var widget = widgetRepository.findByIdAndProjectId(widgetObject.getWidgetId(),
projectDetails.getProjectId()).orElseThrow(
() -> new ReportPortalException(ErrorType.WIDGET_NOT_FOUND_IN_PROJECT,
widgetObject.getWidgetId(),
projectDetails.getProjectName()
));
return WidgetConfigResource.builder().widgetObject(widgetObject)
.widgetResource(WidgetConverter.TO_WIDGET_RESOURCE.apply(widget)).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@RestController
@RequestMapping("/v1/demo/{projectName}")
@PreAuthorize(PROJECT_MANAGER)
@Tag(name = "demo-data-controller", description = "Demo Data Controller")
@Tag(name = "Demo Data", description = "Demo data API collection")
class DemoDataController {

private final DemoDataService demoDataService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.epam.ta.reportportal.model.dashboard;

import com.epam.ta.reportportal.model.widget.WidgetConfigResource;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.Builder;
import lombok.Data;

/**
* @author <a href="mailto:[email protected]">Pavel Bortnik</a>
*/
@Data
@Builder
public class DashboardConfigResource {

@JsonProperty(value = "widgets")
private List<WidgetConfigResource> widgets;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.epam.ta.reportportal.model.widget;

import com.epam.ta.reportportal.model.dashboard.DashboardResource.WidgetObjectModel;
import lombok.Builder;
import lombok.Data;

/**
* @author <a href="mailto:[email protected]">Pavel Bortnik</a>
*/
@Data
@Builder
public class WidgetConfigResource {

private WidgetObjectModel widgetObject;
private WidgetResource widgetResource;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.epam.ta.reportportal.model.EntryCreatedRS;
import com.epam.ta.reportportal.model.dashboard.AddWidgetRq;
import com.epam.ta.reportportal.model.dashboard.CreateDashboardRQ;
import com.epam.ta.reportportal.model.dashboard.DashboardConfigResource;
import com.epam.ta.reportportal.model.dashboard.DashboardResource;
import com.epam.ta.reportportal.model.dashboard.UpdateDashboardRQ;
import com.epam.ta.reportportal.util.ProjectExtractor;
Expand Down Expand Up @@ -154,4 +155,15 @@ public DashboardResource getDashboard(@PathVariable String projectName,
return getDashboardHandler.getDashboard(
dashboardId, projectExtractor.extractProjectDetails(user, projectName));
}


@Transactional
@GetMapping(value = "/copy/{dashboardId}")
@ResponseStatus(OK)
@Operation(summary = "Get Dashboard configuration including its widgets and filters if any")
public DashboardConfigResource getDashboardConfig(@PathVariable String projectName,
@PathVariable Long dashboardId, @AuthenticationPrincipal ReportPortalUser user) {
return getDashboardHandler.getDashboardConfig(
dashboardId, projectExtractor.extractProjectDetails(user, projectName));
}
}

0 comments on commit b6223c1

Please sign in to comment.