Skip to content

Commit

Permalink
added to insert a number of agent into rdb periodically. pinpoint-apm…
Browse files Browse the repository at this point in the history
  • Loading branch information
koo-taejin committed Jun 30, 2016
1 parent c6b4c5f commit a29a227
Show file tree
Hide file tree
Showing 17 changed files with 562 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* @author emeroad
Expand Down Expand Up @@ -50,4 +52,16 @@ public static String longToDateStr(long date, String fmt) {
final SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(new Date(date));
}

public static long timestampToMidNight(long timestamp) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date(timestamp));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

return calendar.getTime().getTime();
}

}
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());
}

}
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;
}

}
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;
}

}
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");
}
}

}
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;
}

}
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);

}
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);
}

}
Loading

0 comments on commit a29a227

Please # to comment.