/*
 * Licensed 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 io.prestosql.server;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;
import io.prestosql.spi.resourcegroups.ResourceGroupId;
import io.prestosql.spi.resourcegroups.ResourceGroupState;
import io.prestosql.spi.resourcegroups.SchedulingPolicy;

import javax.annotation.Nullable;

import java.util.List;

import static java.util.Objects.requireNonNull;

/*
 * This class is exposed to external systems via ResourceGroupStateInfoResource and QueryStateInfoResource.
 * Be careful while changing it.
 */
public class ResourceGroupInfo
{
    private final ResourceGroupId id;
    private final ResourceGroupState state;

    private final SchedulingPolicy schedulingPolicy;
    private final int schedulingWeight;

    private final DataSize softMemoryLimit;
    private final int softConcurrencyLimit;
    private final int hardConcurrencyLimit;
    private final int maxQueuedQueries;

    private final DataSize memoryUsage;
    private final Duration cpuUsage;
    private final int numQueuedQueries;
    private final int numRunningQueries;
    private final int numEligibleSubGroups;

    // Summaries do not include the following fields
    private final List<ResourceGroupInfo> subGroups;
    private final List<QueryStateInfo> runningQueries;

    public ResourceGroupInfo(
            ResourceGroupId id,
            ResourceGroupState state,

            SchedulingPolicy schedulingPolicy,
            int schedulingWeight,

            DataSize softMemoryLimit,
            int softConcurrencyLimit,
            int hardConcurrencyLimit,
            int maxQueuedQueries,

            DataSize memoryUsage,
            Duration cpuUsage,
            int numQueuedQueries,
            int numRunningQueries,
            int numEligibleSubGroups,

            List<ResourceGroupInfo> subGroups,

            List<QueryStateInfo> runningQueries)
    {
        this.id = requireNonNull(id, "id is null");
        this.state = requireNonNull(state, "state is null");

        this.schedulingPolicy = requireNonNull(schedulingPolicy, "schedulingPolicy is null");
        this.schedulingWeight = schedulingWeight;

        this.softMemoryLimit = requireNonNull(softMemoryLimit, "softMemoryLimit is null");

        this.softConcurrencyLimit = softConcurrencyLimit;
        this.hardConcurrencyLimit = hardConcurrencyLimit;
        this.maxQueuedQueries = maxQueuedQueries;

        this.memoryUsage = requireNonNull(memoryUsage, "memoryUsage is null");
        this.cpuUsage = requireNonNull(cpuUsage, "cpuUsage is null");
        this.numQueuedQueries = numQueuedQueries;
        this.numRunningQueries = numRunningQueries;
        this.numEligibleSubGroups = numEligibleSubGroups;

        this.runningQueries = runningQueries;

        this.subGroups = subGroups;
    }

    @JsonProperty
    public ResourceGroupId getId()
    {
        return id;
    }

    @JsonProperty
    public ResourceGroupState getState()
    {
        return state;
    }

    @JsonProperty
    public SchedulingPolicy getSchedulingPolicy()
    {
        return schedulingPolicy;
    }

    @JsonProperty
    public int getSchedulingWeight()
    {
        return schedulingWeight;
    }

    @JsonProperty
    public DataSize getSoftMemoryLimit()
    {
        return softMemoryLimit;
    }

    @JsonProperty
    public DataSize getMemoryUsage()
    {
        return memoryUsage;
    }

    @JsonProperty
    public Duration getCpuUsage()
    {
        return cpuUsage;
    }

    @JsonProperty
    public int getSoftConcurrencyLimit()
    {
        return softConcurrencyLimit;
    }

    @JsonProperty
    public int getHardConcurrencyLimit()
    {
        return hardConcurrencyLimit;
    }

    @JsonProperty
    public int getMaxQueuedQueries()
    {
        return maxQueuedQueries;
    }

    @JsonProperty
    public int getNumQueuedQueries()
    {
        return numQueuedQueries;
    }

    @JsonProperty
    public int getNumRunningQueries()
    {
        return numRunningQueries;
    }

    @JsonProperty
    public int getNumEligibleSubGroups()
    {
        return numEligibleSubGroups;
    }

    @JsonProperty
    @Nullable
    public List<QueryStateInfo> getRunningQueries()
    {
        return runningQueries;
    }

    @JsonProperty
    @Nullable
    public List<ResourceGroupInfo> getSubGroups()
    {
        return subGroups;
    }
}

@@ -2397,24 +2397,34 @@
             
+@Nullable 
 List%3CResourc
@@ -2438,33 +2438,33 @@
 nfo%3E subGroups,%0A
-%0A
+ 
             List
@@ -2458,16 +2458,25 @@
         
+@Nullable
  List%3CQu
@@ -3335,24 +3335,60 @@
 SubGroups;%0A%0A
+        this.subGroups = subGroups;%0A
         this
@@ -3424,45 +3424,8 @@
 ies;
-%0A%0A        this.subGroups = subGroups;
 %0A   
@@ -3930,194 +3930,8 @@
  %7D%0A%0A
-    @JsonProperty%0A    public DataSize getMemoryUsage()%0A    %7B%0A        return memoryUsage;%0A    %7D%0A%0A    @JsonProperty%0A    public Duration getCpuUsage()%0A    %7B%0A        return cpuUsage;%0A    %7D%0A%0A
     
@@ -4237,32 +4237,218 @@
 Queries;%0A    %7D%0A%0A
+    @JsonProperty%0A    public DataSize getMemoryUsage()%0A    %7B%0A        return memoryUsage;%0A    %7D%0A%0A    @JsonProperty%0A    public Duration getCpuUsage()%0A    %7B%0A        return cpuUsage;%0A    %7D%0A%0A
     @JsonPropert
@@ -4796,40 +4796,38 @@
 ist%3C
-QueryStateInfo%3E getRunningQuerie
+ResourceGroupInfo%3E getSubGroup
 s()%0A
@@ -4847,29 +4847,24 @@
  return 
-runningQuerie
+subGroup
 s;%0A    %7D
@@ -4917,38 +4917,40 @@
 ist%3C
-ResourceGroupInfo%3E getSubGroup
+QueryStateInfo%3E getRunningQuerie
 s()%0A
@@ -4966,31 +4966,36 @@
      return 
-subGroup
+runningQuerie
 s;%0A    %7D%0A%7D%0A

Reorder getters in ResourceGroupInfo