Skip to content

Commit

Permalink
Added page to list the peripherals
Browse files Browse the repository at this point in the history
  • Loading branch information
Serp1co authored and mackdk committed Feb 24, 2025
1 parent d4822c2 commit d78059a
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 184 deletions.
22 changes: 21 additions & 1 deletion java/code/src/com/suse/manager/hub/HubManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,27 @@ public List<Channel> collectAllChannels(IssAccessToken accessToken) {
return ChannelFactory.listAllChannels();
}

/**
* Count the registered peripherals on "this" hub
* @param user the SatAdmin
* @return the count of registered peripherals entities
*/
public Long countRegisteredPeripherals(User user) {
ensureSatAdmin(user);
return hubFactory.countPeripherals();
}

/**
* List the peripherals with pagination //TODO: and search
* @param user the SatAdmin
* @param pc the PageControl
* @return a List of Peripherals entities
*/
public List<IssPeripheral> listRegisteredPeripherals(User user, PageControl pc) {
ensureSatAdmin(user);
return hubFactory.listPaginatedPeripherals(pc.getStart() - 1, pc.getPageSize());
}

/**
* add vendor channel to peripheral
*
Expand Down Expand Up @@ -975,7 +996,6 @@ public List<Channel> addVendorChannels(IssAccessToken accessToken, List<String>
.toList();
}


/**
* add custom channels to peripheral
*
Expand Down
13 changes: 13 additions & 0 deletions java/code/src/com/suse/manager/model/hub/HubFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ public long countPeripherals() {
return getSession().createQuery("SELECT count(*) FROM IssPeripheral", Long.class).uniqueResult();
}

/**
* get the list of all the peripheral servers for a hub
* @param offset the first item to retrieve
* @param pageSize the maximum number of items to retrieve
* @return a list of paginated peripherals
*/
public List<IssPeripheral> listPaginatedPeripherals(int offset, int pageSize) {
return getSession().createQuery("FROM IssPeripheral", IssPeripheral.class)
.setFirstResult(offset)
.setMaxResults(pageSize)
.list();
}

