Commit f944405c authored by Administrator's avatar Administrator

Merge branch 'staging' into 'master'

Staging

See merge request !162
parents 1c412b35 94b094bd
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.service.GlobalFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xuxin
* @date 2020/8/3 18:28
* @description
*/
@RestController
@RequestMapping("/globalfield")
public class GlobalFieldController {
@Autowired
private GlobalFieldService globalFieldService;
@GetMapping("/speciallinestate")
public ResultDto querySpecialLineState() {
ResultDto resultDto = new ResultDto();
resultDto.setStatus(0);
resultDto.setData(globalFieldService.querySpecialLineState());
return resultDto;
}
}
...@@ -232,6 +232,33 @@ public class ShopController { ...@@ -232,6 +232,33 @@ public class ShopController {
} }
/**
* 获取cookie
*/
@GetMapping("/cookie/{shopId}")
public ResultDto getShopCookie(@PathVariable String shopId) {
String cookie = shopService.queryShopCookieById(shopId);
ResultDto resultDto = new ResultDto();
resultDto.setStatus(0);
resultDto.setData(cookie);
return resultDto;
}
/**
* 删除cookie
*/
@DeleteMapping("/cookie/{shopId}")
public ResultDto delShopCookie(@PathVariable String shopId) {
Integer result = shopService.deleteShopCookieById(shopId);
ResultDto resultDto = new ResultDto();
resultDto.setStatus(result);
return resultDto;
}
private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) { private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) {
resultDto.setStatus(-1); resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>(); Map<String, Object> statusInfo = new HashMap<>();
......
package com.edgec.browserbackend.browser.domain;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author xuxin
* @date 2020/8/3 14:37
* @description 用来存放一些全局变量
*/
@Data
@Document(collection = "globalfield")
public class GlobalField {
@Id
private String id;
/**
* 专线状态
* on:正常
* off: 挂掉了
*/
private String specialLineState;
/**
* 注册礼金
* 目前是注册就送 16 元
*/
private Integer registerGift;
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.GlobalField;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author xuxin
* @date 2020/8/3 16:54
* @description
*/
public interface GlobalFieldRepository extends MongoRepository<GlobalField, String> {
}
package com.edgec.browserbackend.browser.repository;
/**
* @author xuxin
* @date 2020/8/3 17:00
* @description
*/
public interface GlobalFieldRepositoryCustom {
}
package com.edgec.browserbackend.browser.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* @author xuxin
* @date 2020/8/3 17:02
* @description
*/
public class GlobalFieldRepositoryCustomImpl implements GlobalFieldRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;
}
...@@ -34,4 +34,6 @@ public interface IpResourceRepositoryCustom { ...@@ -34,4 +34,6 @@ public interface IpResourceRepositoryCustom {
List<IpResource> findShopIdInListAndStatus(List<String> shopIds, boolean isDeleted, int status); List<IpResource> findShopIdInListAndStatus(List<String> shopIds, boolean isDeleted, int status);
List<IpResource> findShopIdInListAndRegionLike(List<String> shopIds, boolean isDeleted, String region); List<IpResource> findShopIdInListAndRegionLike(List<String> shopIds, boolean isDeleted, String region);
List<IpResource> specialLineCheckTask();
} }
...@@ -15,6 +15,8 @@ import org.springframework.data.mongodb.core.query.Update; ...@@ -15,6 +15,8 @@ import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sample;
import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Criteria.where;
public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCustom { public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCustom {
...@@ -73,7 +75,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto ...@@ -73,7 +75,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
MatchOperation match = Aggregation.match(matchCriteria); MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(100); SampleOperation sample = sample(100);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class); AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults(); List<IpResource> mappedResults = results.getMappedResults();
...@@ -90,7 +92,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto ...@@ -90,7 +92,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
MatchOperation match = Aggregation.match(matchCriteria); MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(20); SampleOperation sample = sample(20);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class); AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults(); List<IpResource> mappedResults = results.getMappedResults();
...@@ -214,5 +216,22 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto ...@@ -214,5 +216,22 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
return result; return result;
} }
@Override
public List<IpResource> specialLineCheckTask() {
AggregationOperation match = Aggregation.match(
Criteria.where("status").in(Arrays.asList(0L, 2L))
.and("specialLine").is(true)
.and("bind").is(true)
.and("isDeleted").is(false)
.and("validTime").gt(Instant.now().toEpochMilli())
);
AggregationOperation sample = sample(20);
Aggregation aggregation = newAggregation(match, sample);
AggregationResults<IpResource> results = mongoTemplate.aggregate(aggregation, IpResource.class, IpResource.class);
List<IpResource> list = results.getMappedResults();
return list;
}
} }
package com.edgec.browserbackend.browser.service;
/**
* @author xuxin
* @date 2020/8/3 18:03
* @description
*/
public interface GlobalFieldService {
String querySpecialLineState();
}
package com.edgec.browserbackend.browser.service.Impl;
import com.edgec.browserbackend.browser.repository.GlobalFieldRepository;
import com.edgec.browserbackend.browser.service.GlobalFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author xuxin
* @date 2020/8/3 18:04
* @description
*/
@Service
public class GlobalFieldServiceImpl implements GlobalFieldService {
@Autowired
private GlobalFieldRepository globalFieldRepository;
@Override
public String querySpecialLineState() {
String state = "on";
if (!globalFieldRepository.findAll().isEmpty()) {
state = globalFieldRepository.findAll().get(0).getSpecialLineState();
}
return state;
}
}
...@@ -649,6 +649,25 @@ public class ShopServiceImpl implements ShopService { ...@@ -649,6 +649,25 @@ public class ShopServiceImpl implements ShopService {
return result; return result;
} }
@Override
public String queryShopCookieById(String id) {
Shop shop = shopRepository.findById(id).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
return shop.getShopCookie();
}
@Override
public Integer deleteShopCookieById(String id) {
Shop shop = shopRepository.findById(id).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
shop.setShopCookie("");
Shop save = shopRepository.save(shop);
Integer i = 1;
if (StringUtils.isEmpty(save.getShopCookie())) {
i = 0;
}
return i;
}
private String getShopId(String username, ShopResultDto shopResultDto) { private String getShopId(String username, ShopResultDto shopResultDto) {
Shop shop = new Shop(); Shop shop = new Shop();
shopResultDto.setOwner(username); shopResultDto.setOwner(username);
......
...@@ -34,4 +34,8 @@ public interface ShopService { ...@@ -34,4 +34,8 @@ public interface ShopService {
List<String> getBatchShopUsers(String username, List<String> shopIds); List<String> getBatchShopUsers(String username, List<String> shopIds);
Integer dealDirtyData(); Integer dealDirtyData();
String queryShopCookieById(String id);
Integer deleteShopCookieById(String id);
} }
package com.edgec.browserbackend.browser.task;
import com.edgec.browserbackend.account.domain.QueryIpUrlList;
import com.edgec.browserbackend.account.repository.QueryIpUrlListRepository;
import com.edgec.browserbackend.browser.domain.GlobalField;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.repository.GlobalFieldRepository;
import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.common.utils.Trans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/3 14:29
* @description
*/
@Component
public class SpecialLineCheckTask {
private static final Logger log = LoggerFactory.getLogger(SpecialLineCheckTask.class);
@Autowired
private IpResourceRepository ipResourceRepository;
@Autowired
private QueryIpUrlListRepository queryIpUrlListRepository;
@Autowired
private GlobalFieldRepository globalFieldRepository;
@Scheduled(cron = "0 0/5 * * * ?")
public void healthCheck() {
// 1. 随机获取符合条件的 20 个 ip
List<IpResource> ipResourceList = ipResourceRepository.specialLineCheckTask();
// 2. 对获取的ip进行检查
int[] arr = getCheckArray(ipResourceList);
int finalSuccess = arr[0];
int finalFail = arr[1];
GlobalField globalField = getGlobalField();
// 3. 获取 specialLine 的状态,并判断是否需要更新数据库状态
String specialLineState = null;
if ("on".equals(globalField.getSpecialLineState())) {
specialLineState = finalFail == ipResourceList.size() ? "off" : null;
}
if ("off".equals(globalField.getSpecialLineState())) {
specialLineState = finalSuccess > 1 ? "on" : null;
}
if (!StringUtils.isEmpty(specialLineState)) {
globalField.setSpecialLineState(specialLineState);
globalFieldRepository.save(globalField);
}
}
private int[] getCheckArray(List<IpResource> ipResourceList) {
List<QueryIpUrlList> queryIpUrlLists = queryIpUrlListRepository.findAll();
int finalSuccess = 0;
int finalFail = 0;
for (IpResource ipResource : ipResourceList) {
int success = 0;
for (QueryIpUrlList queryIpUrlList : queryIpUrlLists) {
Trans trans = new Trans(ipResource.getProxyUsername(), ipResource.getProxyPassword());
String result = trans.get(queryIpUrlList.getUrl());
if (result.contains(ipResource.getAddr())) {
success++;
break;
}
}
if (success == 0) {
finalFail++;
}
if (success == 1) {
finalSuccess++;
}
// 用于性能优化
if (finalSuccess == 2) {
break;
}
}
int[] arr = {finalSuccess, finalFail};
return arr;
}
private GlobalField getGlobalField() {
List<GlobalField> globalFieldList = globalFieldRepository.findAll();
GlobalField globalField = null;
if (globalFieldList.isEmpty()) {
GlobalField g = new GlobalField();
g.setSpecialLineState("on");
globalField = globalFieldRepository.save(g);
} else {
globalField = globalFieldList.get(0);
}
return globalField;
}
}
...@@ -73,4 +73,4 @@ spring: ...@@ -73,4 +73,4 @@ spring:
profiles: staging profiles: staging
data: data:
mongodb: mongodb:
uri: mongodb://user:${MONGODB_PASSWORD}@localhost:27017/browser uri: mongodb://user:${MONGODB_PASSWORD}@staging-mongodb:27017/browser
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment