Skip to content

Commit

Permalink
add storage tag and so on
Browse files Browse the repository at this point in the history
  • Loading branch information
jialinsun committed Mar 10, 2015
1 parent 392752a commit d3906d9
Show file tree
Hide file tree
Showing 17 changed files with 1,112 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.dianping.cat.service.ModelRequest;
import com.dianping.cat.service.ModelResponse;
import com.dianping.cat.system.config.StorageGroupConfigManager;
import com.dianping.cat.system.config.StorageGroupConfigManager.Department;

public class Handler implements PageHandler<Context> {
@Inject
Expand Down Expand Up @@ -79,36 +80,42 @@ public void handleOutbound(Context ctx) throws ServletException, IOException {

switch (payload.getAction()) {
case HOURLY_DATABASE:
storageReport = buildHourlyReport(payload, model, ipAddress, SQL_TYPE);
storageReport = queryHourlyReport(payload, SQL_TYPE);
storageReport = buildReport(payload, model, storageReport);
StorageSorter sorter = new StorageSorter(storageReport, payload.getSort());
storageReport = sorter.getSortedReport();
break;
case HOURLY_CACHE:
storageReport = buildHourlyReport(payload, model, ipAddress, CACHE_TYPE);
storageReport = queryHourlyReport(payload, CACHE_TYPE);
storageReport = buildReport(payload, model, storageReport);
sorter = new StorageSorter(storageReport, payload.getSort());
storageReport = sorter.getSortedReport();
break;
case HOURLY_DATABASE_GRAPH:
storageReport = buildHourlyReport(payload, model, ipAddress, SQL_TYPE);
storageReport = queryHourlyReport(payload, SQL_TYPE);
storageReport = buildReport(payload, model, storageReport);

buildLineCharts(model, payload, ipAddress, storageReport);
break;
case HOURLY_CACHE_GRAPH:
storageReport = buildHourlyReport(payload, model, ipAddress, CACHE_TYPE);

storageReport = queryHourlyReport(payload, CACHE_TYPE);
storageReport = buildReport(payload, model, storageReport);
buildLineCharts(model, payload, ipAddress, storageReport);
break;
case HISTORY_DATABASE:
storageReport = queryHistoryReport(payload, SQL_TYPE);
storageReport = buildReport(payload, model, storageReport);
break;
case HISTORY_CACHE:
storageReport = queryHistoryReport(payload, CACHE_TYPE);
storageReport = buildReport(payload, model, storageReport);
sorter = new StorageSorter(storageReport, payload.getSort());
storageReport = sorter.getSortedReport();
break;
case DASHBOARD:
StorageAlertInfo alertInfo = m_alertInfoManager.queryAlertInfo(payload.getDate(), model.getMinute());
Map<String, StorageAlertInfo> alertInfo = m_alertInfoManager.queryAlertInfos(payload, model);

model.setDepartments(m_storageGroupConfigManager.queryStorageDepartments());
model.setAlertInfo(alertInfo);
model.setAlertInfos(alertInfo);
model.setReportStart(new Date(payload.getDate()));
model.setReportEnd(new Date(payload.getDate() + TimeHelper.ONE_HOUR - 1));
break;
Expand All @@ -133,11 +140,9 @@ private void buildLineCharts(Model model, Payload payload, String ipAddress, Sto
model.setLongTrend(m_jsonBuilder.toJson(lineCharts.get("long")));
}

private StorageReport buildHourlyReport(Payload payload, Model model, String ipAddress, String type) {
StorageReport storageReport = queryHourlyReport(payload, type);

private StorageReport buildReport(Payload payload, Model model, StorageReport storageReport) {
if (storageReport != null) {
storageReport = m_mergeHelper.mergeAllMachines(storageReport, ipAddress);
storageReport = m_mergeHelper.mergeAllMachines(storageReport, payload.getIpAddress());
storageReport = m_mergeHelper.mergeAllDomains(storageReport, Constants.ALL);

model.setReport(storageReport);
Expand All @@ -154,12 +159,13 @@ private StorageReport buildHourlyReport(Payload payload, Model model, String ipA
}
model.setOperations(ops);
}
StorageSorter sorter = new StorageSorter(storageReport, payload.getSort());

return storageReport;
return sorter.getSortedReport();
}

private StorageReport queryHourlyReport(Payload payload, String type) {
ModelRequest request = new ModelRequest(payload.getDomain() + "-" + type, payload.getDate()).setProperty("ip",
ModelRequest request = new ModelRequest(payload.getId() + "-" + type, payload.getDate()).setProperty("ip",
payload.getIpAddress());

if (m_service.isEligable(request)) {
Expand Down Expand Up @@ -198,6 +204,10 @@ private void normalize(Model model, Payload payload) {
model.setPage(ReportPage.STORAGE);
m_normalizePayload.normalize(model, payload);

Map<String, Department> departments = m_storageGroupConfigManager.queryStorageDepartments();

model.setDepartments(departments);

if (payload.getAction() == Action.DASHBOARD) {
Integer minute = parseQueryMinute(payload);
int maxMinute = 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Model extends AbstractReportModel<Action, Context> {

private StorageReport m_report;

private Set<String> m_operations;
private Set<String> m_operations = new HashSet<String>();

private String m_countTrend;

Expand All @@ -40,16 +40,16 @@ public class Model extends AbstractReportModel<Action, Context> {

private Date m_reportEnd;

private StorageAlertInfo m_alertInfo;
private Map<String, StorageAlertInfo> m_alertInfos;

private Map<String, Department> m_departments;

public Model(Context ctx) {
super(ctx);
}

public StorageAlertInfo getAlertInfo() {
return m_alertInfo;
public Map<String, StorageAlertInfo> getAlertInfos() {
return m_alertInfos;
}

public List<String> getAllOperations() {
Expand Down Expand Up @@ -154,8 +154,8 @@ public Date getReportStart() {
return m_reportStart;
}

public void setAlertInfo(StorageAlertInfo alertInfo) {
m_alertInfo = alertInfo;
public void setAlertInfos(Map<String, StorageAlertInfo> alertInfos) {
m_alertInfos = alertInfos;
}

public void setAvgTrend(String avgTrend) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.unidal.web.mvc.ActionContext;
import org.unidal.web.mvc.payload.annotation.FieldMeta;

import com.dianping.cat.Constants;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.report.ReportPage;
import com.dianping.cat.report.page.AbstractReportPayload;
Expand Down Expand Up @@ -35,6 +36,15 @@ public class Payload extends AbstractReportPayload<Action> {
@FieldMeta("frequency")
private int m_frequency = 10;

@FieldMeta("count")
private int m_minuteCounts = 8;

@FieldMeta("tops")
private int m_topCounts = 10;

@FieldMeta("id")
private String m_id = Constants.CAT;

public Payload() {
super(ReportPage.STORAGE);
}
Expand All @@ -44,17 +54,17 @@ public Action getAction() {
return m_action;
}

public long getCurrentTimeMillis() {
return System.currentTimeMillis() - TimeHelper.ONE_MINUTE * 1;
}

@Override
public long getCurrentDate() {
long timestamp = getCurrentTimeMillis();

return timestamp - timestamp % TimeHelper.ONE_HOUR;
}

public long getCurrentTimeMillis() {
return System.currentTimeMillis() - TimeHelper.ONE_MINUTE * 1;
}

@Override
public long getDate() {
long current = getCurrentDate();
Expand All @@ -76,10 +86,18 @@ public int getFrequency() {
return m_frequency;
}

public String getId() {
return m_id;
}

public String getMinute() {
return m_minute;
}

public int getMinuteCounts() {
return m_minuteCounts;
}

public String getOperations() {
return m_operations;
}
Expand All @@ -97,6 +115,10 @@ public String getSort() {
return m_sort;
}

public int getTopCounts() {
return m_topCounts;
}

public boolean isFullScreen() {
return m_fullScreen;
}
Expand All @@ -117,10 +139,18 @@ public void setFullScreen(boolean fullScreen) {
m_fullScreen = fullScreen;
}

public void setId(String id) {
m_id = id;
}

public void setMinute(String minute) {
m_minute = minute;
}

public void setMinuteCounts(int minuteCounts) {
m_minuteCounts = minuteCounts;
}

public void setOperations(String operations) {
m_operations = operations;
}
Expand All @@ -142,6 +172,10 @@ public void setSort(String sort) {
m_sort = sort;
}

public void setTopCounts(int topCounts) {
m_topCounts = topCounts;
}

@Override
public void validate(ActionContext<?> ctx) {
if (m_action == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.dianping.cat.report.page.storage.topology;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.unidal.dal.jdbc.DalNotFoundException;
import org.unidal.lookup.annotation.Inject;
Expand All @@ -11,8 +18,11 @@
import com.dianping.cat.home.dal.report.Alert;
import com.dianping.cat.home.dal.report.AlertDao;
import com.dianping.cat.home.dal.report.AlertEntity;
import com.dianping.cat.home.storage.alert.entity.Storage;
import com.dianping.cat.home.storage.alert.entity.StorageAlertInfo;
import com.dianping.cat.report.alert.AlertType;
import com.dianping.cat.report.page.storage.Model;
import com.dianping.cat.report.page.storage.Payload;

public class StorageAlertInfoManager {

Expand All @@ -25,13 +35,70 @@ public class StorageAlertInfoManager {
@Inject
private StorageAlertInfoRTContainer m_alertInfoRTContainer;

public StorageAlertInfo queryAlertInfo(long time, int minute) {
private SimpleDateFormat m_sdf = new SimpleDateFormat("HH:mm");

public Map<String, StorageAlertInfo> queryAlertInfos(Payload payload, Model model) {
long time = payload.getDate();
int minute = model.getMinute();
int count = payload.getMinuteCounts();
int tops = payload.getTopCounts();
Map<String, StorageAlertInfo> alertInfos = new LinkedHashMap<String, StorageAlertInfo>();
int start = minute - count + 1;

if (start < 0) {
start = 0;
int lastStart = 60 - (count - minute - 1);

alertInfos.putAll(queryAlertInfos(time - TimeHelper.ONE_HOUR, lastStart, 59, tops));
}

alertInfos.putAll(queryAlertInfos(time, start, minute, tops));

Map<String, StorageAlertInfo> results = new LinkedHashMap<String, StorageAlertInfo>();
List<String> keys = new ArrayList<String>(alertInfos.keySet());

Collections.sort(keys, new StringCompartor());

for (String key : keys) {
results.put(key, alertInfos.get(key));
}
return results;
}

private Map<String, StorageAlertInfo> queryAlertInfos(long time, int start, int end, int tops) {
Map<String, StorageAlertInfo> alertInfos = new LinkedHashMap<String, StorageAlertInfo>();

for (int i = start; i <= end; i++) {
StorageAlertInfo alertInfo = queryAlertInfo(time, i, tops);
Date minute = new Date(time + i * TimeHelper.ONE_MINUTE);

alertInfos.put(m_sdf.format(minute), alertInfo);
}
return alertInfos;
}

public StorageAlertInfo queryAlertInfo(long time, int minute, int tops) {
StorageAlertInfo alertInfo = m_alertInfoRTContainer.find(time, minute);

if (alertInfo == null) {
alertInfo = buildFromDatabase(time, minute);
}
return alertInfo;
List<Entry<String, Storage>> tmp = new ArrayList<Entry<String, Storage>>(alertInfo.getStorages().entrySet());

Collections.sort(tmp, new AlertInfoStorageComparator());

if (tmp.size() > tops) {
tmp = tmp.subList(0, tops);
}
StorageAlertInfo result = new StorageAlertInfo(alertInfo.getId());

result.setStartTime(alertInfo.getStartTime()).setEndTime(alertInfo.getEndTime());
Map<String, Storage> storages = result.getStorages();

for (Entry<String, Storage> storage : tmp) {
storages.put(storage.getKey(), storage.getValue());
}
return result;
}

private StorageAlertInfo buildFromDatabase(long time, int minute) {
Expand All @@ -53,4 +120,42 @@ private StorageAlertInfo buildFromDatabase(long time, int minute) {
}
return alertInfo;
}

public static class AlertInfoStorageComparator implements Comparator<Entry<String, Storage>> {

@Override
public int compare(Entry<String, Storage> o1, Entry<String, Storage> o2) {
int gap = o2.getValue().getLevel() - o1.getValue().getLevel();

return gap == 0 ? o2.getValue().getCount() - o1.getValue().getCount() : gap;
}

}

public static class StringCompartor implements Comparator<String> {

@Override
public int compare(String o1, String o2) {
String hour1 = o1.substring(0, 2);
String hour2 = o2.substring(0, 2);

if (!hour1.equals(hour2)) {
int hour1Value = Integer.parseInt(hour1);
int hour2Value = Integer.parseInt(hour2);

if (hour1Value == 0 && hour2Value == 23) {
return -1;
} else if (hour1Value == 23 && hour2Value == 0) {
return 1;
} else {
return hour2Value - hour1Value;
}
} else {
String first = o1.substring(3, 5);
String end = o2.substring(3, 5);

return Integer.parseInt(end) - Integer.parseInt(first);
}
}
}
}
Loading

0 comments on commit d3906d9

Please sign in to comment.