Skip to content

Commit

Permalink
refactor(web): extract logic from MetricSetPairListController into Me…
Browse files Browse the repository at this point in the history
…tricSetPairListService to make this code reusable (#741)

* refactor(web): extract logic from MetricSetPairListController into MetricSetPairListService to make this code reusable

* Update kayenta-core/src/main/java/com/netflix/kayenta/service/MetricSetPairListService.java

Co-authored-by: Matt Duftler <duftler@google.com>
  • Loading branch information
Anastasiia Smirnova and Matt Duftler committed Jun 8, 2020
1 parent a4df7d4 commit e265094
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 68 deletions.
Expand Up @@ -30,6 +30,7 @@
import com.netflix.kayenta.metrics.MetricsServiceRepository;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.security.MapBackedAccountCredentialsRepository;
import com.netflix.kayenta.service.MetricSetPairListService;
import com.netflix.kayenta.storage.MapBackedStorageServiceRepository;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
Expand Down Expand Up @@ -86,6 +87,14 @@ StorageServiceRepository storageServiceRepository(
return new MapBackedStorageServiceRepository(storageServices.orElse(Collections.emptyList()));
}

@Bean
@ConditionalOnMissingBean
MetricSetPairListService metricSetPairListService(
AccountCredentialsRepository accountCredentialsRepository,
StorageServiceRepository storageServiceRepository) {
return new MetricSetPairListService(accountCredentialsRepository, storageServiceRepository);
}

//
// Configure subtypes of CanaryMetricSetQueryConfig, all of which must be
// defined in the package com.netflix.kayenta.canary.providers
Expand Down
@@ -0,0 +1,91 @@
/*
* Copyright 2020 Playtika, Inc.
*
* 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.netflix.kayenta.service;

import com.netflix.kayenta.metrics.MetricSetPair;
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.storage.ObjectType;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class MetricSetPairListService {

private final AccountCredentialsRepository accountCredentialsRepository;
private final StorageServiceRepository storageServiceRepository;

public List<MetricSetPair> loadMetricSetPairList(String accountName, String metricSetPairListId) {
String resolvedAccountName = getAccount(accountName);
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

return storageService.loadObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
}

public Optional<MetricSetPair> loadMetricSetPair(
String accountName, String metricSetPairListId, String metricSetPairId) {
String resolvedAccountName = getAccount(accountName);
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

List<MetricSetPair> metricSetPairList =
storageService.loadObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
return metricSetPairList.stream()
.filter(metricSetPair -> metricSetPair.getId().equals(metricSetPairId))
.findFirst();
}

public String storeMetricSetPairList(String accountName, List<MetricSetPair> metricSetPairList) {
String resolvedAccountName = getAccount(accountName);
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);
String metricSetPairListId = UUID.randomUUID() + "";

storageService.storeObject(
resolvedAccountName,
ObjectType.METRIC_SET_PAIR_LIST,
metricSetPairListId,
metricSetPairList);
return metricSetPairListId;
}

public void deleteMetricSetPairList(String accountName, String metricSetPairListId) {
String resolvedAccountName = getAccount(accountName);
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

storageService.deleteObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
}

public List<Map<String, Object>> listAllMetricSetPairLists(String accountName) {
String resolvedAccountName = getAccount(accountName);
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

return storageService.listObjectKeys(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST);
}

private String getAccount(String accountName) {
return accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
}
}
Expand Up @@ -17,50 +17,42 @@
package com.netflix.kayenta.controllers;

import com.netflix.kayenta.metrics.MetricSetPair;
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.storage.ObjectType;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
import com.netflix.kayenta.service.MetricSetPairListService;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/metricSetPairList")
@Slf4j
public class MetricSetPairListController {

private final AccountCredentialsRepository accountCredentialsRepository;
private final StorageServiceRepository storageServiceRepository;
private final MetricSetPairListService metricSetPairListService;

@Autowired
public MetricSetPairListController(
AccountCredentialsRepository accountCredentialsRepository,
StorageServiceRepository storageServiceRepository) {
this.accountCredentialsRepository = accountCredentialsRepository;
this.storageServiceRepository = storageServiceRepository;
public MetricSetPairListController(MetricSetPairListService metricSetPairListService) {
this.metricSetPairListService = metricSetPairListService;
}

@ApiOperation(value = "Retrieve a metric set pair list from object storage")
@RequestMapping(value = "/{metricSetPairListId:.+}", method = RequestMethod.GET)
public List<MetricSetPair> loadMetricSetPairList(
@RequestParam(required = false) final String accountName,
@PathVariable final String metricSetPairListId) {
String resolvedAccountName =
accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

return storageService.loadObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
return metricSetPairListService.loadMetricSetPairList(accountName, metricSetPairListId);
}

@ApiOperation(
Expand All @@ -72,20 +64,8 @@ public ResponseEntity<MetricSetPair> loadMetricSetPair(
@RequestParam(required = false) final String accountName,
@PathVariable final String metricSetPairListId,
@PathVariable final String metricSetPairId) {
String resolvedAccountName =
accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

List<MetricSetPair> metricSetPairList =
storageService.loadObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
Optional<MetricSetPair> foundPair =
metricSetPairList.stream()
.filter(metricSetPair -> metricSetPair.getId().equals(metricSetPairId))
.findFirst();
return foundPair
return metricSetPairListService
.loadMetricSetPair(accountName, metricSetPairListId, metricSetPairId)
.map(metricSetPair -> new ResponseEntity<>(metricSetPair, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
Expand All @@ -96,50 +76,25 @@ public Map storeMetricSetPairList(
@RequestParam(required = false) final String accountName,
@RequestBody final List<MetricSetPair> metricSetPairList)
throws IOException {
String resolvedAccountName =
accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);
String metricSetPairListId = UUID.randomUUID() + "";

storageService.storeObject(
resolvedAccountName,
ObjectType.METRIC_SET_PAIR_LIST,
metricSetPairListId,
metricSetPairList);
String metricSetPairListId =
metricSetPairListService.storeMetricSetPairList(accountName, metricSetPairList);

return Collections.singletonMap("metricSetPairListId", metricSetPairListId);
}

@ApiOperation(value = "Delete a metric set pair list")
@ResponseStatus(HttpStatus.NO_CONTENT)
@RequestMapping(value = "/{metricSetPairListId:.+}", method = RequestMethod.DELETE)
public void deleteMetricSetPairList(
@RequestParam(required = false) final String accountName,
@PathVariable final String metricSetPairListId,
HttpServletResponse response) {
String resolvedAccountName =
accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

storageService.deleteObject(
resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);

response.setStatus(HttpStatus.NO_CONTENT.value());
@PathVariable final String metricSetPairListId) {
metricSetPairListService.deleteMetricSetPairList(accountName, metricSetPairListId);
}

@ApiOperation(value = "Retrieve a list of metric set pair list ids and timestamps")
@RequestMapping(method = RequestMethod.GET)
public List<Map<String, Object>> listAllMetricSetPairLists(
@RequestParam(required = false) final String accountName) {
String resolvedAccountName =
accountCredentialsRepository
.getRequiredOneBy(accountName, AccountCredentials.Type.OBJECT_STORE)
.getName();
StorageService storageService = storageServiceRepository.getRequiredOne(resolvedAccountName);

return storageService.listObjectKeys(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST);
return metricSetPairListService.listAllMetricSetPairLists(accountName);
}
}
Expand Up @@ -11,6 +11,7 @@
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.security.MapBackedAccountCredentialsRepository;
import com.netflix.kayenta.service.MetricSetPairListService;
import com.netflix.kayenta.storage.MapBackedStorageServiceRepository;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
Expand Down Expand Up @@ -41,6 +42,7 @@ public abstract class BaseControllerTest {
protected static final String CONFIGS_ACCOUNT = "configs-account";

@Autowired StorageService storageService;
@MockBean MetricSetPairListService metricSetPairListService;
@MockBean ExecutionRepository executionRepository;
@MockBean ExecutionMapper executionMapper;

Expand Down

0 comments on commit e265094

Please sign in to comment.