cls

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetLogCostCmd = &cobra.Command{
	Use:   "getlogcost",
	Short: "获取一个日志主题在前天的花费总额",
	Long: `通过命令行获取一个日志主题在前天所消耗的费用,需要传入的参数有:
  -l: 应用所在日志主题的ID`,
	Run: func(cmd *cobra.Command, args []string) {
		logid, _ := cmd.Flags().GetString("logid")
		region, _ := cmd.Flags().GetString("region")
		fmt.Printf("前天的花费为: %d元\n日志主题为: %s\n", getLogCost(region, logid), logid)
	},
}
View Source
var GetProjectCostCmd = &cobra.Command{
	Use:   "getprojectcost",
	Short: "获取一个应用所花的费用",
	Long: `通过命令行获取一个应用在cls中所消耗的费用,需要传入的参数有:
  -l: 应用所在日志主题的ID
  -p: 应用主机名,如果你检索的日志主题为CVM,其中一组服务器名为 prod-admin-node-1..4,那么此处应该写为 prod-admin-node
  -b: 指定机器人的webhook
  -r: 指定日志所在的region
  -k: 指定应用所在日志主题的类型,host/k8s`,
	Run: func(cmd *cobra.Command, args []string) {
		flags := cmd.Flags()

		if flags.NFlag() == 0 {
			fmt.Println("Please specify a flag")
			os.Exit(1)
		}
		var (
			msg string
			bot string
		)
		file, _ := cmd.Flags().GetString("file")
		region, _ := cmd.Flags().GetString("region")
		if file != "" {
			rst, err := LoadProjectList(file)
			if err != nil {
				fmt.Println("err:", err)
			}
			bot = rst.Bot
			for _, v := range rst.ProjectItems {
				logid := v.LogID
				sumcost := getLogCost(region, logid)
				msg += fmt.Sprintf("\n\n> 主题前日花费:`%d元`\n> 主题ID:`%s`\n", sumcost, logid)
				for _, j := range v.Project {
					percent := GetRst(region, logid, getQuery(v.Kind, j))
					project_cost := fmt.Sprintf("%.2f", float64(sumcost)*percent/100*30)
					msg += fmt.Sprintf("> **应用:** `%s`\n> 日志量占比: `%v%%` \n> 单月花费预计: `%v元`", j, percent, project_cost) + "\n"
				}
			}
		} else {
			logid, _ := cmd.Flags().GetString("logid")
			project, _ := cmd.Flags().GetString("project")
			bot, _ = cmd.Flags().GetString("bot")
			kind, _ := cmd.Flags().GetString("kind")
			sumcost := getLogCost(region, logid)
			percent := GetRst(region, logid, getQuery(kind, project))
			project_cost := fmt.Sprintf("%.2f", float64(sumcost)*percent/100*30)
			msg = fmt.Sprintf("> 应用名称:`%s`\n> 应用在主题日志占比: `%v%%` \n> 应用单月费用预计: `%v元`\n>该主题前天单日的费用为: `%v元`\n该主题的ID为: `%s`", project, percent, project_cost, sumcost, logid)
		}
		fmt.Println(msg)
		err := public.SendMsg(bot, fmt.Sprintf("=========`%v`汇总数据如下=========\n\n\n\n%s\n\n\n\n 说明:目前取样该应用的数据为最近三个小时占该主题的占比,金额则是取的该主题前天整天的费用,计算方式为 `前天单日费用 x 近三小时百分比 x 30天` 得出该应用单月的费用,是一个相对粗略的参考,但排除日志主题新增或减少应用的变更因素,这个计算数值是相对准确的。", time.Now().Format("2006-01-02 15:04"), msg))
		if err != nil {
			fmt.Printf("发送机器人消息失败: %v\n", err)
		}
	},
}

Functions

func GetRst

func GetRst(region, logid, query string) (rst float64)

func GetSecret

func GetSecret() *common.Credential

Types

type DetailSet

type DetailSet struct {
	ActionType       string `json:"ActionType"`
	ActionTypeName   string `json:"ActionTypeName"`
	BillID           string `json:"BillId"`
	BusinessCode     string `json:"BusinessCode"`
	BusinessCodeName string `json:"BusinessCodeName"`
	ComponentSet     []struct {
		BlendedDiscount    string      `json:"BlendedDiscount"`
		CashPayAmount      string      `json:"CashPayAmount"`
		ComponentCode      string      `json:"ComponentCode"`
		ComponentCodeName  string      `json:"ComponentCodeName"`
		ContractPrice      string      `json:"ContractPrice"`
		Cost               string      `json:"Cost"`
		Discount           string      `json:"Discount"`
		IncentivePayAmount string      `json:"IncentivePayAmount"`
		InstanceType       string      `json:"InstanceType"`
		ItemCode           string      `json:"ItemCode"`
		ItemCodeName       string      `json:"ItemCodeName"`
		OriginalCostWithRI string      `json:"OriginalCostWithRI"`
		OriginalCostWithSP string      `json:"OriginalCostWithSP"`
		PriceUnit          string      `json:"PriceUnit"`
		RealCost           string      `json:"RealCost"`
		ReduceType         string      `json:"ReduceType"`
		RiTimeSpan         string      `json:"RiTimeSpan"`
		SPDeductionRate    string      `json:"SPDeductionRate"`
		SinglePrice        string      `json:"SinglePrice"`
		TimeSpan           string      `json:"TimeSpan"`
		TimeUnitName       string      `json:"TimeUnitName"`
		TransferPayAmount  interface{} `json:"TransferPayAmount"`
		UsedAmount         string      `json:"UsedAmount"`
		UsedAmountUnit     string      `json:"UsedAmountUnit"`
		VoucherPayAmount   string      `json:"VoucherPayAmount"`
	} `json:"ComponentSet"`
	FeeBeginTime    string        `json:"FeeBeginTime"`
	FeeEndTime      string        `json:"FeeEndTime"`
	OperateUin      string        `json:"OperateUin"`
	OrderID         string        `json:"OrderId"`
	OwnerUin        string        `json:"OwnerUin"`
	PayModeName     string        `json:"PayModeName"`
	PayTime         string        `json:"PayTime"`
	PayerUin        string        `json:"PayerUin"`
	PriceInfo       []interface{} `json:"PriceInfo"`
	ProductCode     string        `json:"ProductCode"`
	ProductCodeName string        `json:"ProductCodeName"`
	ProjectID       int           `json:"ProjectId"`
	ProjectName     string        `json:"ProjectName"`
	RegionID        string        `json:"RegionId"`
	RegionName      string        `json:"RegionName"`
	ResourceID      string        `json:"ResourceId"`
	ResourceName    string        `json:"ResourceName"`
	Tags            []interface{} `json:"Tags"`
	ZoneName        string        `json:"ZoneName"`
}

func GetClsAlarms

func GetClsAlarms(region, logid string) ([]DetailSet, error)

type DetailSetRsp

type DetailSetRsp struct {
	Response struct {
		Context    string      `json:"Context"`
		DetailSets []DetailSet `json:"DetailSet"`
		RequestID  string      `json:"RequestId"`
		Total      int         `json:"Total"`
	} `json:"Response"`
}

type LogTopicCost

type LogTopicCost struct {
	Response struct {
		Analysis        bool          `json:"Analysis"`
		AnalysisRecords []interface{} `json:"AnalysisRecords"`
		AnalysisResults []struct {
			Data []struct {
				Key   string `json:"Key"`
				Value string `json:"Value"`
			} `json:"Data"`
		} `json:"AnalysisResults"`
		ColNames     []string      `json:"ColNames"`
		Columns      []interface{} `json:"Columns"`
		Context      string        `json:"Context"`
		ListOver     bool          `json:"ListOver"`
		RequestID    string        `json:"RequestId"`
		Results      []interface{} `json:"Results"`
		SamplingRate int           `json:"SamplingRate"`
	} `json:"Response"`
}

type ProjectItem

type ProjectItem struct {
	LogID   string   `yaml:"logid"`
	Kind    string   `yaml:"kind"`
	Project []string `yaml:"project"`
}

type ProjectList

type ProjectList struct {
	Bot          string        `yaml:"bot"`
	ProjectItems []ProjectItem `yaml:"project_items"`
}

func LoadProjectList

func LoadProjectList(file string) (rst *ProjectList, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL