SearchController.java 4.1 KB
Newer Older
L
lepdou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*
 * Copyright 2021 Apollo Authors
 *
 * 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 com.ctrip.framework.apollo.portal.controller;

import com.google.common.collect.Lists;

import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.AppService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;

import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author lepdou 2021-09-13
 */
@RestController("/app")
public class SearchController {

  private AppService       appService;
  private PortalSettings   portalSettings;
  private NamespaceService namespaceService;
  private PortalConfig     portalConfig;

  public SearchController(final AppService appService,
                          final PortalSettings portalSettings,
                          final PortalConfig portalConfig,
                          final NamespaceService namespaceService) {
    this.appService = appService;
    this.portalConfig = portalConfig;
    this.portalSettings = portalSettings;
    this.namespaceService = namespaceService;
  }

  @GetMapping("/apps/search/by-appid-or-name")
  public PageDTO<App> search(@RequestParam(value = "query", required = false) String query, Pageable pageable) {
    if (StringUtils.isEmpty(query)) {
      return appService.findAll(pageable);
    }

    //search app
    PageDTO<App> appPageDTO = appService.searchByAppIdOrAppName(query, pageable);
    if (appPageDTO.hasContent()) {
      return appPageDTO;
    }

    if (!portalConfig.supportSearchByItem()) {
      return new PageDTO<>(Lists.newLinkedList(), pageable, 0);
    }

    //search item
    return searchByItem(query, pageable);
  }

  private PageDTO<App> searchByItem(String itemKey, Pageable pageable) {
    List<App> result = Lists.newLinkedList();

    if (StringUtils.isEmpty(itemKey)) {
      return new PageDTO<>(result, pageable, 0);
    }

    //use the env witch has the most namespace as page index.
    final AtomicLong maxTotal = new AtomicLong(0);

    List<Env> activeEnvs = portalSettings.getActiveEnvs();

    activeEnvs.forEach(env -> {
      PageDTO<NamespaceDTO> namespacePage = namespaceService.findNamespacesByItem(env, itemKey, pageable);
      if (!namespacePage.hasContent()) {
        return;
      }

      long currentEnvNSTotal = namespacePage.getTotal();
      if (currentEnvNSTotal > maxTotal.get()) {
        maxTotal.set(namespacePage.getTotal());
      }

      List<NamespaceDTO> namespaceDTOS = namespacePage.getContent();

      namespaceDTOS.forEach(namespaceDTO -> {
        String cluster = namespaceDTO.getClusterName();
        String namespaceName = namespaceDTO.getNamespaceName();

        App app = new App();
        app.setAppId(namespaceDTO.getAppId());
        app.setName(env.getName() + " / " + cluster + " / " + namespaceName);
        app.setOrgId(env.getName() + "+" + cluster + "+" + namespaceName);
        app.setOrgName("SearchByItem" + "+" + itemKey);

        result.add(app);
      });
    });

    return new PageDTO<>(result, pageable, maxTotal.get());
  }

}