mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Per naming directive: avoid unnecessary compound words, one clean name. Renames the router func + the ApiController method + all ~30 callers/tests/ comments; local effectiveOrg vars → org; test GlobalAdmin→SuperAdmin (matches the one super-admin rule). Pure rename — no behavior change; build + vet + gofumpt clean, org-resolver tests green. Co-authored-by: hanzo-dev <[email protected]>
150 lines
3.6 KiB
Go
150 lines
3.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2024 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// GetUsages
|
|
// @Title GetUsages
|
|
// @Tag Usage API
|
|
// @Description get usages
|
|
// @Param days query string true "days count"
|
|
// @Success 200 {array} object.Usage The Response object
|
|
// @router /get-usages [get]
|
|
func (c *ApiController) GetUsages() {
|
|
_, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
if !c.IsAdmin() {
|
|
c.ResponseError(c.T("auth:this operation requires admin privilege"))
|
|
return
|
|
}
|
|
|
|
days := util.ParseInt(c.Input().Get("days"))
|
|
user := c.Input().Get("selectedUser")
|
|
storeName := c.Input().Get("store")
|
|
|
|
usages, err := object.GetUsages(days, user, storeName)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
usageMetadata, err := object.GetUsageMetadata(c.GetAcceptLanguage(), c.GetOrg())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(usages, usageMetadata)
|
|
}
|
|
|
|
// GetRangeUsages
|
|
// @Title GetRangeUsages
|
|
// @Tag Usage API
|
|
// @Description get range usages
|
|
// @Param count query string true "count of range usages"
|
|
// @Success 200 {array} object.Usage The Response object
|
|
// @router /get-range-usages [get]
|
|
func (c *ApiController) GetRangeUsages() {
|
|
_, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
if !c.IsAdmin() {
|
|
c.ResponseError(c.T("auth:this operation requires admin privilege"))
|
|
return
|
|
}
|
|
|
|
rangeType := c.Input().Get("rangeType")
|
|
count := util.ParseInt(c.Input().Get("count"))
|
|
user := c.Input().Get("user")
|
|
storeName := c.Input().Get("store")
|
|
|
|
usages, err := object.GetRangeUsages(rangeType, count, user, storeName, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
usageMetadata, err := object.GetUsageMetadata(c.GetAcceptLanguage(), c.GetOrg())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(usages, usageMetadata)
|
|
}
|
|
|
|
// GetUsers
|
|
// @Title GetUsers
|
|
// @Tag Usage API
|
|
// @Description get users
|
|
// @Success 200 {array} string The Response object
|
|
// @router /get-users [get]
|
|
func (c *ApiController) GetUsers() {
|
|
_, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
if !c.IsAdmin() {
|
|
c.ResponseError(c.T("auth:this operation requires admin privilege"))
|
|
return
|
|
}
|
|
|
|
user := c.Input().Get("user")
|
|
storeName := c.Input().Get("store")
|
|
user = ""
|
|
users, err := object.GetUsers(storeName, user)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(users)
|
|
}
|
|
|
|
// GetUserTableInfos
|
|
// @Title GetUserTableInfos
|
|
// @Tag Usage API
|
|
// @Description get userTableInfos
|
|
// @Success 200 {array} object.Usage The Response object
|
|
// @router /get-usages [get]
|
|
func (c *ApiController) GetUserTableInfos() {
|
|
_, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
if !c.IsAdmin() {
|
|
c.ResponseError(c.T("auth:this operation requires admin privilege"))
|
|
return
|
|
}
|
|
|
|
user := c.Input().Get("user")
|
|
storeName := c.Input().Get("store")
|
|
user = ""
|
|
userInfos, err := object.GetUserTableInfos(storeName, user)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
c.ResponseOk(userInfos)
|
|
}
|