Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[persistence] Add TaskQuotasConfiguration to workspace configuration #1751

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -58,6 +58,8 @@
import ai.startree.thirdeye.spi.api.PlanNodeApi;
import ai.startree.thirdeye.spi.api.RcaInvestigationApi;
import ai.startree.thirdeye.spi.api.SubscriptionGroupApi;
import ai.startree.thirdeye.spi.api.TaskQuotasConfigurationApi;
import ai.startree.thirdeye.spi.api.TemplateConfigurationApi;
import ai.startree.thirdeye.spi.api.TimeConfigurationApi;
import ai.startree.thirdeye.spi.detection.AnomalyCause;
import ai.startree.thirdeye.spi.detection.AnomalyFeedbackType;
Expand Down Expand Up @@ -738,6 +740,8 @@ public void testUpdateNamespaceConfiguration() {
.setMinimumOnboardingStartTime(996684800000L));
updatedCfg.setAuth(new AuthorizationConfigurationApi());
updatedCfg.setId(namespaceConfigurationId);
updatedCfg.setTemplateConfiguration(new TemplateConfigurationApi());
updatedCfg.setTaskQuotasConfiguration(new TaskQuotasConfigurationApi());
final Response response = request("api/workspace-configuration").put(
Entity.json(updatedCfg));
assertThat(response.getStatus()).isEqualTo(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ai.startree.thirdeye.spi.datalayer.bao.NamespaceConfigurationManager;
import ai.startree.thirdeye.spi.datalayer.dto.AuthorizationConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.NamespaceConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TaskQuotasConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TemplateConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TimeConfigurationDTO;
import com.google.inject.Inject;
Expand Down Expand Up @@ -84,6 +85,10 @@ private boolean updateDefaults(final NamespaceConfigurationDTO existingNamespace
existingNamespaceConfig.setTemplateConfiguration(defaultTemplateConfiguration());
updated = true;
}
if (force || existingNamespaceConfig.getTaskQuotasConfiguration() == null) {
existingNamespaceConfig.setTaskQuotasConfiguration(defaultTaskQuotasConfiguration());
updated = true;
}
return updated;
}

Expand Down Expand Up @@ -151,6 +156,7 @@ private NamespaceConfigurationDTO defaultNamespaceConfiguration(final String nam
namespaceConfigurationDTO
.setTimeConfiguration(defaultTimeConfiguration())
.setTemplateConfiguration(defaultTemplateConfiguration())
.setTaskQuotasConfiguration(defaultTaskQuotasConfiguration())
.setAuth(new AuthorizationConfigurationDTO().setNamespace(namespace));
return namespaceConfigurationDTO;
}
Expand Down Expand Up @@ -208,4 +214,9 @@ private TemplateConfigurationDTO defaultTemplateConfiguration() {
return optional(defaultNamespaceConfiguration.getTemplateConfiguration())
.orElse(new TemplateConfigurationDTO());
}

