<commit_before>package simizer.requests;

import java.util.ArrayList;
import java.util.List;
import simizer.utils.Vector;

public class Request {

  private long id;
  private int node = -1;
  private long nbInstructions;
  protected String params;
  private List<Integer> rscList;
  protected int typeId;
  long procTime;

  /** The timestamp when the client starts sending the {@code Request}. */
  private long clientStartTimestamp;

  /**
   * The timestamp when the server finishes processing the {@code Request}.
   * <p>
   * This is the timestamp when it hands the response to the {@link Network} to
   * be sent back to the client.  It is not the end of the request-response
   * cycle because the response still needs to reach the client.
   */
  private long serverFinishTimestamp;

  /** The delay that comes as a result of {@code Network} delays. */
  private long networkDelay = 0;

  /** The delay from the load balancing code (measured in nanoseconds). */
  private long loadBalancingDelayNS = 0;

  protected String type;
  /** The cost associated with the {@code Request}. */
  private double cost = 0.0;

  /** The number of errors that have occurred for this {@code Request}. */
  private int errorCount = 0;

  /** The size of the {@code Request}.  This value is mostly ignored. */
  private int size;

  public Request(long id, int typeId, long artime, String params, long procTime,
      String type, long nbInst) {

    this.id = id;
    this.clientStartTimestamp = artime;
    this.type = type;
    this.typeId = typeId;
    this.params = params;
    this.procTime = procTime;
    this.rscList = parseResources(params);
    this.nbInstructions = nbInst;
  }

  /**
   * Copy constructor, to refacto (rewrite @link{RequestFactory})
   *
   * @param r
   */
  public Request(Request r) {
    this.id = -1;
    this.clientStartTimestamp = -1;
    this.node = 0;
    this.cost = r.cost;
    this.params = r.params;
    this.procTime = r.procTime;
    this.rscList = r.rscList;
    this.serverFinishTimestamp = 0;
    this.type = r.type;
    this.typeId = r.typeId;
    this.errorCount = 0;

    this.nbInstructions = r.nbInstructions;
  }

  public long getId() {
    return this.id;
  }

  /**
   * Returns the timestamp when the client first sent the {@code Request}.
   * <p>
   * When a client first hands a {@code Request} to the {@link Network} to be
   * sent, this value should be set to the current timestamp.  It can therefore
   * be used to measure the round-trip time of the entire request.
   * 
   * @return the timestamp when the client first sent the {@code Request}
   */
  public long getClientStartTimestamp() {
    return clientStartTimestamp;
  }

  /**
   * Sets the timestamp when the client first sent the {@code Request}.
   * <p>
   * This value should only be set once per {@code Request}.
   * <p>
   * For a more in-depth description of the value, see {@link
   * #getClientStartTimestamp()}.
   *
   * @param clientStartTimestamp the timestamp when the client first sent the
   *            {@code Request}
   */
  public void setClientStartTimestamp(long clientStartTimestamp) {
    this.clientStartTimestamp = clientStartTimestamp;
  }

  /**
   * Returns the timestamp when the server finished processing the {@code
   * Request}.
   *
   * @return the timestamp when the server finished processing the {@code
   *             Request}
   */
  public long getServerFinishTimestamp() {
    return serverFinishTimestamp;
  }

  /**
   * Sets the timestamp when the server finishes processing the {@code Request}.
   *
   * @param serverFinishTimestamp the timestamp when the server finishes processing
   *            the {@code Request}
   */
  public void setServerFinishTimestamp(long serverFinishTimestamp) {
    this.serverFinishTimestamp = serverFinishTimestamp;
  }

  /**
   * Returns the current delay imposed by {@code Network}s.
   *
   * @return the current delay imposed by {@code Network}s
   */
  public long getNetworkDelay() {
    return networkDelay;
  }

  /**
   * Adds the specified value to the total delay from {@code Network}s.
   *
   * @param delay the amount by which the network delay should be increased
   */
  public void addNetworkDelay(long delay) {
    networkDelay += delay;
  }

