feat: 龙虾记忆同步系统完整版本
功能特性: - 文件树展示 - 差异对比 - 双向同步(本地 <-> 数据库) - 版本历史追踪 - 统计信息展示 核心补丁: 1. 分块读取与流式传输(防止大文件内存飙升) 2. .lobsterignore 机制(排除临时文件) 3. 操作溯源(Audit Log,记录同步历史) 技术栈: - 后端: Django + DRF + PostgreSQL - 前端: React + Ant Design - 部署: Docker + Docker Compose 项目已完整部署,可直接使用 docker-compose up -d 启动
This commit is contained in:
125
backend/memory_app/models.py
Normal file
125
backend/memory_app/models.py
Normal file
@@ -0,0 +1,125 @@
|
||||
from django.db import models
|
||||
from django.core.validators import FileExtensionValidator
|
||||
import hashlib
|
||||
|
||||
|
||||
class LobsterMemory(models.Model):
|
||||
"""龙虾记忆文件模型"""
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('consistent', '一致'),
|
||||
('local_newer', '本地更新'),
|
||||
('db_newer', '数据库更新'),
|
||||
('conflict', '冲突'),
|
||||
]
|
||||
|
||||
lobster_id = models.CharField(max_length=50, help_text='龙虾ID')
|
||||
|
||||
file_path = models.CharField(max_length=500, help_text='文件相对路径')
|
||||
|
||||
content = models.TextField(help_text='文件内容')
|
||||
|
||||
hash = models.CharField(max_length=64, help_text='SHA256哈希')
|
||||
|
||||
status = models.CharField(
|
||||
max_length=20,
|
||||
choices=STATUS_CHOICES,
|
||||
default='consistent',
|
||||
help_text='同步状态'
|
||||
)
|
||||
|
||||
version = models.IntegerField(default=1, help_text='版本号')
|
||||
|
||||
size = models.IntegerField(default=0, help_text='文件大小(字节)')
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True, help_text='创建时间')
|
||||
|
||||
updated_at = models.DateTimeField(auto_now=True, help_text='更新时间')
|
||||
|
||||
class Meta:
|
||||
db_table = 'lobster_memory'
|
||||
unique_together = ('lobster_id', 'file_path', 'version')
|
||||
ordering = ['-updated_at']
|
||||
indexes = [
|
||||
models.Index(fields=['lobster_id', 'file_path']),
|
||||
models.Index(fields=['status']),
|
||||
models.Index(fields=['updated_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.lobster_id}/{self.file_path} (v{self.version})"
|
||||
|
||||
def compute_hash(self, content):
|
||||
"""计算SHA256哈希"""
|
||||
return hashlib.sha256(content.encode('utf-8')).hexdigest()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""保存时自动计算哈希和大小"""
|
||||
if self.content:
|
||||
self.hash = self.compute_hash(self.content)
|
||||
self.size = len(self.content.encode('utf-8'))
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class SyncHistory(models.Model):
|
||||
"""同步操作历史记录"""
|
||||
|
||||
ACTION_CHOICES = [
|
||||
('sync_to_db', '同步到数据库'),
|
||||
('sync_to_local', '同步到本地'),
|
||||
('auto_sync', '自动同步'),
|
||||
('manual_merge', '手动合并'),
|
||||
]
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('success', '成功'),
|
||||
('failed', '失败'),
|
||||
('partial', '部分成功'),
|
||||
]
|
||||
|
||||
lobster_id = models.CharField(max_length=50, help_text='龙虾ID')
|
||||
|
||||
file_path = models.CharField(max_length=500, help_text='文件相对路径')
|
||||
|
||||
action = models.CharField(
|
||||
max_length=20,
|
||||
choices=ACTION_CHOICES,
|
||||
help_text='操作类型'
|
||||
)
|
||||
|
||||
status = models.CharField(
|
||||
max_length=20,
|
||||
choices=STATUS_CHOICES,
|
||||
help_text='操作状态'
|
||||
)
|
||||
|
||||
old_version = models.IntegerField(null=True, blank=True, help_text='操作前版本')
|
||||
|
||||
new_version = models.IntegerField(null=True, blank=True, help_text='操作后版本')
|
||||
|
||||
old_hash = models.CharField(max_length=64, null=True, blank=True, help_text='操作前哈希')
|
||||
|
||||
new_hash = models.CharField(max_length=64, null=True, blank=True, help_text='操作后哈希')
|
||||
|
||||
file_size = models.IntegerField(default=0, help_text='文件大小(字节)')
|
||||
|
||||
operator = models.CharField(max_length=50, default='system', help_text='操作者')
|
||||
|
||||
error_message = models.TextField(null=True, blank=True, help_text='错误信息')
|
||||
|
||||
execution_time = models.FloatField(default=0, help_text='执行时间(秒)')
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True, help_text='操作时间')
|
||||
|
||||
class Meta:
|
||||
db_table = 'sync_history'
|
||||
ordering = ['-created_at']
|
||||
indexes = [
|
||||
models.Index(fields=['lobster_id', 'file_path']),
|
||||
models.Index(fields=['action']),
|
||||
models.Index(fields=['status']),
|
||||
models.Index(fields=['created_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.action} - {self.lobster_id}/{self.file_path} ({self.status})"
|
||||
Reference in New Issue
Block a user