feat: mock data.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
@@ -111,6 +112,7 @@ func (sc *StatisticsController) DeleteAIAnalysisRecord(c *gin.Context) {
|
||||
|
||||
type regionStatisticsItem struct {
|
||||
RegionID uint32 `json:"regionId"`
|
||||
KindergartenName string `json:"kindergartenName"`
|
||||
Count int64 `json:"count"`
|
||||
TotalInputTokens int64 `json:"totalInputTokens"`
|
||||
TotalOutputTokens int64 `json:"totalOutputTokens"`
|
||||
@@ -120,6 +122,8 @@ type regionStatisticsItem struct {
|
||||
AvgDurationMs float64 `json:"avgDurationMs"`
|
||||
TotalOriginalFileSize int64 `json:"totalOriginalFileSize"`
|
||||
TotalCompressedSize int64 `json:"totalCompressedSize"`
|
||||
FirstUsedAt *time.Time `json:"firstUsedAt"`
|
||||
LastUsedAt *time.Time `json:"lastUsedAt"`
|
||||
}
|
||||
|
||||
func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
@@ -154,6 +158,8 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
TotalDurationMs int64
|
||||
TotalOriginalFileSize int64
|
||||
TotalCompressedSize int64
|
||||
FirstUsedAt *time.Time
|
||||
LastUsedAt *time.Time
|
||||
}
|
||||
|
||||
var rawResults []rawStats
|
||||
@@ -166,7 +172,9 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
COALESCE(SUM(output_size_bytes), 0) as total_output_size_bytes,
|
||||
COALESCE(SUM(duration_ms), 0) as total_duration_ms,
|
||||
COALESCE(SUM(original_file_size), 0) as total_original_file_size,
|
||||
COALESCE(SUM(compressed_content_size), 0) as total_compressed_size
|
||||
COALESCE(SUM(compressed_content_size), 0) as total_compressed_size,
|
||||
MIN(created_at) as first_used_at,
|
||||
MAX(created_at) as last_used_at
|
||||
`).Group("region_id").Scan(&rawResults).Error
|
||||
|
||||
if err != nil {
|
||||
@@ -174,6 +182,23 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 收集所有 regionId 查询幼儿园名称
|
||||
regionIDs := make([]uint32, 0, len(rawResults))
|
||||
for _, r := range rawResults {
|
||||
if r.RegionID != nil && *r.RegionID > 0 {
|
||||
regionIDs = append(regionIDs, *r.RegionID)
|
||||
}
|
||||
}
|
||||
kindergartenMap := make(map[uint32]string)
|
||||
if len(regionIDs) > 0 {
|
||||
var kindergartens []models.Kindergarten
|
||||
if err := sc.DB.Where("region_id IN ?", regionIDs).Find(&kindergartens).Error; err == nil {
|
||||
for _, k := range kindergartens {
|
||||
kindergartenMap[k.RegionID] = k.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
overall := regionStatisticsItem{}
|
||||
regions := make(map[string]regionStatisticsItem, len(rawResults))
|
||||
|
||||
@@ -186,8 +211,13 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
if r.Count > 0 {
|
||||
avgDuration = float64(r.TotalDurationMs) / float64(r.Count)
|
||||
}
|
||||
kgName := ""
|
||||
if regionID > 0 {
|
||||
kgName = kindergartenMap[regionID]
|
||||
}
|
||||
item := regionStatisticsItem{
|
||||
RegionID: regionID,
|
||||
KindergartenName: kgName,
|
||||
Count: r.Count,
|
||||
TotalInputTokens: r.TotalInputTokens,
|
||||
TotalOutputTokens: r.TotalOutputTokens,
|
||||
@@ -197,6 +227,8 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
AvgDurationMs: avgDuration,
|
||||
TotalOriginalFileSize: r.TotalOriginalFileSize,
|
||||
TotalCompressedSize: r.TotalCompressedSize,
|
||||
FirstUsedAt: r.FirstUsedAt,
|
||||
LastUsedAt: r.LastUsedAt,
|
||||
}
|
||||
|
||||
regions[strconv.FormatUint(uint64(regionID), 10)] = item
|
||||
@@ -209,6 +241,17 @@ func (sc *StatisticsController) StatisticsByRegion(c *gin.Context) {
|
||||
overall.TotalDurationMs += r.TotalDurationMs
|
||||
overall.TotalOriginalFileSize += r.TotalOriginalFileSize
|
||||
overall.TotalCompressedSize += r.TotalCompressedSize
|
||||
|
||||
if r.FirstUsedAt != nil {
|
||||
if overall.FirstUsedAt == nil || r.FirstUsedAt.Before(*overall.FirstUsedAt) {
|
||||
overall.FirstUsedAt = r.FirstUsedAt
|
||||
}
|
||||
}
|
||||
if r.LastUsedAt != nil {
|
||||
if overall.LastUsedAt == nil || r.LastUsedAt.After(*overall.LastUsedAt) {
|
||||
overall.LastUsedAt = r.LastUsedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if overall.Count > 0 {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"hr_receiver/config"
|
||||
"hr_receiver/models"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config.InitConfig()
|
||||
config.ConnectDB()
|
||||
|
||||
// 生成100条测试数据
|
||||
count := 100
|
||||
records := make([]models.AIAnalysisRecord, 0, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
records = append(records, generateRecord())
|
||||
}
|
||||
|
||||
if err := config.DB.CreateInBatches(records, 50).Error; err != nil {
|
||||
panic("failed to insert mock data: " + err.Error())
|
||||
}
|
||||
|
||||
fmt.Printf("成功插入 %d 条 AI 分析记录\n", count)
|
||||
}
|
||||
|
||||
func generateRecord() models.AIAnalysisRecord {
|
||||
// regionID 为 1 或 3
|
||||
regionID := uint32(1)
|
||||
if rand.Intn(2) == 1 {
|
||||
regionID = 3
|
||||
}
|
||||
|
||||
// sourceType: upload 或 cloud
|
||||
sourceType := "upload"
|
||||
if rand.Intn(2) == 1 {
|
||||
sourceType = "cloud"
|
||||
}
|
||||
|
||||
// docx 教案原始文件大小: 50KB ~ 500KB
|
||||
docxSize := int64(rand.Intn(451*1024) + 50*1024)
|
||||
|
||||
// 心率 csv 原始文件大小: 约 80KB (70KB ~ 90KB)
|
||||
csvSize := int64(rand.Intn(20*1024) + 70*1024)
|
||||
|
||||
// 步数 csv 原始文件大小: 约 20KB ~ 40KB (heart_rate_with_steps 时才有)
|
||||
var stepCsvSize int64
|
||||
analysisType := analysisType()
|
||||
if analysisType == "heart_rate_with_steps" {
|
||||
stepCsvSize = int64(rand.Intn(20*1024) + 20*1024)
|
||||
}
|
||||
|
||||
originalFileSize := docxSize + csvSize + stepCsvSize
|
||||
|
||||
// 压缩后内容大小: csv 每4行保留1行,大约压缩为 25% + 表头;docx 提取文本后大约 30%~60%
|
||||
compressedDocx := int64(float64(docxSize) * (0.3 + rand.Float64()*0.3))
|
||||
compressedCsv := int64(float64(csvSize) * (0.22 + rand.Float64()*0.08)) // ~22%-30%
|
||||
var compressedStepCsv int64
|
||||
if stepCsvSize > 0 {
|
||||
compressedStepCsv = int64(float64(stepCsvSize) * (0.22 + rand.Float64()*0.08))
|
||||
}
|
||||
compressedContentSize := compressedDocx + compressedCsv + compressedStepCsv
|
||||
|
||||
// prompt 大小 = 压缩后内容 + 提示词模板 (~1.5KB)
|
||||
promptTemplateSize := 1500 + rand.Intn(500)
|
||||
inputSizeBytes := int(compressedContentSize) + promptTemplateSize
|
||||
|
||||
// AI 输出大小: 3KB ~ 25KB (分析报告)
|
||||
outputSizeBytes := rand.Intn(22*1024) + 3*1024
|
||||
|
||||
// token 估算: 中文混合场景,平均约 3.5 字节/token
|
||||
inputTokens := inputSizeBytes / (3 + rand.Intn(2))
|
||||
outputTokens := outputSizeBytes / (3 + rand.Intn(2))
|
||||
|
||||
// 分析时长: 主要和输出 token 数量相关,1分钟以内
|
||||
// 基础延迟 500ms + 每token约 15~40ms
|
||||
tokenLatency := int64(15 + rand.Intn(26))
|
||||
durationMs := 500 + int64(outputTokens)*tokenLatency
|
||||
if durationMs > 60000 {
|
||||
durationMs = 60000 - int64(rand.Intn(5000))
|
||||
}
|
||||
|
||||
// 上传时间: 最近 90 天内随机
|
||||
uploadTime := time.Now().Add(-time.Duration(rand.Intn(90*24)) * time.Hour).Add(-time.Duration(rand.Intn(60)) * time.Minute).UnixMilli()
|
||||
|
||||
return models.AIAnalysisRecord{
|
||||
RegionID: ®ionID,
|
||||
SourceType: sourceType,
|
||||
InputTokens: inputTokens,
|
||||
OutputTokens: outputTokens,
|
||||
InputSizeBytes: inputSizeBytes,
|
||||
OutputSizeBytes: outputSizeBytes,
|
||||
DurationMs: durationMs,
|
||||
OriginalFileSize: originalFileSize,
|
||||
CompressedContentSize: compressedContentSize,
|
||||
UploadTime: uploadTime,
|
||||
}
|
||||
}
|
||||
|
||||
func analysisType() string {
|
||||
// 约 30% 的带步数分析
|
||||
if rand.Intn(100) < 30 {
|
||||
return "heart_rate_with_steps"
|
||||
}
|
||||
return "heart_rate_only"
|
||||
}
|
||||
Reference in New Issue
Block a user