@@ -87,47 +87,46 @@ def clear_image_usage_cache(self) -> None:
8787 with self ._cache_lock :
8888 self ._cache .clear ()
8989
90- def fetch_all_images (self ) -> list [dict [str , Any ]]:
90+ def fetch_all_images (self , host_id : Optional [ int ] = None ) -> list [dict [str , Any ]]:
9191 image_list = []
9292 host_color_map , host_text_color_map = self ._get_host_color_maps ()
9393
94- for host_id , host_name , docker_client in self ._docker_manager .get_all_clients ():
94+ for host_id_row , host_name , docker_client in self ._docker_manager .get_all_clients ():
95+ if host_id is not None and host_id_row != host_id :
96+ continue
9597 df_images = {}
96- cached_df = self .get_cached_image_usage (host_id )
98+ cached_df = self .get_cached_image_usage (host_id_row )
9799 cached_from_images = cached_df is not None
98100 if not cached_df :
99- cached_df = self ._get_cached_disk_usage (host_id )
101+ cached_df = self ._get_cached_disk_usage (host_id_row )
100102 if cached_df :
101103 df_images = {entry .get ('Id' ): entry for entry in (cached_df .get ('Images' , []) or [])}
102104 if not cached_from_images :
103- self .refresh_image_usage_async (host_id , docker_client , host_name )
105+ self .refresh_image_usage_async (host_id_row , docker_client , host_name )
104106
105- host_color = host_color_map .get (host_id , self ._host_default_color )
106- host_text_color = host_text_color_map .get (host_id , self ._get_contrast_text_color (host_color ))
107+ host_color = host_color_map .get (host_id_row , self ._host_default_color )
108+ host_text_color = host_text_color_map .get (host_id_row , self ._get_contrast_text_color (host_color ))
107109
108110 container_repo_map = {}
109111 container_count_map = {}
112+ container_name_count_map = {}
113+ container_map_ready = False
110114 try :
111- containers = docker_client .containers .list (all = True )
115+ containers = docker_client .api .containers (all = True )
116+ container_map_ready = True
112117 for container in containers :
113- try :
114- image_id = container .image .id
115- if not image_id :
116- continue
117- stripped_id = image_id .replace ('sha256:' , '' )
118- container_count_map [image_id ] = container_count_map .get (image_id , 0 ) + 1
119- container_count_map [stripped_id ] = container_count_map .get (stripped_id , 0 ) + 1
120- image_name = None
121- if container .image .tags :
122- image_name = container .image .tags [0 ]
123- else :
124- image_name = container .attrs .get ('Config' , {}).get ('Image' )
125- if image_name :
126- repo , _ = self ._split_image_reference (image_name )
127- container_repo_map [image_id ] = repo or container_repo_map .get (image_id )
128- container_repo_map [stripped_id ] = repo or container_repo_map .get (stripped_id )
129- except Exception :
118+ image_id = container .get ('ImageID' )
119+ if not image_id :
130120 continue
121+ stripped_id = image_id .replace ('sha256:' , '' )
122+ container_count_map [image_id ] = container_count_map .get (image_id , 0 ) + 1
123+ container_count_map [stripped_id ] = container_count_map .get (stripped_id , 0 ) + 1
124+ image_name = container .get ('Image' )
125+ if image_name :
126+ container_name_count_map [image_name ] = container_name_count_map .get (image_name , 0 ) + 1
127+ repo , _ = self ._split_image_reference (image_name )
128+ container_repo_map [image_id ] = repo or container_repo_map .get (image_id )
129+ container_repo_map [stripped_id ] = repo or container_repo_map .get (stripped_id )
131130 except Exception as error :
132131 self ._logger .debug ("Failed to map container images for host %s: %s" , host_name , error )
133132
@@ -141,15 +140,21 @@ def fetch_all_images(self) -> list[dict[str, Any]]:
141140 containers_count = entry .get ('Containers' )
142141 stripped_id = image_id .replace ('sha256:' , '' )
143142 fallback_count = container_count_map .get (image_id ) or container_count_map .get (stripped_id )
144- if containers_count is None :
143+ if containers_count is None or ( isinstance ( containers_count , int ) and containers_count < 0 ) :
145144 containers_count = fallback_count
146145 elif containers_count == 0 and fallback_count :
147146 containers_count = fallback_count
148147 if containers_count is None and container_count_map :
149148 containers_count = 0
149+ if containers_count == 0 and container_name_count_map :
150+ for tag in entry .get ('RepoTags' ) or []:
151+ tag_count = container_name_count_map .get (tag )
152+ if tag_count :
153+ containers_count = tag_count
154+ break
150155 if containers_count is not None and containers_count < 0 :
151156 containers_count = None
152- containers_pending = cached_df is None and containers_count is None and not container_count_map
157+ containers_pending = containers_count is None and not container_map_ready
153158 repo_tags = entry .get ('RepoTags' ) or []
154159 repo_digests = entry .get ('RepoDigests' ) or []
155160 if not repo_tags :
@@ -175,7 +180,7 @@ def fetch_all_images(self) -> list[dict[str, Any]]:
175180 'containers' : containers_count ,
176181 'containers_pending' : containers_pending ,
177182 'created' : created ,
178- 'host_id' : host_id ,
183+ 'host_id' : host_id_row ,
179184 'host_name' : host_name ,
180185 'host_color' : host_color ,
181186 'host_text_color' : host_text_color ,
@@ -193,15 +198,21 @@ def fetch_all_images(self) -> list[dict[str, Any]]:
193198 containers_count = entry .get ('Containers' )
194199 stripped_id = image_id .replace ('sha256:' , '' )
195200 fallback_count = container_count_map .get (image_id ) or container_count_map .get (stripped_id )
196- if containers_count is None :
201+ if containers_count is None or ( isinstance ( containers_count , int ) and containers_count < 0 ) :
197202 containers_count = fallback_count
198203 elif containers_count == 0 and fallback_count :
199204 containers_count = fallback_count
200205 if containers_count is None and container_count_map :
201206 containers_count = 0
207+ if containers_count == 0 and container_name_count_map :
208+ for tag in entry .get ('RepoTags' ) or []:
209+ tag_count = container_name_count_map .get (tag )
210+ if tag_count :
211+ containers_count = tag_count
212+ break
202213 if containers_count is not None and containers_count < 0 :
203214 containers_count = None
204- containers_pending = cached_df is None and containers_count is None and not container_count_map
215+ containers_pending = containers_count is None and not container_map_ready
205216 repo_tags = entry .get ('RepoTags' ) or []
206217 repo_digests = entry .get ('RepoDigests' ) or []
207218 if not repo_tags :
@@ -227,7 +238,7 @@ def fetch_all_images(self) -> list[dict[str, Any]]:
227238 'containers' : containers_count ,
228239 'containers_pending' : containers_pending ,
229240 'created' : created ,
230- 'host_id' : host_id ,
241+ 'host_id' : host_id_row ,
231242 'host_name' : host_name ,
232243 'host_color' : host_color ,
233244 'host_text_color' : host_text_color ,
0 commit comments