Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summary data fetch DB change #592

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/conf/local-file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ observability:
dbname: ./accuknox-obs.db
system-observability: true
network-observability: false
write-logs-to-db: true
write-logs-to-db: false

publisher:
enable: true
Expand Down
126 changes: 126 additions & 0 deletions src/libs/dbHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,129 @@ func upsertSysSummarySQL(db *sql.DB, summary types.SystemSummary, timeCount type

return nil
}

func GetSystemSummary(cfg types.ConfigDB, filterOptions types.SystemSummary) ([]types.SystemSummary, error) {
var err = errors.New("unknown db driver")
res := []types.SystemSummary{}

if cfg.DBDriver == "mysql" {
res, err = GetSystemSummaryMySQL(cfg, filterOptions)
} else if cfg.DBDriver == "sqlite3" {
res, err = GetSystemSummarySQLite(cfg, filterOptions)
}

return res, err
}

func getSysSummarySQL(db *sql.DB, dbName string, filterOptions types.SystemSummary) ([]types.SystemSummary, error) {
var results *sql.Rows
var err error

resSummary := []types.SystemSummary{}

query := `SELECT cluster_name,cluster_id,workspace_id,namespace_name,namespace_id,container_name,
container_image,container_id,podname,operation,labels,deployment_name,source,destination,destination_namespace,
destination_labels,type,ip,port,protocol,action,count,updated_time FROM ` + dbName

var whereClause string
var args []interface{}

if filterOptions.ClusterName != "" {
concatWhereClause(&whereClause, "cluster_name")
args = append(args, filterOptions.ClusterName)
}
if filterOptions.ClusterId != "" {
concatWhereClause(&whereClause, "cluster_id")
args = append(args, filterOptions.ClusterId)
}
if filterOptions.WorkspaceId != 0 {
concatWhereClause(&whereClause, "workpsace_id")
args = append(args, filterOptions.WorkspaceId)
}
if filterOptions.NamespaceName != "" {
concatWhereClause(&whereClause, "namespace_name")
args = append(args, filterOptions.NamespaceName)
}
if filterOptions.NamespaceId != 0 {
concatWhereClause(&whereClause, "namespace_id")
args = append(args, filterOptions.NamespaceId)
}
if filterOptions.ContainerName != "" {
concatWhereClause(&whereClause, "container_name")
args = append(args, filterOptions.ContainerName)
}
if filterOptions.ContainerImage != "" {
concatWhereClause(&whereClause, "container_image")
args = append(args, filterOptions.ContainerImage)
}
if filterOptions.ContainerID != "" {
concatWhereClause(&whereClause, "container_id")
args = append(args, filterOptions.ContainerID)
}
if filterOptions.PodName != "" {
concatWhereClause(&whereClause, "podname")
args = append(args, filterOptions.PodName)
}
if filterOptions.Operation != "" {
concatWhereClause(&whereClause, "operation")
args = append(args, filterOptions.Operation)
}
if filterOptions.Labels != "" {
concatWhereClause(&whereClause, "labels")
args = append(args, filterOptions.Labels)
}
if filterOptions.Deployment != "" {
concatWhereClause(&whereClause, "deployment_name")
args = append(args, filterOptions.Deployment)
}
if filterOptions.Source != "" {
concatWhereClause(&whereClause, "source")
args = append(args, filterOptions.Source)
}
if filterOptions.Destination != "" {
concatWhereClause(&whereClause, "destination")
args = append(args, filterOptions.Destination)
}

results, err = db.Query(query+whereClause, args...)

if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer results.Close()

for results.Next() {
localSum := types.SystemSummary{}
if err := results.Scan(
&localSum.ClusterName,
&localSum.ClusterId,
&localSum.WorkspaceId,
&localSum.NamespaceName,
&localSum.NamespaceId,
&localSum.ContainerName,
&localSum.ContainerImage,
&localSum.ContainerID,
&localSum.PodName,
&localSum.Operation,
&localSum.Labels,
&localSum.Deployment,
&localSum.Source,
&localSum.Destination,
&localSum.DestNamespace,
&localSum.DestLabels,
&localSum.NwType,
&localSum.IP,
&localSum.Port,
&localSum.Protocol,
&localSum.Action,
&localSum.Count,
&localSum.UpdatedTime,
); err != nil {
return nil, err
}
resSummary = append(resSummary, localSum)
}

return resSummary, err
}
43 changes: 10 additions & 33 deletions src/libs/mysqlHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ func GetPodNamesMySQL(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
var err error

// Get podnames from system table
query := "SELECT pod_name FROM " + TableSystemLogs_TableName + " "
query := "SELECT podname FROM " + TableSystemSummarySQLite + " "

var whereClause string
var sysargs []interface{}
Expand Down Expand Up @@ -1586,38 +1586,6 @@ func GetPodNamesMySQL(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
resPodNames = append(resPodNames, locPodName)
}

// Get podnames from network table
query = "SELECT source_pod_name FROM " + TableNetworkLogs_TableName + " "

whereClause = ""
var nwargs []interface{}

if filter.Namespace != "" {
concatWhereClause(&whereClause, "source_namespace")
nwargs = append(nwargs, filter.Namespace)
}
if filter.Labels != "" {
concatWhereClause(&whereClause, "source_labels")
nwargs = append(nwargs, filter.Labels)
}

results, err = db.Query(query+whereClause, nwargs...)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer results.Close()

for results.Next() {
var locPodName string
if err := results.Scan(
&locPodName,
); err != nil {
return nil, err
}
resPodNames = append(resPodNames, locPodName)
}

return resPodNames, err
}

Expand Down Expand Up @@ -1747,3 +1715,12 @@ func UpsertSystemSummaryMySQL(cfg types.ConfigDB, sysSummary map[types.SystemSum

return nil
}

func GetSystemSummaryMySQL(cfg types.ConfigDB, filterOptions types.SystemSummary) ([]types.SystemSummary, error) {
db := connectMySQL(cfg)
defer db.Close()

res, err := getSysSummarySQL(db, TableSystemSummarySQLite, filterOptions)

return res, err
}
43 changes: 10 additions & 33 deletions src/libs/sqliteHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ func GetPodNamesSQLite(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
var err error

// Get podnames from system table
query := "SELECT pod_name FROM " + TableSystemLogs_TableName + " "
query := "SELECT podname FROM " + TableSystemSummarySQLite + " "

var whereClause string
var sysargs []interface{}
Expand Down Expand Up @@ -1593,38 +1593,6 @@ func GetPodNamesSQLite(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
resPodNames = append(resPodNames, locPodName)
}

// Get podnames from network table
query = "SELECT source_pod_name FROM " + TableNetworkLogs_TableName + " "

whereClause = ""
var nwargs []interface{}

if filter.Namespace != "" {
concatWhereClause(&whereClause, "source_namespace")
nwargs = append(nwargs, filter.Namespace)
}
if filter.Labels != "" {
concatWhereClause(&whereClause, "source_labels")
nwargs = append(nwargs, filter.Labels)
}

results, err = db.Query(query+whereClause, nwargs...)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer results.Close()

for results.Next() {
var locPodName string
if err := results.Scan(
&locPodName,
); err != nil {
return nil, err
}
resPodNames = append(resPodNames, locPodName)
}

return resPodNames, err
}

Expand Down Expand Up @@ -1751,3 +1719,12 @@ func UpsertSystemSummarySQLite(cfg types.ConfigDB, sysSummary map[types.SystemSu

return nil
}

func GetSystemSummarySQLite(cfg types.ConfigDB, filterOptions types.SystemSummary) ([]types.SystemSummary, error) {
db := connectSQLite(cfg, config.GetCfgObservabilityDBName())
defer db.Close()

res, err := getSysSummarySQL(db, TableSystemSummarySQLite, filterOptions)

return res, err
}
Loading