/**
* Lookup {@link IssPeripheral} object by its FQDN
* @param fqdnIn the fqdn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@
import com.suse.manager.reactor.utils.OptionalTypeAdapterFactory;
import com.suse.manager.webui.controllers.ECMAScriptDateAdapter;
import com.suse.manager.webui.controllers.admin.beans.HubDetailsData;
import com.suse.manager.webui.controllers.admin.beans.PeripheralResponse;
import com.suse.manager.webui.controllers.admin.mappers.PaygResponseMappers;
import com.suse.manager.webui.utils.FlashScopeHelper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -146,13 +143,7 @@ public static ModelAndView showISSv3Hub(Request request, Response response, User
* @return the view to show
*/
public static ModelAndView showISSv3Peripherals(Request request, Response response, User user) {
Map<String, Object> data = new HashMap<>();
Type listType = new TypeToken<List<PeripheralResponse>>() { }.getType();
List<PeripheralResponse> src = HUB_FACTORY.listPeripherals().stream()
.map(ph -> new PeripheralResponse(ph.getId(), ph.getFqdn(), 0L, 0L, 0L))
.toList();
data.put("peripherals", GSON.toJson(src, listType));
return new ModelAndView(data, "controllers/admin/templates/list_peripherals.jade");
return new ModelAndView(new HashMap<>(), "controllers/admin/templates/list_peripherals.jade");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,138 +8,72 @@
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*/

package com.suse.manager.webui.controllers.admin.beans;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import com.suse.manager.model.hub.IssPeripheral;
import com.suse.manager.model.hub.IssPeripheralChannels;

public class PeripheralResponse {
private long id;
private String fqdn;
private long nChannelsSync;
private long nAllChannels;
private long nOrgs;
private final long id;
private final String fqdn;
private final long nChannelsSync;
private final long nSyncOrgs;
private final String rootCA;

/**
* Default constructor
* @param idIn the id
* @param fqdnIn the fully qualified domain name
* @param nChannelsSyncIn the number of channels currently synced
* @param nAllChannelsIn the total number of channels
* @param nOrgsIn the number of organizations
* Response for peripherals list
* @param idIn
* @param fqdnIn
* @param nChannelsSyncIn
* @param nSyncOrgsIn
* @param rootCAIn
*/
public PeripheralResponse(long idIn, String fqdnIn, long nChannelsSyncIn, long nAllChannelsIn, long nOrgsIn) {
this.id = idIn;
this.fqdn = fqdnIn;
this.nChannelsSync = nChannelsSyncIn;
this.nAllChannels = nAllChannelsIn;
this.nOrgs = nOrgsIn;
public PeripheralResponse(
long idIn,
String fqdnIn,
long nChannelsSyncIn,
long nSyncOrgsIn,
String rootCAIn
) {
id = idIn;
fqdn = fqdnIn;
nChannelsSync = nChannelsSyncIn;
nSyncOrgs = nSyncOrgsIn;
rootCA = rootCAIn;
}

public long getId() {
return id;
}

public void setId(long idIn) {
this.id = idIn;
}

public String getFqdn() {
return fqdn;
}

public void setFqdn(String fqdnIn) {
this.fqdn = fqdnIn;
}

/**
* gets the number of synced channels
* @return the number of synced channel
*/
public long getnChannelsSync() {
public long getNChannelsSync() {
return nChannelsSync;
}

/**
* sets the number of synced channels
* @param nChannelsSyncIn the number of synced channels
*/
public void setnChannelsSync(long nChannelsSyncIn) {
this.nChannelsSync = nChannelsSyncIn;
public long getNSyncOrgs() {
return nSyncOrgs;
}

/**
* gets the number of all channels
* @return the number of all channels
*/
public long getnAllChannels() {
return nAllChannels;
public String getRootCA() {
return rootCA;
}

/**
* sets the number of all channels
* @param nAllChannelsIn the number of all channels
* Helper converter method from db entity
* @param peripheralEntity
* @return
*/
public void setnAllChannels(long nAllChannelsIn) {
this.nAllChannels = nAllChannelsIn;
}

/**
* gets the number of organizations
* @return the number of organizations
*/
public long getnOrgs() {
return nOrgs;
}

/**
* sets the number of organizations
* @param nOrgsIn the number of organizations
*/
public void setnOrgs(long nOrgsIn) {
this.nOrgs = nOrgsIn;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof PeripheralResponse that)) {
return false;
}

return new EqualsBuilder()
.append(getId(), that.getId())
.append(getnChannelsSync(), that.getnChannelsSync())
.append(getnAllChannels(), that.getnAllChannels())
.append(getnOrgs(), that.getnOrgs())
.append(getFqdn(), that.getFqdn())
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(getId())
.append(getFqdn())
.append(getnChannelsSync())
.append(getnAllChannels())
.append(getnOrgs())
.toHashCode();
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PeripheralResponse{");
sb.append("id=").append(id);
sb.append(", fqdn='").append(fqdn).append('\'');
sb.append(", nChannelsSync=").append(nChannelsSync);
sb.append(", nAllChannels=").append(nAllChannels);
sb.append(", nOrgs=").append(nOrgs);
sb.append('}');
return sb.toString();
public static PeripheralResponse fromIssEntity(IssPeripheral peripheralEntity) {
return new PeripheralResponse(
peripheralEntity.getId(),
peripheralEntity.getFqdn(),
peripheralEntity.getPeripheralChannels().size(),
peripheralEntity.getPeripheralChannels().stream()
.map(IssPeripheralChannels::getPeripheralOrgId).distinct().count(),
peripheralEntity.getRootCa());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.suse.manager.webui.controllers.ECMAScriptDateAdapter;
import com.suse.manager.webui.controllers.admin.beans.CreateTokenRequest;
import com.suse.manager.webui.controllers.admin.beans.HubRegisterRequest;
import com.suse.manager.webui.controllers.admin.beans.PeripheralResponse;
import com.suse.manager.webui.controllers.admin.beans.UpdateRootCARequest;
import com.suse.manager.webui.controllers.admin.beans.ValidityRequest;
import com.suse.manager.webui.utils.FlashScopeHelper;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void initRoutes() {
post("/manager/api/admin/hub/access-tokens", withProductAdmin(this::createToken));
post("/manager/api/admin/hub/access-tokens/:id/validity", withProductAdmin(this::setAccessTokenValidity));
delete("/manager/api/admin/hub/access-tokens/:id", withProductAdmin(this::deleteAccessToken));
get("/manager/api/admin/hub/peripherals", withProductAdmin(this::pass));
get("/manager/api/admin/hub/peripherals", withProductAdmin(this::listPaginatedPeripherals));
post("/manager/api/admin/hub/peripherals", withProductAdmin(this::registerPeripheral));
get("/manager/api/admin/hub/peripherals/:id", withProductAdmin(this::pass));
patch("/manager/api/admin/hub/peripherals/:id", withProductAdmin(this::pass));
Expand Down Expand Up @@ -212,6 +213,16 @@ private String registerPeripheral(Request request, Response response, User satAd
}
}

private String listPaginatedPeripherals(Request request, Response response, User satAdmin) {
PageControlHelper pageHelper = new PageControlHelper(request);
PageControl pc = pageHelper.getPageControl();
long totalSize = hubManager.countRegisteredPeripherals(satAdmin);
List<PeripheralResponse> peripherals = hubManager.listRegisteredPeripherals(satAdmin, pc).stream()
.map(PeripheralResponse::fromIssEntity).toList();
TypeToken<PagedDataResultJson<PeripheralResponse, Long>> type = new TypeToken<>() { };
return json(GSON, response, new PagedDataResultJson<>(peripherals, totalSize, Collections.emptySet()), type);
}

private String listTokens(Request request, Response response, User user) {
PageControlHelper pageHelper = new PageControlHelper(request);
PageControl pc = pageHelper.getPageControl();
Expand Down
Loading

0 comments on commit d78059a

Please sign in to comment.