public XContentBuilder toXContent()

in src/main/java/org/opensearch/ad/model/AnomalyResult.java [517:590]


    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        XContentBuilder xContentBuilder = builder
            .startObject()
            .field(DETECTOR_ID_FIELD, detectorId)
            .field(SCHEMA_VERSION_FIELD, schemaVersion);
        // In normal AD result, we always pass data start/end times. In custom result index,
        // we need to write/delete a dummy AD result to verify if user has write permission
        // to the custom result index. Just pass in null start/end time for this dummy anomaly
        // result to make sure it won't be queried by mistake.
        if (dataStartTime != null) {
            xContentBuilder.field(DATA_START_TIME_FIELD, dataStartTime.toEpochMilli());
        }
        if (dataEndTime != null) {
            xContentBuilder.field(DATA_END_TIME_FIELD, dataEndTime.toEpochMilli());
        }
        if (featureData != null) {
            // can be null during preview
            xContentBuilder.field(FEATURE_DATA_FIELD, featureData.toArray());
        }
        if (executionStartTime != null) {
            // can be null during preview
            xContentBuilder.field(EXECUTION_START_TIME_FIELD, executionStartTime.toEpochMilli());
        }
        if (executionEndTime != null) {
            // can be null during preview
            xContentBuilder.field(EXECUTION_END_TIME_FIELD, executionEndTime.toEpochMilli());
        }
        if (anomalyScore != null && !anomalyScore.isNaN()) {
            xContentBuilder.field(ANOMALY_SCORE_FIELD, anomalyScore);
        }
        if (anomalyGrade != null && !anomalyGrade.isNaN()) {
            xContentBuilder.field(ANOMALY_GRADE_FIELD, anomalyGrade);
        }
        if (confidence != null && !confidence.isNaN()) {
            xContentBuilder.field(CONFIDENCE_FIELD, confidence);
        }
        if (error != null) {
            xContentBuilder.field(ERROR_FIELD, error);
        }
        if (entity != null) {
            xContentBuilder.field(ENTITY_FIELD, entity);
        }
        if (user != null) {
            xContentBuilder.field(USER_FIELD, user);
        }
        if (taskId != null) {
            xContentBuilder.field(TASK_ID_FIELD, taskId);
        }
        if (modelId != null) {
            xContentBuilder.field(MODEL_ID_FIELD, modelId);
        }

        // output extra fields such as attribution and expected only when this is an anomaly
        if (anomalyGrade != null && anomalyGrade > 0) {
            if (approxAnomalyStartTime != null) {
                xContentBuilder.field(APPROX_ANOMALY_START_FIELD, approxAnomalyStartTime.toEpochMilli());
            }
            if (relevantAttribution != null) {
                xContentBuilder.array(RELEVANT_ATTRIBUTION_FIELD, relevantAttribution.toArray());
            }
            if (pastValues != null) {
                xContentBuilder.array(PAST_VALUES_FIELD, pastValues.toArray());
            }

            if (expectedValuesList != null) {
                xContentBuilder.array(EXPECTED_VALUES_FIELD, expectedValuesList.toArray());
            }
        }

        if (threshold != null && !threshold.isNaN()) {
            xContentBuilder.field(THRESHOLD_FIELD, threshold);
        }
        return xContentBuilder.endObject();
    }