github.com/qiniu/go-sdk/v7@v7.27.0/pili/stat_model.go (about)

     1  package pili
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  const (
    10  	// FlowDefaultSelect 上下行流量默认查询字段
    11  	FlowDefaultSelect = "flow"
    12  
    13  	// CodecDefaultSelect 转码使用量默认查询字段
    14  	CodecDefaultSelect = "duration"
    15  
    16  	// NropDefaultSelect 鉴黄使用量默认查询字段
    17  	NropDefaultSelect = "count"
    18  )
    19  
    20  // GetStatCommonRequest 统计接口通用请求参数
    21  type GetStatCommonRequest struct {
    22  	// Begin 开始时间
    23  	// 支持格式:20060102、20060102150405
    24  	Begin string `validate:"required,len=8|len=14"`
    25  
    26  	// End 结束时间
    27  	// 支持格式:20060102、20060102150405
    28  	// 超过当前时间,则以当前时间为准
    29  	// 时间范围为左闭右开区间
    30  	End string `validate:"len=8|len=14"`
    31  
    32  	// G 时间粒度
    33  	// 可选项 5min hour day month,部分接口仅支持部分粒度
    34  	G string `validate:"required,oneof=5min hour day month"`
    35  }
    36  
    37  // GetStatUpflowRequest 获取上行流量请求参数
    38  type GetStatUpflowRequest struct {
    39  	// 通用请求参数
    40  	GetStatCommonRequest
    41  
    42  	// Where 查询条件
    43  	// hub 直播空间
    44  	// domain 域名
    45  	// area 区域 中国大陆(cn)、香港(hk)、台湾(tw)、亚太(apac)、美洲(am)、欧洲/中东/非洲(emea)
    46  	Where map[string][]string `validate:"dive,keys,oneof=hub domain area"`
    47  
    48  	// Select 查询值
    49  	// flow 流量,单位:byte,带宽可以从流量转换,公式为 带宽=流量*8/时间粒度,单位:bps
    50  	Select []string `validate:"dive,eq=flow"`
    51  }
    52  
    53  // GroupStatUpflowRequest 分组获取上行流量请求参数
    54  type GroupStatUpflowRequest struct {
    55  	// 通用请求参数
    56  	GetStatCommonRequest
    57  
    58  	// Group 分组
    59  	Group string `validate:"required,oneof=hub domain area"`
    60  
    61  	// Where 查询条件
    62  	// hub 直播空间
    63  	// domain 域名
    64  	// area 区域 中国大陆(cn)、香港(hk)、台湾(tw)、亚太(apac)、美洲(am)、欧洲/中东/非洲(emea)
    65  	Where map[string][]string `validate:"dive,keys,oneof=hub domain area"`
    66  
    67  	// Select 查询值
    68  	// flow 流量,单位:byte,带宽可以从流量转换,公式为 带宽=流量*8/时间粒度,单位:bps
    69  	Select []string `validate:"dive,eq=flow"`
    70  }
    71  
    72  // GetStatDownflowRequest 获取下行流量请求参数
    73  type GetStatDownflowRequest struct {
    74  	// 通用请求参数
    75  	GetStatCommonRequest
    76  
    77  	// Where 查询条件
    78  	// hub 直播空间
    79  	// domain 域名
    80  	// area 区域 中国大陆(cn)、香港(hk)、台湾(tw)、亚太(apac)、美洲(am)、欧洲/中东/非洲(emea)
    81  	Where map[string][]string `validate:"dive,keys,oneof=hub domain area"`
    82  
    83  	// Select 查询值
    84  	// flow 流量,单位:byte,带宽可以从流量转换,公式为 带宽=流量*8/时间粒度,单位:bps
    85  	Select []string `validate:"dive,eq=flow"`
    86  }
    87  
    88  // GroupStatDownflowRequest 分组获取下行流量请求参数
    89  type GroupStatDownflowRequest struct {
    90  	// 通用请求参数
    91  	GetStatCommonRequest
    92  
    93  	// Group 分组
    94  	Group string `validate:"required,oneof=hub domain area"`
    95  
    96  	// Where 查询条件
    97  	// hub 直播空间
    98  	// domain 域名
    99  	// area 区域 中国大陆(cn)、香港(hk)、台湾(tw)、亚太(apac)、美洲(am)、欧洲/中东/非洲(emea)
   100  	Where map[string][]string `validate:"dive,keys,oneof=hub domain area"`
   101  
   102  	// Select 查询值
   103  	// flow 流量,单位:byte,带宽可以从流量转换,公式为 带宽=流量*8/时间粒度,单位:bps
   104  	Select []string `validate:"dive,eq=flow"`
   105  }
   106  
   107  // GetStatCodecRequest 获取直播转码使用量请求参数
   108  type GetStatCodecRequest struct {
   109  	// 通用请求参数
   110  	GetStatCommonRequest
   111  
   112  	// Where 查询条件
   113  	// hub 直播空间
   114  	// profile 转码规格
   115  	Where map[string][]string `validate:"dive,keys,oneof=hub profile"`
   116  
   117  	// Select 查询值
   118  	// duration 时长,单位:毫秒
   119  	Select []string `validate:"dive,eq=duration"`
   120  }
   121  
   122  // GroupStatCodecRequest 分组获取直播转码使用量请求参数
   123  type GroupStatCodecRequest struct {
   124  	// 通用请求参数
   125  	GetStatCommonRequest
   126  
   127  	// Group 分组
   128  	Group string `validate:"required,oneof=hub profile"`
   129  
   130  	// Where 查询条件
   131  	// hub 直播空间
   132  	// profile 转码规格
   133  	Where map[string][]string `validate:"dive,keys,oneof=hub profile"`
   134  
   135  	// Select 查询值
   136  	// duration 时长,单位:毫秒
   137  	Select []string `validate:"dive,eq=duration"`
   138  }
   139  
   140  // GetStatNropRequest 获取直播鉴黄使用量请求参数
   141  type GetStatNropRequest struct {
   142  	// 通用请求参数
   143  	GetStatCommonRequest
   144  
   145  	// Where 查询条件
   146  	// hub 直播空间
   147  	// assured 鉴黄结果是否确定,true或false
   148  	Where map[string][]string `validate:"dive,keys,oneof=hub assured"`
   149  
   150  	// Select 查询值
   151  	// count 鉴黄次数
   152  	Select []string `validate:"dive,eq=count"`
   153  }
   154  
   155  // GroupStatNropRequest 分组获取直播鉴黄使用量请求参数
   156  type GroupStatNropRequest struct {
   157  	// 通用请求参数
   158  	GetStatCommonRequest
   159  
   160  	// Group 分组
   161  	Group string `validate:"required,oneof=hub assured"`
   162  
   163  	// Where 查询条件
   164  	// hub 直播空间
   165  	// assured 鉴黄结果是否确定,true或false
   166  	Where map[string][]string `validate:"dive,keys,oneof=hub assured"`
   167  
   168  	// Select 查询值
   169  	// count 鉴黄次数
   170  	Select []string `validate:"dive,eq=count"`
   171  }
   172  
   173  // GetStatCasterRequest 获取导播台使用量请求参数
   174  type GetStatCasterRequest struct {
   175  	// 通用请求参数
   176  	GetStatCommonRequest
   177  
   178  	// Where 查询条件
   179  	// container 容器
   180  	// resolution 分辨率
   181  	Where map[string][]string `validate:"dive,keys,oneof=container resolution"`
   182  
   183  	// Select 查询值
   184  	// upflow 上行流量,单位:byte
   185  	// downflow 下行流量,单位:byte
   186  	// duration 使用时长,单位:秒
   187  	Select []string `validate:"required,dive,oneof=upflow downflow duration"`
   188  }
   189  
   190  // GroupStatCasterRequest 分组获取导播台使用量请求参数
   191  type GroupStatCasterRequest struct {
   192  	// 通用请求参数
   193  	GetStatCommonRequest
   194  
   195  	// Group 分组
   196  	Group string `validate:"required,oneof=container resolution"`
   197  
   198  	// Where 查询条件
   199  	// container 容器
   200  	// resolution 分辨率
   201  	Where map[string][]string `validate:"dive,keys,oneof=container resolution"`
   202  
   203  	// Select 查询值
   204  	// upflow 上行流量,单位:byte
   205  	// downflow 下行流量,单位:byte
   206  	// duration 使用时长,单位:秒
   207  	Select []string `validate:"required,dive,oneof=upflow downflow duration"`
   208  }
   209  
   210  // GetStatPubRequest 获取Pub服务使用量请求参数
   211  type GetStatPubRequest struct {
   212  	// 通用请求参数
   213  	GetStatCommonRequest
   214  
   215  	// Where 查询条件
   216  	// tp 状态
   217  	Where map[string][]string `validate:"dive,keys,eq=tp"`
   218  
   219  	// Select 查询值
   220  	// count 转推次数
   221  	// duration 转推时长,单位:毫秒
   222  	Select []string `validate:"required,dive,oneof=count duration"`
   223  }
   224  
   225  // GroupStatPubRequest 分组获取Pub服务使用量请求参数
   226  type GroupStatPubRequest struct {
   227  	// 通用请求参数
   228  	GetStatCommonRequest
   229  
   230  	// Group 分组
   231  	Group string `validate:"required,eq=tp"`
   232  
   233  	// Where 查询条件
   234  	// tp 状态
   235  	Where map[string][]string `validate:"dive,keys,eq=tp"`
   236  
   237  	// Select 查询值
   238  	// count 转推次数
   239  	// duration 转推时长,单位:毫秒
   240  	Select []string `validate:"required,dive,oneof=count duration"`
   241  }
   242  
   243  // StatResponse 统计返回值
   244  type StatResponse struct {
   245  	// Time 时间
   246  	Time time.Time `json:"time"`
   247  
   248  	// Values 数据
   249  	// 形式如:"values":{"flow":4527162977},其中"flow"对应select项
   250  	Values map[string]int `json:"values"`
   251  }
   252  
   253  // StatGroupResponse 分组统计返回值
   254  type StatGroupResponse struct {
   255  	// Time 时间
   256  	Time time.Time `json:"time"`
   257  
   258  	// Values 数据
   259  	// 形式如:"values":{"SD":{"duration":4527162977}},其中"SD"对应Group项,"duration"对应Select项
   260  	Values map[string]map[string]int `json:"values"`
   261  }
   262  
   263  // Form 请求参数格式化
   264  func Form(obj interface{}) map[string][]string {
   265  	value := reflect.ValueOf(obj)
   266  	if value.Kind() == reflect.Ptr {
   267  		value = value.Elem()
   268  	}
   269  	if value.Kind() != reflect.Struct {
   270  		return nil
   271  	}
   272  
   273  	ret := make(map[string][]string)
   274  
   275  	typ := value.Type()
   276  	for i := 0; i < value.NumField(); i++ {
   277  		switch value.Field(i).Kind() {
   278  		case reflect.Struct:
   279  			div := Form(value.Field(i).Interface())
   280  			for k, v := range div {
   281  				ret[k] = v
   282  			}
   283  		case reflect.String:
   284  			val := value.Field(i).Interface().(string)
   285  			if len(val) > 0 {
   286  				ret[strings.ToLower(typ.Field(i).Name)] = []string{val}
   287  			}
   288  		case reflect.Slice:
   289  			ret[strings.ToLower(typ.Field(i).Name)] = value.Field(i).Interface().([]string)
   290  		case reflect.Map:
   291  			for k, v := range value.Field(i).Interface().(map[string][]string) {
   292  				ret["$"+k] = v
   293  			}
   294  		}
   295  	}
   296  
   297  	return ret
   298  }