package com.navercorp.pinpoint.web.authorization.controller;

import com.navercorp.pinpoint.web.service.AgentInfoService;
import com.navercorp.pinpoint.web.view.tree.StaticTreeView;
import com.navercorp.pinpoint.web.view.tree.TreeView;
import com.navercorp.pinpoint.web.vo.agent.AgentAndStatus;
import com.navercorp.pinpoint.web.vo.agent.AgentInfoFilter;
import com.navercorp.pinpoint.web.vo.agent.AgentInfoFilterChain;
import com.navercorp.pinpoint.web.vo.agent.AgentStatusAndLink;
import com.navercorp.pinpoint.web.vo.agent.DefaultAgentInfoFilter;
import com.navercorp.pinpoint.web.vo.tree.InstancesList;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByApplication;
import com.navercorp.pinpoint.web.vo.tree.AgentsMapByHost;
import com.navercorp.pinpoint.web.vo.tree.SortByAgentInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author intr3p1d
 */
@RestController
@RequestMapping(value = "/agents")
public class AgentListController {
    private final AgentInfoService agentInfoService;

    public AgentListController(AgentInfoService agentInfoService) {
        this.agentInfoService = Objects.requireNonNull(agentInfoService, "agentInfoService");
    }

    @GetMapping(value = "/all")
    public TreeView<InstancesList<AgentAndStatus>> getAllAgentsList() {
        long timestamp = System.currentTimeMillis();
        return getAllAgentsList(timestamp);
    }

    @GetMapping(value = "/all", params = {"from", "to"})
    public TreeView<InstancesList<AgentAndStatus>> getAllAgentsList(
            @RequestParam("from") long from,
            @RequestParam("to") long to) {
        AgentInfoFilter filter = new DefaultAgentInfoFilter(from);
        long timestamp = to;
        AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(filter, timestamp);
        return treeView(allAgentsList);
    }

    @GetMapping(value = "/all", params = {"timestamp"})
    public TreeView<InstancesList<AgentAndStatus>> getAllAgentsList(
            @RequestParam("timestamp") long timestamp) {
        AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(AgentInfoFilter::accept, timestamp);
        return treeView(allAgentsList);
    }

    private static TreeView<InstancesList<AgentAndStatus>> treeView(AgentsMapByApplication agentsListsList) {
        List<InstancesList<AgentAndStatus>> list = agentsListsList.getAgentsListsList();
        return new StaticTreeView<>(list);
    }


    @GetMapping(value = "/v1", params = {"application", "sortBy"})
    public TreeView<InstancesList<AgentStatusAndLink>> getAgentsList(
            @RequestParam("application") String applicationName,
            @RequestParam("sortBy") SortByAgentInfo.Rules sortBy) {
        long timestamp = System.currentTimeMillis();
        return getAgentsList(applicationName, timestamp, sortBy);
    }

    @GetMapping(value = "/v1", params = {"application", "from", "to", "sortBy"})
    public TreeView<InstancesList<AgentStatusAndLink>> getAgentsList(
            @RequestParam("application") String applicationName,
            @RequestParam("from") long from,
            @RequestParam("to") long to,
            @RequestParam("sortBy") SortByAgentInfo.Rules sortBy) {
        AgentInfoFilter currentRunFilter = new AgentInfoFilterChain(
                new DefaultAgentInfoFilter(from)
        );
        long timestamp = to;
        AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(currentRunFilter, applicationName, timestamp, sortBy);
        return treeView(list);
    }

    @GetMapping(value = "/v1", params = {"application", "timestamp", "sortBy"})
    public TreeView<InstancesList<AgentStatusAndLink>> getAgentsList(
            @RequestParam("application") String applicationName,
            @RequestParam("timestamp") long timestamp,
            @RequestParam("sortBy") SortByAgentInfo.Rules sortBy) {
        AgentInfoFilter runningAgentFilter = new AgentInfoFilterChain(
                AgentInfoFilter::filterRunning
        );
        AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(runningAgentFilter, applicationName, timestamp, sortBy);
        return treeView(list);
    }

    private static TreeView<InstancesList<AgentStatusAndLink>> treeView(AgentsMapByHost agentsMapByHost) {
        List<InstancesList<AgentStatusAndLink>> list = agentsMapByHost.getAgentsListsList();
        return new StaticTreeView<>(list);
    }

}

@@ -1434,30 +1434,16 @@
 Mapping(
-value = %22/all%22
 )%0A    pu
@@ -1573,41 +1573,160 @@
     
-return getAllAgentsList(timestamp
+AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(AgentInfoFilter::accept, timestamp);%0A        return treeView(allAgentsList
 );%0A 
@@ -1747,32 +1747,16 @@
 Mapping(
-value = %22/all%22, 
 params =
@@ -2182,360 +2182,8 @@
  %7D%0A%0A
-    @GetMapping(value = %22/all%22, params = %7B%22timestamp%22%7D)%0A    public TreeView%3CInstancesList%3CAgentAndStatus%3E%3E getAllAgentsList(%0A            @RequestParam(%22timestamp%22) long timestamp) %7B%0A        AgentsMapByApplication allAgentsList = this.agentInfoService.getAllAgentsList(AgentInfoFilter::accept, timestamp);%0A        return treeView(allAgentsList);%0A    %7D%0A%0A
     
@@ -2440,39 +2440,24 @@
 @GetMapping(
-value = %22/v1%22, 
 params = %7B%22a
@@ -2748,29 +2748,233 @@
     
-return getAgentsList(
+AgentInfoFilter runningAgentFilter = new AgentInfoFilterChain(%0A                AgentInfoFilter::filterRunning%0A        );%0A        AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(runningAgentFilter, 
 appl
@@ -3002,24 +3002,55 @@
 p, sortBy);%0A
+        return treeView(list);%0A
     %7D%0A%0A    @
@@ -3056,39 +3056,24 @@
 @GetMapping(
-value = %22/v1%22, 
 params = %7B%22a
@@ -3738,654 +3738,8 @@
  %7D%0A%0A
-    @GetMapping(value = %22/v1%22, params = %7B%22application%22, %22timestamp%22, %22sortBy%22%7D)%0A    public TreeView%3CInstancesList%3CAgentStatusAndLink%3E%3E getAgentsList(%0A            @RequestParam(%22application%22) String applicationName,%0A            @RequestParam(%22timestamp%22) long timestamp,%0A            @RequestParam(%22sortBy%22) SortByAgentInfo.Rules sortBy) %7B%0A        AgentInfoFilter runningAgentFilter = new AgentInfoFilterChain(%0A                AgentInfoFilter::filterRunning%0A        );%0A        AgentsMapByHost list = this.agentInfoService.getAgentsListByApplicationName(runningAgentFilter, applicationName, timestamp, sortBy);%0A        return treeView(list);%0A    %7D%0A%0A
     

Change api url