Package com.dianping.cat.report.service.impl

Source Code of com.dianping.cat.report.service.impl.ServiceReportService

package com.dianping.cat.report.service.impl;

import java.util.Date;
import java.util.List;

import org.unidal.dal.jdbc.DalException;
import org.unidal.dal.jdbc.DalNotFoundException;

import com.dianping.cat.Cat;
import com.dianping.cat.Constants;
import com.dianping.cat.core.dal.DailyReport;
import com.dianping.cat.core.dal.DailyReportEntity;
import com.dianping.cat.core.dal.HourlyReport;
import com.dianping.cat.core.dal.HourlyReportContent;
import com.dianping.cat.core.dal.HourlyReportContentEntity;
import com.dianping.cat.core.dal.HourlyReportEntity;
import com.dianping.cat.core.dal.MonthlyReport;
import com.dianping.cat.core.dal.MonthlyReportEntity;
import com.dianping.cat.core.dal.WeeklyReport;
import com.dianping.cat.core.dal.WeeklyReportEntity;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.home.dal.report.DailyReportContent;
import com.dianping.cat.home.dal.report.DailyReportContentEntity;
import com.dianping.cat.home.dal.report.MonthlyReportContent;
import com.dianping.cat.home.dal.report.MonthlyReportContentEntity;
import com.dianping.cat.home.dal.report.WeeklyReportContent;
import com.dianping.cat.home.dal.report.WeeklyReportContentEntity;
import com.dianping.cat.home.service.entity.ServiceReport;
import com.dianping.cat.home.service.transform.DefaultNativeParser;
import com.dianping.cat.report.service.AbstractReportService;
import com.dianping.cat.report.task.service.ServiceReportMerger;

public class ServiceReportService extends AbstractReportService<ServiceReport> {

  @Override
  public ServiceReport makeReport(String domain, Date start, Date end) {
    ServiceReport report = new ServiceReport(domain);

    report.setStartTime(start);
    report.setEndTime(end);
    return report;
  }

  @Override
  public ServiceReport queryDailyReport(String domain, Date start, Date end) {
    ServiceReportMerger merger = new ServiceReportMerger(new ServiceReport(domain));
    long startTime = start.getTime();
    long endTime = end.getTime();
    String name = Constants.REPORT_SERVICE;

    for (; startTime < endTime; startTime = startTime + TimeHelper.ONE_DAY) {
      try {
        DailyReport report = m_dailyReportDao.findByDomainNamePeriod(domain, name, new Date(startTime),
              DailyReportEntity.READSET_FULL);
        String xml = report.getContent();

        if (xml != null && xml.length() > 0) {
          ServiceReport reportModel = com.dianping.cat.home.service.transform.DefaultSaxParser.parse(xml);
          reportModel.accept(merger);
        } else {
          ServiceReport reportModel = queryFromDailyBinary(report.getId(), domain);
          reportModel.accept(merger);
        }
      } catch (DalNotFoundException e) {
        //ignore
      } catch (Exception e) {
        Cat.logError(e);
      }
    }
    ServiceReport serviceReport = merger.getServiceReport();

    serviceReport.setStartTime(start);
    serviceReport.setEndTime(end);
    return serviceReport;
  }

  private ServiceReport queryFromDailyBinary(int id, String domain) throws DalException {
    DailyReportContent content = m_dailyReportContentDao.findByPK(id, DailyReportContentEntity.READSET_FULL);

    if (content != null) {
      return DefaultNativeParser.parse(content.getContent());
    } else {
      return new ServiceReport(domain);
    }
  }

  private ServiceReport queryFromHourlyBinary(int id, String domain) throws DalException {
    HourlyReportContent content = m_hourlyReportContentDao.findByPK(id, HourlyReportContentEntity.READSET_FULL);

    if (content != null) {
      return DefaultNativeParser.parse(content.getContent());
    } else {
      return new ServiceReport(domain);
    }
  }

  private ServiceReport queryFromMonthlyBinary(int id, String domain) throws DalException {
    MonthlyReportContent content = m_monthlyReportContentDao.findByPK(id, MonthlyReportContentEntity.READSET_FULL);

    if (content != null) {
      return DefaultNativeParser.parse(content.getContent());
    } else {
      return new ServiceReport(domain);
    }
  }

  private ServiceReport queryFromWeeklyBinary(int id, String domain) throws DalException {
    WeeklyReportContent content = m_weeklyReportContentDao.findByPK(id, WeeklyReportContentEntity.READSET_FULL);

    if (content != null) {
      return DefaultNativeParser.parse(content.getContent());
    } else {
      return new ServiceReport(domain);
    }
  }

  @Override
  public ServiceReport queryHourlyReport(String domain, Date start, Date end) {
    ServiceReportMerger merger = new ServiceReportMerger(new ServiceReport(domain));
    long startTime = start.getTime();
    long endTime = end.getTime();
    String name = Constants.REPORT_SERVICE;

    for (; startTime < endTime; startTime = startTime + TimeHelper.ONE_HOUR) {
      List<HourlyReport> reports = null;
      try {
        reports = m_hourlyReportDao.findAllByDomainNamePeriod(new Date(startTime), domain, name,
              HourlyReportEntity.READSET_FULL);
      } catch (DalException e) {
        Cat.logError(e);
      }
      if (reports != null) {
        for (HourlyReport report : reports) {
          String xml = report.getContent();

          try {
            if (xml != null && xml.length() > 0) {
              ServiceReport reportModel = com.dianping.cat.home.service.transform.DefaultSaxParser.parse(xml);
              reportModel.accept(merger);
            } else {
              ServiceReport reportModel = queryFromHourlyBinary(report.getId(), domain);
              reportModel.accept(merger);
            }
          } catch (DalNotFoundException e) {
            //ignore
          } catch (Exception e) {
            Cat.logError(e);
          }
        }
      }
    }
    ServiceReport serviceReport = merger.getServiceReport();

    serviceReport.setStartTime(start);
    serviceReport.setEndTime(new Date(end.getTime() - 1));

    return serviceReport;
  }

  @Override
  public ServiceReport queryMonthlyReport(String domain, Date start) {
    try {
      MonthlyReport entity = m_monthlyReportDao.findReportByDomainNamePeriod(start, domain,
            Constants.REPORT_SERVICE, MonthlyReportEntity.READSET_FULL);
      String content = entity.getContent();

      if (content != null && content.length() > 0) {
        return com.dianping.cat.home.service.transform.DefaultSaxParser.parse(content);
      } else {
        return queryFromMonthlyBinary(entity.getId(), domain);
      }
    } catch (DalNotFoundException e) {
      //ignore
    } catch (Exception e) {
      Cat.logError(e);
    }
    return new ServiceReport(domain);
  }

  @Override
  public ServiceReport queryWeeklyReport(String domain, Date start) {
    try {
      WeeklyReport entity = m_weeklyReportDao.findReportByDomainNamePeriod(start, domain, Constants.REPORT_SERVICE,
            WeeklyReportEntity.READSET_FULL);
      String content = entity.getContent();

      if (content != null && content.length() > 0) {
        return com.dianping.cat.home.service.transform.DefaultSaxParser.parse(content);
      } else {
        return queryFromWeeklyBinary(entity.getId(), domain);
      }
    } catch (DalNotFoundException e) {
      //ignore
    } catch (Exception e) {
      Cat.logError(e);
    }
    return new ServiceReport(domain);
  }

}
TOP

Related Classes of com.dianping.cat.report.service.impl.ServiceReportService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.