| | |
| | | "context"
|
| | | "fmt"
|
| | | "math"
|
| | | "os"
|
| | | "path/filepath"
|
| | | "sort"
|
| | | "strconv"
|
| | | "strings"
|
| | | "time"
|
| | |
|
| | | "basic.com/valib/logger.git"
|
| | | "github.com/milvus-io/milvus-sdk-go/v2/entity"
|
| | | )
|
| | |
|
| | |
| | | IsWarning int64 `json:"is_warning"`
|
| | | ZhDescClass string `json:"zh_desc_class"`
|
| | | TaskName string `json:"task_name"`
|
| | | WarnTaskName string `json:"warn_task_name"`
|
| | | EventLevelName string `json:"event_level_name"`
|
| | | DetectNum int64 `json:"detect_num"`
|
| | | Suggestion string `json:"suggestion"`
|
| | |
| | | }
|
| | |
|
| | | type TaskOption struct {
|
| | | TaskId int64 `db:"task_id" json:"taskId"`
|
| | | TaskName string `db:"task_name" json:"taskName"`
|
| | | TaskId int64 `db:"task_id" json:"taskId"`
|
| | | TaskName string `db:"task_name" json:"taskName"`
|
| | | EventLevel int64 `db:"event_level" json:"eventLevel"`
|
| | | }
|
| | |
|
| | | type CheckOption struct {
|
| | |
| | | Warning int64 `json:"warning`
|
| | | Page int64 `json:"page"`
|
| | | PageSize int64 `json:"pageSize"`
|
| | | }
|
| | |
|
| | | // 时间段
|
| | | type TimeRange struct {
|
| | | StartTime string `json:"startTime"`
|
| | | EndTime string `json:"endTime"`
|
| | | }
|
| | |
|
| | | // 删除日志
|
| | | func DeleteRecord(collectionName string, filterExpr string) (int, error) {
|
| | | dataOp := &DataOperator{client: dbClient}
|
| | | deletedFiles, err := dataOp.DelRecord(collectionName, filterExpr)
|
| | | return deletedFiles, err
|
| | | }
|
| | |
|
| | | // 清理日志
|
| | | func (do *DataOperator) DelRecord(collectionName string, filterExpr string) (int, error) {
|
| | | ctx := context.Background()
|
| | | queryResult, err := do.client.client.Query(
|
| | | ctx,
|
| | | collectionName,
|
| | | []string{}, // 所有分区
|
| | | filterExpr,
|
| | | []string{"id", "image_path", "video_path", "image_desc_path"}, // 返回所有字段
|
| | | // client.WithLimit(pageSize),
|
| | | // client.WithOffset(offset),
|
| | | )
|
| | |
|
| | | // 2. 提取ID和文件路径
|
| | | var ids []int64
|
| | | var imgPaths []string
|
| | | var videoPaths []string
|
| | | var imgDesPaths []string
|
| | | if len(queryResult) == 0 {
|
| | | return 0, nil
|
| | | }
|
| | | count := queryResult[0].Len()
|
| | | for i := 0; i < count; i++ {
|
| | | for _, field := range queryResult {
|
| | | switch field.Name() {
|
| | | case "id":
|
| | | IdColumn := field.(*entity.ColumnInt64).Data()
|
| | | if len(IdColumn) > 0 {
|
| | | id := IdColumn[i]
|
| | | ids = append(ids, id)
|
| | | }
|
| | | case "image_path":
|
| | | ImagePathColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(ImagePathColumn) > 0 {
|
| | | imagePath := ImagePathColumn[i]
|
| | | imgPaths = append(imgPaths, imagePath)
|
| | | }
|
| | | case "image_desc_path":
|
| | | ImageDesPathColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(ImageDesPathColumn) > 0 {
|
| | | imageDesPath := ImageDesPathColumn[i]
|
| | | imgDesPaths = append(imgDesPaths, imageDesPath)
|
| | | }
|
| | | case "video_path":
|
| | | VideoPathColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(VideoPathColumn) > 0 {
|
| | | VideoPath := VideoPathColumn[i]
|
| | | videoPaths = append(videoPaths, VideoPath)
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | if len(ids) > 0 {
|
| | | idField := entity.NewColumnInt64("id", ids)
|
| | | err = do.client.client.DeleteByPks(ctx, collectionName, "", idField)
|
| | | if err != nil {
|
| | | return 0, fmt.Errorf("failed to delete from Milvus: %v", err)
|
| | | }
|
| | | err = do.client.client.DeleteByPks(ctx, "smartrag", "", idField)
|
| | | if err != nil {
|
| | | return 0, fmt.Errorf("failed to delete from Milvus: %v", err)
|
| | | }
|
| | | }
|
| | |
|
| | | // 图片删除文件
|
| | | deletedFiles := 0
|
| | | for _, path := range imgPaths {
|
| | | if err := do.deleteFile(path); err != nil {
|
| | | logger.Debug("failed to delete file " + path + " " + err.Error())
|
| | | } else {
|
| | | deletedFiles++
|
| | | }
|
| | | }
|
| | | //删除描述图片
|
| | | for _, path := range imgDesPaths {
|
| | | if err := do.deleteFile(path); err != nil {
|
| | | logger.Debug("failed to delete file " + path + " " + err.Error())
|
| | | } else {
|
| | | deletedFiles++
|
| | | }
|
| | | }
|
| | |
|
| | | //删除视频文件
|
| | | for _, path := range videoPaths {
|
| | | if err := do.deleteFile(path); err != nil {
|
| | | logger.Debug("failed to delete file " + path + " " + err.Error())
|
| | | } else {
|
| | | deletedFiles++
|
| | | }
|
| | | }
|
| | |
|
| | | return deletedFiles, err
|
| | | }
|
| | |
|
| | | func (do *DataOperator) deleteFile(relativePath string) error {
|
| | | fullPath := relativePath
|
| | |
|
| | | // 安全检查,防止路径遍历攻击
|
| | | if !isSafePath(fullPath) {
|
| | | return fmt.Errorf("invalid file path")
|
| | | }
|
| | |
|
| | | if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
| | | return fmt.Errorf("file does not exist")
|
| | | }
|
| | |
|
| | | return os.Remove(fullPath)
|
| | | }
|
| | |
|
| | | // isSafePath 检查路径是否安全,防止路径遍历攻击
|
| | | func isSafePath(targetPath string) bool {
|
| | | rel := targetPath
|
| | | return rel != ".." && rel[:2] != ".."+string(filepath.Separator)
|
| | | }
|
| | |
|
| | | // 根据分页取数据
|
| | |
| | | if len(VideoPointIdColumn) > 0 {
|
| | | record.VideoPointId = VideoPointIdColumn[i]
|
| | | }
|
| | | case "video_point_name":
|
| | | VideoPointColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(VideoPointColumn) > 0 {
|
| | | record.CameraName = VideoPointColumn[i]
|
| | | }
|
| | | case "detect_id":
|
| | | DetectIdColumn := field.(*entity.ColumnInt64Array).Data()
|
| | | if len(DetectIdColumn) > 0 {
|
| | |
| | | TNColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(TNColumn) > 0 {
|
| | | record.TaskName = TNColumn[i]
|
| | | }
|
| | | case "warn_task_id":
|
| | | WarnColumn := field.(*entity.ColumnVarChar).Data()
|
| | | if len(WarnColumn) > 0 {
|
| | | record.WarnTaskName = WarnColumn[i]
|
| | | }
|
| | | case "event_level_name":
|
| | | EVColumn := field.(*entity.ColumnVarChar).Data()
|
| | |
| | | []string{}, // 所有分区
|
| | | filterExpr,
|
| | | []string{"rule_id", "task_id", "is_waning", "zh_desc_class", "task_name", "event_level_name", "detect_num",
|
| | | "event_level_id", "video_point_id", "detect_id", "image_path",
|
| | | "video_path", "detect_time", "knowledge_id", "risk_description", "suggestion", "id", "is_desc"}, // 返回所有字段
|
| | | "event_level_id", "video_point_id", "video_point_name", "detect_id", "image_path",
|
| | | "video_path", "detect_time", "knowledge_id", "risk_description", "suggestion", "id", "is_desc", "warn_task_id"}, // 返回所有字段
|
| | | // client.WithLimit(pageSize),
|
| | | // client.WithOffset(offset),
|
| | | )
|
| | |
| | | placeholders[i] = "?"
|
| | | args[i] = id
|
| | | }
|
| | | sqlStr := `select task_id,task_name from mal_smart_task where task_id in (` + strings.Join(placeholders, ",") + `)`
|
| | | if err := db.Raw(sqlStr).Scan(&items).Error; err != nil {
|
| | | sqlStr := `select task_id,task_name,event_level from mal_smart_task where task_id in (` + strings.Join(placeholders, ",") + `)`
|
| | | if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil {
|
| | | return nil, err
|
| | | }
|
| | | return
|