@@ -144,10 +144,10 @@ func (bolt *Bolt) GetAll(bucket string) ([][]byte, error) {
144144 return result , err
145145}
146146
147- func (bolt * Bolt ) GetAllByName (id string , bucket string ) ([][]byte , error ) {
147+ func (bolt * Bolt ) GetAllUpdatesByMachineId (id string ) ([][]byte , error ) {
148148 var result [][]byte
149149 err := bolt .db .View (func (tx * bbolt.Tx ) error {
150- bucket := tx .Bucket ([]byte (bucket ))
150+ bucket := tx .Bucket ([]byte ("history" ))
151151 cursor := bucket .Cursor ()
152152 for k , v := cursor .First (); k != nil ; k , v = cursor .Next () {
153153 var update structs.Update
@@ -164,54 +164,64 @@ func (bolt *Bolt) GetAllByName(id string, bucket string) ([][]byte, error) {
164164 return result , err
165165}
166166
167- func (bolt * Bolt ) GetAllByOperatorId (id string , bucket string ) ([][]byte , error ) {
168- var result [][]byte
169- err := bolt .db .View (func (tx * bbolt.Tx ) error {
170- bucket := tx .Bucket ([]byte (bucket ))
171- cursor := bucket .Cursor ()
172- for k , v := cursor .First (); k != nil ; k , v = cursor .Next () {
173- var machine structs.Machine
174- err := json .Unmarshal (v , & machine )
175- if err != nil {
176- return err
177- }
178- if machine .Operator .Id == id {
179- result = append (result , v )
180- }
167+ func (bolt * Bolt ) GetAllMachinesByOperatorId (id string ) (structs.Machines , error ) {
168+ var result structs.Machines
169+ machines , err := bolt .GetAllMachines (false )
170+ if err != nil {
171+ return nil , err
172+ }
173+ for _ , m := range machines {
174+ if m .Operator .Id != id {
175+ continue
181176 }
182- return nil
183- })
184- return result , err
177+ result = append ( result , m )
178+ }
179+ return result , nil
185180}
186181
187- func (bolt * Bolt ) GetActiveMachines () (structs.Machines , error ) {
188- machines , _ := bolt .GetAll ("machine" )
182+ func (bolt * Bolt ) GetAllMachinesBySystemId (systemId string ) (structs.Machines , error ) {
183+ var result structs.Machines
184+ machines , err := bolt .GetAllMachines (false )
185+
186+ for _ , m := range machines {
187+ if m .System .Id != systemId {
188+ continue
189+ }
190+ result = append (result , m )
191+ }
192+ return result , err
193+ }
189194
190- Machines := structs.Machines {}
195+ func (bolt * Bolt ) GetAllMachines (onlyActive bool ) (structs.Machines , error ) {
196+ var result structs.Machines
197+ config , err := bolt .GetConfig ()
198+ machines , err := bolt .GetAll ("machine" )
199+ if err != nil {
200+ return nil , err
201+ }
191202 for _ , v := range machines {
192- machine := structs.Machine {}
193- err : = json .Unmarshal (v , & machine )
203+ m := structs.Machine {}
204+ err = json .Unmarshal (v , & m )
194205 if err != nil {
195206 return nil , err
196207 }
197-
198- lastUpdate , _ := bolt .GetLastByName (machine .Id , "history" )
199-
200- if lastUpdate != nil {
201- update := structs.Update {}
202- err = json .Unmarshal (lastUpdate , & update )
203- if err != nil {
204- return nil , err
205- }
206- machine .Update = update
208+ if m .Inactive && onlyActive {
209+ continue
207210 }
208-
209- if ! machine .Inactive {
210- Machines = append (Machines , machine )
211+ if m .Interval == 0 {
212+ m .Interval = config .General .Interval
211213 }
214+ result = append (result , m )
215+ }
216+
217+ return result , nil
218+ }
219+
220+ func (bolt * Bolt ) GetActiveMachines () (structs.Machines , error ) {
221+ Machines , err := bolt .GetAllMachines (true )
222+ if err != nil {
223+ return nil , err
212224 }
213- // sort machines by oldest update first
214- sort .Sort (structs .ByDate (Machines ))
215225
216226 config , err := bolt .GetConfig ()
217227 if err != nil {
@@ -221,6 +231,17 @@ func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
221231 for i := range Machines {
222232 currentDate := time .Now ()
223233
234+ lastUpdate , _ := bolt .GetLastByName (Machines [i ].Id , "history" )
235+
236+ if lastUpdate != nil {
237+ update := structs.Update {}
238+ err = json .Unmarshal (lastUpdate , & update )
239+ if err != nil {
240+ return nil , err
241+ }
242+ Machines [i ].Update = update
243+ }
244+
224245 if Machines [i ].Update .Date == "" {
225246 Machines [i ].Update .Date = "0000-00-00"
226247 Machines [i ].Status = "danger"
@@ -254,6 +275,11 @@ func (bolt *Bolt) GetActiveMachines() (structs.Machines, error) {
254275
255276 }
256277
278+ // sort machines by oldest update first
279+ sort .Slice (Machines , func (i , j int ) bool {
280+ return Machines [i ].Update .Date < Machines [j ].Update .Date
281+ })
282+
257283 return Machines , nil
258284}
259285
@@ -287,6 +313,26 @@ func (bolt *Bolt) GetOperatorById(id string) (structs.Operator, error) {
287313 return operator , nil
288314}
289315
316+ func (bolt * Bolt ) GetMachinesBySystem (systemId string ) (structs.Machines , error ) {
317+ machines , err := bolt .GetAll ("machine" )
318+ if err != nil {
319+ return nil , err
320+ }
321+ machinesOfSystem := structs.Machines {}
322+ for _ , v := range machines {
323+ machine := structs.Machine {}
324+ err := json .Unmarshal (v , & machine )
325+ if err != nil {
326+ return nil , err
327+ }
328+ if machine .System .Id == systemId {
329+ machinesOfSystem = append (machinesOfSystem , machine )
330+ }
331+ }
332+
333+ return machinesOfSystem , nil
334+ }
335+
290336func (bolt * Bolt ) Close () error {
291337 return bolt .db .Close ()
292338}
0 commit comments