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

com.jelastic.api.system.utils.GridUtils Maven / Gradle / Ivy

The newest version!
/*Server class MD5: 7d5b3540e07e11d990cd1087cc74e887*/
package com.jelastic.api.system.utils;

import com.jelastic.api.common.Constants;
import com.jelastic.api.system.persistence.pricing.TariffGrid;
import com.jelastic.api.system.persistence.pricing.TariffGridItem;
import com.jelastic.api.system.persistence.pricing.TariffOption;
import com.jelastic.api.system.persistence.pricing.TariffOptionValue;
import com.jelastic.api.utils.MD5;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.stream.Collectors;
import java.util.*;

/**
 * @name Jelastic API Client
 * @version 8.11.2
 * @copyright Jelastic, Inc.
 */
public class GridUtils {

    public static final String ITEM_NAME = "name";

    public static final String ITEM_OPTIONS = "options";

    public static final String VM_MEMORY_OPTION = "vm_memory";

    public static final String VM_CPU_OPTION = "vm_vcpu";

    public static final String VM_EXTERNAL_RESOURCE_NAME = "VM";

    public static String getOptionValueByName(TariffGridItem item, String name) {
        TariffOptionValue value = item.getOptions().stream().filter(o -> o.getName().equals(name)).findFirst().orElse(null);
        return value == null ? null : value.getValue();
    }

    public static Set parseOptions(String options) {
        Set items = new HashSet<>();
        try {
            JSONArray optionsJSON = new JSONArray(options);
            for (int i = 0; i < optionsJSON.length(); i++) {
                TariffGridItem item = new TariffGridItem();
                try {
                    JSONObject option = optionsJSON.getJSONObject(i);
                    if (option.has(ITEM_NAME)) {
                        item.setName(option.getString(ITEM_NAME));
                    }
                    JSONObject opts = null;
                    if (option.has(ITEM_OPTIONS)) {
                        opts = option.getJSONObject(ITEM_OPTIONS);
                    } else {
                        opts = option;
                    }
                    Set values = new HashSet<>();
                    Iterator keys = opts.keys();
                    while (keys.hasNext()) {
                        TariffOptionValue optionValue = new TariffOptionValue();
                        String key = keys.next().toString();
                        optionValue.setOption(new TariffOption(key));
                        optionValue.setValue(opts.getString(key));
                        values.add(optionValue);
                    }
                    item.setOptions(values);
                } catch (JSONException ex) {
                    item.setName(optionsJSON.getString(i));
                }
                items.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            throw new RuntimeException(String.format("Can't parse options: [%s], Error: [%s]", options, e.getMessage()));
        }
        return items;
    }

    public static > List> combinations(List items, int size) {
        if (size == 1) {
            List> result = new ArrayList<>();
            for (T item : items) {
                result.add(Collections.singletonList(item));
            }
            return result;
        }
        List> result = new ArrayList<>();
        for (int i = 0; i <= items.size() - size; i++) {
            T firstItem = items.get(i);
            List> additionalItems = combinations(items.subList(i + 1, items.size()), size - 1);
            for (List additional : additionalItems) {
                List combination = new ArrayList<>();
                combination.add(firstItem);
                combination.addAll(additional);
                result.add(combination);
            }
        }
        return result;
    }

    public static String findItemNameByOptions(Set grids, Set options) {
        List items = new ArrayList<>();
        grids.forEach(g -> items.addAll(g.getItems()));
        return findItemNameByOptions(items, options);
    }

    public static String findItemNameByOptions(List items, Set options) {
        Map itemsNames = new HashMap<>();
        items.forEach(i -> itemsNames.putIfAbsent(i.getMD5ID(), i.getName()));
        List ops = options.stream().map(k -> k.getOption().getName() + Constants.COLON_SEPARATOR + k.getValue()).collect(Collectors.toList());
        List combinations = new ArrayList<>();
        for (int i = 0; i < ops.size(); i++) {
            List> variants = GridUtils.combinations(ops, i + 1);
            for (List variant : variants) {
                String opString = variant.stream().sorted().collect(Collectors.joining(Constants.COMMA_SEPARATOR));
                String optsMd5 = new MD5().get(opString);
                combinations.add(optsMd5);
            }
        }
        return combinations.stream().map(itemsNames::get).filter(Objects::nonNull).findFirst().orElse(null);
    }

    public static int getNextOrder(Set items) {
        return new TreeSet<>(items).last().getOrder() + 1;
    }
}