use of org.jfree.data.time.RegularTimePeriod in project GT by Tencent.
the class APTLogFileParse method getData.
public static JfreeChartDatas getData(String fileName, APTLogFileHeader afh) {
if (afh == null) {
return null;
}
JfreeChartDatas result = new JfreeChartDatas();
int pkgNumber = afh.pkgNames.length;
int dataItemCount = afh.dataItems.length;
result.pkgNames = new String[pkgNumber];
//取进程名的后缀
for (int i = 0; i < pkgNumber; i++) {
int index = afh.pkgNames[i].lastIndexOf(".");
;
if (index == -1) {
index = 0;
}
result.pkgNames[i] = afh.pkgNames[i].substring(index + 1, afh.pkgNames[i].length());
}
result.monitorItem = afh.monitorItem;
result.dataItems = new String[dataItemCount];
System.arraycopy(afh.dataItems, 0, result.dataItems, 0, dataItemCount);
result.tsDataList = new ArrayList<TSData>();
for (int i = 0; i < pkgNumber * dataItemCount; i++) {
TSData tsData = new TSData();
result.tsDataList.add(tsData);
}
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String line = "";
for (int i = 0; i < 3; i++) {
br.readLine();
}
Date lastDate = null;
while ((line = br.readLine()) != null) {
String[] datas = line.split(Constant.APTLOG_FILECONTENT_SPLIT);
if (datas == null || datas.length != pkgNumber * dataItemCount + 1) {
continue;
}
Date date = Constant.SIMPLE_DATE_FORMAT_MILLISECOND.parse(datas[0]);
/**
* 过滤相同时间点的数据
*/
if (date.equals(lastDate)) {
continue;
}
lastDate = date;
RegularTimePeriod time = new Millisecond(date);
for (int i = 0; i < pkgNumber * dataItemCount; i++) {
float value = Float.parseFloat(datas[i + 1]);
DataUnit du = new DataUnit(time, value);
result.tsDataList.get(i).dataUnitList.add(du);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return result;
}
use of org.jfree.data.time.RegularTimePeriod in project adempiere by adempiere.
the class MChartDatasource method addData.
public void addData(MChart parent) {
String value = getValueColumn();
String category;
String unit = "D";
if (!parent.isTimeSeries())
category = getCategoryColumn();
else {
if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week)) {
unit = "W";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month)) {
unit = "MM";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter)) {
unit = "Q";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Year)) {
unit = "Y";
}
category = " TRUNC(" + getDateColumn() + ", '" + unit + "') ";
}
String series = DB.TO_STRING(getName());
boolean hasSeries = false;
if (getSeriesColumn() != null) {
series = getSeriesColumn();
hasSeries = true;
}
String where = getWhereClause();
if (!Util.isEmpty(where)) {
where = Env.parseContext(getCtx(), parent.getWindowNo(), where, true);
}
boolean hasWhere = false;
String sql = "SELECT " + value + ", " + category + ", " + series + " FROM " + getFromClause();
if (!Util.isEmpty(where)) {
sql += " WHERE " + where;
hasWhere = true;
}
Date currentDate = Env.getContextAsDate(getCtx(), "#Date");
Date startDate = null;
Date endDate = null;
int scope = parent.getTimeScope();
int offset = getTimeOffset();
if (parent.isTimeSeries() && scope != 0) {
offset += -scope;
startDate = increment(currentDate, parent.getTimeUnit(), offset);
endDate = increment(startDate, parent.getTimeUnit(), scope);
}
if (startDate != null && endDate != null) {
sql += hasWhere ? " AND " : " WHERE ";
sql += category + ">=TRUNC(" + DB.TO_DATE(new Timestamp(startDate.getTime())) + ", '" + unit + "') AND ";
sql += category + "<=TRUNC(" + DB.TO_DATE(new Timestamp(endDate.getTime())) + ", '" + unit + "') ";
}
MRole role = MRole.getDefault(getCtx(), false);
sql = role.addAccessSQL(sql, null, true, false);
if (hasSeries)
sql += " GROUP BY " + series + ", " + category + " ORDER BY " + series + ", " + category;
else
sql += " GROUP BY " + category + " ORDER BY " + category;
log.log(Level.FINE, sql);
PreparedStatement pstmt = null;
ResultSet rs = null;
TimeSeries tseries = null;
Dataset dataset = parent.getDataset();
try {
pstmt = DB.prepareStatement(sql, null);
rs = pstmt.executeQuery();
while (rs.next()) {
String key = rs.getString(2);
String seriesName = rs.getString(3);
if (seriesName == null)
seriesName = getName();
String queryWhere = "";
if (hasWhere)
queryWhere += where + " AND ";
queryWhere += series + " = " + DB.TO_STRING(seriesName) + " AND " + category + " = ";
if (parent.isTimeSeries() && dataset instanceof TimeSeriesCollection) {
if (tseries == null || !tseries.getKey().equals(seriesName)) {
if (tseries != null)
((TimeSeriesCollection) dataset).addSeries(tseries);
tseries = new TimeSeries(seriesName);
}
Date date = rs.getDate(2);
RegularTimePeriod period = null;
if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Day))
period = new Day(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week))
period = new Week(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month))
period = new Month(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter))
period = new Quarter(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Year))
period = new Year(date);
tseries.add(period, rs.getBigDecimal(1));
key = period.toString();
queryWhere += DB.TO_DATE(new Timestamp(date.getTime()));
} else {
queryWhere += DB.TO_STRING(key);
}
MQuery query = new MQuery(getAD_Table_ID());
String keyCol = MTable.get(getCtx(), getAD_Table_ID()).getKeyColumns()[0];
String whereClause = keyCol + " IN (SELECT " + getKeyColumn() + " FROM " + getFromClause() + " WHERE " + queryWhere + " )";
query.addRestriction(whereClause.toString());
query.setRecordCount(1);
HashMap<String, MQuery> map = parent.getQueries();
if (dataset instanceof DefaultPieDataset) {
((DefaultPieDataset) dataset).setValue(key, rs.getBigDecimal(1));
map.put(key, query);
} else if (dataset instanceof DefaultCategoryDataset) {
((DefaultCategoryDataset) dataset).addValue(rs.getBigDecimal(1), seriesName, key);
map.put(seriesName + "__" + key, query);
} else if (dataset instanceof TimeSeriesCollection) {
map.put(seriesName + "__" + key, query);
}
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (tseries != null)
((TimeSeriesCollection) dataset).addSeries(tseries);
}
use of org.jfree.data.time.RegularTimePeriod in project pinot by linkedin.
the class AnomalyGraphGenerator method createTimeSeries.
/**
* Returns data with series 0 = current and 1 = baseline. This method assumes there is exactly one
* metric in the response data.
*/
private TimeSeriesCollection createTimeSeries(final TimeOnTimeComparisonResponse data) {
final TimeSeries baseline = new TimeSeries(BASELINE_LABEL);
final TimeSeries current = new TimeSeries(CURRENT_LABEL);
for (int i = 0; i < data.getNumRows(); i++) {
Row row = data.getRow(i);
// Plot the baseline data as an overlay on the corresponding current data point.
// long baselineStart = row.getBaselineStart().getMillis();
long currentStart = row.getCurrentStart().getMillis();
Date date = new Date(currentStart);
RegularTimePeriod timePeriod = new Millisecond(date);
Metric metric = row.getMetrics().get(0);
baseline.add(timePeriod, metric.getBaselineValue());
current.add(timePeriod, metric.getCurrentValue());
}
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(current);
dataset.addSeries(baseline);
return dataset;
}
Aggregations