// Copyright 2012 Google Inc.
//
// 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.

// Author: snagori@google.com (Shrikant Nagori)
// Protobufs to store all information related to logging.
syntax = "proto2";

option optimize_for = LITE_RUNTIME;

package net_instaweb;


// This proto stores all information related to latency.
// Next id for TimingInfo : 10
message TimingInfo  {
  // Local cache lookup time in ms.
  optional int64 cache1_ms = 1;
  // Remote cache lookup time in ms.
  optional int64 cache2_ms = 2;
  // Time taken to fetch the http header.
  optional int64 header_fetch_ms = 3;
  // Time taken to fetch the resource from the source.
  optional int64 fetch_ms = 4;
  // Time taken to send the first byte back to the client.
  optional int64 time_to_first_byte_ms = 5;
  // Absolute time when the request started, in ms since 1970.
  optional int64 request_start_ms = 6;
  // Time from the start of the request to when parsing begins. Note that this
  // is only set for HTML requests that are parsed.
  optional int64 time_to_start_parse_ms = 7;
  // Time from the start of the request to when the fetch is triggered. Note
  // that this is only set for requests that require a fetch.
  optional int64 time_to_start_fetch_ms = 8;
  // Time for processing the request.
  optional int64 processing_time_ms = 9;
}

// This proto stores all Blink related information.
// Next id for BlinkInfo : 8
message BlinkInfo {
  enum BlinkRequestFlow {
    BLINK_CACHE_HIT = 1;
    BLINK_CACHE_MISS_FOUND_RESOURCE = 2;
    BLINK_CACHE_MISS_FETCH_NON_OK = 3;
    BLINK_CACHE_MISS_TRIGGERED_REWRITE = 4;
    FOUND_MALFORMED_HTML = 5;
    FOUND_LAST_STATUS_CODE_NON_OK = 6;
    FOUND_CONTENT_LENGTH_OVER_THRESHOLD = 7;
  };
  // Tracks the flow of a Blink request.
  optional BlinkRequestFlow blink_request_flow = 1;

  // The event id of the user request for which blink flow was triggered.
  optional string request_event_id_time_usec = 2;

  // Tracks whether html change detection full-diff found a match or not.
  optional bool html_match = 3;

  enum UserAgent {
    NOT_SET = 0;
    BLINK_DESKTOP_WHITELIST = 1;
    BLINK_DESKTOP_BLACKLIST = 2;
    BLINK_MOBILE = 3;
    NULL_OR_EMPTY = 4;
    NOT_SUPPORT_BLINK = 5;
  };

  optional UserAgent blink_user_agent = 4;

  // Url of the request.
  optional string url = 5;

  // Tracks whether html change detection smart_diff found a match or not.
  optional bool html_smart_diff_match = 6;

  // Tracks whether we hit the #mismatches threshold for blacklisting.
  optional bool mismatch_blacklist_threshold_exceeded = 7;
}

// This proto stores information related to metadata cache lookups.
// Next id for MetadataCacheInfo: 4
message MetadataCacheInfo {
  optional int32 num_hits = 1;
  optional int32 num_revalidates = 2;
  optional int32 num_misses = 3;
}

// This proto stores all information related to split_html.
// Next id for SplitHtmlInfo : 2
message SplitHtmlInfo {
  // Total size of below-the-fold JSON.
  optional int64 json_size = 1;
}

// This proto stores all information related to property cohorts.
// Next is for PropertyCohortInfo: 7
message PropertyCohortInfo {
  // The name of the property cohort.
  optional string name = 1;
  // Whether the cohort was found in the cache
  optional bool is_cache_hit = 2 [ default = false ];
  // The list of properties that were found in the cache.
  repeated string properties_found = 3;
  // The device type for which this cohort was looked up.
  // Refer to net/instaweb/http/public/user_agent_matcher.h to see what the
  // different values map to.
  optional int32 device_type = 4;
  // The cache type for which this cohort was looked up.
  // Refer to net/instaweb/automatic/public/proxy_fetch.h to see what the
  // different values map to.
  optional int32 cache_type = 5;
  // Cache result defined as CacheInterface::KeyState in
  // net/instaweb/util/public/cache_interface.h.
  optional int32 cache_key_state = 6;
}

// This proto stores information related to property cache lookups.
// Next id for PropertyPageInfo: 2
message PropertyPageInfo {
  repeated PropertyCohortInfo cohort_info = 1;
}

