代码拉取完成,页面将自动刷新
package devstatscode
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
)
// Tags contain list of TSDB tags
type Tags struct {
Tags []Tag `yaml:"tags"`
}
// Tag contain each TSDB tag data
type Tag struct {
Name string `yaml:"name"`
SQLFile string `yaml:"sql"`
SeriesName string `yaml:"series_name"`
NameTag string `yaml:"name_tag"`
ValueTag string `yaml:"value_tag"`
OtherTags map[string][2]string `yaml:"other_tags"`
Limit int `yaml:"limit"`
Disabled bool `yaml:"disabled"`
}
// ProcessTag - insert given Tag into Postgres TSDB
func ProcessTag(con *sql.DB, ctx *Ctx, tg *Tag, replaces [][]string) {
// Batch TS points
var pts TSPoints
// Skip disabled tags
if tg.Disabled && !ctx.TestMode {
return
}
// Local or cron mode
dataPrefix := ctx.DataDir
if ctx.Local {
dataPrefix = "./"
}
// Per project directory for SQL files
dir := Metrics
if ctx.Project != "" {
dir += ctx.Project + "/"
}
// Read SQL file
bytes, err := ReadFile(ctx, dataPrefix+dir+tg.SQLFile+".sql")
FatalOnError(err)
sqlQuery := string(bytes)
// Handle excluding bots
bytes, err = ReadFile(ctx, dataPrefix+"util_sql/exclude_bots.sql")
FatalOnError(err)
excludeBots := string(bytes)
// Transform SQL
limit := tg.Limit
if limit <= 0 {
limit = 255
}
sqlQuery = strings.Replace(sqlQuery, "{{lim}}", strconv.Itoa(limit), -1)
sqlQuery = strings.Replace(sqlQuery, "{{exclude_bots}}", excludeBots, -1)
// Replaces
for _, replace := range replaces {
if len(replace) != 2 {
FatalOnError(fmt.Errorf("replace(s) should have length 2, invalid: %+v", replace))
}
sqlQuery = strings.Replace(sqlQuery, replace[0], replace[1], -1)
}
// Execute SQL
rows := QuerySQLWithErr(con, ctx, sqlQuery)
defer func() { FatalOnError(rows.Close()) }()
// Drop current tags
if !ctx.SkipTSDB {
table := "t" + tg.SeriesName
if TableExists(con, ctx, table) {
ExecSQLWithErr(con, ctx, "truncate "+table)
}
}
tm := TimeParseAny("2012-07-01")
// Columns
columns, err := rows.Columns()
FatalOnError(err)
colIdx := make(map[string]int)
for i, column := range columns {
colIdx[column] = i
}
// Iterate tag values
tags := make(map[string]string)
iVals := make([]interface{}, len(columns))
for i := range columns {
iVals[i] = new([]byte)
}
got := false
for rows.Next() {
got = true
FatalOnError(rows.Scan(iVals...))
sVals := []string{}
for _, iVal := range iVals {
sVal := ""
if iVal != nil {
sVal = string(*iVal.(*[]byte))
}
sVals = append(sVals, sVal)
}
strVal := sVals[0]
if tg.NameTag != "" {
tags[tg.NameTag] = strVal
}
if tg.ValueTag != "" {
tags[tg.ValueTag] = NormalizeName(strVal)
}
if tg.OtherTags != nil {
for tName, tData := range tg.OtherTags {
tValue := tData[0]
cIdx, ok := colIdx[tValue]
if !ok {
Fatalf("other tag: name: %s: column %s not found", tName, tValue)
}
tags[tName] = sVals[cIdx]
tNorm := strings.ToLower(tData[1])
if tNorm == "1" || tNorm == "t" || tNorm == "y" {
tags[tName+"_norm"] = NormalizeName(sVals[cIdx])
}
}
}
if ctx.Debug > 0 {
Printf("'%s': %+v\n", tg.SeriesName, tags)
}
// Add batch point
pt := NewTSPoint(ctx, tg.SeriesName, "", tags, nil, tm, false)
AddTSPoint(ctx, &pts, pt)
tm = tm.Add(time.Hour)
}
FatalOnError(rows.Err())
if !got {
Printf("Warning: Tag '%+v' have no values\n", tg)
}
// Write the batch
if !ctx.SkipTSDB {
WriteTSPoints(ctx, con, &pts, "", []uint8{}, nil)
} else if ctx.Debug > 0 {
Printf("Skipping tags series write\n")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。