Search in sources :

Example 1 with RegularTimePeriod

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;
}
Also used : IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) Millisecond(org.jfree.data.time.Millisecond) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod)

Example 2 with RegularTimePeriod

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);
}
Also used : Quarter(org.jfree.data.time.Quarter) DBException(org.adempiere.exceptions.DBException) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) TimeSeries(org.jfree.data.time.TimeSeries) SQLException(java.sql.SQLException) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) Dataset(org.jfree.data.general.Dataset) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) Month(org.jfree.data.time.Month) Year(org.jfree.data.time.Year) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ResultSet(java.sql.ResultSet) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Day(org.jfree.data.time.Day) Week(org.jfree.data.time.Week) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Example 3 with RegularTimePeriod

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;
}
Also used : Millisecond(org.jfree.data.time.Millisecond) TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) Metric(com.linkedin.thirdeye.client.comparison.Row.Metric) Row(com.linkedin.thirdeye.client.comparison.Row) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Date(java.sql.Date)

Aggregations

RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)3 Date (java.util.Date)2 Millisecond (org.jfree.data.time.Millisecond)2 TimeSeries (org.jfree.data.time.TimeSeries)2 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)2 Row (com.linkedin.thirdeye.client.comparison.Row)1 Metric (com.linkedin.thirdeye.client.comparison.Row.Metric)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Date (java.sql.Date)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 DBException (org.adempiere.exceptions.DBException)1 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)1 Dataset (org.jfree.data.general.Dataset)1 DefaultPieDataset (org.jfree.data.general.DefaultPieDataset)1 Day (org.jfree.data.time.Day)1