// Information related to flush subresources filter.
// Next id for FlushEarlyFlowInfo: 2
message FlushEarlyFlowInfo {
  // An estimate of the time available at the client to download resources
  // before it starts receiving html content.
  optional int64 available_time_ms = 1;
}

// Information related to each resource url considered by the flush subresources
// filter.
// Next id for FlushEarlyResourceInfo: 5
message FlushEarlyResourceInfo {
  enum ResourceType {
    UNKNOWN_TYPE = 0;
    PAGESPEED = 1;
    NON_PAGESPEED = 2;
    PRIVATE_CACHEABLE = 3;
    DEFERJS_SCRIPT = 4;
  }

  enum ContentType {
    UNKNOWN_CONTENT_TYPE = 0;
    IMAGE = 1;
    JS = 2;
    CSS = 3;
  }

  // Content type of the resource.
  optional ContentType content_type = 1;
  // Type of the resource based on url and its cache ttl.
  optional ResourceType resource_type = 2;
  // Is this resource affected by available bandwidth?
  optional bool is_bandwidth_affected = 3;
  // Is the resource in HEAD or BODY?
  optional bool in_head = 4;
}

// Information that applies to a resource.
// Next id for RewriteResourceInfo: 4
message RewriteResourceInfo {
  // True if we inlined the resource.
  optional bool is_inlined = 1 [ default = false ];
  // True if the resource is critical.
  optional bool is_critical = 2 [ default = false ];
  // True if the resource has a pagespeed_no_defer attribute.
  optional bool has_pagespeed_no_defer = 3 [ default = false ];
}

// Information related to each resource considered by the image rewrite filter.
// Next id for ImageRewriteResourceInfo: 2
message ImageRewriteResourceInfo {
  // True if the low_res_src html attribute was inserted.
  optional bool is_low_res_src_inserted = 1 [ default = false ];
  // Size of the low resolution image. If it is zero, the low res image was not
  // found.
  optional int32 low_res_size = 2 [ default = 0 ];
}

// This proto stores information describing each rewriter's activity in a
// request.
// Next id for RewriterInfo: 6
message RewriterInfo {
  enum RewriterApplicationStatus {
    UNKNOWN_STATUS = 0;
    APPLIED_OK = 1;
    NOT_APPLIED = 2;
  }

  // The rewriter's id.
  optional string id = 1;
  // Status of this rewriter application.
  optional RewriterApplicationStatus status = 2;
  // Flush subresources specific information.
  optional FlushEarlyResourceInfo flush_early_resource_info = 3;
  // Information about a resource.
  optional RewriteResourceInfo rewrite_resource_info = 4;
  // Image rewrite filter specific information.
  optional ImageRewriteResourceInfo image_rewrite_resource_info = 5;
}

// This proto stores all information which we would like to log.
// Next id for LoggingInfo: 16
message LoggingInfo {
  // Used to track time taken by various components of page speed service
  // processing.
  optional TimingInfo timing_info = 1;
  // Stores all Blink related information.
  optional BlinkInfo blink_info = 2;
  // Sorted list of applied rewriter ids separated by comma.
  optional string applied_rewriters = 3;
  // Metadata cache info.
  optional MetadataCacheInfo metadata_cache_info = 4;
  // Whether the original resource is cacheable.
  optional bool is_original_resource_cacheable = 5 [ default = true ];
  // Furious ID used for this request.
  optional int32 furious_id = 6;
  // All information related to split_html.
  optional SplitHtmlInfo split_html_info = 7;
  // All information related to the pcache lookup.
  optional PropertyPageInfo property_page_info = 8;
  // Rewriter information for this request.
  repeated RewriterInfo rewriter_info = 9;
  // Whether the response is HTML.
  optional bool is_html_response = 10 [ default = false ];
  // Whether request URL is disallowed.
  optional bool is_url_disallowed = 11 [ default = false ];
  // RewriteOptions signature hash.
  optional int64 options_signature_hash = 12;
  // Whether request is disabled like ModPagespeed=off.
  optional bool is_request_disabled = 13 [ default = false ];
  // Information about flush subresources filter.
  optional FlushEarlyFlowInfo flush_early_flow_info = 14;
  // Whether the size limit for rewriter_info was exceeded.
  optional bool rewriter_info_size_limit_exceeded = 15 [ default = false ];
}
