23 changed files with 363 additions and 78 deletions
@ -0,0 +1,29 @@ |
|||
-- |
|||
-- Copyright © 2016-2021 The Thingsboard Authors |
|||
-- |
|||
-- 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. |
|||
-- |
|||
|
|||
CREATE TABLE IF NOT EXISTS entity_alarm ( |
|||
tenant_id uuid NOT NULL, |
|||
entity_id uuid NOT NULL, |
|||
created_time bigint NOT NULL, |
|||
type varchar(255) NOT NULL, |
|||
customer_id uuid, |
|||
alarm_id uuid |
|||
CONSTRAINT entity_alarm_pkey PRIMARY KEY(entity_id, alarm_id), |
|||
CONSTRAINT fk_entity_alarm_id FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE |
|||
); |
|||
|
|||
CREATE INDEX IF NOT EXISTS idx_alarm_tenant_status_created_time ON alarm(tenant_id, status, created_time DESC); |
|||
CREATE INDEX IF NOT EXISTS idx_entity_alarm_created_time ON entity_alarm(tenant_id, entity_id, created_time DESC); |
|||
@ -0,0 +1,40 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.common.data.alarm; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.server.common.data.HasTenantId; |
|||
import org.thingsboard.server.common.data.id.AlarmId; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
public class EntityAlarm implements HasTenantId { |
|||
|
|||
private TenantId tenantId; |
|||
private EntityId entityId; |
|||
private long createdTime; |
|||
private String alarmType; |
|||
|
|||
private CustomerId customerId; |
|||
private AlarmId alarmId; |
|||
|
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.dao.model.sql; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.server.common.data.alarm.EntityAlarm; |
|||
|
|||
import javax.persistence.Transient; |
|||
import java.io.Serializable; |
|||
import java.util.UUID; |
|||
|
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Data |
|||
public class EntityAlarmCompositeKey implements Serializable { |
|||
|
|||
@Transient |
|||
private static final long serialVersionUID = -245388185894468450L; |
|||
|
|||
private UUID entityId; |
|||
private UUID alarmId; |
|||
|
|||
public EntityAlarmCompositeKey(EntityAlarm entityAlarm) { |
|||
this.entityId = entityAlarm.getEntityId().getId(); |
|||
this.alarmId = entityAlarm.getAlarmId().getId(); |
|||
} |
|||
} |
|||
@ -0,0 +1,99 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.dao.model.sql; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.alarm.EntityAlarm; |
|||
import org.thingsboard.server.common.data.id.AlarmId; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.model.ToData; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.Id; |
|||
import javax.persistence.IdClass; |
|||
import javax.persistence.Table; |
|||
import java.util.UUID; |
|||
|
|||
import static org.thingsboard.server.dao.model.ModelConstants.CREATED_TIME_PROPERTY; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.CUSTOMER_ID_PROPERTY; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ALARM_COLUMN_FAMILY_NAME; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_TYPE_COLUMN; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.TENANT_ID_COLUMN; |
|||
|
|||
@Data |
|||
@Entity |
|||
@Table(name = ENTITY_ALARM_COLUMN_FAMILY_NAME) |
|||
@IdClass(EntityAlarmCompositeKey.class) |
|||
public final class EntityAlarmEntity implements ToData<EntityAlarm> { |
|||
|
|||
@Column(name = TENANT_ID_COLUMN, columnDefinition = "uuid") |
|||
private UUID tenantId; |
|||
|
|||
@Column(name = ENTITY_TYPE_COLUMN) |
|||
private String entityType; |
|||
|
|||
@Id |
|||
@Column(name = ENTITY_ID_COLUMN, columnDefinition = "uuid") |
|||
private UUID entityId; |
|||
|
|||
@Id |
|||
@Column(name = "alarm_id", columnDefinition = "uuid") |
|||
private UUID alarmId; |
|||
|
|||
@Column(name = CREATED_TIME_PROPERTY) |
|||
private long createdTime; |
|||
|
|||
@Column(name = "alarm_type") |
|||
private String alarmType; |
|||
|
|||
@Column(name = CUSTOMER_ID_PROPERTY, columnDefinition = "uuid") |
|||
private UUID customerId; |
|||
|
|||
public EntityAlarmEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public EntityAlarmEntity(EntityAlarm entityAlarm) { |
|||
tenantId = entityAlarm.getTenantId().getId(); |
|||
entityId = entityAlarm.getEntityId().getId(); |
|||
entityType = entityAlarm.getEntityId().getEntityType().name(); |
|||
alarmId = entityAlarm.getAlarmId().getId(); |
|||
alarmType = entityAlarm.getAlarmType(); |
|||
createdTime = entityAlarm.getCreatedTime(); |
|||
if (entityAlarm.getCustomerId() != null) { |
|||
customerId = entityAlarm.getCustomerId().getId(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public EntityAlarm toData() { |
|||
EntityAlarm result = new EntityAlarm(); |
|||
result.setTenantId(new TenantId(tenantId)); |
|||
result.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId)); |
|||
result.setAlarmId(new AlarmId(alarmId)); |
|||
result.setAlarmType(alarmType); |
|||
result.setCreatedTime(createdTime); |
|||
if (customerId != null) { |
|||
result.setCustomerId(new CustomerId(customerId)); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.dao.sql.alarm; |
|||
|
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.thingsboard.server.dao.model.sql.EntityAlarmCompositeKey; |
|||
import org.thingsboard.server.dao.model.sql.EntityAlarmEntity; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
public interface EntityAlarmRepository extends CrudRepository<EntityAlarmEntity, EntityAlarmCompositeKey> { |
|||
|
|||
List<EntityAlarmEntity> findAllByAlarmId(UUID alarmId); |
|||
|
|||
} |
|||
Loading…
Reference in new issue