// Timeseries cross product query with apm_metrics data source and span_kind
// returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_metrics::MetricsAPI;
use datadog_api_client::datadogV2::model::ApmMetricsDataSource;
use datadog_api_client::datadogV2::model::ApmMetricsQuery;
use datadog_api_client::datadogV2::model::ApmMetricsSpanKind;
use datadog_api_client::datadogV2::model::ApmMetricsStat;
use datadog_api_client::datadogV2::model::FormulaLimit;
use datadog_api_client::datadogV2::model::QueryFormula;
use datadog_api_client::datadogV2::model::QuerySortOrder;
use datadog_api_client::datadogV2::model::TimeseriesFormulaQueryRequest;
use datadog_api_client::datadogV2::model::TimeseriesFormulaRequest;
use datadog_api_client::datadogV2::model::TimeseriesFormulaRequestAttributes;
use datadog_api_client::datadogV2::model::TimeseriesFormulaRequestType;
use datadog_api_client::datadogV2::model::TimeseriesQuery;

#[tokio::main]
async fn main() {
    let body = TimeseriesFormulaQueryRequest::new(TimeseriesFormulaRequest::new(
        TimeseriesFormulaRequestAttributes::new(
            1636625471000,
            vec![TimeseriesQuery::ApmMetricsQuery(Box::new(
                ApmMetricsQuery::new(
                    ApmMetricsDataSource::APM_METRICS,
                    "a".to_string(),
                    ApmMetricsStat::HITS,
                )
                .group_by(vec!["resource_name".to_string()])
                .query_filter("env:prod".to_string())
                .service("web-store".to_string())
                .span_kind(ApmMetricsSpanKind::SERVER),
            ))],
            1636629071000,
        )
        .formulas(vec![QueryFormula::new("a".to_string())
            .limit(FormulaLimit::new().count(10).order(QuerySortOrder::DESC))])
        .interval(5000),
        TimeseriesFormulaRequestType::TIMESERIES_REQUEST,
    ));
    let configuration = datadog::Configuration::new();
    let api = MetricsAPI::with_config(configuration);
    let resp = api.query_timeseries_data(body).await;
    if let Ok(value) = resp {
        println!("{:#?}", value);
    } else {
        println!("{:#?}", resp.unwrap_err());
    }
}
