load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")

oncall("thrift")

# Exposes the fb303 source tree (incl. the detail/, thrift/ and github/
# sub-packages) so the conda fb303 feedstock can build OSS fb303 from the
# in-fbsource sources via local_source_tree. Mirrors fbsource//xplat/folly:src-tree.
# @fb-only[end= ]: fb_native.export_file(
    # @fb-only[end= ]: name = "src-tree",
    # @fb-only[end= ]: src = ".",
    # @fb-only[end= ]: mode = "reference",
    # @fb-only[end= ]: visibility = ["PUBLIC"],
# @fb-only[end= ]: )

cpp_library(
    name = "base_service",
    srcs = [
        "BaseService.cpp",
    ],
    headers = [
        "BaseService.h",
        "LimitUtils.h",
    ],
    deps = [
        "//thrift/lib/cpp2:flags",
    ],
    exported_deps = [
        "fbsource//xplat/folly:small_vector",
        "fbsource//xplat/folly/executors:cpu_thread_pool_executor",
        ":service_data",
        "//fb303/thrift:fb303_core-cpp2-services",
        "//thrift/lib/cpp2/server:cpp2_conn_context",
    ],
)

cpp_library(
    name = "callback_values_map",
    headers = [
        "CallbackValuesMap.h",
        "CallbackValuesMap-inl.h",
    ],
    modular_headers = True,
    exported_deps = [
        "fbsource//third-party/glog:glog",
        "fbsource//xplat/folly:chrono",
        "fbsource//xplat/folly:map_util",
        "fbsource//xplat/folly:range",
        "fbsource//xplat/folly:synchronized",
        "fbsource//xplat/folly/algorithm:binary_heap",
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/container:regex_match_cache",
        "fbsource//xplat/folly/container:reserve",
        "fbsource//xplat/folly/functional:invoke",
        "fbsource//xplat/folly/hash:hash",
        "fbsource//xplat/folly/synchronization:relaxed_atomic",
        "//fb303/detail:regex_util",
    ],
)

cpp_library(
    name = "dynamic_counters",
    headers = ["DynamicCounters.h"],
    modular_headers = True,
    exported_deps = [
        ":callback_values_map",
    ],
)

cpp_library(
    name = "export_type",
    srcs = ["ExportType.cpp"],
    headers = ["ExportType.h"],
    modular_headers = True,
    exported_deps = ["fbsource//xplat/folly/container:array"],
)

cpp_library(
    name = "exported_stat_map",
    srcs = ["ExportedStatMap.cpp"],
    headers = ["ExportedStatMap.h"],
    modular_headers = True,
    deps = [
        ":timeseries_exporter",
    ],
    exported_deps = [
        "fbsource//xplat/folly:synchronized",
        "fbsource//xplat/folly/container:f14_hash",
        ":export_type",
        ":timeseries",
    ],
)

cpp_library(
    name = "exported_stat_map_impl",
    srcs = ["ExportedStatMapImpl.cpp"],
    headers = ["ExportedStatMapImpl.h"],
    modular_headers = True,
    deps = [
        ":timeseries_exporter",
    ],
    exported_deps = [
        ":exported_stat_map",
    ],
)

cpp_library(
    name = "function_stat_handler",
    srcs = ["TFunctionStatHandler.cpp"],
    headers = ["TFunctionStatHandler.h"],
    deps = [
        ":legacy_clock",
        ":service_data",
    ],
    exported_deps = [
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/executors:function_scheduler",
        ":base_service",
        "//thrift/lib/cpp:event_handler_base",
    ],
)

cpp_library(
    name = "legacy_clock",
    srcs = ["LegacyClock.cpp"],
    headers = ["LegacyClock.h"],
    modular_headers = True,
    deps = [
        "fbsource//xplat/folly/lang:exception",
    ],
)

cpp_library(
    name = "logging",
    srcs = [
        "FollyLoggingHandler.cpp",
    ],
    headers = [
        "FollyLoggingHandler.h",
    ],
    modular_headers = True,
    deps = [
        "fbsource//xplat/folly/json:dynamic",
        "fbsource//xplat/folly/logging:init",
        "fbsource//xplat/folly/logging:logging",
        ":service_data",
    ],
    exported_deps = [
        "fbsource//xplat/folly:range",
    ],
)

cpp_library(
    name = "histogram_exporter",
    srcs = [
        "ExportedHistogramMap.cpp",
        "ExportedHistogramMapImpl.cpp",
        "HistogramExporter.cpp",
    ],
    headers = [
        "ExportedHistogramMap.h",
        "ExportedHistogramMapImpl.h",
        "HistogramExporter.h",
    ],
    modular_headers = True,
    deps = [
        "fbsource//third-party/fmt:fmt",
        "fbsource//third-party/glog:glog",
        "fbsource//xplat/folly:format",
        ":legacy_clock",
        ":timeseries_exporter",
    ],
    exported_deps = [
        "fbsource//xplat/folly:function",
        "fbsource//xplat/folly:map_util",
        "fbsource//xplat/folly:small_vector",
        "fbsource//xplat/folly:synchronized",
        "fbsource//xplat/folly/container:f14_hash",
        ":dynamic_counters",
        ":export_type",
        ":exported_stat_map_impl",
        ":timeseries_histogram",
    ],
)

