Skip to content

Commit a155e30

Browse files
committed
feat(model): model yükleme süreci güncellendi; gereksiz kodlar kaldırıldı; yeni model veri yapısı eklendi; provider verileri güncellendi
1 parent a80f87d commit a155e30

File tree

8 files changed

+626
-405
lines changed

8 files changed

+626
-405
lines changed

user-service/src/main/java/com/craftpilot/userservice/command/ModelDataLoaderCommand.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,50 @@
99
import org.springframework.stereotype.Component;
1010

1111
import java.time.Duration;
12+
import java.util.Map;
1213

1314
@Component
1415
@RequiredArgsConstructor
1516
@Slf4j
16-
@ConditionalOnProperty(name = "spring.load-models", havingValue = "true", matchIfMissing = true)
17+
@ConditionalOnProperty(name = "app.load-models", havingValue = "true", matchIfMissing = true)
1718
public class ModelDataLoaderCommand implements CommandLineRunner {
1819

1920
private final ModelDataLoader modelDataLoader;
2021

21-
@Value("${spring.models.file:newmodels.json}")
22+
@Value("${app.models.file:newmodels.json}")
2223
private String modelsFile;
2324

24-
@Value("${spring.models.timeout:300}")
25+
@Value("${app.models.timeout-seconds:60}")
2526
private int loadTimeoutSeconds;
27+
28+
@Value("${app.models.load-on-startup:true}")
29+
private boolean loadOnStartup;
2630

2731
@Override
2832
public void run(String... args) {
33+
if (!loadOnStartup) {
34+
log.info("CommandLineRunner: Model yükleme devre dışı (app.models.load-on-startup=false)");
35+
return;
36+
}
37+
38+
// ApplicationReadyEvent ile çift yüklemeyi önlemek için durumu kontrol et
39+
Map<String, Object> status = modelDataLoader.getModelLoadingStatus();
40+
if ((boolean)status.get("loadInProgress")) {
41+
log.info("CommandLineRunner: Model yükleme işlemi zaten başlatıldı, devam eden işlem bekleniyor");
42+
return;
43+
}
44+
2945
log.info("CommandLineRunner ile model yükleme başlatılıyor. '{}' dosyasından modeller yükleniyor", modelsFile);
3046

3147
// Dosya yolunu belirle
3248
String jsonFilePath = args.length > 0 && args[0] != null && !args[0].isEmpty() ?
3349
args[0] : modelsFile;
3450

51+
// jsonFilePath değerini düzelt, eğer "classpath:" ile başlamıyorsa ekle
52+
if (!jsonFilePath.startsWith("classpath:") && !jsonFilePath.startsWith("file:")) {
53+
jsonFilePath = "classpath:" + jsonFilePath;
54+
}
55+
3556
try {
3657
// Modelleri yükle ve işlem tamamlanana kadar bekle
3758
Integer count = modelDataLoader.loadModelsFromFile(jsonFilePath)
@@ -42,11 +63,13 @@ public void run(String... args) {
4263
.doOnError(error -> {
4364
log.error("CommandLineRunner: AI model yükleme sırasında hata: {}", error.getMessage(), error);
4465
})
66+
.onErrorReturn(0) // Hata durumunda 0 döndür ama uygulamanın çalışmasını engelleme
4567
.block(Duration.ofSeconds(loadTimeoutSeconds + 10)); // Bloke etme süresi timeout + 10 saniye
4668

4769
log.info("CommandLineRunner: Model yükleme işlemi sonucu: {} model yüklendi", count != null ? count : 0);
4870
} catch (Exception e) {
4971
log.error("CommandLineRunner: Model yükleme işlemi sırasında beklenmeyen hata: {}", e.getMessage(), e);
72+
// Uygulama başlangıcını engelleme
5073
}
5174
}
5275
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.craftpilot.userservice.controller;
2+
3+
import com.craftpilot.userservice.service.ModelDataLoader;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
import reactor.core.publisher.Mono;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
@RestController
15+
@RequestMapping("/api/admin/models")
16+
@RequiredArgsConstructor
17+
@Slf4j
18+
public class ModelLoaderController {
19+
20+
private final ModelDataLoader modelDataLoader;
21+
22+
@PostMapping("/reload")
23+
public Mono<ResponseEntity<Map<String, Object>>> reloadModels() {
24+
log.info("Model verilerini yeniden yükleme API çağrısı alındı");
25+
26+
return modelDataLoader.reloadModelData()
27+
.map(count -> {
28+
Map<String, Object> response = new HashMap<>();
29+
response.put("success", true);
30+
response.put("message", "AI model verileri başarıyla yüklendi");
31+
response.put("loadedModels", count);
32+
return ResponseEntity.ok(response);
33+
})
34+
.onErrorResume(e -> {
35+
log.error("Model yeniden yüklenirken hata: {}", e.getMessage(), e);
36+
Map<String, Object> response = new HashMap<>();
37+
response.put("success", false);
38+
response.put("message", "Model yükleme hatası: " + e.getMessage());
39+
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response));
40+
});
41+
}
42+
43+
@GetMapping("/status")
44+
public ResponseEntity<Map<String, Object>> getModelLoaderStatus() {
45+
log.info("Model yükleme durumu API çağrısı alındı");
46+
return ResponseEntity.ok(modelDataLoader.getModelLoadingStatus());
47+
}
48+
49+
@PostMapping("/load-from-file")
50+
public Mono<ResponseEntity<Map<String, Object>>> loadFromCustomFile(@RequestBody Map<String, String> request) {
51+
String filePath = request.get("filePath");
52+
if (filePath == null || filePath.isEmpty()) {
53+
Map<String, Object> response = new HashMap<>();
54+
response.put("success", false);
55+
response.put("message", "Dosya yolu belirtilmedi");
56+
return Mono.just(ResponseEntity.badRequest().body(response));
57+
}
58+
59+
log.info("Özel dosyadan model yükleme başlatılıyor: {}", filePath);
60+
61+
// Dosya yolu güvenlik kontrolü
62+
if (!filePath.startsWith("classpath:") && !filePath.startsWith("file:") && !filePath.startsWith("http")) {
63+
filePath = "file:" + filePath;
64+
}
65+
66+
final String validatedFilePath = filePath;
67+
68+
return modelDataLoader.loadModelsFromFile(validatedFilePath)
69+
.map(count -> {
70+
Map<String, Object> response = new HashMap<>();
71+
response.put("success", true);
72+
response.put("message", "Özel dosyadan AI model verileri başarıyla yüklendi");
73+
response.put("loadedModels", count);
74+
response.put("filePath", validatedFilePath);
75+
response.put("timestamp", System.currentTimeMillis());
76+
return ResponseEntity.ok(response);
77+
})
78+
.onErrorResume(e -> {
79+
log.error("Özel dosyadan model yüklenirken hata: {}", e.getMessage(), e);
80+
Map<String, Object> response = new HashMap<>();
81+
response.put("success", false);
82+
response.put("message", "Özel dosyadan model yükleme hatası: " + e.getMessage());
83+
response.put("filePath", validatedFilePath);
84+
response.put("errorType", e.getClass().getSimpleName());
85+
response.put("timestamp", System.currentTimeMillis());
86+
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response));
87+
});
88+
}
89+
}

