@@ -96,10 +96,6 @@ func NewIContainerService() IContainerService {
9696}
9797
9898func (u * ContainerService ) Page (req dto.PageContainer ) (int64 , interface {}, error ) {
99- var (
100- records []container.Summary
101- list []container.Summary
102- )
10399 client , err := docker .NewDockerClient ()
104100 if err != nil {
105101 return 0 , nil , err
@@ -117,114 +113,32 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro
117113 if err != nil {
118114 return 0 , nil , err
119115 }
120- if req .ExcludeAppStore {
121- for _ , item := range containers {
122- if created , ok := item .Labels [composeCreatedBy ]; ok && created == "Apps" {
123- continue
124- }
125- list = append (list , item )
126- }
127- } else {
128- list = containers
129- }
130-
131- if len (req .Name ) != 0 {
132- length , count := len (list ), 0
133- for count < length {
134- if ! strings .Contains (list [count ].Names [0 ][1 :], req .Name ) && ! strings .Contains (list [count ].Image , req .Name ) {
135- list = append (list [:count ], list [(count + 1 ):]... )
136- length --
137- } else {
138- count ++
139- }
140- }
141- }
142- if req .State != "all" {
143- length , count := len (list ), 0
144- for count < length {
145- if list [count ].State != req .State {
146- list = append (list [:count ], list [(count + 1 ):]... )
147- length --
148- } else {
149- count ++
150- }
151- }
152- }
153- switch req .OrderBy {
154- case "name" :
155- sort .Slice (list , func (i , j int ) bool {
156- if req .Order == constant .OrderAsc {
157- return list [i ].Names [0 ][1 :] < list [j ].Names [0 ][1 :]
158- }
159- return list [i ].Names [0 ][1 :] > list [j ].Names [0 ][1 :]
160- })
161- default :
162- sort .Slice (list , func (i , j int ) bool {
163- if req .Order == constant .OrderAsc {
164- return list [i ].Created < list [j ].Created
165- }
166- return list [i ].Created > list [j ].Created
167- })
168- }
116+ records := searchWithFilter (req , containers )
169117
170- total , start , end := len (list ), (req .Page - 1 )* req .PageSize , req .Page * req .PageSize
118+ var backData []dto.ContainerInfo
119+ total , start , end := len (records ), (req .Page - 1 )* req .PageSize , req .Page * req .PageSize
171120 if start > total {
172- records = make ([]container. Summary , 0 )
121+ backData = make ([]dto. ContainerInfo , 0 )
173122 } else {
174123 if end >= total {
175124 end = total
176125 }
177- records = list [start :end ]
126+ backData = records [start :end ]
178127 }
179128
180- backDatas := make ([]dto.ContainerInfo , len (records ))
181- for i := 0 ; i < len (records ); i ++ {
182- item := records [i ]
183- IsFromCompose := false
184- if _ , ok := item .Labels [composeProjectLabel ]; ok {
185- IsFromCompose = true
186- }
187- IsFromApp := false
188- if created , ok := item .Labels [composeCreatedBy ]; ok && created == "Apps" {
189- IsFromApp = true
190- }
191-
192- exposePorts := transPortToStr (records [i ].Ports )
193- info := dto.ContainerInfo {
194- ContainerID : item .ID ,
195- CreateTime : time .Unix (item .Created , 0 ).Format (constant .DateTimeLayout ),
196- Name : item .Names [0 ][1 :],
197- ImageId : strings .Split (item .ImageID , ":" )[1 ],
198- ImageName : item .Image ,
199- State : item .State ,
200- RunTime : item .Status ,
201- Ports : exposePorts ,
202- IsFromApp : IsFromApp ,
203- IsFromCompose : IsFromCompose ,
204- SizeRw : item .SizeRw ,
205- SizeRootFs : item .SizeRootFs ,
206- }
207- install , _ := appInstallRepo .GetFirst (appInstallRepo .WithContainerName (info .Name ))
129+ for i := 0 ; i < len (backData ); i ++ {
130+ install , _ := appInstallRepo .GetFirst (appInstallRepo .WithContainerName (backData [i ].Name ))
208131 if install .ID > 0 {
209- info .AppInstallName = install .Name
210- info .AppName = install .App .Name
132+ backData [ i ] .AppInstallName = install .Name
133+ backData [ i ] .AppName = install .App .Name
211134 websites , _ := websiteRepo .GetBy (websiteRepo .WithAppInstallId (install .ID ))
212135 for _ , website := range websites {
213- info .Websites = append (info .Websites , website .PrimaryDomain )
214- }
215- }
216- backDatas [i ] = info
217- if item .NetworkSettings != nil && len (item .NetworkSettings .Networks ) > 0 {
218- networks := make ([]string , 0 , len (item .NetworkSettings .Networks ))
219- for key := range item .NetworkSettings .Networks {
220- networks = append (networks , item .NetworkSettings .Networks [key ].IPAddress )
136+ backData [i ].Websites = append (backData [i ].Websites , website .PrimaryDomain )
221137 }
222- sort .Strings (networks )
223- backDatas [i ].Network = networks
224138 }
225139 }
226140
227- return int64 (total ), backDatas , nil
141+ return int64 (total ), backData , nil
228142}
229143
230144func (u * ContainerService ) List () []dto.ContainerOptions {
@@ -1771,3 +1685,110 @@ func loadContainerPortForInfo(itemPorts []container.Port) []dto.PortHelper {
17711685 }
17721686 return exposedPorts
17731687}
1688+
1689+ func searchWithFilter (req dto.PageContainer , containers []container.Summary ) []dto.ContainerInfo {
1690+ var (
1691+ records []dto.ContainerInfo
1692+ list []container.Summary
1693+ )
1694+
1695+ if req .ExcludeAppStore {
1696+ for _ , item := range containers {
1697+ if created , ok := item .Labels [composeCreatedBy ]; ok && created == "Apps" {
1698+ continue
1699+ }
1700+ list = append (list , item )
1701+ }
1702+ } else {
1703+ list = containers
1704+ }
1705+
1706+ if len (req .Name ) != 0 {
1707+ length , count := len (list ), 0
1708+ for count < length {
1709+ if ! strings .Contains (list [count ].Names [0 ][1 :], req .Name ) && ! strings .Contains (list [count ].Image , req .Name ) {
1710+ list = append (list [:count ], list [(count + 1 ):]... )
1711+ length --
1712+ } else {
1713+ count ++
1714+ }
1715+ }
1716+ }
1717+ if req .State != "all" {
1718+ length , count := len (list ), 0
1719+ for count < length {
1720+ if list [count ].State != req .State {
1721+ list = append (list [:count ], list [(count + 1 ):]... )
1722+ length --
1723+ } else {
1724+ count ++
1725+ }
1726+ }
1727+ }
1728+ switch req .OrderBy {
1729+ case "name" :
1730+ sort .Slice (list , func (i , j int ) bool {
1731+ if req .Order == constant .OrderAsc {
1732+ return list [i ].Names [0 ][1 :] < list [j ].Names [0 ][1 :]
1733+ }
1734+ return list [i ].Names [0 ][1 :] > list [j ].Names [0 ][1 :]
1735+ })
1736+ default :
1737+ sort .Slice (list , func (i , j int ) bool {
1738+ if req .Order == constant .OrderAsc {
1739+ return list [i ].Created < list [j ].Created
1740+ }
1741+ return list [i ].Created > list [j ].Created
1742+ })
1743+ }
1744+ for _ , item := range list {
1745+ IsFromCompose := false
1746+ if _ , ok := item .Labels [composeProjectLabel ]; ok {
1747+ IsFromCompose = true
1748+ }
1749+ IsFromApp := false
1750+ if created , ok := item .Labels [composeCreatedBy ]; ok && created == "Apps" {
1751+ IsFromApp = true
1752+ }
1753+ exposePorts := transPortToStr (item .Ports )
1754+ info := dto.ContainerInfo {
1755+ ContainerID : item .ID ,
1756+ CreateTime : time .Unix (item .Created , 0 ).Format (constant .DateTimeLayout ),
1757+ Name : item .Names [0 ][1 :],
1758+ Ports : exposePorts ,
1759+ ImageId : strings .Split (item .ImageID , ":" )[1 ],
1760+ ImageName : item .Image ,
1761+ State : item .State ,
1762+ RunTime : item .Status ,
1763+ SizeRw : item .SizeRw ,
1764+ SizeRootFs : item .SizeRootFs ,
1765+ IsFromApp : IsFromApp ,
1766+ IsFromCompose : IsFromCompose ,
1767+ }
1768+ if item .NetworkSettings != nil && len (item .NetworkSettings .Networks ) > 0 {
1769+ networks := make ([]string , 0 , len (item .NetworkSettings .Networks ))
1770+ for key := range item .NetworkSettings .Networks {
1771+ networks = append (networks , item .NetworkSettings .Networks [key ].IPAddress )
1772+ }
1773+ sort .Strings (networks )
1774+ info .Network = networks
1775+ }
1776+ records = append (records , info )
1777+ }
1778+ dscriptions , _ := settingRepo .GetDescriptionList (repo .WithByType ("container" ))
1779+ for i := 0 ; i < len (records ); i ++ {
1780+ for _ , desc := range dscriptions {
1781+ if desc .ID == records [i ].ContainerID {
1782+ records [i ].Description = desc .Description
1783+ records [i ].IsPinned = desc .IsPinned
1784+ }
1785+ }
1786+ }
1787+ sort .Slice (records , func (i , j int ) bool {
1788+ if records [i ].IsPinned == records [j ].IsPinned {
1789+ return list [i ].Created > list [j ].Created
1790+ }
1791+ return records [i ].IsPinned
1792+ })
1793+ return records
1794+ }
0 commit comments