cpp_library(
    name = "service_data",
    srcs = ["ServiceData.cpp"],
    headers = ["ServiceData.h"],
    modular_headers = True,
    deps = [
        "fbsource//xplat/folly:conv",
        "fbsource//xplat/folly:indestructible",
        "fbsource//xplat/folly:map_util",
        "fbsource//xplat/folly:string",
        "fbsource//xplat/folly/container:reserve",
        "//fb303/detail:regex_util",
    ],
    exported_deps = [
        "fbsource//xplat/folly:chrono",
        "fbsource//xplat/folly:optional",
        "fbsource//xplat/folly:range",
        "fbsource//xplat/folly:synchronized",
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/container:regex_match_cache",
        "fbsource//xplat/folly/synchronization:relaxed_atomic",
        ":dynamic_counters",
        ":exported_stat_map_impl",
        ":histogram_exporter",
        ":legacy_clock",
        "//fb303/detail:quantile_stat_map",
    ],
    external_deps = [
        ("boost", None, "boost_regex"),
        "gflags",
    ],
)

cpp_library(
    name = "simple_lru_map",
    headers = ["SimpleLRUMap.h"],
    modular_headers = True,
    exported_deps = ["fbsource//third-party/glog:glog"],
)

cpp_library(
    name = "thread_cached_service_data",
    srcs = ["ThreadCachedServiceData.cpp"],
    headers = ["ThreadCachedServiceData.h"],
    modular_headers = True,
    deps = [
        "fbsource//xplat/folly:indestructible",
        "fbsource//xplat/folly:memory",
        "fbsource//xplat/folly:singleton",
    ],
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        "fbsource//xplat/folly:concurrent_lazy",
        "fbsource//xplat/folly:cpp_attributes",
        "fbsource//xplat/folly:format",
        "fbsource//xplat/folly:map_util",
        "fbsource//xplat/folly:overload",
        "fbsource//xplat/folly:range",
        "fbsource//xplat/folly:synchronized",
        "fbsource//xplat/folly:thread_local",
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/executors:function_scheduler",
        "fbsource//xplat/folly/hash:rapidhash",
        "fbsource//xplat/folly/lang:align",
        "fbsource//xplat/folly/synchronization:call_once",
        ":export_type",
        ":simple_lru_map",
        ":thread_local_stats_map",
    ],
)

cpp_library(
    name = "thread_local_stats",
    srcs = ["ThreadLocalStats.cpp"],
    headers = [
        "TLStatsLockTraits.h",
        "ThreadLocalStats.h",
        "ThreadLocalStats-inl.h",
    ],
    modular_headers = True,
    deps = ["fbsource//xplat/folly/system:hardware_concurrency"],
    exported_deps = [
        "fbsource//third-party/glog:glog",
        "fbsource//xplat/folly:conv",
        "fbsource//xplat/folly:cpp_attributes",
        "fbsource//xplat/folly:portability",
        "fbsource//xplat/folly:range",
        "fbsource//xplat/folly:shared_mutex",
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/portability:asm",
        "fbsource//xplat/folly/stats:histogram",
        "fbsource//xplat/folly/synchronization:atomic_util",
        "fbsource//xplat/folly/synchronization:distributed_mutex",
        "fbsource//xplat/folly/synchronization:relaxed_atomic",
        ":export_type",
        ":exported_stat_map_impl",
        ":histogram_exporter",
        ":legacy_clock",
        ":service_data",
        ":timeseries_exporter",
    ],
    external_deps = [
        "gflags",
    ],
)

cpp_library(
    name = "thread_local_stats_map",
    srcs = [
        "ThreadLocalStatsMap.cpp",
    ],
    headers = [
        "ThreadLocalStatsMap.h",
        "ThreadLocalStatsMap-inl.h",
    ],
    modular_headers = True,
    exported_deps = [
        "fbsource//xplat/folly:range",
        "fbsource//xplat/folly:thread_local",
        "fbsource//xplat/folly/container:f14_hash",
        "fbsource//xplat/folly/hash:hash",
        "fbsource//xplat/folly/memory:sanitize_leak",
        ":thread_local_stats",
    ],
)

cpp_library(
    name = "timeseries",
    srcs = ["Timeseries.cpp"],
    headers = [
        "Timeseries.h",
    ],
    modular_headers = True,
    exported_deps = [
        "fbsource//xplat/folly/stats:bucketed_time_series",
        "fbsource//xplat/folly/stats:multi_level_time_series",
    ],
)

cpp_library(
    name = "timeseries_exporter",
    srcs = ["TimeseriesExporter.cpp"],
    headers = ["TimeseriesExporter.h"],
    modular_headers = True,
    deps = [
        "fbsource//third-party/glog:glog",
        "fbsource//xplat/folly:small_vector",
        ":dynamic_counters",
        ":legacy_clock",
    ],
    exported_deps = [
        "fbsource//xplat/folly:synchronized",
        ":export_type",
        ":timeseries",
    ],
)

cpp_library(
    name = "timeseries_histogram",
    headers = [
        "TimeseriesHistogram.h",
        "TimeseriesHistogram-inl.h",
    ],
    modular_headers = True,
    exported_deps = [
        "fbsource//xplat/folly/stats:histogram",
        "fbsource//xplat/folly/stats:timeseries_histogram",
        "//fb303:timeseries",
    ],
)

cpp_library(
    name = "tl_stats_async_aggregator",
    srcs = ["TLStatsAsyncAggregator.cpp"],
    headers = ["TLStatsAsyncAggregator.h"],
    modular_headers = True,
    exported_deps = [
        "fbsource//xplat/folly/io/async:async_base",
        ":thread_local_stats",
    ],
)

cpp_library(
    name = "quantile_stat",
    srcs = [
        "QuantileStat.cpp",
    ],
    headers = [
        "QuantileStat.h",
        "QuantileStat-inl.h",
    ],
    modular_headers = True,
    exported_deps = [
        "fbsource//xplat/folly/stats:quantile_estimator",
        "fbsource//xplat/folly/stats:tdigest",
    ],
)
