| 130 | } |
| 131 | |
| 132 | func (i *imlAPIUseService) Incr(ctx context.Context, incr *IncrAPIUse) error { |
| 133 | info, err := i.store.First(ctx, map[string]interface{}{ |
| 134 | "api": incr.API, |
| 135 | "service": incr.Service, |
| 136 | "provider": incr.Provider, |
| 137 | "model": incr.Model, |
| 138 | "day": incr.Day, |
| 139 | "hour": incr.Hour, |
| 140 | "minute": incr.Minute, |
| 141 | }) |
| 142 | if err != nil { |
| 143 | if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 144 | return err |
| 145 | } |
| 146 | info = &api.AiAPIUse{ |
| 147 | API: incr.API, |
| 148 | Service: incr.Service, |
| 149 | Provider: incr.Provider, |
| 150 | Model: incr.Model, |
| 151 | Day: incr.Day, |
| 152 | Hour: incr.Hour, |
| 153 | Minute: incr.Minute, |
| 154 | } |
| 155 | } |
| 156 | info.InputToken += incr.InputToken |
| 157 | info.OutputToken += incr.OutputToken |
| 158 | info.TotalToken += incr.TotalToken |
| 159 | return i.store.Save(ctx, info) |
| 160 | } |
| 161 | |
| 162 | func (i *imlAPIUseService) SumByApisPage(ctx context.Context, providerId string, start, end int64, offset, limit int, order string, apiIds ...string) ([]*APIUse, int64, error) { |
| 163 | list, total, err := i.store.SumByGroupPage(ctx, "api", order, offset, limit, "api,sum(input_token) as input_token,sum(output_token) as output_token,sum(total_token) as total_token", "provider = ? and api in (?) and minute >= ? and minute <= ?", providerId, apiIds, start, end) |