user-service/src/main/java/com/craftpilot/userservice/model/ai/AIModel.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.data.annotation.Id;
88
import org.springframework.data.mongodb.core.index.Indexed;
99
import org.springframework.data.mongodb.core.mapping.Document;
10+
import java.time.LocalDateTime;
1011

1112
@Data
1213
@Builder
@@ -21,11 +22,23 @@ public class AIModel {
2122
private String modelId;
2223
private String modelName;
2324
private String provider;
25+
private String providerId;
2426
private Integer maxInputTokens;
2527
private String requiredPlan;
2628
private Integer creditCost;
2729
private String creditType;
2830
private String category;
2931
private Integer contextLength;
3032
private Boolean isActive;
33+
private LocalDateTime createdAt;
34+
private LocalDateTime updatedAt;
35+
36+
// Additional helper methods
37+
public void setActive(boolean active) {
38+
this.isActive = active;
39+
}
40+
41+
public boolean getActive() {
42+
return this.isActive != null ? this.isActive : false;
43+
}
3144
}

user-service/src/main/java/com/craftpilot/userservice/model/ai/Provider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.NoArgsConstructor;
77
import org.springframework.data.annotation.Id;
88
import org.springframework.data.mongodb.core.mapping.Document;
9+
import java.time.LocalDateTime;
910
import java.util.List;
1011

1112
@Data
@@ -19,6 +20,9 @@ public class Provider {
1920
private String icon;
2021
private String description;
2122
private List<AIModel> models;
23+
private boolean active;
24+
private LocalDateTime createdAt;
25+
private LocalDateTime updatedAt;
2226

2327
/**
2428
* MongoDB ID'yi döndürür. Bu durumda name alanıdır.

0 commit comments

Comments
 (0)