All Downloads are FREE. Search and download functionalities are using the official Maven repository.

step.grid.GridReportBuilder Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (C) 2020, exense GmbH
 *  
 * This file is part of STEP
 *  
 * STEP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * STEP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with STEP.  If not, see .
 ******************************************************************************/
package step.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import step.grid.reports.TokenGroupCapacity;
import step.grid.tokenpool.Interest;

/**
 * @deprecated
 * This class is deprecated and will be removed in the future. Please use GridReportBuilder from
 * the client package instead
 */
@Deprecated
public class GridReportBuilder {

	private Grid grid;
	
	public GridReportBuilder(Grid adapterGrid) {
		super();
		this.grid = adapterGrid;
	}

	public List getUsageByIdentity(List groupbys) {		
		Map, TokenGroupCapacity> countsByIdentity = new HashMap<>();
		
		for(TokenWrapper aToken:grid.getTokens()) {
			Map key = new HashMap<>(); 

			for(String groupby:groupbys) {
				key.put(groupby, getValue(groupby, aToken));
			}
			
			if(!countsByIdentity.containsKey(key)) {
				countsByIdentity.put(key, new TokenGroupCapacity(key));
			}
			TokenGroupCapacity c = countsByIdentity.get(key);
			c.incrementCapacity();
			c.incrementUsage(aToken.getState());
		}
		
		return new ArrayList<>(countsByIdentity.values());
	}
	
	public Set getTokenAttributeKeys() {
		Set result = new HashSet<>();
		for(TokenWrapper token:grid.getTokens()) {
			result.addAll(token.getAttributes().keySet());
			if(token.getInterests()!=null) {
				result.addAll(token.getInterests().keySet());				
			}
		}
		return result;
	}
	
	private static final String UID_KEY = "id";
	
	private static final String URL_KEY = "url";
	
	private String getValue(String key, TokenWrapper aToken) {
		if(key.equals(UID_KEY)) {
			return aToken.getID();
		}
		if(key.equals(URL_KEY)) {
			AgentRef ref = aToken.getAgent();
			return ref!=null?ref.getAgentUrl():"-";
		}
		if(aToken.getAttributes()!=null) {
			String attribute = aToken.getAttributes().get(key);
			if(attribute!=null) {
				return attribute;						
			}
		}
		if(aToken.getInterests()!=null) {
			Interest interest = aToken.getInterests().get(key);
			if(interest!=null) {
				return interest.getSelectionPattern().toString();	
			}
		}
		return null;
	}
	
	public List getTokenAssociations(boolean onlyWithOwner) {
		List tokens = new ArrayList<>();
		for(TokenWrapper token:grid.getTokens()) {
			TokenWrapperOwner currentOwner = token.getCurrentOwner();
			if(currentOwner!=null||(currentOwner==null&&!onlyWithOwner)) {
				tokens.add(token);
			}
		}
		return tokens;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy