CompanyUsageStatController.java
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// statistics/controller/CompanyUsageStatController.java
package com.aigeo.statistics.controller;
import com.aigeo.statistics.entity.CompanyUsageStat;
import com.aigeo.statistics.service.CompanyUsageStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/company-usage-stats")
public class CompanyUsageStatController {
@Autowired
private CompanyUsageStatService companyUsageStatService;
@GetMapping
public List<CompanyUsageStat> getAllUsageStats() {
return companyUsageStatService.getAllUsageStats();
}
@GetMapping("/{id}")
public ResponseEntity<CompanyUsageStat> getUsageStatById(@PathVariable Integer id) {
Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatById(id);
return usageStat.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@GetMapping("/company/{companyId}")
public List<CompanyUsageStat> getUsageStatsByCompanyId(@PathVariable Integer companyId) {
return companyUsageStatService.getUsageStatsByCompanyId(companyId);
}
@GetMapping("/company/{companyId}/date/{statDate}")
public ResponseEntity<CompanyUsageStat> getUsageStatByCompanyIdAndStatDate(
@PathVariable Integer companyId,
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date statDate) {
Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatByCompanyIdAndStatDate(companyId, statDate);
return usageStat.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@GetMapping("/company/{companyId}/date-range")
public List<CompanyUsageStat> getUsageStatsByCompanyIdAndDateRange(
@PathVariable Integer companyId,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
// Convert Date to LocalDate for the first parameter
java.time.LocalDate localStartDate = startDate.toInstant()
.atZone(java.time.ZoneId.systemDefault())
.toLocalDate();
return companyUsageStatService.getUsageStatsByCompanyIdAndDateRange(companyId, localStartDate, endDate);
}
@GetMapping("/company/{companyId}/total-article-count")
public Long getTotalArticleCountByCompanyId(@PathVariable Integer companyId) {
return companyUsageStatService.getTotalArticleCountByCompanyId(companyId);
}
@GetMapping("/company/{companyId}/total-ai-token-usage")
public Long getTotalAiTokenUsageByCompanyId(@PathVariable Integer companyId) {
return companyUsageStatService.getTotalAiTokenUsageByCompanyId(companyId);
}
@GetMapping("/company/{companyId}/total-website-count")
public Long getTotalWebsiteCountByCompanyId(@PathVariable Integer companyId) {
return companyUsageStatService.getTotalWebsiteCountByCompanyId(companyId);
}
@PostMapping
public CompanyUsageStat createUsageStat(@RequestBody CompanyUsageStat usageStat) {
return companyUsageStatService.saveUsageStat(usageStat);
}
@PutMapping("/{id}")
public ResponseEntity<CompanyUsageStat> updateUsageStat(@PathVariable Integer id,
@RequestBody CompanyUsageStat usageStatDetails) {
Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatById(id);
if (usageStat.isPresent()) {
CompanyUsageStat updatedUsageStat = usageStat.get();
updatedUsageStat.setArticleCount(usageStatDetails.getArticleCount());
updatedUsageStat.setAiTokenUsage(usageStatDetails.getAiTokenUsage());
updatedUsageStat.setWebsiteCount(usageStatDetails.getWebsiteCount());
CompanyUsageStat savedUsageStat = companyUsageStatService.saveUsageStat(updatedUsageStat);
return ResponseEntity.ok(savedUsageStat);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUsageStat(@PathVariable Integer id) {
companyUsageStatService.deleteUsageStat(id);
return ResponseEntity.noContent().build();
}
}