intelligent_monitoring_backend/Code/backend/internal/dbs/db_init.go

101 lines
3.1 KiB
Go

package dbs
import (
"fmt"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gen"
"gorm.io/gorm"
)
var isInit = false
var mysqlUser string
var mysqlPassword string
var mysqlUrl string
var mysqlDatabase string
func GetGormDB() (db *gorm.DB) {
var MySQLDSN string
if !isInit {
viper.SetConfigName("database")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
mysqlUser = viper.GetString("mysql.username")
mysqlPassword = viper.GetString("mysql.password")
mysqlUrl = viper.GetString("mysql.url")
mysqlDatabase = viper.GetString("mysql.databasename")
isInit = true
}
if mysqlPassword == "" || len(mysqlPassword) == 0 {
MySQLDSN = fmt.Sprintf("%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
mysqlUser, mysqlUrl, mysqlDatabase)
} else {
MySQLDSN = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
mysqlUser, mysqlPassword, mysqlUrl, mysqlDatabase)
}
db, err2 := gorm.Open(mysql.Open(MySQLDSN))
if err2 != nil {
panic(fmt.Errorf("cannot establish db connection: %w", err2))
}
return
}
func GenerateModel() {
g := gen.NewGenerator(gen.Config{
OutPath: "../../internal/query",
})
g.UseDB(GetGormDB())
g.ApplyBasic(
g.GenerateModel("cameras"),
g.GenerateModelAs("person_detection_record", "PersonDetectionRecord"),
g.GenerateModelAs("trajectory_detection_record", "TrajectoryDetectionRecord"),
g.GenerateModelAs("person", "Person"))
g.Execute()
}
func insertlocal() {
//Create_local_component("hadoop", "3.0.0", "../../shell/init/hadoop.sh", "node1")
//Create_local_component("spark", "3.3.0", "../../shell/init/spark.sh", "node1")
//Create_local_component("hbase", "2.0.0", "../../shell/init/hbase.sh", "node1")
//Create_local_component("zookeeper", "3.7.1", "../../shell/init/zookeeper.sh", "node1")
//Create_local_component("hive", "3.1.2", "../../shell/init/hive.sh", "node1")
//Create_local_component("flink", "1.16.2", "../../shell/init/flink.sh", "node1")
//Create_local_component("streampark", "2.0.0", "../../shell/init/streampark.sh", "node1")
//Create_local_component("zeppelin", "0.10.1", "../../shell/init/zeppelin.sh", "node1")
}
func insertK8s() {
//Create_k8s_component("hadoop", "hadoop:3.0.0")
//Create_k8s_component("spark", "spark:3.3.0")
//Create_k8s_component("hbase", "hadpp:2.0.0")
//Create_k8s_component("zookeeper", "zookeeper:3.7.1")
//Create_k8s_component("hive", "hadpp:3.1.2")
//Create_k8s_component("flink", "flink:1.16.2")
//Create_k8s_component("streampark", "streampark:2.0.0")
//Create_k8s_component("zeppelin", "zeppelin:0.10.1")
}
func insertlocalStatus() {
//created := "created"
//node := "node1"
//Create_local_status("hadoop", created, node)
//Create_local_status("spark", created, node)
//Create_local_status("hbase", created, node)
//Create_local_status("zookeeper", created, node)
//Create_local_status("hive", created, node)
//Create_local_status("flink", created, node)
//Create_local_status("streampark", created, node)
//Create_local_status("zeppelin", created, node)
}