Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import ai.startree.thirdeye.spi.api.AlertApi;
import ai.startree.thirdeye.spi.api.AlertEvaluationApi;
import ai.startree.thirdeye.spi.api.AlertInsightsApi;
import ai.startree.thirdeye.spi.api.AlertInsightsRequestApi;
import ai.startree.thirdeye.spi.api.AlertTemplateApi;
import ai.startree.thirdeye.spi.api.AnomalyApi;
import ai.startree.thirdeye.spi.api.AnomalyFeedbackApi;
Expand Down Expand Up @@ -176,6 +177,7 @@ public HappyPathTest(final PinotVersion pinotVersion) {
private long anomalyId;
private long alertLastUpdateTime;
private long alertId;
private AlertApi alertApi;
private long namespaceConfigurationId;

@BeforeClass
Expand Down Expand Up @@ -335,7 +337,8 @@ public void testCreateAlert() {

assert200(response);
final List<AlertApi> alerts = response.readEntity(ALERT_LIST_TYPE);
alertId = alerts.getFirst().getId();
alertApi = alerts.getFirst();
alertId = alertApi.getId();
UPDATE_ALERT_API.setId(alertId);
}

Expand Down Expand Up @@ -387,8 +390,24 @@ public void testEvaluateAllHappyPathAlertsOnEmptyTimeframe(final String alertJso
}

@Test(dependsOnMethods = "testCreateAlert")
public void testAlertInsights() {
final Response response = request("api/alerts/" + alertId + "/insights").get();
public void testAlertInsightsWithId() {
final AlertApi alertApi = new AlertApi().setId(alertId);
final Response response = request("api/alerts/insights").post(
Entity.json(new AlertInsightsRequestApi().setAlert(alertApi)));
assert200(response);
final AlertInsightsApi insights = response.readEntity(AlertInsightsApi.class);
assertThat(insights.getTemplateWithProperties().getMetadata().getGranularity()).isEqualTo(
"P1D");
assertThat(insights.getDefaultStartTime()).isEqualTo(PAGEVIEWS_DATASET_START_TIME_PLUS_ONE_DAY);
assertThat(insights.getDefaultEndTime()).isEqualTo(PAGEVIEWS_DATASET_END_TIME_PLUS_ONE_DAY);
assertThat(insights.getDatasetStartTime()).isEqualTo(PAGEVIEWS_DATASET_START_TIME);
assertThat(insights.getDatasetEndTime()).isEqualTo(PAGEVIEWS_DATASET_END_TIME);
}

@Test(dependsOnMethods = "testCreateAlert")
public void testAlertInsightsWithoutId() {
final Response response = request("api/alerts/insights").post(
Entity.json(new AlertInsightsRequestApi().setAlert(alertApi)));
assert200(response);
final AlertInsightsApi insights = response.readEntity(AlertInsightsApi.class);
assertThat(insights.getTemplateWithProperties().getMetadata().getGranularity()).isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ public AlertResource(final AlertService alertService) {
this.alertService = alertService;
}

@Path("{id}/insights")
@GET
@Timed(percentiles = {0.5, 0.75, 0.90, 0.95, 0.98, 0.99, 0.999})
@Produces(MediaType.APPLICATION_JSON)
@Deprecated(forRemoval = true)
public Response getInsights(
@Parameter(hidden = true) @Auth final ThirdEyeServerPrincipal principal,
@PathParam("id") final Long id) {
return Response.ok(alertService.getInsightsById(principal, id)).build();
}

@Path("insights")
@POST
@Timed(percentiles = {0.5, 0.75, 0.90, 0.95, 0.98, 0.99, 0.999})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,6 @@ protected AlertApi toApi(final AlertDTO dto) {
return ApiBeanMapper.toApi(dto);
}

public AlertInsightsApi getInsightsById(final ThirdEyeServerPrincipal principal, final Long id) {
final AlertDTO dto = getDto(id);
authorizationManager.ensureNamespace(principal, dto);
authorizationManager.ensureCanRead(principal, dto);
return alertInsightsProvider.getInsights(principal, dto);
}

public AlertInsightsApi getInsights(final ThirdEyeServerPrincipal principal,
final AlertInsightsRequestApi request) {
return alertInsightsProvider.getInsights(principal, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import static ai.startree.thirdeye.mapper.ApiBeanMapper.toAlertDto;
import static ai.startree.thirdeye.mapper.ApiBeanMapper.toAlertTemplateApi;
import static ai.startree.thirdeye.spi.Constants.UTC_TIMEZONE;
import static ai.startree.thirdeye.spi.ThirdEyeException.checkThirdEye;
import static ai.startree.thirdeye.spi.ThirdEyeStatus.ERR_ALERT_INSIGHTS;
import static ai.startree.thirdeye.spi.ThirdEyeStatus.ERR_DATASET_NOT_FOUND_IN_NAMESPACE;
import static ai.startree.thirdeye.spi.ThirdEyeStatus.ERR_MISSING_CONFIGURATION_FIELD;
import static ai.startree.thirdeye.spi.ThirdEyeStatus.ERR_MISSING_ID;
import static ai.startree.thirdeye.spi.ThirdEyeStatus.ERR_OBJECT_DOES_NOT_EXIST;
import static ai.startree.thirdeye.spi.util.AlertMetadataUtils.getDelay;
import static ai.startree.thirdeye.spi.util.AlertMetadataUtils.getGranularity;
import static ai.startree.thirdeye.spi.util.SpiUtils.optional;
Expand All @@ -38,6 +41,7 @@
import ai.startree.thirdeye.spi.api.AnalysisRunInfo;
import ai.startree.thirdeye.spi.api.AuthorizationConfigurationApi;
import ai.startree.thirdeye.spi.auth.ThirdEyePrincipal;
import ai.startree.thirdeye.spi.datalayer.bao.AlertManager;
import ai.startree.thirdeye.spi.datalayer.bao.DataSourceManager;
import ai.startree.thirdeye.spi.datalayer.bao.DatasetConfigManager;
import ai.startree.thirdeye.spi.datalayer.dto.AlertDTO;
Expand Down Expand Up @@ -84,25 +88,33 @@ public class AlertInsightsProvider {
private final DatasetConfigManager datasetConfigManager;
private final DataSourceManager dataSourceDao;
private final MinMaxTimeLoader minMaxTimeLoader;
final AuthorizationManager authorizationManager;
private final AuthorizationManager authorizationManager;
private final AlertManager alertManager;

@Inject
public AlertInsightsProvider(final AlertTemplateRenderer alertTemplateRenderer,
final DatasetConfigManager datasetConfigManager, final DataSourceManager dataSourceManager,
final MinMaxTimeLoader minMaxTimeLoader,
final AuthorizationManager authorizationManager) {
final AuthorizationManager authorizationManager,
final AlertManager alertManager) {
this.alertTemplateRenderer = alertTemplateRenderer;
this.datasetConfigManager = datasetConfigManager;
this.dataSourceDao = dataSourceManager;
this.minMaxTimeLoader = minMaxTimeLoader;
this.authorizationManager = authorizationManager;
this.alertManager = alertManager;
}

public AlertInsightsApi getInsights(final ThirdEyePrincipal principal,
final AlertInsightsRequestApi request) {
final AlertApi alertApi = request.getAlert();
// creating a dummy entity to check access then inject namespace - rewrite this - todo authz possible to redesign this?
final AlertDTO alertDto = toAlertDto(alertApi);
final AlertDTO alertDto;
if (alertApi.getId() != null) {
alertDto = alertManager.findById(alertApi.getId());
checkThirdEye(alertDto != null, ERR_OBJECT_DOES_NOT_EXIST, "alert not found for id: " + alertApi.getId());
} else {
alertDto = toAlertDto(alertApi);
}
authorizationManager.enrichNamespace(principal, alertDto);
authorizationManager.ensureCanRead(principal, alertDto);
alertApi.setAuth(new AuthorizationConfigurationApi().setNamespace(alertDto.namespace()));
Expand Down
Loading