Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/908#discussion_r154933984
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/enricher/stock/aggregator/AggregationJob.java
 ---
    @@ -0,0 +1,158 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock.aggregator;
    +
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.commons.lang3.StringUtils;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey;
    +
    +public final class AggregationJob implements Runnable {
    +
    +    public static BasicAttributeSensorAndConfigKey<Map<String, String>> 
DASHBOARD_COST_PER_MONTH = new BasicAttributeSensorAndConfigKey(Map.class, 
"dashboard.costPerMonth", "A map of VM ID to monthly cost. Please note if the 
same VM ID is reported more than once, only one of the sensors will be 
propagated");
    +    public static BasicAttributeSensorAndConfigKey<List<Map<String, 
String>>> DASHBOARD_HA_PRIMARIES = new 
BasicAttributeSensorAndConfigKey(List.class, "dashboard.ha.primaries", "A list 
of high availability primaries "); //TODO: find out what this is and add a 
better description
    +    public static BasicAttributeSensorAndConfigKey<List<Map<String, 
String>>> DASHBOARD_LICENSES = new BasicAttributeSensorAndConfigKey(List.class, 
"dashboard.licenses", "The licences in use for the running entities. This is a 
list of maps. The map should contain two keys: product and key");
    +    public static BasicAttributeSensorAndConfigKey<Map<String, 
List<Map<String, Object>>>> DASHBOARD_LOCATIONS = new 
BasicAttributeSensorAndConfigKey(Map.class, "dashboard.locations", "Locations 
in which the VMs are running. A map where the key is category of location (e.g. 
server) and the value is a list of maps containing name/icon/count");
    +    public static BasicAttributeSensorAndConfigKey<List<Map<String, 
String>>> DASHBOARD_POLICY_HIGHLIGHTS = new 
BasicAttributeSensorAndConfigKey(List.class, "dashboard.policyHighlights", 
"Highlights from policies. List of Masps, where each map should contain text 
and category");
    +
    +    private final Entity entity;
    +
    +    private static final String NAME_STRING = "name";
    +    private static final String COUNT_STRING = "count";
    +
    +    public AggregationJob(Entity entity) {
    +        this.entity = entity;
    +    }
    +
    +    private void updateFromConfig(Entity entity, 
BasicAttributeSensorAndConfigKey<List<Map<String, String>>> sensorAndConfigKey, 
List<Map<String, String>> list) {
    +        List<Map<String, String>> listFromEntity = 
entity.sensors().get(sensorAndConfigKey);
    +        if (listFromEntity == null || listFromEntity.isEmpty()) {
    +            listFromEntity = entity.config().get(sensorAndConfigKey);
    +        }
    +
    +        if (listFromEntity != null) {
    +            list.addAll(listFromEntity);
    +        }
    +    }
    +
    +    @Override
    +    public void run() {
    +        HashMap<String, String> costPerMonth = new HashMap<>();
    +        List<Map<String, String>> haPrimaries = new ArrayList<>();
    +        List<Map<String, String>> dashboardLicences = new ArrayList<>();
    +        Map<String, List<Map<String, Object>>> dashboardLocations = new 
HashMap<>();
    +        List<Map<String, String>> dashboardPolicyHighlights = new 
ArrayList<>();
    +
    +        entity.getChildren().forEach(childEntity -> 
scanEntity(childEntity, costPerMonth, haPrimaries, dashboardLicences, 
dashboardLocations, dashboardPolicyHighlights));
    +        entity.sensors().set(DASHBOARD_COST_PER_MONTH, costPerMonth);
    +        entity.sensors().set(DASHBOARD_HA_PRIMARIES, haPrimaries);
    +        entity.sensors().set(DASHBOARD_LICENSES, dashboardLicences);
    +        entity.sensors().set(DASHBOARD_LOCATIONS, dashboardLocations);
    +        entity.sensors().set(DASHBOARD_POLICY_HIGHLIGHTS, 
dashboardPolicyHighlights);
    +    }
    +
    +    protected void scanEntity(Entity entity,
    +                              Map<String, String> costPerMonth,
    +                              List<Map<String, String>> haPrimaries,
    +                              List<Map<String, String>> licences,
    +                              Map<String, List<Map<String, Object>>> 
locations,
    +                              List<Map<String, String>> policyHighlights) {
    +
    +        updateFromConfig(entity, DASHBOARD_HA_PRIMARIES, haPrimaries);
    +        updateFromConfig(entity, DASHBOARD_LICENSES, licences);
    +        updateFromConfig(entity, DASHBOARD_POLICY_HIGHLIGHTS, 
policyHighlights);
    +
    +        //Cost per month
    +        Map<String, String> costPerMonthFromEntity = 
entity.sensors().get(DASHBOARD_COST_PER_MONTH);
    +        if (costPerMonthFromEntity == null || 
costPerMonthFromEntity.isEmpty()) {
    +            costPerMonthFromEntity = 
entity.config().get(DASHBOARD_COST_PER_MONTH);
    +        }
    +
    +        if (costPerMonthFromEntity != null) {
    +            costPerMonth.putAll(costPerMonthFromEntity);
    +        }
    +
    +        //Locations merge
    +        //We are merging a Map, of Lists of Maps
    +        //The outer map is location type, e.g. servers to a list of data 
for that type
    +        //The inner map should contain the data e.g. name/icon/count.
    +
    +        //Further complicating this is that the Map you get back from 
sensors/config doesn't conform to the generic expectations.
    +        //I.E. even if you type this as Map<String, List<Map<String, 
String>>> you will still get back an integer from
    +        //the inner map- hence all the weird casting bellow.
    +        Map<String, List<Map<String, Object>>> outerLocationMapFromEntity 
= entity.sensors().get(DASHBOARD_LOCATIONS);
    --- End diff --
    
    I assumed if we were going to look at locations, we'd do 
`entity.getLocations()` or some such. Is it expected that some enricher is 
added at each leaf node will set the `DASHBOARD_LOCATIONS` sensor on that 
entity?


---

Reply via email to