< Summary

Information
Class: CounterDrone.Core.Simulation.FrameDataStore
Assembly: CounterDrone.Core
File(s): C:\Users\Tellme\apps\CounterDroneBackend\src\CounterDrone.Core\Simulation\FrameDataStore.cs
Line coverage
82%
Covered lines: 48
Uncovered lines: 10
Coverable lines: 58
Total lines: 88
Line coverage: 82.7%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_RetentionDays()100%11100%
.ctor(...)100%11100%
CreateOrOpen(...)100%11100%
WriteFrame(...)100%22100%
ReadFrames(...)100%210%
DeleteFrameDb(...)0%620%
CleanupExpired()83.33%66100%

File(s)

C:\Users\Tellme\apps\CounterDroneBackend\src\CounterDrone.Core\Simulation\FrameDataStore.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using CounterDrone.Core.Models;
 5using SQLite;
 6
 7namespace CounterDrone.Core.Simulation
 8{
 9    public class FrameDataStore
 10    {
 11        private readonly IPathProvider _paths;
 12        /// <summary>帧数据保留天数,默认 180 天(6 个月)</summary>
 2013        public int RetentionDays { get; set; } = 180;
 14
 1515        public FrameDataStore(IPathProvider paths)
 1516        {
 1517            _paths = paths;
 1518        }
 19
 20        public SQLiteConnection CreateOrOpen(string taskId)
 1521        {
 1522            var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db");
 1523            var db = new SQLiteConnection(dbPath);
 1524            db.CreateTable<SimFrameRecord>();
 1525            db.CreateIndex("SimFrameRecord", new[] { "TaskId", "FrameIndex" });
 1526            return db;
 1527        }
 28
 29        public void WriteFrame(SQLiteConnection db, string taskId, int frameIdx, float timestamp, List<EntitySnapshot> s
 554430        {
 2844831            foreach (var s in snapshots)
 590832            {
 590833                db.Insert(new SimFrameRecord
 590834                {
 590835                    TaskId = taskId,
 590836                    FrameIndex = frameIdx,
 590837                    Timestamp = timestamp,
 590838                    EntityId = s.EntityId,
 590839                    EntityType = (int)s.EntityType,
 590840                    PosX = s.PosX,
 590841                    PosY = s.PosY,
 590842                    PosZ = s.PosZ,
 590843                    RotX = s.RotX,
 590844                    RotY = s.RotY,
 590845                    RotZ = s.RotZ,
 590846                    StateData = s.StateData,
 590847                });
 590848            }
 554449        }
 50
 51        public List<SimFrameRecord> ReadFrames(SQLiteConnection db, string taskId, int fromFrame, int toFrame)
 052        {
 053            return db.Table<SimFrameRecord>()
 054                     .Where(f => f.TaskId == taskId && f.FrameIndex >= fromFrame && f.FrameIndex <= toFrame)
 055                     .OrderBy(f => f.FrameIndex)
 056                     .ToList();
 057        }
 58
 59        public void DeleteFrameDb(string taskId)
 060        {
 061            var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db");
 062            if (File.Exists(dbPath)) File.Delete(dbPath);
 063        }
 64
 65        /// <summary>清理过期的帧数据库文件</summary>
 66        /// <returns>被删除的文件数量</returns>
 67        public int CleanupExpired()
 368        {
 369            var dir = _paths.GetFramesDir();
 370            if (!Directory.Exists(dir)) return 0;
 71
 372            var cutoff = DateTime.Now.AddDays(-RetentionDays);
 373            var deleted = 0;
 74
 1575            foreach (var file in Directory.GetFiles(dir, "*.db"))
 376            {
 377                var creationTime = File.GetCreationTime(file);
 378                if (creationTime < cutoff)
 279                {
 280                    File.Delete(file);
 281                    deleted++;
 282                }
 383            }
 84
 385            return deleted;
 386        }
 87    }
 88}