36451a613d
- Added comprehensive technical design document for AI Brand Monitoring System V5, outlining system architecture, data models, sampling strategies, and monitoring protocols. - Key changes include a shift to a sampling-based trend monitoring approach, updated data collection and storage strategies, and new metrics for performance evaluation. - Implemented migration scripts to support the flattening of brand questions and versioning of question texts, ensuring historical data integrity and version control.
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type WorkspaceRecentArticle struct {
|
|
ID int64
|
|
GenerateStatus string
|
|
PublishStatus string
|
|
SourceType string
|
|
GenerationMode *string
|
|
CreatedAt time.Time
|
|
Title *string
|
|
WordCount int
|
|
SourceLabel *string
|
|
TemplateName *string
|
|
}
|
|
|
|
type WorkspacePlan struct {
|
|
PlanCode string
|
|
PlanName string
|
|
QuotaPolicyJSON []byte
|
|
StartAt time.Time
|
|
EndAt time.Time
|
|
}
|
|
|
|
type WorkspaceRepository interface {
|
|
CountArticlesByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
CountPublishedArticlesByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error)
|
|
GetRecentArticles(ctx context.Context, tenantID int64) ([]WorkspaceRecentArticle, error)
|
|
GetQuotaSummary(ctx context.Context, tenantID int64) (int, error)
|
|
GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error)
|
|
}
|
|
|
|
type workspaceRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewWorkspaceRepository(db generated.DBTX) WorkspaceRepository {
|
|
return &workspaceRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *workspaceRepository) CountArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountArticlesByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) CountPublishedArticlesByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountPublishedArticlesByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error) {
|
|
return r.q.CountBrandsByTenant(ctx, tenantID)
|
|
}
|
|
|
|
func (r *workspaceRepository) GetRecentArticles(ctx context.Context, tenantID int64) ([]WorkspaceRecentArticle, error) {
|
|
rows, err := r.q.GetRecentArticles(ctx, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
articles := make([]WorkspaceRecentArticle, 0, len(rows))
|
|
for _, row := range rows {
|
|
articles = append(articles, WorkspaceRecentArticle{
|
|
ID: row.ID,
|
|
GenerateStatus: row.GenerateStatus,
|
|
PublishStatus: row.PublishStatus,
|
|
SourceType: row.SourceType,
|
|
GenerationMode: nullableAnyText(row.GenerationMode),
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
Title: nullableText(row.Title),
|
|
WordCount: intFromInt4(row.WordCount),
|
|
SourceLabel: nullableText(row.SourceLabel),
|
|
TemplateName: nullableText(row.TemplateName),
|
|
})
|
|
}
|
|
|
|
return articles, nil
|
|
}
|
|
|
|
func (r *workspaceRepository) GetQuotaSummary(ctx context.Context, tenantID int64) (int, error) {
|
|
balance, err := r.q.GetQuotaSummary(ctx, tenantID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(balance), nil
|
|
}
|
|
|
|
func (r *workspaceRepository) GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error) {
|
|
row, err := r.q.GetActivePlanForTenant(ctx, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &WorkspacePlan{
|
|
PlanCode: row.PlanCode,
|
|
PlanName: row.PlanName,
|
|
QuotaPolicyJSON: row.QuotaPolicyJson,
|
|
StartAt: timeFromTimestamp(row.StartAt),
|
|
EndAt: timeFromTimestamp(row.EndAt),
|
|
}, nil
|
|
}
|