63 changed files with 848 additions and 982 deletions
@ -1,143 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2025 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.controller; |
|
||||
|
|
||||
import io.swagger.v3.oas.annotations.Parameter; |
|
||||
import io.swagger.v3.oas.annotations.media.Schema; |
|
||||
import jakarta.validation.Valid; |
|
||||
import org.springframework.security.access.prepost.PreAuthorize; |
|
||||
import org.springframework.validation.annotation.Validated; |
|
||||
import org.springframework.web.bind.annotation.DeleteMapping; |
|
||||
import org.springframework.web.bind.annotation.GetMapping; |
|
||||
import org.springframework.web.bind.annotation.PathVariable; |
|
||||
import org.springframework.web.bind.annotation.PostMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestBody; |
|
||||
import org.springframework.web.bind.annotation.RequestMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestParam; |
|
||||
import org.springframework.web.bind.annotation.RestController; |
|
||||
import org.thingsboard.server.common.data.ai.AiModelSettings; |
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
||||
import org.thingsboard.server.common.data.id.AiModelSettingsId; |
|
||||
import org.thingsboard.server.common.data.page.PageData; |
|
||||
import org.thingsboard.server.config.annotations.ApiOperation; |
|
||||
import org.thingsboard.server.service.security.permission.Operation; |
|
||||
import org.thingsboard.server.service.security.permission.Resource; |
|
||||
|
|
||||
import java.util.Optional; |
|
||||
import java.util.UUID; |
|
||||
|
|
||||
import static org.thingsboard.server.controller.ControllerConstants.AI_MODEL_SETTINGS_TEXT_SEARCH_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
|
||||
|
|
||||
@Validated |
|
||||
@RestController |
|
||||
@RequestMapping("/api/ai/model/settings") |
|
||||
class AiModelSettingsController extends BaseController { |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Create or update AI model settings (saveAiModelSettings)", |
|
||||
notes = "Creates or updates an AI model settings record.\n\n" + |
|
||||
"• **Create:** Omit the `id` to create a new record. The platform assigns a UUID to the new settings and returns it in the `id` field of the response.\n\n" + |
|
||||
"• **Update:** Include an existing `id` to modify that record. If no matching record exists, the API responds with **404 Not Found**.\n\n" + |
|
||||
"Tenant ID for the AI model settings will be taken from the authenticated user making the request, regardless of any value provided in the request body." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@PostMapping |
|
||||
public AiModelSettings saveAiModelSettings(@RequestBody @Valid AiModelSettings settings) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
settings.setTenantId(user.getTenantId()); |
|
||||
checkEntity(settings.getId(), settings, Resource.AI_MODEL_SETTINGS); |
|
||||
return tbAiModelSettingsService.save(settings, user); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Get AI model settings by ID (getAiModelSettingsById)", |
|
||||
notes = "Fetches an AI model settings record by its `id`." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@GetMapping("/{settingsUuid}") |
|
||||
public AiModelSettings getAiModelSettingsById( |
|
||||
@Parameter( |
|
||||
description = "ID of the AI model settings record", |
|
||||
required = true, |
|
||||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|
||||
) |
|
||||
@PathVariable UUID settingsUuid |
|
||||
) throws ThingsboardException { |
|
||||
return checkAiModelSettingsId(new AiModelSettingsId(settingsUuid), Operation.READ); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Get AI model settings (getAiModelSettings)", |
|
||||
notes = "Returns a page of AI model settings. " + |
|
||||
PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@GetMapping |
|
||||
public PageData<AiModelSettings> getAiModelSettings( |
|
||||
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true) |
|
||||
@RequestParam int pageSize, |
|
||||
@Parameter(description = PAGE_NUMBER_DESCRIPTION, required = true) |
|
||||
@RequestParam int page, |
|
||||
@Parameter(description = AI_MODEL_SETTINGS_TEXT_SEARCH_DESCRIPTION) |
|
||||
@RequestParam(required = false) String textSearch, |
|
||||
@Parameter(description = SORT_PROPERTY_DESCRIPTION, schema = @Schema(allowableValues = {"createdTime", "name", "provider", "modelId"})) |
|
||||
@RequestParam(required = false) String sortProperty, |
|
||||
@Parameter(description = SORT_ORDER_DESCRIPTION, schema = @Schema(allowableValues = {"ASC", "DESC"})) |
|
||||
@RequestParam(required = false) String sortOrder |
|
||||
) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.READ); |
|
||||
var pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
|
||||
return aiModelSettingsService.findAiModelSettingsByTenantId(user.getTenantId(), pageLink); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Delete AI model settings by ID (deleteAiModelSettingsById)", |
|
||||
notes = "Deletes the AI model settings record by its `id`. " + |
|
||||
"If a record with the specified `id` exists, the record is deleted and the endpoint returns `true`. " + |
|
||||
"If no such record exists, the endpoint returns `false`." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@DeleteMapping("/{settingsUuid}") |
|
||||
public boolean deleteAiModelSettingsById( |
|
||||
@Parameter( |
|
||||
description = "ID of the AI model settings record", |
|
||||
required = true, |
|
||||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|
||||
) |
|
||||
@PathVariable UUID settingsUuid |
|
||||
) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
var settingsId = new AiModelSettingsId(settingsUuid); |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.DELETE); |
|
||||
Optional<AiModelSettings> toDelete = aiModelSettingsService.findAiModelSettingsByTenantIdAndId(user.getTenantId(), settingsId); |
|
||||
if (toDelete.isEmpty()) { |
|
||||
return false; |
|
||||
} |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.DELETE, settingsId, toDelete.get()); |
|
||||
return tbAiModelSettingsService.delete(toDelete.get(), user); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,84 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2025 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.ai.model; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes; |
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AmazonBedrockChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AnthropicChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AzureOpenAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GitHubModelsChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GoogleAiGeminiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GoogleVertexAiGeminiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.MistralAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.OpenAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AiProvider; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AmazonBedrockProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AnthropicProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AzureOpenAiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GitHubModelsProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GoogleAiGeminiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GoogleVertexAiGeminiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.MistralAiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.OpenAiProviderConfig; |
|
||||
|
|
||||
@JsonTypeInfo( |
|
||||
use = JsonTypeInfo.Id.NAME, |
|
||||
include = JsonTypeInfo.As.PROPERTY, |
|
||||
property = "provider", |
|
||||
visible = true |
|
||||
) |
|
||||
@JsonSubTypes({ |
|
||||
@JsonSubTypes.Type(value = OpenAiChatModel.class, name = "OPENAI"), |
|
||||
@JsonSubTypes.Type(value = AzureOpenAiChatModel.class, name = "AZURE_OPENAI"), |
|
||||
@JsonSubTypes.Type(value = GoogleAiGeminiChatModel.class, name = "GOOGLE_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = GoogleVertexAiGeminiChatModel.class, name = "GOOGLE_VERTEX_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = MistralAiChatModel.class, name = "MISTRAL_AI"), |
|
||||
@JsonSubTypes.Type(value = AnthropicChatModel.class, name = "ANTHROPIC"), |
|
||||
@JsonSubTypes.Type(value = AmazonBedrockChatModel.class, name = "AMAZON_BEDROCK"), |
|
||||
@JsonSubTypes.Type(value = GitHubModelsChatModel.class, name = "GITHUB_MODELS") |
|
||||
}) |
|
||||
public interface AiModel<C extends AiModelConfig> { |
|
||||
|
|
||||
AiProvider provider(); |
|
||||
|
|
||||
@JsonTypeInfo( |
|
||||
use = JsonTypeInfo.Id.NAME, |
|
||||
include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
|
||||
property = "provider" |
|
||||
) |
|
||||
@JsonSubTypes({ |
|
||||
@JsonSubTypes.Type(value = OpenAiProviderConfig.class, name = "OPENAI"), |
|
||||
@JsonSubTypes.Type(value = AzureOpenAiProviderConfig.class, name = "AZURE_OPENAI"), |
|
||||
@JsonSubTypes.Type(value = GoogleAiGeminiProviderConfig.class, name = "GOOGLE_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = GoogleVertexAiGeminiProviderConfig.class, name = "GOOGLE_VERTEX_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = MistralAiProviderConfig.class, name = "MISTRAL_AI"), |
|
||||
@JsonSubTypes.Type(value = AnthropicProviderConfig.class, name = "ANTHROPIC"), |
|
||||
@JsonSubTypes.Type(value = AmazonBedrockProviderConfig.class, name = "AMAZON_BEDROCK"), |
|
||||
@JsonSubTypes.Type(value = GitHubModelsProviderConfig.class, name = "GITHUB_MODELS") |
|
||||
}) |
|
||||
AiProviderConfig providerConfig(); |
|
||||
|
|
||||
@JsonProperty("modelType") |
|
||||
AiModelType modelType(); |
|
||||
|
|
||||
C modelConfig(); |
|
||||
|
|
||||
AiModel<C> withModelConfig(C config); |
|
||||
|
|
||||
} |
|
||||
@ -1,41 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2025 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.ai.model.chat; |
|
||||
|
|
||||
import dev.langchain4j.model.chat.ChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.AiModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.AiModelType; |
|
||||
|
|
||||
public sealed interface AiChatModel<C extends AiChatModelConfig<C>> extends AiModel<C> |
|
||||
permits |
|
||||
OpenAiChatModel, AzureOpenAiChatModel, GoogleAiGeminiChatModel, |
|
||||
GoogleVertexAiGeminiChatModel, MistralAiChatModel, AnthropicChatModel, |
|
||||
AmazonBedrockChatModel, GitHubModelsChatModel { |
|
||||
|
|
||||
ChatModel configure(Langchain4jChatModelConfigurer configurer); |
|
||||
|
|
||||
@Override |
|
||||
default AiModelType modelType() { |
|
||||
return AiModelType.CHAT; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
C modelConfig(); |
|
||||
|
|
||||
@Override |
|
||||
AiChatModel<C> withModelConfig(C config); |
|
||||
|
|
||||
} |
|
||||
@ -0,0 +1,149 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.ai; |
||||
|
|
||||
|
import com.google.common.util.concurrent.FluentFuture; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.springframework.transaction.event.TransactionalEventListener; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.ai.AiModel; |
||||
|
import org.thingsboard.server.common.data.id.AiModelId; |
||||
|
import org.thingsboard.server.common.data.id.EntityId; |
||||
|
import org.thingsboard.server.common.data.id.HasId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.dao.entity.CachedVersionedEntityService; |
||||
|
import org.thingsboard.server.dao.model.sql.AiModelEntity; |
||||
|
import org.thingsboard.server.dao.service.DataValidator; |
||||
|
import org.thingsboard.server.dao.sql.JpaExecutorService; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, AiModel, AiModelCacheEvictEvent> implements AiModelService { |
||||
|
|
||||
|
private final DataValidator<AiModel> aiModelValidator; |
||||
|
|
||||
|
private final JpaExecutorService jpaExecutor; |
||||
|
private final AiModelDao aiModelDao; |
||||
|
|
||||
|
@Override |
||||
|
@TransactionalEventListener |
||||
|
public void handleEvictEvent(AiModelCacheEvictEvent event) { |
||||
|
var cacheKey = event.cacheKey(); |
||||
|
if (event instanceof AiModelCacheEvictEvent.Saved savedEvent) { |
||||
|
cache.put(cacheKey, savedEvent.savedModel()); |
||||
|
} else if (event instanceof AiModelCacheEvictEvent.Deleted) { |
||||
|
cache.evict(cacheKey); |
||||
|
} else { |
||||
|
throw new UnsupportedOperationException("Unsupported event type: " + event.getClass().getSimpleName()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional |
||||
|
public AiModel save(AiModel model) { |
||||
|
aiModelValidator.validate(model, AiModel::getTenantId); |
||||
|
|
||||
|
AiModel savedModel; |
||||
|
try { |
||||
|
savedModel = aiModelDao.saveAndFlush(model.getTenantId(), model); |
||||
|
} catch (Exception e) { |
||||
|
checkConstraintViolation(e, |
||||
|
"ai_model_name_unq_key", "AI model with such name already exist!", |
||||
|
"ai_model_external_id_unq_key", "AI model with such external ID already exists!"); |
||||
|
throw e; |
||||
|
} |
||||
|
|
||||
|
var cacheKey = AiModelCacheKey.of(savedModel.getTenantId(), savedModel.getId()); |
||||
|
publishEvictEvent(new AiModelCacheEvictEvent.Saved(cacheKey, savedModel)); |
||||
|
|
||||
|
return savedModel; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Optional<AiModel> findAiModelById(TenantId tenantId, AiModelId modelId) { |
||||
|
return Optional.ofNullable(aiModelDao.findById(tenantId, modelId.getId())); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public PageData<AiModel> findAiModelsByTenantId(TenantId tenantId, PageLink pageLink) { |
||||
|
validatePageLink(pageLink, AiModelEntity.ALLOWED_SORT_PROPERTIES); |
||||
|
return aiModelDao.findAllByTenantId(tenantId, pageLink); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Optional<AiModel> findAiModelByTenantIdAndId(TenantId tenantId, AiModelId modelId) { |
||||
|
var cacheKey = AiModelCacheKey.of(tenantId, modelId); |
||||
|
return Optional.ofNullable(cache.get(cacheKey, () -> aiModelDao.findByTenantIdAndId(tenantId, modelId).orElse(null))); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public FluentFuture<Optional<AiModel>> findAiModelByTenantIdAndIdAsync(TenantId tenantId, AiModelId modelId) { |
||||
|
return FluentFuture.from(jpaExecutor.submit(() -> findAiModelByTenantIdAndId(tenantId, modelId))); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional |
||||
|
public boolean deleteByTenantIdAndId(TenantId tenantId, AiModelId modelId) { |
||||
|
return deleteByTenantIdAndIdInternal(tenantId, modelId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Optional<HasId<?>> findEntity(TenantId tenantId, EntityId entityId) { |
||||
|
return findAiModelByTenantIdAndId(tenantId, (AiModelId) entityId) |
||||
|
.map(model -> model); // necessary to cast to HasId<?>
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long countByTenantId(TenantId tenantId) { |
||||
|
return aiModelDao.countByTenantId(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional |
||||
|
public void deleteEntity(TenantId tenantId, EntityId id, boolean force) { |
||||
|
deleteByTenantIdAndIdInternal(tenantId, new AiModelId(id.getId())); |
||||
|
} |
||||
|
|
||||
|
private boolean deleteByTenantIdAndIdInternal(TenantId tenantId, AiModelId modelId) { |
||||
|
boolean deleted = aiModelDao.deleteByTenantIdAndId(tenantId, modelId); |
||||
|
if (deleted) { |
||||
|
publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, modelId))); |
||||
|
} |
||||
|
return deleted; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional |
||||
|
public void deleteByTenantId(TenantId tenantId) { |
||||
|
Set<AiModelId> deleted = aiModelDao.deleteByTenantId(tenantId); |
||||
|
deleted.forEach(id -> publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, id)))); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public EntityType getEntityType() { |
||||
|
return EntityType.AI_MODEL; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -1,149 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2025 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.ai; |
|
||||
|
|
||||
import com.google.common.util.concurrent.FluentFuture; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
import org.springframework.transaction.event.TransactionalEventListener; |
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.ai.AiModelSettings; |
|
||||
import org.thingsboard.server.common.data.id.AiModelSettingsId; |
|
||||
import org.thingsboard.server.common.data.id.EntityId; |
|
||||
import org.thingsboard.server.common.data.id.HasId; |
|
||||
import org.thingsboard.server.common.data.id.TenantId; |
|
||||
import org.thingsboard.server.common.data.page.PageData; |
|
||||
import org.thingsboard.server.common.data.page.PageLink; |
|
||||
import org.thingsboard.server.dao.entity.CachedVersionedEntityService; |
|
||||
import org.thingsboard.server.dao.model.sql.AiModelSettingsEntity; |
|
||||
import org.thingsboard.server.dao.service.DataValidator; |
|
||||
import org.thingsboard.server.dao.sql.JpaExecutorService; |
|
||||
|
|
||||
import java.util.Optional; |
|
||||
import java.util.Set; |
|
||||
|
|
||||
import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
|
||||
|
|
||||
@Service |
|
||||
@RequiredArgsConstructor |
|
||||
class AiModelSettingsServiceImpl extends CachedVersionedEntityService<AiModelSettingsCacheKey, AiModelSettings, AiModelSettingsCacheEvictEvent> implements AiModelSettingsService { |
|
||||
|
|
||||
private final DataValidator<AiModelSettings> aiModelSettingsValidator; |
|
||||
|
|
||||
private final JpaExecutorService jpaExecutor; |
|
||||
private final AiModelSettingsDao aiModelSettingsDao; |
|
||||
|
|
||||
@Override |
|
||||
@TransactionalEventListener |
|
||||
public void handleEvictEvent(AiModelSettingsCacheEvictEvent event) { |
|
||||
var cacheKey = event.cacheKey(); |
|
||||
if (event instanceof AiModelSettingsCacheEvictEvent.Saved savedEvent) { |
|
||||
cache.put(cacheKey, savedEvent.savedSettings()); |
|
||||
} else if (event instanceof AiModelSettingsCacheEvictEvent.Deleted) { |
|
||||
cache.evict(cacheKey); |
|
||||
} else { |
|
||||
throw new UnsupportedOperationException("Unsupported event type: " + event.getClass().getSimpleName()); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
@Transactional |
|
||||
public AiModelSettings save(AiModelSettings settings) { |
|
||||
aiModelSettingsValidator.validate(settings, AiModelSettings::getTenantId); |
|
||||
|
|
||||
AiModelSettings savedSettings; |
|
||||
try { |
|
||||
savedSettings = aiModelSettingsDao.saveAndFlush(settings.getTenantId(), settings); |
|
||||
} catch (Exception e) { |
|
||||
checkConstraintViolation(e, |
|
||||
"ai_model_settings_name_unq_key", "AI model settings with such name already exist!", |
|
||||
"ai_model_settings_external_id_unq_key", "AI model settings with such external ID already exist!"); |
|
||||
throw e; |
|
||||
} |
|
||||
|
|
||||
var cacheKey = AiModelSettingsCacheKey.of(savedSettings.getTenantId(), savedSettings.getId()); |
|
||||
publishEvictEvent(new AiModelSettingsCacheEvictEvent.Saved(cacheKey, savedSettings)); |
|
||||
|
|
||||
return savedSettings; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public Optional<AiModelSettings> findAiModelSettingsById(TenantId tenantId, AiModelSettingsId settingsId) { |
|
||||
return Optional.ofNullable(aiModelSettingsDao.findById(tenantId, settingsId.getId())); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public PageData<AiModelSettings> findAiModelSettingsByTenantId(TenantId tenantId, PageLink pageLink) { |
|
||||
validatePageLink(pageLink, AiModelSettingsEntity.ALLOWED_SORT_PROPERTIES); |
|
||||
return aiModelSettingsDao.findAllByTenantId(tenantId, pageLink); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public Optional<AiModelSettings> findAiModelSettingsByTenantIdAndId(TenantId tenantId, AiModelSettingsId settingsId) { |
|
||||
var cacheKey = AiModelSettingsCacheKey.of(tenantId, settingsId); |
|
||||
return Optional.ofNullable(cache.get(cacheKey, () -> aiModelSettingsDao.findByTenantIdAndId(tenantId, settingsId).orElse(null))); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public FluentFuture<Optional<AiModelSettings>> findAiModelSettingsByTenantIdAndIdAsync(TenantId tenantId, AiModelSettingsId settingsId) { |
|
||||
return FluentFuture.from(jpaExecutor.submit(() -> findAiModelSettingsByTenantIdAndId(tenantId, settingsId))); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
@Transactional |
|
||||
public boolean deleteByTenantIdAndId(TenantId tenantId, AiModelSettingsId settingsId) { |
|
||||
return deleteByTenantIdAndIdInternal(tenantId, settingsId); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public Optional<HasId<?>> findEntity(TenantId tenantId, EntityId entityId) { |
|
||||
return findAiModelSettingsByTenantIdAndId(tenantId, (AiModelSettingsId) entityId) |
|
||||
.map(settings -> settings); // necessary to cast to HasId<?>
|
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public long countByTenantId(TenantId tenantId) { |
|
||||
return aiModelSettingsDao.countByTenantId(tenantId); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
@Transactional |
|
||||
public void deleteEntity(TenantId tenantId, EntityId id, boolean force) { |
|
||||
deleteByTenantIdAndIdInternal(tenantId, new AiModelSettingsId(id.getId())); |
|
||||
} |
|
||||
|
|
||||
private boolean deleteByTenantIdAndIdInternal(TenantId tenantId, AiModelSettingsId settingsId) { |
|
||||
boolean deleted = aiModelSettingsDao.deleteByTenantIdAndId(tenantId, settingsId); |
|
||||
if (deleted) { |
|
||||
publishEvictEvent(new AiModelSettingsCacheEvictEvent.Deleted(AiModelSettingsCacheKey.of(tenantId, settingsId))); |
|
||||
} |
|
||||
return deleted; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
@Transactional |
|
||||
public void deleteByTenantId(TenantId tenantId) { |
|
||||
Set<AiModelSettingsId> deleted = aiModelSettingsDao.deleteByTenantId(tenantId); |
|
||||
deleted.forEach(id -> publishEvictEvent(new AiModelSettingsCacheEvictEvent.Deleted(AiModelSettingsCacheKey.of(tenantId, id)))); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public EntityType getEntityType() { |
|
||||
return EntityType.AI_MODEL_SETTINGS; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
Loading…
Reference in new issue