以2小时的时间间隔计算平均值的最有效方法是什么

时间:2016-03-29 11:04:58

标签: java mongodb

我有一组日常数据,我使用mongo-java驱动程序从mongo db中检索。我查询的表只有今天的日常数据。一旦我得到了结果集,我就可以在一天中每隔两小时(00:00 - 02:00,02:00 - 04:00等)计算文档中其中一个字段(双值)的平均值

这是我目前正在使用的代码。在将其映射到DailyData模型(使用JsonTransformer)后,每日数据列表将通过方法的参数发送。

我已经删除了代码以使其更容易阅读:

public List<Model> getDailyData(List<DailyData> data) throws ParseException {

    //These are all the variables I use in my code, I have omitted their usage in this snippet in order to trim it down

    double occurenceOfDateRange1 = 0;
    double occurenceOfDateRange2 = 0;
    double occurenceOfDateRange3 = 0;
    double occurenceOfDateRange4 = 0;
    double occurenceOfDateRange5 = 0;
    double occurenceOfDateRange6 = 0;
    double occurenceOfDateRange7 = 0;
    double occurenceOfDateRange8 = 0;
    double occurenceOfDateRange9 = 0;
    double occurenceOfDateRange10 = 0;
    double occurenceOfDateRange11 = 0;
    double occurenceOfDateRange12 = 0;

    double percentage1 = 0;
    double percentage2 = 0;
    double percentage3 = 0;
    double percentage4 = 0;
    double percentage5 = 0;
    double percentage6 = 0;
    double percentage7 = 0;
    double percentage8 = 0;
    double percentage9 = 0;
    double percentage10 = 0;
    double percentage11 = 0;
    double percentage12 = 0;

    List<Model> modelList = new ArrayList<Model>();

    String pattern = "yyyy-MM-ddHH:mm:ss";

//There are 9 more date variables created like these three below (06:00:00, 08:00:00, 10:00:00 etc to 23:59:59)

    String dateStr1 = getdate() + " 00:00:00"; //getDate() refers to a separate method in which I return the current date in yyyy/MM/dd string format
    Date date1 = new SimpleDateFormat(pattern).parse(dateStr1);

    String dateStr2 = getdate() + " 02:00:00";
    Date date2 = new SimpleDateFormat(pattern).parse(dateStr2);

    String dateStr3 = getdate() + " 04:00:00";
    Date date3 = new SimpleDateFormat(pattern).parse(dateStr3);

    //the other 9 date variables are created here

    if (data != null) {
        for (int num = 0; num < data.size(); num++) {
            Date recordDate = new SimpleDateFormat(pattern).parse(data.get(num).getCheckDate());

          //Ten more checks along with the following two are done, I have omitted them to slim the code down (checks between date3 and date4, between date4 and date 5 etc)

            if (recordDate.after(date1) && recordDate.before(date2)) {
                percentage1 += data.get(num).getPercentage();
                occurenceOfDateRange1 += 1;

            }

            if (recordDate.after(date2) && recordDate.before(date3)) {
                percentage2 += data.get(num).getPercentage();
                occurenceOfDateRange2 += 1;
            }

            //checks for the rest of the dates are done here
        }

        Model model = new Model();

        //the following if blocks are repeated 10 more times for percentage3 to percentage12

        if (percentage1 != 0) {
            model = new Model();
            model.setDate_time_stamp(dateStr1);
            if (occurenceOfDateRange1 != 0) {
                model.setAvgPerc(percentage1 / occurenceOfDateRange1);
            } else {
                model.setAvgPerc(0);
            }
            modelList.add(model);
        }

        if (percentage2 != 0) {
            model = new Model();
            model.setDate_time_stamp(dateStr2);
            if (occurenceOfDateRange2 != 0) {
                model.setAvgPerc(percentage2 / occurenceOfDateRange2);
            } else {
                model.setAvgPerc(0);
            }
            modelList.add(model);
        }
    }

    return modelList;
}

我的问题是 - 如果没有使用那么多if块,是否有更高效(或更聪明,如果你愿意)计算两小时间隔之间的平均百分比的方法?

1 个答案:

答案 0 :(得分:1)

我想我可能会这样做:

public List<Model> getDailyData(List<DailyData> data) throws ParseException {

    List<Model> modelList = new ArrayList<Model>();

    String pattern = "yyyy-MM-ddHH:mm:ss";

    double[] occurenceOfDateRange = {0.0, 0.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 0.0, 0.0};
    double[] percentages = {0.0, 0.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 0.0, 0.0,
        0.0, 0.0, 0.0};

    if (data != null) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        for (int num = 0; num < data.size(); num++) {
            Calendar recordDate = Calendar.getInstance();
            recordDate.setTime(
                    new SimpleDateFormat(pattern).parse(data.get(num).getCheckDate()));

            percentages[recordDate.get(Calendar.HOUR_OF_DAY) / 2] += data.get(num).getPercentage();
            occurenceOfDateRange[recordDate.get(Calendar.HOUR_OF_DAY) / 2] += 1;

        }

        Calendar base = Calendar.getInstance();
        base.set(Calendar.HOUR, 0);

        for (int i = 0; i < 12; i++) {
            Model model = new Model();
            model.setDate_time_stamp(df.format(base));
            if (occurenceOfDateRange[i] != 0) {
                model.setAvgPerc(percentages[i] / occurenceOfDateRange[i]);
            } else {
                model.setAvgPerc(0);
            }
            modelList.add(model);
            base.set(Calendar.HOUR, base.get(Calendar.HOUR) + 2);
        }
    }

    return modelList;
}