private TaskQuotasConfigurationDTO defaultTaskQuotasConfiguration() {
return optional(defaultNamespaceConfiguration.getTaskQuotasConfiguration())
.orElse(new TaskQuotasConfigurationDTO());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import ai.startree.thirdeye.spi.datalayer.dto.DatasetConfigDTO;
import ai.startree.thirdeye.spi.datalayer.dto.MetricConfigDTO;
import ai.startree.thirdeye.spi.datalayer.dto.NamespaceConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TaskQuotasConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TemplateConfigurationDTO;
import ai.startree.thirdeye.spi.datalayer.dto.TimeConfigurationDTO;
import ai.startree.thirdeye.spi.metric.MetricType;
import ai.startree.thirdeye.spi.util.SpiUtils;
Expand Down Expand Up @@ -66,12 +68,10 @@ public static DatasetConfigDTO getTestDatasetConfig(String collection) {

public static NamespaceConfigurationDTO buildNamespaceConfiguration(String namespace) {
final NamespaceConfigurationDTO dto = new NamespaceConfigurationDTO();
dto.setTimeConfiguration(new TimeConfigurationDTO()
.setDateTimePattern(Constants.NOTIFICATIONS_DEFAULT_DATE_PATTERN)
.setTimezone(DEFAULT_CHRONOLOGY.getZone())
.setMinimumOnboardingStartTime(946684800000L));
dto.setAuth(new AuthorizationConfigurationDTO()
.setNamespace(namespace));
dto.setTimeConfiguration(new TimeConfigurationDTO());
dto.setAuth(new AuthorizationConfigurationDTO().setNamespace(namespace));
dto.setTemplateConfiguration(new TemplateConfigurationDTO());
dto.setTaskQuotasConfiguration(new TaskQuotasConfigurationDTO());
return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,11 @@ private void compareDtos(NamespaceConfigurationDTO dto1, NamespaceConfigurationD
.isEqualTo(dto2.getTimeConfiguration().getTimezone());
assertThat(dto1.getTimeConfiguration().getMinimumOnboardingStartTime())
.isEqualTo(dto2.getTimeConfiguration().getMinimumOnboardingStartTime());
assertThat(dto1.getTemplateConfiguration().getSqlLimitStatement())
.isEqualTo(dto2.getTemplateConfiguration().getSqlLimitStatement());
assertThat(dto1.getTaskQuotasConfiguration().getDetectionTaskQuota())
.isEqualTo(dto2.getTaskQuotasConfiguration().getDetectionTaskQuota());
assertThat(dto1.getTaskQuotasConfiguration().getNotificationTaskQuota())
.isEqualTo(dto2.getTaskQuotasConfiguration().getNotificationTaskQuota());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ protected NamespaceConfigurationApi toApi(final NamespaceConfigurationDTO dto) {
protected void validateUpdate(NamespaceConfigurationApi existing,
NamespaceConfigurationApi updated) {
if (!Objects.equals(existing.getId(), updated.getId()) ||
!Objects.equals(existing.getAuth().getNamespace(), updated.getAuth().getNamespace())) {
!Objects.equals(existing.getAuth().getNamespace(), updated.getAuth().getNamespace()) ||
!Objects.equals(existing.getTaskQuotasConfiguration().getDetectionTaskQuota(),
updated.getTaskQuotasConfiguration().getDetectionTaskQuota()) ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will throw a NPE if updated.getTaskQuotasConfiguration() returns null.
Let's override equals properly in the Quota DTO, (in intelliJ: code --> generate --> equals and hashCode)
then let's just use

Objects.equals(existing.getTaskQuotasConfiguration(), updated.getTaskQuotasConfiguration())

this will manage the case where .getTaskQuotasConfiguration() returns null and check all the fields, so it'll be more future proof if we ever add fields to the QuotaDto. (as long as equals is maintained correctly in the DTO but it's standard practice)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!
do we need to do this for the underlying taskQuotasConfigurationApi as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this equals will still fail if the object pointers are different
which will likely be the case since we are comparing two different objects

we need a way to just compare the values and not the pointers of objects or underlying fields

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2025-01-15 at 8 54 50 PM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

his will throw a NPE if updated.getTaskQuotasConfiguration() returns null

addressed all other feedback
will handle this one as a separate commit

do let me know if there are any other concerns

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this equals will still fail if the object pointers are different
which will likely be the case since we are comparing two different objects

we need a way to just compare the values and not the pointers of objects or underlying fields

please read
https://www.baeldung.com/java-equals-hashcode-contracts

and Effective Java items 10, 11, 12, 13

see an example of overriding here:


but no need to write manually you can generate it, see message above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had to add the same to taskQuotasConfiguration as well. that's what was missing. Thank you!

!Objects.equals(existing.getTaskQuotasConfiguration().getNotificationTaskQuota(),
updated.getTaskQuotasConfiguration().getNotificationTaskQuota())) {
throw badRequest(
ThirdEyeStatus.ERR_NAMESPACE_CONFIGURATION_VALIDATION_FAILED,
existing.namespace(),
"Updating Id or auth is not allowed for Namespace Configuration");
"Updating Id, auth, or taskQuotasConfiguration is not allowed for Namespace Configuration");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class NamespaceConfigurationApi implements ThirdEyeCrudApi<NamespaceConfi

private TemplateConfigurationApi templateConfiguration;

private TaskQuotasConfigurationApi taskQuotasConfiguration;

public TimeConfigurationApi getTimeConfiguration() {
return timeConfiguration;
Expand Down Expand Up @@ -60,4 +61,14 @@ public NamespaceConfigurationApi setTemplateConfiguration(
this.templateConfiguration = templateConfiguration;
return this;
}

public TaskQuotasConfigurationApi getTaskQuotasConfiguration() {
return taskQuotasConfiguration;
}

public NamespaceConfigurationApi setTaskQuotasConfiguration(
final TaskQuotasConfigurationApi taskQuotasConfiguration) {
this.taskQuotasConfiguration = taskQuotasConfiguration;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 StarTree Inc
*
* Licensed under the StarTree Community License (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.startree.ai/legal/startree-community-license
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OF ANY KIND,
* either express or implied.
* See the License for the specific language governing permissions and limitations under
* the License.
*/
package ai.startree.thirdeye.spi.api;

public class TaskQuotasConfigurationApi {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's have a first level named QuotasConfiguration where we will put all quotas.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie let's rename, we may want to put more quota values in this field that are not related to tasks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. on it!


private Long DetectionTaskQuota;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's have a more explicit name. quotas could be monthly rate, minutely rate, absolute number since account creation, per namespace, per user, etc...

also it's non standard to start a field name with an UPPER character. let's use lower character.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinion on the name, but I'd go with something like

maximumDetectionTasksPerMonth 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

private Long NotificationTaskQuota;

public Long getDetectionTaskQuota() {
return DetectionTaskQuota;
}

public TaskQuotasConfigurationApi setDetectionTaskQuota(final Long detectionTaskQuota) {
this.DetectionTaskQuota = detectionTaskQuota;
return this;
}

public Long getNotificationTaskQuota() {
return NotificationTaskQuota;
}

public TaskQuotasConfigurationApi setNotificationTaskQuota(final Long notificationTaskQuota) {
this.NotificationTaskQuota = notificationTaskQuota;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class NamespaceConfigurationDTO extends AbstractDTO {

TimeConfigurationDTO timeConfiguration;

private TemplateConfigurationDTO templateConfiguration;
private TemplateConfigurationDTO templateConfiguration;

private TaskQuotasConfigurationDTO taskQuotasConfiguration;

public TimeConfigurationDTO getTimeConfiguration() {
return timeConfiguration;
Expand All @@ -44,4 +46,14 @@ public NamespaceConfigurationDTO setTemplateConfiguration(
this.templateConfiguration = templateConfiguration;
return this;
}

public TaskQuotasConfigurationDTO getTaskQuotasConfiguration() {
return taskQuotasConfiguration;
}

public NamespaceConfigurationDTO setTaskQuotasConfiguration(
final TaskQuotasConfigurationDTO taskQuotasConfiguration) {
this.taskQuotasConfiguration = taskQuotasConfiguration;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 StarTree Inc
*
* Licensed under the StarTree Community License (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.startree.ai/legal/startree-community-license
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OF ANY KIND,
* either express or implied.
* See the License for the specific language governing permissions and limitations under
* the License.
*/
package ai.startree.thirdeye.spi.datalayer.dto;

public class TaskQuotasConfigurationDTO {

/**
* Monthly quota for number of detection task runs
*/
private Long DetectionTaskQuota;
/**
* Monthly quota for number of notification task runs
*/
private Long NotificationTaskQuota;

public Long getDetectionTaskQuota() {
return DetectionTaskQuota;
}

public TaskQuotasConfigurationDTO setDetectionTaskQuota(final Long detectionTaskQuota) {
this.DetectionTaskQuota = detectionTaskQuota;
return this;
}

public Long getNotificationTaskQuota() {
return NotificationTaskQuota;
}

public TaskQuotasConfigurationDTO setNotificationTaskQuota(final Long notificationTaskQuota) {
this.NotificationTaskQuota = notificationTaskQuota;
return this;
}
}
Loading