// Path: src/main/java/com/stackify/api/StackifyError.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class StackifyError {
// 
//     /**
//      * Environment
//      */
//     @JsonProperty("EnvironmentDetail")
//     private EnvironmentDetail environmentDetail;
// 
//     /**
//      * Date/time of the error
//      */
//     @JsonProperty("OccurredEpochMillis")
//     private Long occurredEpochMillis;
// 
//     /**
//      * Error details
//      */
//     @JsonProperty("Error")
//     private ErrorItem error;
// 
//     /**
//      * Details of the web request
//      */
//     @JsonProperty("WebRequestDetail")
//     private WebRequestDetail webRequestDetail;
// 
//     /**
//      * Server variables
//      */
//     @JsonProperty("ServerVariables")
//     private Map<String, String> serverVariables;
// 
//     /**
//      * Customer name
//      */
//     @JsonProperty("CustomerName")
//     private String customerName;
// 
//     /**
//      * User name
//      */
//     @JsonProperty("UserName")
//     private String userName;
// 
// }


import com.stackify.api.StackifyError;
/*
 * Copyright 2013 Stackify
 *
 * 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.stackify.api.common.error;


/**
 * Utility class for limiting transmission of duplicate errors 
 * 
 * @author Eric Martin
 */
public class ErrorGovernor {

    /**
     * Number of instances of a unique error that are allowed to be sent in one minute 
     */
    private static final int MAX_DUP_ERROR_PER_MINUTE = 100;

    /**
     * Elapsed time before the errorToCounter map is purged of expired entries 
     */
    private static final int CLEAN_UP_MINUTES = 15;
    
    /**
     * Map from
     *     MD5(<type>-<typeCode>-<method>)
     * to
     *     Unix epoch minute, error count for that minute
     */
    private final ErrorCounter errorCounter = new ErrorCounter();

    /**
     * The next time the errorToCounter dictionary needs to be purged of expired entries
     */
    private long nextErrorToCounterCleanUp = getUnixEpochMinutes() + CLEAN_UP_MINUTES;

    /**
     * Determines if the error should be sent based on our throttling criteria
     * @param error The error
     * @return True if this error should be sent to Stackify, false otherwise
     */
// Path: src/main/java/com/stackify/api/StackifyError.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class StackifyError {
// 
//     /**
//      * Environment
//      */
//     @JsonProperty("EnvironmentDetail")
//     private EnvironmentDetail environmentDetail;
// 
//     /**
//      * Date/time of the error
//      */
//     @JsonProperty("OccurredEpochMillis")
//     private Long occurredEpochMillis;
// 
//     /**
//      * Error details
//      */
//     @JsonProperty("Error")
//     private ErrorItem error;
// 
//     /**
//      * Details of the web request
//      */
//     @JsonProperty("WebRequestDetail")
//     private WebRequestDetail webRequestDetail;
// 
//     /**
//      * Server variables
//      */
//     @JsonProperty("ServerVariables")
//     private Map<String, String> serverVariables;
// 
//     /**
//      * Customer name
//      */
//     @JsonProperty("CustomerName")
//     private String customerName;
// 
//     /**
//      * User name
//      */
//     @JsonProperty("UserName")
//     private String userName;
// 
// }


// Path: src/main/java/com/stackify/api/common/error/ErrorGovernor.java
import com.stackify.api.StackifyError;

/*
 * Copyright 2013 Stackify
 *
 * 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.stackify.api.common.error;


/**
 * Utility class for limiting transmission of duplicate errors 
 * 
 * @author Eric Martin
 */
public class ErrorGovernor {

    /**
     * Number of instances of a unique error that are allowed to be sent in one minute 
     */
    private static final int MAX_DUP_ERROR_PER_MINUTE = 100;

    /**
     * Elapsed time before the errorToCounter map is purged of expired entries 
     */
    private static final int CLEAN_UP_MINUTES = 15;
    
    /**
     * Map from
     *     MD5(<type>-<typeCode>-<method>)
     * to
     *     Unix epoch minute, error count for that minute
     */
    private final ErrorCounter errorCounter = new ErrorCounter();

    /**
     * The next time the errorToCounter dictionary needs to be purged of expired entries
     */
    private long nextErrorToCounterCleanUp = getUnixEpochMinutes() + CLEAN_UP_MINUTES;

    /**
     * Determines if the error should be sent based on our throttling criteria
     * @param error The error
     * @return True if this error should be sent to Stackify, false otherwise
     */
    public boolean errorShouldBeSent(final StackifyError error) {