-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix: 重构场景联动,迁移指标函数 #384
Merged
Merged
fix: 重构场景联动,迁移指标函数 #384
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ts/things-component/src/main/java/org/jetlinks/community/things/ThingsDataProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.jetlinks.community.things; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import reactor.function.Consumer3; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = "jetlinks.things.data") | ||
public class ThingsDataProperties { | ||
private Set<String> autoUpdateThingTypes = new HashSet<>(); | ||
|
||
/** | ||
* 存储相关配置 | ||
*/ | ||
private Store store = new Store(); | ||
|
||
@Getter | ||
@Setter | ||
public static class Store { | ||
//每个属性保存记录最大数量 | ||
private int maxSizeEachProperty = 8; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...mponent/src/main/java/org/jetlinks/community/things/impl/entity/PropertyMetricEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.jetlinks.community.things.impl.entity; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hswebframework.ezorm.rdb.mapping.annotation.DefaultValue; | ||
import org.hswebframework.web.api.crud.entity.GenericEntity; | ||
import org.hswebframework.web.api.crud.entity.RecordCreationEntity; | ||
import org.hswebframework.web.crud.annotation.EnableEntityEvent; | ||
import org.hswebframework.web.crud.generator.Generators; | ||
import org.hswebframework.web.utils.DigestUtils; | ||
import org.jetlinks.community.PropertyMetric; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Table; | ||
|
||
@Getter | ||
@Setter | ||
@Table(name = "thing_property_metric") | ||
@EnableEntityEvent | ||
public class PropertyMetricEntity extends GenericEntity<String> implements RecordCreationEntity { | ||
|
||
@Schema(description = "物类型,如: device") | ||
@Column(length = 32, nullable = false, updatable = false) | ||
private String thingType; | ||
|
||
@Schema(description = "物ID,如: 设备ID") | ||
@Column(length = 64, nullable = false, updatable = false) | ||
private String thingId; | ||
|
||
@Schema(description = "属性标识") | ||
@Column(length = 64, nullable = false, updatable = false) | ||
private String property; | ||
|
||
@Schema(description = "指标标识") | ||
@Column(length = 64, nullable = false, updatable = false) | ||
private String metric; | ||
|
||
@Schema(description = "指标名称") | ||
@Column(length = 64) | ||
private String metricName; | ||
|
||
@Schema(description = "指标值") | ||
@Column(length = 256, nullable = false) | ||
private String value; | ||
|
||
@Schema(description = "是否为范围值") | ||
@Column(nullable = false) | ||
@DefaultValue("false") | ||
private Boolean range; | ||
|
||
@Schema(description = "创建人ID") | ||
@Column(length = 64, nullable = false, updatable = false) | ||
private String creatorId; | ||
|
||
@Schema(description = "创建时间") | ||
@Column(nullable = false, updatable = false) | ||
@DefaultValue(generator = Generators.CURRENT_TIME) | ||
private Long createTime; | ||
|
||
public void genericId(){ | ||
setId(genericId( | ||
thingType,thingId,property,metric | ||
)); | ||
} | ||
public static String genericId(String thingType, String thingId,String property, String metric) { | ||
return DigestUtils.md5Hex(String.join("|", thingType, thingId, property, metric)); | ||
} | ||
|
||
public PropertyMetric toMetric(){ | ||
PropertyMetric propertyMetric=new PropertyMetric(); | ||
propertyMetric.setId(metric); | ||
propertyMetric.setValue(value); | ||
propertyMetric.setRange(range); | ||
propertyMetric.setName(metricName); | ||
return propertyMetric; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...src/main/java/org/jetlinks/community/things/impl/metric/DefaultPropertyMetricManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package org.jetlinks.community.things.impl.metric; | ||
|
||
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; | ||
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult; | ||
import org.hswebframework.web.crud.events.EntityCreatedEvent; | ||
import org.hswebframework.web.crud.events.EntityDeletedEvent; | ||
import org.hswebframework.web.crud.events.EntityModifyEvent; | ||
import org.hswebframework.web.crud.events.EntitySavedEvent; | ||
import org.jetlinks.core.event.EventBus; | ||
import org.jetlinks.core.event.Subscription; | ||
import org.jetlinks.core.things.Thing; | ||
import org.jetlinks.core.things.ThingId; | ||
import org.jetlinks.core.things.ThingTemplate; | ||
import org.jetlinks.core.things.ThingsRegistry; | ||
import org.jetlinks.community.PropertyMetadataConstants; | ||
import org.jetlinks.community.PropertyMetric; | ||
import org.jetlinks.community.gateway.annotation.Subscribe; | ||
import org.jetlinks.community.things.impl.entity.PropertyMetricEntity; | ||
import org.jetlinks.community.things.metric.AbstractPropertyMetricManager; | ||
import org.springframework.context.event.EventListener; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class DefaultPropertyMetricManager extends AbstractPropertyMetricManager { | ||
|
||
private final EventBus eventBus; | ||
|
||
private final ReactiveRepository<PropertyMetricEntity, String> repository; | ||
|
||
public DefaultPropertyMetricManager(ThingsRegistry registry, | ||
EventBus eventBus, | ||
ReactiveRepository<PropertyMetricEntity, String> repository) { | ||
super(registry); | ||
this.eventBus = eventBus; | ||
this.repository = repository; | ||
} | ||
|
||
public Flux<PropertyMetric> getPropertyMetrics(String thingType, | ||
String thingId, | ||
String property) { | ||
return Mono | ||
.zip( | ||
//数据库中记录的 | ||
repository | ||
.createQuery() | ||
.where(PropertyMetricEntity::getThingType, thingType) | ||
.and(PropertyMetricEntity::getThingId, thingId) | ||
.and(PropertyMetricEntity::getProperty, property) | ||
.fetch() | ||
.map(PropertyMetricEntity::toMetric) | ||
.collectMap(PropertyMetric::getId), | ||
//物模型中配置的 | ||
registry | ||
.getThing(thingType, thingId) | ||
.flatMap(Thing::getTemplate) | ||
.flatMap(ThingTemplate::getMetadata) | ||
.flatMapIterable(metadata -> metadata | ||
.getProperty(property) | ||
.map(PropertyMetadataConstants.Metrics::getMetrics) | ||
.orElse(Collections.emptyList())) | ||
.collectMap(PropertyMetric::getId, Function.identity(), LinkedHashMap::new), | ||
(exists, inMetadata) -> { | ||
for (Map.Entry<String, PropertyMetric> entry : exists.entrySet()) { | ||
String metric = entry.getKey(); | ||
PropertyMetric independent = entry.getValue(); | ||
PropertyMetric fromMetadata = inMetadata.get(metric); | ||
if (fromMetadata == null) { | ||
inMetadata.put(metric, independent); | ||
continue; | ||
} | ||
fromMetadata.setValue(independent.getValue()); | ||
} | ||
return Flux.fromIterable(inMetadata.values()); | ||
}) | ||
.flatMapMany(Function.identity()); | ||
} | ||
|
||
public Mono<SaveResult> savePropertyMetrics(String thingType, | ||
String thingId, | ||
String property, | ||
Flux<PropertyMetric> metrics) { | ||
return metrics | ||
.map(metric -> { | ||
PropertyMetricEntity entity = new PropertyMetricEntity(); | ||
entity.setThingId(thingId); | ||
entity.setThingType(thingType); | ||
entity.setMetric(metric.getId()); | ||
entity.setMetricName(metric.getName()); | ||
entity.setProperty(property); | ||
entity.setValue(String.valueOf(metric.getValue())); | ||
entity.setRange(metric.isRange()); | ||
entity.genericId(); | ||
return entity; | ||
}) | ||
.as(repository::save); | ||
|
||
} | ||
|
||
@Override | ||
protected Mono<PropertyMetric> loadPropertyMetric(ThingId thingId, | ||
String property, | ||
String metric) { | ||
|
||
return repository | ||
.findById(PropertyMetricEntity.genericId(thingId.getType(), thingId.getId(), property, metric)) | ||
.map(PropertyMetricEntity::toMetric); | ||
} | ||
|
||
@EventListener | ||
public void handleEntityChanged(EntityModifyEvent<PropertyMetricEntity> event) { | ||
event.async( | ||
Flux.fromIterable(event.getAfter()) | ||
.flatMap(this::handleMetricChangedEvent) | ||
); | ||
} | ||
|
||
@EventListener | ||
public void handleEntityChanged(EntityCreatedEvent<PropertyMetricEntity> event) { | ||
event.async( | ||
Flux.fromIterable(event.getEntity()) | ||
.flatMap(this::handleMetricChangedEvent) | ||
); | ||
} | ||
|
||
@EventListener | ||
public void handleEntityChanged(EntityDeletedEvent<PropertyMetricEntity> event) { | ||
event.async( | ||
Flux.fromIterable(event.getEntity()) | ||
.flatMap(this::handleMetricChangedEvent) | ||
); | ||
} | ||
|
||
@EventListener | ||
public void handleEntityChanged(EntitySavedEvent<PropertyMetricEntity> event) { | ||
event.async( | ||
Flux.fromIterable(event.getEntity()) | ||
.flatMap(this::handleMetricChangedEvent) | ||
); | ||
} | ||
|
||
@Subscribe(value = "/_sys/thing-property-metric/clear-cache", features = Subscription.Feature.broker) | ||
public Mono<Void> handleMetricChangedEvent(CacheKey key) { | ||
cache.remove(key); | ||
return Mono.empty(); | ||
} | ||
|
||
private Mono<Void> handleMetricChangedEvent(PropertyMetricEntity entity) { | ||
CacheKey key = CacheKey.of(ThingId.of(entity.getThingType(), entity.getThingId()), entity.getProperty(), entity.getMetric()); | ||
cache.remove(key); | ||
return eventBus | ||
.publish("/_sys/thing-property-metric/clear-cache", key) | ||
.then(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
版本号不能写死
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的