com.kise.kairosdb.core.groupby.FiscalCalendarGroubBy.java Source code

Java tutorial

Introduction

Here is the source code for com.kise.kairosdb.core.groupby.FiscalCalendarGroubBy.java

Source

/*
* Copyright 2015 Kratos Integral Systems Europe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.kise.kairosdb.core.groupby;

import com.kise.kairosdb.util.fiscalperiod.FiscalPeriod;
import com.kise.kairosdb.util.fiscalperiod.FiscalPeriod445Calendar;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONWriter;
import org.kairosdb.core.DataPoint;
import org.kairosdb.core.aggregator.annotation.GroupByName;
import org.kairosdb.core.formatter.FormatterException;
import org.kairosdb.core.groupby.GroupBy;
import org.kairosdb.core.groupby.GroupByResult;

/**
 *
 * @author lcoulet
 */
@GroupByName(name = "fiscal_period", description = "Groups data points by fiscal Period.")
public class FiscalCalendarGroubBy implements GroupBy {

    private FiscalPeriod445Calendar.CalendarType calendarType = FiscalPeriod445Calendar.CalendarType.CALENDAR_544;
    private HashMap<Integer, FiscalPeriod> periods = new HashMap<>();

    @Override
    public int getGroupId(DataPoint dataPoint, Map<String, String> tags) {
        FiscalPeriod445Calendar fiscalPeriod = new FiscalPeriod445Calendar(calendarType, dataPoint.getTimestamp());
        if (!periods.containsKey(fiscalPeriod.getID())) {
            periods.put(fiscalPeriod.getID(), fiscalPeriod);
        }
        return fiscalPeriod.getID();
    }

    @Override
    public GroupByResult getGroupByResult(int id) {
        return new GroupByResult() {
            @Override
            public String toJson() throws FormatterException {
                StringWriter stringWriter = new StringWriter();
                try {
                    JSONWriter writer = new JSONWriter(stringWriter);
                    FiscalPeriod p = periods.get(id);
                    writer.object();
                    writer.key("name").value("fiscal_period");
                    writer.key("group").object();
                    writer.key("name").value("FY" + p.getFiscalYear() + "-P" + p.getFiscalPeriod());
                    writer.key("fiscal_year").value(p.getFiscalYear());
                    writer.key("quarter").value("Q" + p.getQuarter());
                    writer.key("fiscal_period").value(p.getFiscalPeriod());
                    writer.endObject();
                    writer.endObject();
                } catch (JSONException e) {
                    throw new FormatterException(e);
                }

                return stringWriter.toString();
            }
        };
    }

    @Override
    public void setStartDate(long startDate) {

    }

    /**
     * @param calendarType the calendarType to set
     */
    public void setCalendarType(String calendarType) {

        this.calendarType = FiscalPeriod445Calendar.CalendarType.valueOf(calendarType.toUpperCase());
    }

}