  /**
   * Sets the load balancing delay.
   * <p>
   * In order to accurately measure the time taken by the various load balancing
   * algorithms, this value is measured as accurately as possible.  It is
   * measured in nanoseconds to provide enough granularity.
   *
   * @param nanoseconds the amount of the delay, measured in nanoseconds
   */
  public void setLoadBalancingDelayNS(long nanoseconds) {
    this.loadBalancingDelayNS = nanoseconds;
  }

  /**
   * Sets the cost associated with this {@code Request}.
   *
   * @param cost the cost to associated with this {@code Request}
   */
  public void setCost(double cost) {
    this.cost = cost;
  }

  /**
   * Returns the cost associated with this {@code Request}.
   *
   * @return the cost associated with this {@code Request}
   */
  public double getCost() {
    return this.cost;
  }

  /**
   * Returns the number of errors that have occurred.
   *
   * @return the number of errors that have occurred
   */
  public int getErrorCount() {
    return this.errorCount;
  }

  /**
   * Reports errors for this {@code Request}.
   * <p>
   * The {@code Request} object keeps a running tally of the number of errors
   * that have occurred.  Each time an errorCount occurred, it should be
   * reported using this method.
   *
   * @param count the number of errors to report.  This value is added to the
   *            running total of errors that have occurred.
   */
  public void reportErrors(int count) {
    this.errorCount += count;
  }

  /**
   * Returns the size of the {@code Request}.
   *
   * @return the size of the {@code Request}
   */
  public int getSize() {
    return this.size;
  }

  public String getParameters() {
    return this.params;
  }

  @Override
  public String toString() {
    return (id
            + ";" + clientStartTimestamp
            + ";" + params
            + ";" + serverFinishTimestamp
            + ";" + networkDelay
            + ";" + node
            + ";" + loadBalancingDelayNS
            + ";" + cost
            + ";" + errorCount + "r");
  }

  public String display() {
    return (id + ","
            + params.replaceAll("&|=", ",")
            + "," + node
            + "," + cost);
  }

  // I don't think this has what I perceived to be the intended behavior.  It
  // makes it non-overiddable, but it doesn't prevent the list from being
  // changed.
  public final List<Integer> getResources() {
    return this.rscList;
  }

  public String[] getParamsStr() {

    String[] tmp = params.split("&|=");
    String[] result = new String[tmp.length / 2];
    int y = 0;
    for (int i = 1; i < tmp.length; i += 2) {
      result[y++] = tmp[i];
    }
    return result;
  }

  public int[] getParams() {
    String[] s = params.split(",");
    int[] res = new int[s.length];
    for (int i = 0; i < s.length; i++) {
      res[i] = Integer.parseInt(s[i]);
    }
    return res;
  }

  public void setNode(int nId) {
    this.node = nId;
  }

  public long getProcTime() {
    return this.procTime;
  }

  private static List<Integer> parseResources(String params) {
    List<Integer> rList = new ArrayList<>();
    String[] tokens = params.split("&|=");
    for (int i = 1; i < tokens.length; i += 2) {
      rList.add(Integer.parseInt(tokens[i]));
    }
    return rList;
  }

  public Integer getNodeId() {
    return this.node;
  }

  public long getNbInst() {
    return this.nbInstructions;
  }

  public void setNbInst(long l) {
    this.nbInstructions = l;
  }

  public String getRequestType() {
    return this.type;
  }

  public int getAppId() {
    return this.typeId;
  }

  public String getType() {
    return this.type;
  }

  public void setAppId(int i) {
    this.typeId = i;
  }





  /** @deprecated @return */
  public int getTypeId() {
    return this.typeId;
  }

  // new type of request for request factory

