/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.apache.metron.profiler;

import org.apache.metron.common.configuration.profiler.ProfileConfig;
import org.json.simple.JSONObject;

import java.util.Optional;

/**
 * Responsible for building and maintaining a Profile.
 *
 * <p>Telemetry messages are applied to a profile using {@link ProfileBuilder#apply(JSONObject, long)}.  A
 * {@link ProfileMeasurement} is generated by calling {@link ProfileBuilder#flush()}.
 *
 * A {@link ProfileBuilder} is responsible only for building the profile for a specific [profile, entity]
 * pair.  There will exist many instances, one for each [profile, entity] pair that exists
 * within the incoming telemetry data applied to the profile.
 */
public interface ProfileBuilder {

  /**
   * Apply a message to the profile.
   *
   * @param message The message to apply.
   * @param timestamp The timestamp of the message.
   */
  void apply(JSONObject message, long timestamp);

  /**
   * Flush the Profile.
   *
   * <p>Completes the period and returns the {@link ProfileMeasurement}.  Clears all state in preparation for
   * the next window period.
   *
   * @return Returns the {@link ProfileMeasurement}.
   */
  Optional<ProfileMeasurement> flush();

  /**
   * Has the {@link ProfileBuilder} been initialized?
   *
   * @return True, if initialization has occurred.  False, otherwise.
   */
  boolean isInitialized();

  /**
   * Returns the definition of the profile being built.
   *
   * @return The profile definition.
   */
  ProfileConfig getDefinition();

  /**
   * Returns the value of a variable within the current profile state.
   *
   * @param variable The variable name.
   * @return The value of the variable.
   */
  Object valueOf(String variable);

  /**
   * Returns the name of the entity.
   *
   * <p>Each {@code ProfileBuilder} instance is responsible for one (profile, entity) pair.
   *
   * @return The entity.
   */
  String getEntity();
}
