forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added to insert a number of agent into rdb periodically. pinpoint-apm…
- Loading branch information
1 parent
c6b4c5f
commit a29a227
Showing
17 changed files
with
562 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 46 additions & 41 deletions
87
web/src/main/java/com/navercorp/pinpoint/web/batch/BatchJobLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,46 @@ | ||
/* | ||
* Copyright 2014 NAVER Corp. | ||
* | ||
* 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.navercorp.pinpoint.web.batch; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
|
||
/** | ||
* @author minwoo.jung<minwoo.jung@navercorp.com> | ||
* | ||
*/ | ||
public class BatchJobLauncher extends JobLaunchSupport { | ||
|
||
public void alarmJob() { | ||
JobParameters params = createTimeParameter(); | ||
run("alarmJob", params); | ||
} | ||
|
||
private JobParameters createTimeParameter() { | ||
JobParametersBuilder builder = new JobParametersBuilder(); | ||
Date now = new Date(); | ||
builder.addDate("schedule.date", now); | ||
return builder.toJobParameters(); | ||
} | ||
} | ||
/* | ||
* Copyright 2014 NAVER Corp. | ||
* | ||
* 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.navercorp.pinpoint.web.batch; | ||
|
||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author minwoo.jung<minwoo.jung@navercorp.com> | ||
* | ||
*/ | ||
public class BatchJobLauncher extends JobLaunchSupport { | ||
|
||
public void alarmJob() { | ||
JobParameters params = createTimeParameter(); | ||
run("alarmJob", params); | ||
} | ||
|
||
private JobParameters createTimeParameter() { | ||
JobParametersBuilder builder = new JobParametersBuilder(); | ||
Date now = new Date(); | ||
builder.addDate("schedule.date", now); | ||
return builder.toJobParameters(); | ||
} | ||
|
||
public void agentCountJob() { | ||
run("agentCountJob", createTimeParameter()); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
web/src/main/java/com/navercorp/pinpoint/web/batch/job/AgentCountProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.navercorp.pinpoint.web.batch.job; | ||
|
||
import com.navercorp.pinpoint.common.util.DateUtils; | ||
import com.navercorp.pinpoint.web.vo.AgentCountStatistics; | ||
import com.navercorp.pinpoint.web.vo.AgentInfo; | ||
import com.navercorp.pinpoint.web.vo.ApplicationAgentList; | ||
import org.springframework.batch.item.ItemProcessor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public class AgentCountProcessor implements ItemProcessor<ApplicationAgentList, AgentCountStatistics> { | ||
|
||
@Override | ||
public AgentCountStatistics process(ApplicationAgentList item) throws Exception { | ||
if (item == null) { | ||
return null; | ||
} | ||
|
||
int agentCount = getAgentCount(item.getApplicationAgentList()); | ||
AgentCountStatistics agentCountStatistics = new AgentCountStatistics(agentCount, DateUtils.timestampToMidNight(System.currentTimeMillis())); | ||
return agentCountStatistics; | ||
} | ||
|
||
private int getAgentCount(Map<String, List<AgentInfo>> applicationAgentListMap) { | ||
int agentCount = 0; | ||
for (Map.Entry<String, List<AgentInfo>> eachApplicationAgentList : applicationAgentListMap.entrySet()) { | ||
agentCount += eachApplicationAgentList.getValue().size(); | ||
} | ||
|
||
return agentCount; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
web/src/main/java/com/navercorp/pinpoint/web/batch/job/AgentCountReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.navercorp.pinpoint.web.batch.job; | ||
|
||
import com.navercorp.pinpoint.web.service.AgentInfoService; | ||
import com.navercorp.pinpoint.web.vo.ApplicationAgentList; | ||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.StepExecutionListener; | ||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.NonTransientResourceException; | ||
import org.springframework.batch.item.ParseException; | ||
import org.springframework.batch.item.UnexpectedInputException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public class AgentCountReader implements ItemReader<ApplicationAgentList>, StepExecutionListener { | ||
|
||
@Autowired | ||
private AgentInfoService agentInfoService; | ||
|
||
private final Queue<ApplicationAgentList> queue = new LinkedList<>(); | ||
|
||
@Override | ||
public void beforeStep(StepExecution stepExecution) { | ||
ApplicationAgentList applicationAgentList = agentInfoService.getApplicationAgentList(ApplicationAgentList.Key.APPLICATION_NAME); | ||
queue.add(applicationAgentList); | ||
} | ||
|
||
@Override | ||
public ApplicationAgentList read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { | ||
return queue.poll(); | ||
} | ||
|
||
@Override | ||
public ExitStatus afterStep(StepExecution stepExecution) { | ||
return null; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
web/src/main/java/com/navercorp/pinpoint/web/batch/job/AgentCountWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.navercorp.pinpoint.web.batch.job; | ||
|
||
import com.navercorp.pinpoint.web.dao.AgentStatisticsDao; | ||
import com.navercorp.pinpoint.web.vo.AgentCountStatistics; | ||
import org.springframework.batch.core.JobExecutionException; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public class AgentCountWriter implements ItemWriter<AgentCountStatistics> { | ||
|
||
@Autowired | ||
AgentStatisticsDao agentStatisticsDao; | ||
|
||
@Override | ||
public void write(List<? extends AgentCountStatistics> items) throws Exception { | ||
if (items.size() == 1) { | ||
AgentCountStatistics agentCountStatistics = items.get(0); | ||
if (agentCountStatistics == null || agentCountStatistics.getAgentCount() < 0) { | ||
throw new JobExecutionException("Bad parameter"); | ||
} | ||
boolean success = agentStatisticsDao.insertAgentCount(agentCountStatistics); | ||
if (!success) { | ||
throw new JobExecutionException("insert AgentCount failed."); | ||
} | ||
} else { | ||
throw new JobExecutionException("Bad parameter"); | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
web/src/main/java/com/navercorp/pinpoint/web/controller/AgentStatisticsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.navercorp.pinpoint.web.controller; | ||
|
||
import com.navercorp.pinpoint.common.util.DateUtils; | ||
import com.navercorp.pinpoint.web.service.AgentStatisticsService; | ||
import com.navercorp.pinpoint.web.vo.AgentCountStatistics; | ||
import com.navercorp.pinpoint.web.vo.Range; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
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.ResponseBody; | ||
|
||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
@Controller | ||
public class AgentStatisticsController { | ||
|
||
@Autowired | ||
AgentStatisticsService agentStatisticsService; | ||
|
||
@RequestMapping(value = "/insertAgentCount", method = RequestMethod.GET, params = {"agentCount"}) | ||
@ResponseBody | ||
public Map<String, String> insertAgentCount(@RequestParam("agentCount") int agentCount) { | ||
return insertAgentCount(agentCount, new Date().getTime()); | ||
} | ||
|
||
@RequestMapping(value = "/insertAgentCount", method = RequestMethod.GET, params = {"agentCount", "timestamp"}) | ||
@ResponseBody | ||
public Map<String, String> insertAgentCount(@RequestParam("agentCount") int agentCount, @RequestParam("timestamp") long timestamp) { | ||
if (timestamp < 0) { | ||
Map<String, String> result = new HashMap<>(); | ||
result.put("result", "FAIL"); | ||
result.put("message", "negative timestamp."); | ||
return result; | ||
} | ||
|
||
AgentCountStatistics agentCountStatistics = new AgentCountStatistics(agentCount, DateUtils.timestampToMidNight(timestamp)); | ||
boolean success = agentStatisticsService.insertAgentCount(agentCountStatistics); | ||
|
||
if (success) { | ||
Map<String, String> result = new HashMap<>(); | ||
result.put("result", "SUCCESS"); | ||
return result; | ||
} else { | ||
Map<String, String> result = new HashMap<>(); | ||
result.put("result", "FAIL"); | ||
result.put("message", "insert DAO error."); | ||
return result; | ||
} | ||
} | ||
|
||
@RequestMapping(value = "/selectAgentCount", method = RequestMethod.GET) | ||
@ResponseBody | ||
public List<AgentCountStatistics> selectAgentCount() { | ||
return selectAgentCount(0L, System.currentTimeMillis()); | ||
} | ||
|
||
@RequestMapping(value = "/selectAgentCount", method = RequestMethod.GET, params = {"to"}) | ||
@ResponseBody | ||
public List<AgentCountStatistics> selectAgentCount(@RequestParam("to") long to) { | ||
return selectAgentCount(0L, to); | ||
} | ||
|
||
@RequestMapping(value = "/selectAgentCount", method = RequestMethod.GET, params = {"from", "to"}) | ||
@ResponseBody | ||
public List<AgentCountStatistics> selectAgentCount(@RequestParam("from") long from, @RequestParam("to") long to) { | ||
Range range = new Range(DateUtils.timestampToMidNight(from), DateUtils.timestampToMidNight(to), true); | ||
List<AgentCountStatistics> agentCountStatisticsList = agentStatisticsService.selectAgentCount(range); | ||
|
||
Collections.sort(agentCountStatisticsList, new Comparator<AgentCountStatistics>() { | ||
@Override | ||
public int compare(AgentCountStatistics o1, AgentCountStatistics o2) { | ||
o1.getTimestamp(); | ||
o2.getTimestamp(); | ||
|
||
if (o1.getTimestamp() > o2.getTimestamp()) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
}); | ||
|
||
return agentCountStatisticsList; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
web/src/main/java/com/navercorp/pinpoint/web/dao/AgentStatisticsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.navercorp.pinpoint.web.dao; | ||
|
||
import com.navercorp.pinpoint.web.vo.AgentCountStatistics; | ||
import com.navercorp.pinpoint.web.vo.Range; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public interface AgentStatisticsDao { | ||
|
||
boolean insertAgentCount(AgentCountStatistics agentCountStatistics); | ||
|
||
List<AgentCountStatistics> selectAgentCount(Range range); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
web/src/main/java/com/navercorp/pinpoint/web/dao/mysql/MysqlAgentStatisticsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.navercorp.pinpoint.web.dao.mysql; | ||
|
||
import com.navercorp.pinpoint.web.dao.AgentStatisticsDao; | ||
import com.navercorp.pinpoint.web.vo.AgentCountStatistics; | ||
import com.navercorp.pinpoint.web.vo.Range; | ||
import org.mybatis.spring.SqlSessionTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
@Repository | ||
public class MysqlAgentStatisticsDao implements AgentStatisticsDao { | ||
|
||
private static final String NAMESPACE = MysqlAgentStatisticsDao.class.getPackage().getName() + "." + MysqlAgentStatisticsDao.class.getSimpleName() + "."; | ||
|
||
@Autowired | ||
@Qualifier("sqlSessionTemplate") | ||
private SqlSessionTemplate sqlSessionTemplate; | ||
|
||
@Override | ||
public boolean insertAgentCount(AgentCountStatistics agentCountStatistics) { | ||
int insert = sqlSessionTemplate.insert(NAMESPACE + "insertAgentCount", agentCountStatistics); | ||
return insert > 0; | ||
} | ||
|
||
@Override | ||
public List<AgentCountStatistics> selectAgentCount(Range range) { | ||
return sqlSessionTemplate.selectList(NAMESPACE + "selectAgentCount", range); | ||
} | ||
|
||
} |
Oops, something went wrong.