@@ -18,47 +18,87 @@ package database
1818import (
1919 "context"
2020
21- "github.com/amreo/mu"
2221 "github.com/ercole-io/ercole/v2/api-service/dto"
2322 "github.com/ercole-io/ercole/v2/utils"
2423 "go.mongodb.org/mongo-driver/bson"
2524)
2625
27- func (md * MongoDatabase ) GetOracleDiskGroups (filter dto.GlobalFilter ) ([]dto.OracleDatabaseDiskGroupDto , error ) {
26+ func (md * MongoDatabase ) GetOracleDiskGroups (hostname string , dbname string ) ([]dto.OracleDatabaseDiskGroupDto , error ) {
27+ return md .load (dto.GlobalFilter {}, hostname , dbname )
28+ }
29+
30+ func (md * MongoDatabase ) ListOracleDiskGroups (filter dto.GlobalFilter ) ([]dto.OracleDatabaseDiskGroupDto , error ) {
31+ return md .load (filter , "" , "" )
32+ }
33+
34+ func (md * MongoDatabase ) load (filter dto.GlobalFilter , hostname string , dbname string ) ([]dto.OracleDatabaseDiskGroupDto , error ) {
2835 ctx := context .TODO ()
2936
37+ percentage := func (amount interface {}, total string ) bson.M {
38+ return bson.M {
39+ "$cond" : bson.A {
40+ bson.M {"$eq" : bson.A {total , 0 }},
41+ 0 ,
42+ bson.M {"$round" : bson.A {
43+ bson.M {"$multiply" : bson.A {
44+ bson.M {"$divide" : bson.A {amount , total }},
45+ 100 ,
46+ }},
47+ 2 ,
48+ }},
49+ },
50+ }
51+ }
52+
53+ var pipeline bson.A
54+
55+ if hostname != "" {
56+ pipeline = append (pipeline , bson.D {{Key : "$match" , Value : bson.M {"hostname" : hostname }}})
57+ }
58+
59+ if filter .OlderThan != utils .MAX_TIME && ! filter .OlderThan .IsZero () {
60+ pipeline = append (pipeline , FilterByOldnessSteps (filter .OlderThan )... )
61+ }
62+
63+ if filter .Location != "" || filter .Environment != "" {
64+ loc := FilterByLocationAndEnvironmentSteps (filter .Location , filter .Environment ).(bson.A )
65+ pipeline = append (pipeline , loc ... )
66+ }
67+
68+ pipeline = append (pipeline ,
69+ bson.D {{Key : "$match" , Value : bson.M {"archived" : false }}},
70+ bson.D {{Key : "$unwind" , Value : bson.M {"path" : "$features.oracle.database.databases" }}},
71+ bson.D {{Key : "$unwind" , Value : bson.M {"path" : "$features.oracle.database.databases.diskGroups" }}},
72+ bson.D {{Key : "$group" , Value : bson.M {
73+ "_id" : bson.M {
74+ "hostname" : "$hostname" ,
75+ "diskGroupName" : "$features.oracle.database.databases.diskGroups.diskGroupName" ,
76+ },
77+ "hostname" : bson.M {"$first" : "$hostname" },
78+ "databases" : bson.M {"$addToSet" : "$features.oracle.database.databases.name" },
79+ "diskGroupName" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.diskGroupName" },
80+ "totalSpace" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.totalSpace" },
81+ "freeSpace" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.freeSpace" },
82+ }}},
83+ bson.D {{Key : "$project" , Value : bson.M {
84+ "_id" : 0 ,
85+ "hostname" : 1 ,
86+ "databases" : 1 ,
87+ "oracleDatabaseDiskGroup.diskGroupName" : "$diskGroupName" ,
88+ "oracleDatabaseDiskGroup.totalSpace" : "$totalSpace" ,
89+ "oracleDatabaseDiskGroup.freeSpace" : "$freeSpace" ,
90+ "percentageFreeSpace" : percentage ("$freeSpace" , "$totalSpace" ),
91+ "usedSpace" : bson.M {"$subtract" : bson.A {"$totalSpace" , "$freeSpace" }},
92+ "percentageUsedSpace" : percentage (bson.M {"$subtract" : bson.A {"$totalSpace" , "$freeSpace" }}, "$totalSpace" ),
93+ }}})
94+
95+ if dbname != "" {
96+ pipeline = append (pipeline , bson.D {{Key : "$match" , Value : bson.M {"databases" : dbname }}})
97+ }
98+
3099 result := make ([]dto.OracleDatabaseDiskGroupDto , 0 )
31100
32- cur , err := md .Client .Database (md .Config .Mongodb .DBName ).Collection ("hosts" ).Aggregate (
33- ctx ,
34- mu .MAPipeline (
35- FilterByOldnessSteps (filter .OlderThan ),
36- FilterByLocationAndEnvironmentSteps (filter .Location , filter .Environment ),
37- bson.M {"$unwind" : bson.M {"path" : "$features.oracle.database.databases" }},
38- bson.M {"$unwind" : bson.M {"path" : "$features.oracle.database.databases.diskGroups" }},
39- bson.M {"$group" : bson.M {
40- "_id" : bson.M {
41- "hostname" : "$hostname" ,
42- "diskGroupName" : "$features.oracle.database.databases.diskGroups.diskGroupName" ,
43- },
44- "hostname" : bson.M {"$first" : "$hostname" },
45- "databases" : bson.M {"$addToSet" : "$features.oracle.database.databases.name" },
46- "diskGroupName" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.diskGroupName" },
47- "totalSpace" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.totalSpace" },
48- "freeSpace" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.freeSpace" },
49- "usedSpace" : bson.M {"$first" : "$features.oracle.database.databases.diskGroups.usedSpace" },
50- }},
51- bson.M {"$project" : bson.M {
52- "_id" : 0 ,
53- "hostname" : 1 ,
54- "databases" : 1 ,
55- "oracleDatabaseDiskGroup.diskGroupName" : "$diskGroupName" ,
56- "oracleDatabaseDiskGroup.totalSpace" : "$totalSpace" ,
57- "oracleDatabaseDiskGroup.freeSpace" : "$freeSpace" ,
58- "oracleDatabaseDiskGroup.usedSpace" : "$usedSpace" ,
59- }},
60- ),
61- )
101+ cur , err := md .Client .Database (md .Config .Mongodb .DBName ).Collection ("hosts" ).Aggregate (ctx , pipeline )
62102 if err != nil {
63103 return nil , utils .NewError (err , "DB ERROR" )
64104 }
0 commit comments