chore: nbi reference doc and tools for generate data or cdr
This commit is contained in:
225
docs/nbi/pm/pm_generator.go
Normal file
225
docs/nbi/pm/pm_generator.go
Normal file
@@ -0,0 +1,225 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// KPIValue 表示单个 KPI 值
|
||||
type KPIValue struct {
|
||||
Name string `json:"Name"`
|
||||
Value int `json:"Value"`
|
||||
}
|
||||
|
||||
// KPI 表示单个 KPI 项
|
||||
type KPI struct {
|
||||
KPIID string `json:"KPIID"`
|
||||
KPIValues []KPIValue `json:"KPIValues"`
|
||||
}
|
||||
|
||||
// PMObject 表示性能管理对象
|
||||
type PMObject struct {
|
||||
ObjectType string `json:"ObjectType"`
|
||||
KPIs []KPI `json:"KPIs"`
|
||||
}
|
||||
|
||||
// PMData 表示完整的性能管理数据
|
||||
type PMData []PMObject
|
||||
|
||||
// generateRandomValue 生成 0-16 之间的随机数
|
||||
func generateRandomValue() int {
|
||||
return rand.Intn(17) // 0-16
|
||||
}
|
||||
|
||||
// generatePMDataFromSchema 从 schema 文件生成随机数据
|
||||
func generatePMDataFromSchema(schemaFile string, outputFile string) error {
|
||||
// 读取 schema 文件
|
||||
schemaData, err := os.ReadFile(schemaFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取 schema 文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析 JSON
|
||||
var pmSchema PMData
|
||||
if err := json.Unmarshal(schemaData, &pmSchema); err != nil {
|
||||
return fmt.Errorf("解析 JSON 失败: %v", err)
|
||||
}
|
||||
|
||||
// 生成新的随机数据
|
||||
var newPMData PMData
|
||||
for _, pmObj := range pmSchema {
|
||||
newObj := PMObject{
|
||||
ObjectType: pmObj.ObjectType,
|
||||
KPIs: make([]KPI, len(pmObj.KPIs)),
|
||||
}
|
||||
|
||||
for i, kpi := range pmObj.KPIs {
|
||||
newKPI := KPI{
|
||||
KPIID: kpi.KPIID,
|
||||
KPIValues: make([]KPIValue, len(kpi.KPIValues)),
|
||||
}
|
||||
|
||||
for j, kpiVal := range kpi.KPIValues {
|
||||
newKPI.KPIValues[j] = KPIValue{
|
||||
Name: kpiVal.Name,
|
||||
Value: generateRandomValue(),
|
||||
}
|
||||
}
|
||||
|
||||
newObj.KPIs[i] = newKPI
|
||||
}
|
||||
|
||||
newPMData = append(newPMData, newObj)
|
||||
}
|
||||
|
||||
// 写入新文件
|
||||
outputData, err := json.MarshalIndent(newPMData, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化 JSON 失败: %v", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(outputFile, outputData, 0644); err != nil {
|
||||
return fmt.Errorf("写入文件失败: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("成功生成随机数据文件: %s\n", outputFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
// generatePMDataFromMultipleSchemas 批量处理多个 schema 文件
|
||||
func generatePMDataFromMultipleSchemas(schemaDir string) error {
|
||||
// 查找所有 *-generated.json 文件
|
||||
pattern := filepath.Join(schemaDir, "*-generated.json")
|
||||
schemaFiles, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("查找 schema 文件失败: %v", err)
|
||||
}
|
||||
|
||||
if len(schemaFiles) == 0 {
|
||||
return fmt.Errorf("未找到任何 schema 文件 (pattern: %s)", pattern)
|
||||
}
|
||||
|
||||
fmt.Printf("找到 %d 个 schema 文件\n", len(schemaFiles))
|
||||
|
||||
successCount := 0
|
||||
failCount := 0
|
||||
|
||||
for _, schemaFile := range schemaFiles {
|
||||
// 生成输出文件名:将 "-generated.json" 替换为 "-random.json"
|
||||
outputFile := strings.Replace(schemaFile, "-generated.json", "-random.json", 1)
|
||||
|
||||
fmt.Printf("\n处理文件: %s\n", filepath.Base(schemaFile))
|
||||
fmt.Printf("输出文件: %s\n", filepath.Base(outputFile))
|
||||
|
||||
if err := generatePMDataFromSchema(schemaFile, outputFile); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
failCount++
|
||||
} else {
|
||||
successCount++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\n处理完成! 成功: %d, 失败: %d\n", successCount, failCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// printPMDataSummary 打印 PM 数据摘要信息
|
||||
func printPMDataSummary(file string) error {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var pmData PMData
|
||||
if err := json.Unmarshal(data, &pmData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("\n文件: %s\n", filepath.Base(file))
|
||||
fmt.Printf("对象类型数量: %d\n", len(pmData))
|
||||
|
||||
totalKPIs := 0
|
||||
for _, obj := range pmData {
|
||||
fmt.Printf(" - %s: %d 个 KPI\n", obj.ObjectType, len(obj.KPIs))
|
||||
totalKPIs += len(obj.KPIs)
|
||||
}
|
||||
fmt.Printf("总 KPI 数量: %d\n", totalKPIs)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 示例用法函数
|
||||
func main() {
|
||||
// 设置随机种子
|
||||
// rand.Seed(time.Now().UnixNano())
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("用法:")
|
||||
fmt.Println(" go run pm_generator.go <command> [args]")
|
||||
fmt.Println("")
|
||||
fmt.Println("命令:")
|
||||
fmt.Println(" batch <directory> - 批量处理目录下的所有 *-generated.json 文件")
|
||||
fmt.Println(" single <input> <output> - 处理单个文件")
|
||||
fmt.Println(" summary <file> - 显示文件摘要信息")
|
||||
fmt.Println("")
|
||||
fmt.Println("示例:")
|
||||
fmt.Println(" go run pm_generator.go batch .")
|
||||
fmt.Println(" go run pm_generator.go single AMF-PM-generated.json AMF-PM-random.json")
|
||||
fmt.Println(" go run pm_generator.go summary AMF-PM-random.json")
|
||||
return
|
||||
}
|
||||
|
||||
command := os.Args[1]
|
||||
|
||||
switch command {
|
||||
case "batch":
|
||||
dir := "."
|
||||
if len(os.Args) > 2 {
|
||||
dir = os.Args[2]
|
||||
}
|
||||
|
||||
fmt.Println("PM 数据随机生成器 - 批量模式")
|
||||
fmt.Println(strings.Repeat("=", 50))
|
||||
|
||||
if err := generatePMDataFromMultipleSchemas(dir); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
case "single":
|
||||
if len(os.Args) < 4 {
|
||||
fmt.Println("用法: go run pm_generator.go single <input_file> <output_file>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
inputFile := os.Args[2]
|
||||
outputFile := os.Args[3]
|
||||
|
||||
fmt.Printf("处理单个文件: %s -> %s\n", inputFile, outputFile)
|
||||
|
||||
if err := generatePMDataFromSchema(inputFile, outputFile); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
case "summary":
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println("用法: go run pm_generator.go summary <file>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
file := os.Args[2]
|
||||
if err := printPMDataSummary(file); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
default:
|
||||
fmt.Printf("未知命令: %s\n", command)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user