  /**
   * @deprecated @param id
   * @param typeId
   * @param artime
   * @param params
   * @param procTime
   * @param type
   */
  public Request(long id, int typeId, long artime, String params, long procTime, String type) {
    this.id = id;
    this.clientStartTimestamp = artime;
    this.type = type;<commit_msg>Reorganize the Request class (and deprecations).<commit_after>package simizer.requests;

import java.util.ArrayList;
import java.util.List;
import simizer.utils.Vector;

public class Request {

  private long id;
  private int node = -1;
  
  protected String params;
  private List<Integer> rscList;
  protected String type;
  protected int typeId;

  private long nbInstructions;  // @deprecated
  long procTime;  // @deprecated

  /** The timestamp when the client starts sending the {@code Request}. */
  private long clientStartTimestamp;

  /**
   * The timestamp when the server finishes processing the {@code Request}.
   * <p>
   * This is the timestamp when it hands the response to the {@link Network} to
   * be sent back to the client.  It is not the end of the request-response
   * cycle because the response still needs to reach the client.
   */
  private long serverFinishTimestamp;

  /** The delay that comes as a result of {@code Network} delays. */
  private long networkDelay = 0;

  /** The delay from the load balancing code (measured in nanoseconds). */
  private long loadBalancingDelayNS = 0;

  /** The cost associated with the {@code Request}. */
  private double cost = 0.0;

  /** The number of errors that have occurred for this {@code Request}. */
  private int errorCount = 0;

  /** The size of the {@code Request}.  This value is mostly ignored. */
  private int size;

  public Request(long id, int typeId, long artime, String params, long procTime,
      String type, long nbInst) {

    this.id = id;
    this.clientStartTimestamp = artime;
    this.type = type;
    this.typeId = typeId;
    this.params = params;
    this.procTime = procTime;
    this.rscList = parseResources(params);
    this.nbInstructions = nbInst;
  }

  /**
   * Copy constructor, to refacto (rewrite @link{RequestFactory})
   *
   * @param r
   */
  public Request(Request r) {
    this.id = -1;
    this.clientStartTimestamp = -1;
    this.node = 0;
    this.cost = r.cost;
    this.params = r.params;
    this.procTime = r.procTime;
    this.rscList = r.rscList;
    this.serverFinishTimestamp = 0;
    this.type = r.type;
    this.typeId = r.typeId;
    this.errorCount = 0;

    this.nbInstructions = r.nbInstructions;
  }

  public long getId() {
    return this.id;
  }

  /**
   * Returns the timestamp when the client first sent the {@code Request}.
   * <p>
   * When a client first hands a {@code Request} to the {@link Network} to be
   * sent, this value should be set to the current timestamp.  It can therefore
   * be used to measure the round-trip time of the entire request.
   * 
   * @return the timestamp when the client first sent the {@code Request}
   */
  public long getClientStartTimestamp() {
    return clientStartTimestamp;
  }

  /**
   * Sets the timestamp when the client first sent the {@code Request}.
   * <p>
   * This value should only be set once per {@code Request}.
   * <p>
   * For a more in-depth description of the value, see {@link
   * #getClientStartTimestamp()}.
   *
   * @param clientStartTimestamp the timestamp when the client first sent the
   *            {@code Request}
   */
  public void setClientStartTimestamp(long clientStartTimestamp) {
    this.clientStartTimestamp = clientStartTimestamp;
  }

  /**
   * Returns the timestamp when the server finished processing the {@code
   * Request}.
   *
   * @return the timestamp when the server finished processing the {@code
   *             Request}
   */
  public long getServerFinishTimestamp() {
    return serverFinishTimestamp;
  }

  /**
   * Sets the timestamp when the server finishes processing the {@code Request}.
   *
   * @param serverFinishTimestamp the timestamp when the server finishes processing
   *            the {@code Request}
   */
  public void setServerFinishTimestamp(long serverFinishTimestamp) {
    this.serverFinishTimestamp = serverFinishTimestamp;
  }

  /**
   * Returns the current delay imposed by {@code Network}s.
   *
   * @return the current delay imposed by {@code Network}s
   */
  public long getNetworkDelay() {
    return networkDelay;
  }

  /**
   * Adds the specified value to the total delay from {@code Network}s.
   *
   * @param delay the amount by which the network delay should be increased
   */
  public void addNetworkDelay(long delay) {
    networkDelay += delay;
  }

  /**
   * Sets the load balancing delay.
   * <p>
   * In order to accurately measure the time taken by the various load balancing
   * algorithms, this value is measured as accurately as possible.  It is
   * measured in nanoseconds to provide enough granularity.
   *
   * @param nanoseconds the amount of the delay, measured in nanoseconds
   */
  public void setLoadBalancingDelayNS(long nanoseconds) {
    this.loadBalancingDelayNS = nanoseconds;
  }

  /**
   * Sets the cost associated with this {@code Request}.
   *
   * @param cost the cost to associated with this {@code Request}
   */
  public void setCost(double cost) {
    this.cost = cost;
  }

  /**
   * Returns the cost associated with this {@code Request}.
   *
   * @return the cost associated with this {@code Request}
   */
  public double getCost() {
    return this.cost;
  }

  /**
   * Returns the number of errors that have occurred.
   *
   * @return the number of errors that have occurred
   */
  public int getErrorCount() {
    return this.errorCount;
  }

  /**
   * Reports errors for this {@code Request}.
   * <p>
   * The {@code Request} object keeps a running tally of the number of errors
   * that have occurred.  Each time an errorCount occurred, it should be
   * reported using this method.
   *
   * @param count the number of errors to report.  This value is added to the
   *            running total of errors that have occurred.
   */
  public void reportErrors(int count) {
    this.errorCount += count;
  }

  /**
   * Returns the size of the {@code Request}.
   *
   * @return the size of the {@code Request}
   */
  public int getSize() {
    return this.size;
  }

  @Override
  public String toString() {
    return (id
            + ";" + clientStartTimestamp
            + ";" + params
            + ";" + serverFinishTimestamp
            + ";" + networkDelay
            + ";" + node
            + ";" + loadBalancingDelayNS
            + ";" + cost
            + ";" + errorCount + "r");
  }

  public String display() {
    return (id + ","
            + params.replaceAll("&|=", ",")
            + "," + node
            + "," + cost);
  }

  public String getParameters() {
    return this.params;
  }

  // I don't think this has what I perceived to be the intended behavior.  It
  // makes it non-overiddable, but it doesn't prevent the list from being
  // changed.
  public final List<Integer> getResources() {
    return this.rscList;
  }

  public String[] getParamsStr() {

    String[] tmp = params.split("&|=");
    String[] result = new String[tmp.length / 2];
    int y = 0;
    for (int i = 1; i < tmp.length; i += 2) {
      result[y++] = tmp[i];
    }
    return result;
  }

  public int[] getParams() {
    String[] s = params.split(",");
    int[] res = new int[s.length];
    for (int i = 0; i < s.length; i++) {
      res[i] = Integer.parseInt(s[i]);
    }
    return res;
  }

  public void setNode(int nId) {
    this.node = nId;
  }

  /** @deprecated */
  public long getProcTime() {
    return this.procTime;
  }

  private static List<Integer> parseResources(String params) {
    List<Integer> rList = new ArrayList<>();
    String[] tokens = params.split("&|=");
    for (int i = 1; i < tokens.length; i += 2) {
      rList.add(Integer.parseInt(tokens[i]));
    }
    return rList;
  }

  public Integer getNodeId() {
    return this.node;
  }

  /** @deprecated */
  public long getNbInst() {
    return this.nbInstructions;
  }

  /** @deprecated */
  public void setNbInst(long l) {
    this.nbInstructions = l;
  }

  public String getRequestType() {
    return this.type;
  }

  public int getAppId() {
    return this.typeId;
  }

  public String getType() {
    return this.type;
  }

  public void setAppId(int i) {
    this.typeId = i;
  }

  /** @deprecated @return */
  public int getTypeId() {
    return this.typeId;
  }

  // new type of request for request factory

  /**
   * @deprecated @param id
   * @param typeId
   * @param artime
   * @param params
   * @param procTime
   * @param type
   */
  public Request(long id, int typeId, long artime, String params, long procTime, String type) {
    this.id = id;
    this.clientStartTimestamp = artime;
    this.type = type;
{'fraction_non_alphanumeric': 0.10668154062940348, 'fraction_numerical': 0.0021136683889149835, 'mean_word_length': 2.6209608843537415, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1344893', 'n_tokens_mistral': 5149, 'n_tokens_neox': 4979, 'n_words': 1785}