Files
sub2api/backend/ent/usagelog_update.go
IanShaw 45bd9ac705 运维监控系统安全加固和功能优化 (#21)
* fix(ops): 修复运维监控系统的关键安全和稳定性问题

## 修复内容

### P0 严重问题
1. **DNS Rebinding防护** (ops_alert_service.go)
   - 实现IP钉住机制防止验证后的DNS rebinding攻击
   - 自定义Transport.DialContext强制只允许拨号到验证过的公网IP
   - 扩展IP黑名单,包括云metadata地址(169.254.169.254)
   - 添加完整的单元测试覆盖

2. **OpsAlertService生命周期管理** (wire.go)
   - 在ProvideOpsMetricsCollector中添加opsAlertService.Start()调用
   - 确保stopCtx正确初始化,避免nil指针问题
   - 实现防御式启动,保证服务启动顺序

3. **数据库查询排序** (ops_repo.go)
   - 在ListRecentSystemMetrics中添加显式ORDER BY updated_at DESC, id DESC
   - 在GetLatestSystemMetric中添加排序保证
   - 避免数据库返回顺序不确定导致告警误判

### P1 重要问题
4. **并发安全** (ops_metrics_collector.go)
   - 为lastGCPauseTotal字段添加sync.Mutex保护
   - 防止数据竞争

5. **Goroutine泄漏** (ops_error_logger.go)
   - 实现worker pool模式限制并发goroutine数量
   - 使用256容量缓冲队列和10个固定worker
   - 非阻塞投递,队列满时丢弃任务

6. **生命周期控制** (ops_alert_service.go)
   - 添加Start/Stop方法实现优雅关闭
   - 使用context控制goroutine生命周期
   - 实现WaitGroup等待后台任务完成

7. **Webhook URL验证** (ops_alert_service.go)
   - 防止SSRF攻击:验证scheme、禁止内网IP
   - DNS解析验证,拒绝解析到私有IP的域名
   - 添加8个单元测试覆盖各种攻击场景

8. **资源泄漏** (ops_repo.go)
   - 修复多处defer rows.Close()问题
   - 简化冗余的defer func()包装

9. **HTTP超时控制** (ops_alert_service.go)
   - 创建带10秒超时的http.Client
   - 添加buildWebhookHTTPClient辅助函数
   - 防止HTTP请求无限期挂起

10. **数据库查询优化** (ops_repo.go)
    - 将GetWindowStats的4次独立查询合并为1次CTE查询
    - 减少网络往返和表扫描次数
    - 显著提升性能

11. **重试机制** (ops_alert_service.go)
    - 实现邮件发送重试:最多3次,指数退避(1s/2s/4s)
    - 添加webhook备用通道
    - 实现完整的错误处理和日志记录

12. **魔法数字** (ops_repo.go, ops_metrics_collector.go)
    - 提取硬编码数字为有意义的常量
    - 提高代码可读性和可维护性

## 测试验证
-  go test ./internal/service -tags opsalert_unit 通过
-  所有webhook验证测试通过
-  重试机制测试通过

## 影响范围
- 运维监控系统安全性显著提升
- 系统稳定性和性能优化
- 无破坏性变更,向后兼容

* feat(ops): 运维监控系统V2 - 完整实现

## 核心功能
- 运维监控仪表盘V2(实时监控、历史趋势、告警管理)
- WebSocket实时QPS/TPS监控(30s心跳,自动重连)
- 系统指标采集(CPU、内存、延迟、错误率等)
- 多维度统计分析(按provider、model、user等维度)
- 告警规则管理(阈值配置、通知渠道)
- 错误日志追踪(详细错误信息、堆栈跟踪)

## 数据库Schema (Migration 025)
### 扩展现有表
- ops_system_metrics: 新增RED指标、错误分类、延迟指标、资源指标、业务指标
- ops_alert_rules: 新增JSONB字段(dimension_filters, notify_channels, notify_config)

### 新增表
- ops_dimension_stats: 多维度统计数据
- ops_data_retention_config: 数据保留策略配置

### 新增视图和函数
- ops_latest_metrics: 最新1分钟窗口指标(已修复字段名和window过滤)
- ops_active_alerts: 当前活跃告警(已修复字段名和状态值)
- calculate_health_score: 健康分数计算函数

## 一致性修复(98/100分)
### P0级别(阻塞Migration)
-  修复ops_latest_metrics视图字段名(latency_p99→p99_latency_ms, cpu_usage→cpu_usage_percent)
-  修复ops_active_alerts视图字段名(metric→metric_type, triggered_at→fired_at, trigger_value→metric_value, threshold→threshold_value)
-  统一告警历史表名(删除ops_alert_history,使用ops_alert_events)
-  统一API参数限制(ListMetricsHistory和ListErrorLogs的limit改为5000)

### P1级别(功能完整性)
-  修复ops_latest_metrics视图未过滤window_minutes(添加WHERE m.window_minutes = 1)
-  修复数据回填UPDATE逻辑(QPS计算改为request_count/(window_minutes*60.0))
-  添加ops_alert_rules JSONB字段后端支持(Go结构体+序列化)

### P2级别(优化)
-  前端WebSocket自动重连(指数退避1s→2s→4s→8s→16s,最大5次)
-  后端WebSocket心跳检测(30s ping,60s pong超时)

## 技术实现
### 后端 (Go)
- Handler层: ops_handler.go(REST API), ops_ws_handler.go(WebSocket)
- Service层: ops_service.go(核心逻辑), ops_cache.go(缓存), ops_alerts.go(告警)
- Repository层: ops_repo.go(数据访问), ops.go(模型定义)
- 路由: admin.go(新增ops相关路由)
- 依赖注入: wire_gen.go(自动生成)

### 前端 (Vue3 + TypeScript)
- 组件: OpsDashboardV2.vue(仪表盘主组件)
- API: ops.ts(REST API + WebSocket封装)
- 路由: index.ts(新增/admin/ops路由)
- 国际化: en.ts, zh.ts(中英文支持)

## 测试验证
-  所有Go测试通过
-  Migration可正常执行
-  WebSocket连接稳定
-  前后端数据结构对齐

* refactor: 代码清理和测试优化

## 测试文件优化
- 简化integration test fixtures和断言
- 优化test helper函数
- 统一测试数据格式

## 代码清理
- 移除未使用的代码和注释
- 简化concurrency_cache实现
- 优化middleware错误处理

## 小修复
- 修复gateway_handler和openai_gateway_handler的小问题
- 统一代码风格和格式

变更统计: 27个文件,292行新增,322行删除(净减少30行)

* fix(ops): 运维监控系统安全加固和功能优化

## 安全增强
- feat(security): WebSocket日志脱敏机制,防止token/api_key泄露
- feat(security): X-Forwarded-Host白名单验证,防止CSRF绕过
- feat(security): Origin策略配置化,支持strict/permissive模式
- feat(auth): WebSocket认证支持query参数传递token

## 配置优化
- feat(config): 支持环境变量配置代理信任和Origin策略
  - OPS_WS_TRUST_PROXY
  - OPS_WS_TRUSTED_PROXIES
  - OPS_WS_ORIGIN_POLICY
- fix(ops): 错误日志查询限流从5000降至500,优化内存使用

## 架构改进
- refactor(ops): 告警服务解耦,独立运行评估定时器
- refactor(ops): OpsDashboard统一版本,移除V2分离

## 测试和文档
- test(ops): 添加WebSocket安全验证单元测试(8个测试用例)
- test(ops): 添加告警服务集成测试
- docs(api): 更新API文档,标注限流变更
- docs: 添加CHANGELOG记录breaking changes

## 修复文件
Backend:
- backend/internal/server/middleware/logger.go
- backend/internal/handler/admin/ops_handler.go
- backend/internal/handler/admin/ops_ws_handler.go
- backend/internal/server/middleware/admin_auth.go
- backend/internal/service/ops_alert_service.go
- backend/internal/service/ops_metrics_collector.go
- backend/internal/service/wire.go

Frontend:
- frontend/src/views/admin/ops/OpsDashboard.vue
- frontend/src/router/index.ts
- frontend/src/api/admin/ops.ts

Tests:
- backend/internal/handler/admin/ops_ws_handler_test.go (新增)
- backend/internal/service/ops_alert_service_integration_test.go (新增)

Docs:
- CHANGELOG.md (新增)
- docs/API-运维监控中心2.0.md (更新)

* fix(migrations): 修复calculate_health_score函数类型匹配问题

在ops_latest_metrics视图中添加显式类型转换,确保参数类型与函数签名匹配

* fix(lint): 修复golangci-lint检查发现的所有问题

- 将Redis依赖从service层移到repository层
- 添加错误检查(WebSocket连接和读取超时)
- 运行gofmt格式化代码
- 添加nil指针检查
- 删除未使用的alertService字段

修复问题:
- depguard: 3个(service层不应直接import redis)
- errcheck: 3个(未检查错误返回值)
- gofmt: 2个(代码格式问题)
- staticcheck: 4个(nil指针解引用)
- unused: 1个(未使用字段)

代码统计:
- 修改文件:11个
- 删除代码:490行
- 新增代码:105行
- 净减少:385行
2026-01-02 20:01:12 +08:00

1801 lines
55 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/account"
"github.com/Wei-Shaw/sub2api/ent/apikey"
"github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
"github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/usersubscription"
)
// UsageLogUpdate is the builder for updating UsageLog entities.
type UsageLogUpdate struct {
config
hooks []Hook
mutation *UsageLogMutation
}
// Where appends a list predicates to the UsageLogUpdate builder.
func (_u *UsageLogUpdate) Where(ps ...predicate.UsageLog) *UsageLogUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetUserID sets the "user_id" field.
func (_u *UsageLogUpdate) SetUserID(v int64) *UsageLogUpdate {
_u.mutation.SetUserID(v)
return _u
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableUserID(v *int64) *UsageLogUpdate {
if v != nil {
_u.SetUserID(*v)
}
return _u
}
// SetAPIKeyID sets the "api_key_id" field.
func (_u *UsageLogUpdate) SetAPIKeyID(v int64) *UsageLogUpdate {
_u.mutation.SetAPIKeyID(v)
return _u
}
// SetNillableAPIKeyID sets the "api_key_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableAPIKeyID(v *int64) *UsageLogUpdate {
if v != nil {
_u.SetAPIKeyID(*v)
}
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *UsageLogUpdate) SetAccountID(v int64) *UsageLogUpdate {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableAccountID(v *int64) *UsageLogUpdate {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetRequestID sets the "request_id" field.
func (_u *UsageLogUpdate) SetRequestID(v string) *UsageLogUpdate {
_u.mutation.SetRequestID(v)
return _u
}
// SetNillableRequestID sets the "request_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableRequestID(v *string) *UsageLogUpdate {
if v != nil {
_u.SetRequestID(*v)
}
return _u
}
// SetModel sets the "model" field.
func (_u *UsageLogUpdate) SetModel(v string) *UsageLogUpdate {
_u.mutation.SetModel(v)
return _u
}
// SetNillableModel sets the "model" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableModel(v *string) *UsageLogUpdate {
if v != nil {
_u.SetModel(*v)
}
return _u
}
// SetGroupID sets the "group_id" field.
func (_u *UsageLogUpdate) SetGroupID(v int64) *UsageLogUpdate {
_u.mutation.SetGroupID(v)
return _u
}
// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableGroupID(v *int64) *UsageLogUpdate {
if v != nil {
_u.SetGroupID(*v)
}
return _u
}
// ClearGroupID clears the value of the "group_id" field.
func (_u *UsageLogUpdate) ClearGroupID() *UsageLogUpdate {
_u.mutation.ClearGroupID()
return _u
}
// SetSubscriptionID sets the "subscription_id" field.
func (_u *UsageLogUpdate) SetSubscriptionID(v int64) *UsageLogUpdate {
_u.mutation.SetSubscriptionID(v)
return _u
}
// SetNillableSubscriptionID sets the "subscription_id" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableSubscriptionID(v *int64) *UsageLogUpdate {
if v != nil {
_u.SetSubscriptionID(*v)
}
return _u
}
// ClearSubscriptionID clears the value of the "subscription_id" field.
func (_u *UsageLogUpdate) ClearSubscriptionID() *UsageLogUpdate {
_u.mutation.ClearSubscriptionID()
return _u
}
// SetInputTokens sets the "input_tokens" field.
func (_u *UsageLogUpdate) SetInputTokens(v int) *UsageLogUpdate {
_u.mutation.ResetInputTokens()
_u.mutation.SetInputTokens(v)
return _u
}
// SetNillableInputTokens sets the "input_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableInputTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetInputTokens(*v)
}
return _u
}
// AddInputTokens adds value to the "input_tokens" field.
func (_u *UsageLogUpdate) AddInputTokens(v int) *UsageLogUpdate {
_u.mutation.AddInputTokens(v)
return _u
}
// SetOutputTokens sets the "output_tokens" field.
func (_u *UsageLogUpdate) SetOutputTokens(v int) *UsageLogUpdate {
_u.mutation.ResetOutputTokens()
_u.mutation.SetOutputTokens(v)
return _u
}
// SetNillableOutputTokens sets the "output_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableOutputTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetOutputTokens(*v)
}
return _u
}
// AddOutputTokens adds value to the "output_tokens" field.
func (_u *UsageLogUpdate) AddOutputTokens(v int) *UsageLogUpdate {
_u.mutation.AddOutputTokens(v)
return _u
}
// SetCacheCreationTokens sets the "cache_creation_tokens" field.
func (_u *UsageLogUpdate) SetCacheCreationTokens(v int) *UsageLogUpdate {
_u.mutation.ResetCacheCreationTokens()
_u.mutation.SetCacheCreationTokens(v)
return _u
}
// SetNillableCacheCreationTokens sets the "cache_creation_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheCreationTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetCacheCreationTokens(*v)
}
return _u
}
// AddCacheCreationTokens adds value to the "cache_creation_tokens" field.
func (_u *UsageLogUpdate) AddCacheCreationTokens(v int) *UsageLogUpdate {
_u.mutation.AddCacheCreationTokens(v)
return _u
}
// SetCacheReadTokens sets the "cache_read_tokens" field.
func (_u *UsageLogUpdate) SetCacheReadTokens(v int) *UsageLogUpdate {
_u.mutation.ResetCacheReadTokens()
_u.mutation.SetCacheReadTokens(v)
return _u
}
// SetNillableCacheReadTokens sets the "cache_read_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheReadTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetCacheReadTokens(*v)
}
return _u
}
// AddCacheReadTokens adds value to the "cache_read_tokens" field.
func (_u *UsageLogUpdate) AddCacheReadTokens(v int) *UsageLogUpdate {
_u.mutation.AddCacheReadTokens(v)
return _u
}
// SetCacheCreation5mTokens sets the "cache_creation_5m_tokens" field.
func (_u *UsageLogUpdate) SetCacheCreation5mTokens(v int) *UsageLogUpdate {
_u.mutation.ResetCacheCreation5mTokens()
_u.mutation.SetCacheCreation5mTokens(v)
return _u
}
// SetNillableCacheCreation5mTokens sets the "cache_creation_5m_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheCreation5mTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetCacheCreation5mTokens(*v)
}
return _u
}
// AddCacheCreation5mTokens adds value to the "cache_creation_5m_tokens" field.
func (_u *UsageLogUpdate) AddCacheCreation5mTokens(v int) *UsageLogUpdate {
_u.mutation.AddCacheCreation5mTokens(v)
return _u
}
// SetCacheCreation1hTokens sets the "cache_creation_1h_tokens" field.
func (_u *UsageLogUpdate) SetCacheCreation1hTokens(v int) *UsageLogUpdate {
_u.mutation.ResetCacheCreation1hTokens()
_u.mutation.SetCacheCreation1hTokens(v)
return _u
}
// SetNillableCacheCreation1hTokens sets the "cache_creation_1h_tokens" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheCreation1hTokens(v *int) *UsageLogUpdate {
if v != nil {
_u.SetCacheCreation1hTokens(*v)
}
return _u
}
// AddCacheCreation1hTokens adds value to the "cache_creation_1h_tokens" field.
func (_u *UsageLogUpdate) AddCacheCreation1hTokens(v int) *UsageLogUpdate {
_u.mutation.AddCacheCreation1hTokens(v)
return _u
}
// SetInputCost sets the "input_cost" field.
func (_u *UsageLogUpdate) SetInputCost(v float64) *UsageLogUpdate {
_u.mutation.ResetInputCost()
_u.mutation.SetInputCost(v)
return _u
}
// SetNillableInputCost sets the "input_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableInputCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetInputCost(*v)
}
return _u
}
// AddInputCost adds value to the "input_cost" field.
func (_u *UsageLogUpdate) AddInputCost(v float64) *UsageLogUpdate {
_u.mutation.AddInputCost(v)
return _u
}
// SetOutputCost sets the "output_cost" field.
func (_u *UsageLogUpdate) SetOutputCost(v float64) *UsageLogUpdate {
_u.mutation.ResetOutputCost()
_u.mutation.SetOutputCost(v)
return _u
}
// SetNillableOutputCost sets the "output_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableOutputCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetOutputCost(*v)
}
return _u
}
// AddOutputCost adds value to the "output_cost" field.
func (_u *UsageLogUpdate) AddOutputCost(v float64) *UsageLogUpdate {
_u.mutation.AddOutputCost(v)
return _u
}
// SetCacheCreationCost sets the "cache_creation_cost" field.
func (_u *UsageLogUpdate) SetCacheCreationCost(v float64) *UsageLogUpdate {
_u.mutation.ResetCacheCreationCost()
_u.mutation.SetCacheCreationCost(v)
return _u
}
// SetNillableCacheCreationCost sets the "cache_creation_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheCreationCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetCacheCreationCost(*v)
}
return _u
}
// AddCacheCreationCost adds value to the "cache_creation_cost" field.
func (_u *UsageLogUpdate) AddCacheCreationCost(v float64) *UsageLogUpdate {
_u.mutation.AddCacheCreationCost(v)
return _u
}
// SetCacheReadCost sets the "cache_read_cost" field.
func (_u *UsageLogUpdate) SetCacheReadCost(v float64) *UsageLogUpdate {
_u.mutation.ResetCacheReadCost()
_u.mutation.SetCacheReadCost(v)
return _u
}
// SetNillableCacheReadCost sets the "cache_read_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableCacheReadCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetCacheReadCost(*v)
}
return _u
}
// AddCacheReadCost adds value to the "cache_read_cost" field.
func (_u *UsageLogUpdate) AddCacheReadCost(v float64) *UsageLogUpdate {
_u.mutation.AddCacheReadCost(v)
return _u
}
// SetTotalCost sets the "total_cost" field.
func (_u *UsageLogUpdate) SetTotalCost(v float64) *UsageLogUpdate {
_u.mutation.ResetTotalCost()
_u.mutation.SetTotalCost(v)
return _u
}
// SetNillableTotalCost sets the "total_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableTotalCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetTotalCost(*v)
}
return _u
}
// AddTotalCost adds value to the "total_cost" field.
func (_u *UsageLogUpdate) AddTotalCost(v float64) *UsageLogUpdate {
_u.mutation.AddTotalCost(v)
return _u
}
// SetActualCost sets the "actual_cost" field.
func (_u *UsageLogUpdate) SetActualCost(v float64) *UsageLogUpdate {
_u.mutation.ResetActualCost()
_u.mutation.SetActualCost(v)
return _u
}
// SetNillableActualCost sets the "actual_cost" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableActualCost(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetActualCost(*v)
}
return _u
}
// AddActualCost adds value to the "actual_cost" field.
func (_u *UsageLogUpdate) AddActualCost(v float64) *UsageLogUpdate {
_u.mutation.AddActualCost(v)
return _u
}
// SetRateMultiplier sets the "rate_multiplier" field.
func (_u *UsageLogUpdate) SetRateMultiplier(v float64) *UsageLogUpdate {
_u.mutation.ResetRateMultiplier()
_u.mutation.SetRateMultiplier(v)
return _u
}
// SetNillableRateMultiplier sets the "rate_multiplier" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableRateMultiplier(v *float64) *UsageLogUpdate {
if v != nil {
_u.SetRateMultiplier(*v)
}
return _u
}
// AddRateMultiplier adds value to the "rate_multiplier" field.
func (_u *UsageLogUpdate) AddRateMultiplier(v float64) *UsageLogUpdate {
_u.mutation.AddRateMultiplier(v)
return _u
}
// SetBillingType sets the "billing_type" field.
func (_u *UsageLogUpdate) SetBillingType(v int8) *UsageLogUpdate {
_u.mutation.ResetBillingType()
_u.mutation.SetBillingType(v)
return _u
}
// SetNillableBillingType sets the "billing_type" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableBillingType(v *int8) *UsageLogUpdate {
if v != nil {
_u.SetBillingType(*v)
}
return _u
}
// AddBillingType adds value to the "billing_type" field.
func (_u *UsageLogUpdate) AddBillingType(v int8) *UsageLogUpdate {
_u.mutation.AddBillingType(v)
return _u
}
// SetStream sets the "stream" field.
func (_u *UsageLogUpdate) SetStream(v bool) *UsageLogUpdate {
_u.mutation.SetStream(v)
return _u
}
// SetNillableStream sets the "stream" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableStream(v *bool) *UsageLogUpdate {
if v != nil {
_u.SetStream(*v)
}
return _u
}
// SetDurationMs sets the "duration_ms" field.
func (_u *UsageLogUpdate) SetDurationMs(v int) *UsageLogUpdate {
_u.mutation.ResetDurationMs()
_u.mutation.SetDurationMs(v)
return _u
}
// SetNillableDurationMs sets the "duration_ms" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableDurationMs(v *int) *UsageLogUpdate {
if v != nil {
_u.SetDurationMs(*v)
}
return _u
}
// AddDurationMs adds value to the "duration_ms" field.
func (_u *UsageLogUpdate) AddDurationMs(v int) *UsageLogUpdate {
_u.mutation.AddDurationMs(v)
return _u
}
// ClearDurationMs clears the value of the "duration_ms" field.
func (_u *UsageLogUpdate) ClearDurationMs() *UsageLogUpdate {
_u.mutation.ClearDurationMs()
return _u
}
// SetFirstTokenMs sets the "first_token_ms" field.
func (_u *UsageLogUpdate) SetFirstTokenMs(v int) *UsageLogUpdate {
_u.mutation.ResetFirstTokenMs()
_u.mutation.SetFirstTokenMs(v)
return _u
}
// SetNillableFirstTokenMs sets the "first_token_ms" field if the given value is not nil.
func (_u *UsageLogUpdate) SetNillableFirstTokenMs(v *int) *UsageLogUpdate {
if v != nil {
_u.SetFirstTokenMs(*v)
}
return _u
}
// AddFirstTokenMs adds value to the "first_token_ms" field.
func (_u *UsageLogUpdate) AddFirstTokenMs(v int) *UsageLogUpdate {
_u.mutation.AddFirstTokenMs(v)
return _u
}
// ClearFirstTokenMs clears the value of the "first_token_ms" field.
func (_u *UsageLogUpdate) ClearFirstTokenMs() *UsageLogUpdate {
_u.mutation.ClearFirstTokenMs()
return _u
}
// SetUser sets the "user" edge to the User entity.
func (_u *UsageLogUpdate) SetUser(v *User) *UsageLogUpdate {
return _u.SetUserID(v.ID)
}
// SetAPIKey sets the "api_key" edge to the APIKey entity.
func (_u *UsageLogUpdate) SetAPIKey(v *APIKey) *UsageLogUpdate {
return _u.SetAPIKeyID(v.ID)
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *UsageLogUpdate) SetAccount(v *Account) *UsageLogUpdate {
return _u.SetAccountID(v.ID)
}
// SetGroup sets the "group" edge to the Group entity.
func (_u *UsageLogUpdate) SetGroup(v *Group) *UsageLogUpdate {
return _u.SetGroupID(v.ID)
}
// SetSubscription sets the "subscription" edge to the UserSubscription entity.
func (_u *UsageLogUpdate) SetSubscription(v *UserSubscription) *UsageLogUpdate {
return _u.SetSubscriptionID(v.ID)
}
// Mutation returns the UsageLogMutation object of the builder.
func (_u *UsageLogUpdate) Mutation() *UsageLogMutation {
return _u.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (_u *UsageLogUpdate) ClearUser() *UsageLogUpdate {
_u.mutation.ClearUser()
return _u
}
// ClearAPIKey clears the "api_key" edge to the APIKey entity.
func (_u *UsageLogUpdate) ClearAPIKey() *UsageLogUpdate {
_u.mutation.ClearAPIKey()
return _u
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *UsageLogUpdate) ClearAccount() *UsageLogUpdate {
_u.mutation.ClearAccount()
return _u
}
// ClearGroup clears the "group" edge to the Group entity.
func (_u *UsageLogUpdate) ClearGroup() *UsageLogUpdate {
_u.mutation.ClearGroup()
return _u
}
// ClearSubscription clears the "subscription" edge to the UserSubscription entity.
func (_u *UsageLogUpdate) ClearSubscription() *UsageLogUpdate {
_u.mutation.ClearSubscription()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *UsageLogUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *UsageLogUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *UsageLogUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *UsageLogUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *UsageLogUpdate) check() error {
if v, ok := _u.mutation.RequestID(); ok {
if err := usagelog.RequestIDValidator(v); err != nil {
return &ValidationError{Name: "request_id", err: fmt.Errorf(`ent: validator failed for field "UsageLog.request_id": %w`, err)}
}
}
if v, ok := _u.mutation.Model(); ok {
if err := usagelog.ModelValidator(v); err != nil {
return &ValidationError{Name: "model", err: fmt.Errorf(`ent: validator failed for field "UsageLog.model": %w`, err)}
}
}
if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.user"`)
}
if _u.mutation.APIKeyCleared() && len(_u.mutation.APIKeyIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.api_key"`)
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.account"`)
}
return nil
}
func (_u *UsageLogUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(usagelog.Table, usagelog.Columns, sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64))
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.RequestID(); ok {
_spec.SetField(usagelog.FieldRequestID, field.TypeString, value)
}
if value, ok := _u.mutation.Model(); ok {
_spec.SetField(usagelog.FieldModel, field.TypeString, value)
}
if value, ok := _u.mutation.InputTokens(); ok {
_spec.SetField(usagelog.FieldInputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedInputTokens(); ok {
_spec.AddField(usagelog.FieldInputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.OutputTokens(); ok {
_spec.SetField(usagelog.FieldOutputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedOutputTokens(); ok {
_spec.AddField(usagelog.FieldOutputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreationTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreationTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreationTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreationTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheReadTokens(); ok {
_spec.SetField(usagelog.FieldCacheReadTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheReadTokens(); ok {
_spec.AddField(usagelog.FieldCacheReadTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreation5mTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreation5mTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreation5mTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreation5mTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreation1hTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreation1hTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreation1hTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreation1hTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.InputCost(); ok {
_spec.SetField(usagelog.FieldInputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedInputCost(); ok {
_spec.AddField(usagelog.FieldInputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.OutputCost(); ok {
_spec.SetField(usagelog.FieldOutputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedOutputCost(); ok {
_spec.AddField(usagelog.FieldOutputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.CacheCreationCost(); ok {
_spec.SetField(usagelog.FieldCacheCreationCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedCacheCreationCost(); ok {
_spec.AddField(usagelog.FieldCacheCreationCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.CacheReadCost(); ok {
_spec.SetField(usagelog.FieldCacheReadCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedCacheReadCost(); ok {
_spec.AddField(usagelog.FieldCacheReadCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.TotalCost(); ok {
_spec.SetField(usagelog.FieldTotalCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedTotalCost(); ok {
_spec.AddField(usagelog.FieldTotalCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.ActualCost(); ok {
_spec.SetField(usagelog.FieldActualCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedActualCost(); ok {
_spec.AddField(usagelog.FieldActualCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.RateMultiplier(); ok {
_spec.SetField(usagelog.FieldRateMultiplier, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedRateMultiplier(); ok {
_spec.AddField(usagelog.FieldRateMultiplier, field.TypeFloat64, value)
}
if value, ok := _u.mutation.BillingType(); ok {
_spec.SetField(usagelog.FieldBillingType, field.TypeInt8, value)
}
if value, ok := _u.mutation.AddedBillingType(); ok {
_spec.AddField(usagelog.FieldBillingType, field.TypeInt8, value)
}
if value, ok := _u.mutation.Stream(); ok {
_spec.SetField(usagelog.FieldStream, field.TypeBool, value)
}
if value, ok := _u.mutation.DurationMs(); ok {
_spec.SetField(usagelog.FieldDurationMs, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedDurationMs(); ok {
_spec.AddField(usagelog.FieldDurationMs, field.TypeInt, value)
}
if _u.mutation.DurationMsCleared() {
_spec.ClearField(usagelog.FieldDurationMs, field.TypeInt)
}
if value, ok := _u.mutation.FirstTokenMs(); ok {
_spec.SetField(usagelog.FieldFirstTokenMs, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedFirstTokenMs(); ok {
_spec.AddField(usagelog.FieldFirstTokenMs, field.TypeInt, value)
}
if _u.mutation.FirstTokenMsCleared() {
_spec.ClearField(usagelog.FieldFirstTokenMs, field.TypeInt)
}
if _u.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.UserTable,
Columns: []string{usagelog.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.UserTable,
Columns: []string{usagelog.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.APIKeyCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.APIKeyTable,
Columns: []string{usagelog.APIKeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.APIKeyIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.APIKeyTable,
Columns: []string{usagelog.APIKeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.AccountTable,
Columns: []string{usagelog.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.AccountTable,
Columns: []string{usagelog.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.GroupCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.GroupTable,
Columns: []string{usagelog.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.GroupTable,
Columns: []string{usagelog.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.SubscriptionCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.SubscriptionTable,
Columns: []string{usagelog.SubscriptionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.SubscriptionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.SubscriptionTable,
Columns: []string{usagelog.SubscriptionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{usagelog.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// UsageLogUpdateOne is the builder for updating a single UsageLog entity.
type UsageLogUpdateOne struct {
config
fields []string
hooks []Hook
mutation *UsageLogMutation
}
// SetUserID sets the "user_id" field.
func (_u *UsageLogUpdateOne) SetUserID(v int64) *UsageLogUpdateOne {
_u.mutation.SetUserID(v)
return _u
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableUserID(v *int64) *UsageLogUpdateOne {
if v != nil {
_u.SetUserID(*v)
}
return _u
}
// SetAPIKeyID sets the "api_key_id" field.
func (_u *UsageLogUpdateOne) SetAPIKeyID(v int64) *UsageLogUpdateOne {
_u.mutation.SetAPIKeyID(v)
return _u
}
// SetNillableAPIKeyID sets the "api_key_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableAPIKeyID(v *int64) *UsageLogUpdateOne {
if v != nil {
_u.SetAPIKeyID(*v)
}
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *UsageLogUpdateOne) SetAccountID(v int64) *UsageLogUpdateOne {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableAccountID(v *int64) *UsageLogUpdateOne {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetRequestID sets the "request_id" field.
func (_u *UsageLogUpdateOne) SetRequestID(v string) *UsageLogUpdateOne {
_u.mutation.SetRequestID(v)
return _u
}
// SetNillableRequestID sets the "request_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableRequestID(v *string) *UsageLogUpdateOne {
if v != nil {
_u.SetRequestID(*v)
}
return _u
}
// SetModel sets the "model" field.
func (_u *UsageLogUpdateOne) SetModel(v string) *UsageLogUpdateOne {
_u.mutation.SetModel(v)
return _u
}
// SetNillableModel sets the "model" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableModel(v *string) *UsageLogUpdateOne {
if v != nil {
_u.SetModel(*v)
}
return _u
}
// SetGroupID sets the "group_id" field.
func (_u *UsageLogUpdateOne) SetGroupID(v int64) *UsageLogUpdateOne {
_u.mutation.SetGroupID(v)
return _u
}
// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableGroupID(v *int64) *UsageLogUpdateOne {
if v != nil {
_u.SetGroupID(*v)
}
return _u
}
// ClearGroupID clears the value of the "group_id" field.
func (_u *UsageLogUpdateOne) ClearGroupID() *UsageLogUpdateOne {
_u.mutation.ClearGroupID()
return _u
}
// SetSubscriptionID sets the "subscription_id" field.
func (_u *UsageLogUpdateOne) SetSubscriptionID(v int64) *UsageLogUpdateOne {
_u.mutation.SetSubscriptionID(v)
return _u
}
// SetNillableSubscriptionID sets the "subscription_id" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableSubscriptionID(v *int64) *UsageLogUpdateOne {
if v != nil {
_u.SetSubscriptionID(*v)
}
return _u
}
// ClearSubscriptionID clears the value of the "subscription_id" field.
func (_u *UsageLogUpdateOne) ClearSubscriptionID() *UsageLogUpdateOne {
_u.mutation.ClearSubscriptionID()
return _u
}
// SetInputTokens sets the "input_tokens" field.
func (_u *UsageLogUpdateOne) SetInputTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetInputTokens()
_u.mutation.SetInputTokens(v)
return _u
}
// SetNillableInputTokens sets the "input_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableInputTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetInputTokens(*v)
}
return _u
}
// AddInputTokens adds value to the "input_tokens" field.
func (_u *UsageLogUpdateOne) AddInputTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddInputTokens(v)
return _u
}
// SetOutputTokens sets the "output_tokens" field.
func (_u *UsageLogUpdateOne) SetOutputTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetOutputTokens()
_u.mutation.SetOutputTokens(v)
return _u
}
// SetNillableOutputTokens sets the "output_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableOutputTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetOutputTokens(*v)
}
return _u
}
// AddOutputTokens adds value to the "output_tokens" field.
func (_u *UsageLogUpdateOne) AddOutputTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddOutputTokens(v)
return _u
}
// SetCacheCreationTokens sets the "cache_creation_tokens" field.
func (_u *UsageLogUpdateOne) SetCacheCreationTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetCacheCreationTokens()
_u.mutation.SetCacheCreationTokens(v)
return _u
}
// SetNillableCacheCreationTokens sets the "cache_creation_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheCreationTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheCreationTokens(*v)
}
return _u
}
// AddCacheCreationTokens adds value to the "cache_creation_tokens" field.
func (_u *UsageLogUpdateOne) AddCacheCreationTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddCacheCreationTokens(v)
return _u
}
// SetCacheReadTokens sets the "cache_read_tokens" field.
func (_u *UsageLogUpdateOne) SetCacheReadTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetCacheReadTokens()
_u.mutation.SetCacheReadTokens(v)
return _u
}
// SetNillableCacheReadTokens sets the "cache_read_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheReadTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheReadTokens(*v)
}
return _u
}
// AddCacheReadTokens adds value to the "cache_read_tokens" field.
func (_u *UsageLogUpdateOne) AddCacheReadTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddCacheReadTokens(v)
return _u
}
// SetCacheCreation5mTokens sets the "cache_creation_5m_tokens" field.
func (_u *UsageLogUpdateOne) SetCacheCreation5mTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetCacheCreation5mTokens()
_u.mutation.SetCacheCreation5mTokens(v)
return _u
}
// SetNillableCacheCreation5mTokens sets the "cache_creation_5m_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheCreation5mTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheCreation5mTokens(*v)
}
return _u
}
// AddCacheCreation5mTokens adds value to the "cache_creation_5m_tokens" field.
func (_u *UsageLogUpdateOne) AddCacheCreation5mTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddCacheCreation5mTokens(v)
return _u
}
// SetCacheCreation1hTokens sets the "cache_creation_1h_tokens" field.
func (_u *UsageLogUpdateOne) SetCacheCreation1hTokens(v int) *UsageLogUpdateOne {
_u.mutation.ResetCacheCreation1hTokens()
_u.mutation.SetCacheCreation1hTokens(v)
return _u
}
// SetNillableCacheCreation1hTokens sets the "cache_creation_1h_tokens" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheCreation1hTokens(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheCreation1hTokens(*v)
}
return _u
}
// AddCacheCreation1hTokens adds value to the "cache_creation_1h_tokens" field.
func (_u *UsageLogUpdateOne) AddCacheCreation1hTokens(v int) *UsageLogUpdateOne {
_u.mutation.AddCacheCreation1hTokens(v)
return _u
}
// SetInputCost sets the "input_cost" field.
func (_u *UsageLogUpdateOne) SetInputCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetInputCost()
_u.mutation.SetInputCost(v)
return _u
}
// SetNillableInputCost sets the "input_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableInputCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetInputCost(*v)
}
return _u
}
// AddInputCost adds value to the "input_cost" field.
func (_u *UsageLogUpdateOne) AddInputCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddInputCost(v)
return _u
}
// SetOutputCost sets the "output_cost" field.
func (_u *UsageLogUpdateOne) SetOutputCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetOutputCost()
_u.mutation.SetOutputCost(v)
return _u
}
// SetNillableOutputCost sets the "output_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableOutputCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetOutputCost(*v)
}
return _u
}
// AddOutputCost adds value to the "output_cost" field.
func (_u *UsageLogUpdateOne) AddOutputCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddOutputCost(v)
return _u
}
// SetCacheCreationCost sets the "cache_creation_cost" field.
func (_u *UsageLogUpdateOne) SetCacheCreationCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetCacheCreationCost()
_u.mutation.SetCacheCreationCost(v)
return _u
}
// SetNillableCacheCreationCost sets the "cache_creation_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheCreationCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheCreationCost(*v)
}
return _u
}
// AddCacheCreationCost adds value to the "cache_creation_cost" field.
func (_u *UsageLogUpdateOne) AddCacheCreationCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddCacheCreationCost(v)
return _u
}
// SetCacheReadCost sets the "cache_read_cost" field.
func (_u *UsageLogUpdateOne) SetCacheReadCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetCacheReadCost()
_u.mutation.SetCacheReadCost(v)
return _u
}
// SetNillableCacheReadCost sets the "cache_read_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableCacheReadCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetCacheReadCost(*v)
}
return _u
}
// AddCacheReadCost adds value to the "cache_read_cost" field.
func (_u *UsageLogUpdateOne) AddCacheReadCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddCacheReadCost(v)
return _u
}
// SetTotalCost sets the "total_cost" field.
func (_u *UsageLogUpdateOne) SetTotalCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetTotalCost()
_u.mutation.SetTotalCost(v)
return _u
}
// SetNillableTotalCost sets the "total_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableTotalCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetTotalCost(*v)
}
return _u
}
// AddTotalCost adds value to the "total_cost" field.
func (_u *UsageLogUpdateOne) AddTotalCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddTotalCost(v)
return _u
}
// SetActualCost sets the "actual_cost" field.
func (_u *UsageLogUpdateOne) SetActualCost(v float64) *UsageLogUpdateOne {
_u.mutation.ResetActualCost()
_u.mutation.SetActualCost(v)
return _u
}
// SetNillableActualCost sets the "actual_cost" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableActualCost(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetActualCost(*v)
}
return _u
}
// AddActualCost adds value to the "actual_cost" field.
func (_u *UsageLogUpdateOne) AddActualCost(v float64) *UsageLogUpdateOne {
_u.mutation.AddActualCost(v)
return _u
}
// SetRateMultiplier sets the "rate_multiplier" field.
func (_u *UsageLogUpdateOne) SetRateMultiplier(v float64) *UsageLogUpdateOne {
_u.mutation.ResetRateMultiplier()
_u.mutation.SetRateMultiplier(v)
return _u
}
// SetNillableRateMultiplier sets the "rate_multiplier" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableRateMultiplier(v *float64) *UsageLogUpdateOne {
if v != nil {
_u.SetRateMultiplier(*v)
}
return _u
}
// AddRateMultiplier adds value to the "rate_multiplier" field.
func (_u *UsageLogUpdateOne) AddRateMultiplier(v float64) *UsageLogUpdateOne {
_u.mutation.AddRateMultiplier(v)
return _u
}
// SetBillingType sets the "billing_type" field.
func (_u *UsageLogUpdateOne) SetBillingType(v int8) *UsageLogUpdateOne {
_u.mutation.ResetBillingType()
_u.mutation.SetBillingType(v)
return _u
}
// SetNillableBillingType sets the "billing_type" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableBillingType(v *int8) *UsageLogUpdateOne {
if v != nil {
_u.SetBillingType(*v)
}
return _u
}
// AddBillingType adds value to the "billing_type" field.
func (_u *UsageLogUpdateOne) AddBillingType(v int8) *UsageLogUpdateOne {
_u.mutation.AddBillingType(v)
return _u
}
// SetStream sets the "stream" field.
func (_u *UsageLogUpdateOne) SetStream(v bool) *UsageLogUpdateOne {
_u.mutation.SetStream(v)
return _u
}
// SetNillableStream sets the "stream" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableStream(v *bool) *UsageLogUpdateOne {
if v != nil {
_u.SetStream(*v)
}
return _u
}
// SetDurationMs sets the "duration_ms" field.
func (_u *UsageLogUpdateOne) SetDurationMs(v int) *UsageLogUpdateOne {
_u.mutation.ResetDurationMs()
_u.mutation.SetDurationMs(v)
return _u
}
// SetNillableDurationMs sets the "duration_ms" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableDurationMs(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetDurationMs(*v)
}
return _u
}
// AddDurationMs adds value to the "duration_ms" field.
func (_u *UsageLogUpdateOne) AddDurationMs(v int) *UsageLogUpdateOne {
_u.mutation.AddDurationMs(v)
return _u
}
// ClearDurationMs clears the value of the "duration_ms" field.
func (_u *UsageLogUpdateOne) ClearDurationMs() *UsageLogUpdateOne {
_u.mutation.ClearDurationMs()
return _u
}
// SetFirstTokenMs sets the "first_token_ms" field.
func (_u *UsageLogUpdateOne) SetFirstTokenMs(v int) *UsageLogUpdateOne {
_u.mutation.ResetFirstTokenMs()
_u.mutation.SetFirstTokenMs(v)
return _u
}
// SetNillableFirstTokenMs sets the "first_token_ms" field if the given value is not nil.
func (_u *UsageLogUpdateOne) SetNillableFirstTokenMs(v *int) *UsageLogUpdateOne {
if v != nil {
_u.SetFirstTokenMs(*v)
}
return _u
}
// AddFirstTokenMs adds value to the "first_token_ms" field.
func (_u *UsageLogUpdateOne) AddFirstTokenMs(v int) *UsageLogUpdateOne {
_u.mutation.AddFirstTokenMs(v)
return _u
}
// ClearFirstTokenMs clears the value of the "first_token_ms" field.
func (_u *UsageLogUpdateOne) ClearFirstTokenMs() *UsageLogUpdateOne {
_u.mutation.ClearFirstTokenMs()
return _u
}
// SetUser sets the "user" edge to the User entity.
func (_u *UsageLogUpdateOne) SetUser(v *User) *UsageLogUpdateOne {
return _u.SetUserID(v.ID)
}
// SetAPIKey sets the "api_key" edge to the APIKey entity.
func (_u *UsageLogUpdateOne) SetAPIKey(v *APIKey) *UsageLogUpdateOne {
return _u.SetAPIKeyID(v.ID)
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *UsageLogUpdateOne) SetAccount(v *Account) *UsageLogUpdateOne {
return _u.SetAccountID(v.ID)
}
// SetGroup sets the "group" edge to the Group entity.
func (_u *UsageLogUpdateOne) SetGroup(v *Group) *UsageLogUpdateOne {
return _u.SetGroupID(v.ID)
}
// SetSubscription sets the "subscription" edge to the UserSubscription entity.
func (_u *UsageLogUpdateOne) SetSubscription(v *UserSubscription) *UsageLogUpdateOne {
return _u.SetSubscriptionID(v.ID)
}
// Mutation returns the UsageLogMutation object of the builder.
func (_u *UsageLogUpdateOne) Mutation() *UsageLogMutation {
return _u.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (_u *UsageLogUpdateOne) ClearUser() *UsageLogUpdateOne {
_u.mutation.ClearUser()
return _u
}
// ClearAPIKey clears the "api_key" edge to the APIKey entity.
func (_u *UsageLogUpdateOne) ClearAPIKey() *UsageLogUpdateOne {
_u.mutation.ClearAPIKey()
return _u
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *UsageLogUpdateOne) ClearAccount() *UsageLogUpdateOne {
_u.mutation.ClearAccount()
return _u
}
// ClearGroup clears the "group" edge to the Group entity.
func (_u *UsageLogUpdateOne) ClearGroup() *UsageLogUpdateOne {
_u.mutation.ClearGroup()
return _u
}
// ClearSubscription clears the "subscription" edge to the UserSubscription entity.
func (_u *UsageLogUpdateOne) ClearSubscription() *UsageLogUpdateOne {
_u.mutation.ClearSubscription()
return _u
}
// Where appends a list predicates to the UsageLogUpdate builder.
func (_u *UsageLogUpdateOne) Where(ps ...predicate.UsageLog) *UsageLogUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *UsageLogUpdateOne) Select(field string, fields ...string) *UsageLogUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated UsageLog entity.
func (_u *UsageLogUpdateOne) Save(ctx context.Context) (*UsageLog, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *UsageLogUpdateOne) SaveX(ctx context.Context) *UsageLog {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *UsageLogUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *UsageLogUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *UsageLogUpdateOne) check() error {
if v, ok := _u.mutation.RequestID(); ok {
if err := usagelog.RequestIDValidator(v); err != nil {
return &ValidationError{Name: "request_id", err: fmt.Errorf(`ent: validator failed for field "UsageLog.request_id": %w`, err)}
}
}
if v, ok := _u.mutation.Model(); ok {
if err := usagelog.ModelValidator(v); err != nil {
return &ValidationError{Name: "model", err: fmt.Errorf(`ent: validator failed for field "UsageLog.model": %w`, err)}
}
}
if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.user"`)
}
if _u.mutation.APIKeyCleared() && len(_u.mutation.APIKeyIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.api_key"`)
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UsageLog.account"`)
}
return nil
}
func (_u *UsageLogUpdateOne) sqlSave(ctx context.Context) (_node *UsageLog, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(usagelog.Table, usagelog.Columns, sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UsageLog.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, usagelog.FieldID)
for _, f := range fields {
if !usagelog.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != usagelog.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.RequestID(); ok {
_spec.SetField(usagelog.FieldRequestID, field.TypeString, value)
}
if value, ok := _u.mutation.Model(); ok {
_spec.SetField(usagelog.FieldModel, field.TypeString, value)
}
if value, ok := _u.mutation.InputTokens(); ok {
_spec.SetField(usagelog.FieldInputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedInputTokens(); ok {
_spec.AddField(usagelog.FieldInputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.OutputTokens(); ok {
_spec.SetField(usagelog.FieldOutputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedOutputTokens(); ok {
_spec.AddField(usagelog.FieldOutputTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreationTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreationTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreationTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreationTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheReadTokens(); ok {
_spec.SetField(usagelog.FieldCacheReadTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheReadTokens(); ok {
_spec.AddField(usagelog.FieldCacheReadTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreation5mTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreation5mTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreation5mTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreation5mTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.CacheCreation1hTokens(); ok {
_spec.SetField(usagelog.FieldCacheCreation1hTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedCacheCreation1hTokens(); ok {
_spec.AddField(usagelog.FieldCacheCreation1hTokens, field.TypeInt, value)
}
if value, ok := _u.mutation.InputCost(); ok {
_spec.SetField(usagelog.FieldInputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedInputCost(); ok {
_spec.AddField(usagelog.FieldInputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.OutputCost(); ok {
_spec.SetField(usagelog.FieldOutputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedOutputCost(); ok {
_spec.AddField(usagelog.FieldOutputCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.CacheCreationCost(); ok {
_spec.SetField(usagelog.FieldCacheCreationCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedCacheCreationCost(); ok {
_spec.AddField(usagelog.FieldCacheCreationCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.CacheReadCost(); ok {
_spec.SetField(usagelog.FieldCacheReadCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedCacheReadCost(); ok {
_spec.AddField(usagelog.FieldCacheReadCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.TotalCost(); ok {
_spec.SetField(usagelog.FieldTotalCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedTotalCost(); ok {
_spec.AddField(usagelog.FieldTotalCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.ActualCost(); ok {
_spec.SetField(usagelog.FieldActualCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedActualCost(); ok {
_spec.AddField(usagelog.FieldActualCost, field.TypeFloat64, value)
}
if value, ok := _u.mutation.RateMultiplier(); ok {
_spec.SetField(usagelog.FieldRateMultiplier, field.TypeFloat64, value)
}
if value, ok := _u.mutation.AddedRateMultiplier(); ok {
_spec.AddField(usagelog.FieldRateMultiplier, field.TypeFloat64, value)
}
if value, ok := _u.mutation.BillingType(); ok {
_spec.SetField(usagelog.FieldBillingType, field.TypeInt8, value)
}
if value, ok := _u.mutation.AddedBillingType(); ok {
_spec.AddField(usagelog.FieldBillingType, field.TypeInt8, value)
}
if value, ok := _u.mutation.Stream(); ok {
_spec.SetField(usagelog.FieldStream, field.TypeBool, value)
}
if value, ok := _u.mutation.DurationMs(); ok {
_spec.SetField(usagelog.FieldDurationMs, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedDurationMs(); ok {
_spec.AddField(usagelog.FieldDurationMs, field.TypeInt, value)
}
if _u.mutation.DurationMsCleared() {
_spec.ClearField(usagelog.FieldDurationMs, field.TypeInt)
}
if value, ok := _u.mutation.FirstTokenMs(); ok {
_spec.SetField(usagelog.FieldFirstTokenMs, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedFirstTokenMs(); ok {
_spec.AddField(usagelog.FieldFirstTokenMs, field.TypeInt, value)
}
if _u.mutation.FirstTokenMsCleared() {
_spec.ClearField(usagelog.FieldFirstTokenMs, field.TypeInt)
}
if _u.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.UserTable,
Columns: []string{usagelog.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.UserTable,
Columns: []string{usagelog.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.APIKeyCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.APIKeyTable,
Columns: []string{usagelog.APIKeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.APIKeyIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.APIKeyTable,
Columns: []string{usagelog.APIKeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.AccountTable,
Columns: []string{usagelog.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.AccountTable,
Columns: []string{usagelog.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.GroupCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.GroupTable,
Columns: []string{usagelog.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.GroupTable,
Columns: []string{usagelog.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.SubscriptionCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.SubscriptionTable,
Columns: []string{usagelog.SubscriptionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.SubscriptionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usagelog.SubscriptionTable,
Columns: []string{usagelog.SubscriptionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &UsageLog{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{usagelog.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}