[
  {
    "$project": {
      "_id": 0,
      "s0": "$$ROOT"
    }
  },
  {
    "$match": {
      "$expr": {
        "$eq": [
          {
            "$let": {
              "vars": {
                "a": {
                  "$let": {
                    "vars": {
                      "operand": {
                        "$ne": [
                          {
                            "$ifNull": [
                              "$s0.duration_ms",
                              null
                            ]
                          },
                          null
                        ]
                      }
                    },
                    "in": {
                      "$cond": [
                        {
                          "$eq": [
                            {
                              "$type": "$$operand"
                            },
                            "bool"
                          ]
                        },
                        "$$operand",
                        null
                      ]
                    }
                  }
                },
                "b": {
                  "$let": {
                    "vars": {
                      "operand": {
                        "$ne": [
                          {
                            "$ifNull": [
                              "$s0.release",
                              null
                            ]
                          },
                          null
                        ]
                      }
                    },
                    "in": {
                      "$cond": [
                        {
                          "$eq": [
                            {
                              "$type": "$$operand"
                            },
                            "bool"
                          ]
                        },
                        "$$operand",
                        null
                      ]
                    }
                  }
                }
              },
              "in": {
                "$cond": [
                  {
                    "$in": [
                      false,
                      [
                        "$$a",
                        "$$b"
                      ]
                    ]
                  },
                  false,
                  {
                    "$cond": [
                      {
                        "$in": [
                          null,
                          [
                            "$$a",
                            "$$b"
                          ]
                        ]
                      },
                      null,
                      true
                    ]
                  }
                ]
              }
            }
          },
          true
        ]
      }
    }
  },
  {
    "$set": {
      "win0_k0": {
        "v": {
          "$ifNull": [
            "$s0.duration_ms",
            null
          ]
        }
      }
    }
  },
  {
    "$setWindowFields": {
      "partitionBy": {
        "p0": {
          "$ifNull": [
            "$s0.release",
            null
          ]
        }
      },
      "sortBy": {
        "win0_k0": -1
      },
      "output": {
        "win0_rank": {
          "$rank": {}
        }
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "result": {
        "release_name": {
          "$ifNull": [
            "$s0.release",
            null
          ]
        },
        "run_id": {
          "$ifNull": [
            "$s0._id",
            null
          ]
        },
        "service": {
          "$ifNull": [
            "$s0.service",
            null
          ]
        },
        "duration_ms": {
          "$ifNull": [
            "$s0.duration_ms",
            null
          ]
        },
        "slow_rank": "$win0_rank"
      }
    }
  },
  {
    "$replaceWith": "$result"
  },
  {
    "$project": {
      "_id": 0,
      "s0": "$$ROOT"
    }
  },
  {
    "$match": {
      "$expr": {
        "$eq": [
          {
            "$let": {
              "vars": {
                "a": {
                  "$ifNull": [
                    "$s0.slow_rank",
                    null
                  ]
                },
                "b": {
                  "$literal": 3
                }
              },
              "in": {
                "$cond": [
                  {
                    "$or": [
                      {
                        "$eq": [
                          "$$a",
                          null
                        ]
                      },
                      {
                        "$eq": [
                          "$$b",
                          null
                        ]
                      }
                    ]
                  },
                  null,
                  {
                    "$lte": [
                      "$$a",
                      "$$b"
                    ]
                  }
                ]
              }
            }
          },
          true
        ]
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "result": {
        "release_name": {
          "$ifNull": [
            "$s0.release_name",
            null
          ]
        },
        "slow_rank": {
          "$ifNull": [
            "$s0.slow_rank",
            null
          ]
        },
        "service": {
          "$ifNull": [
            "$s0.service",
            null
          ]
        },
        "duration_ms": {
          "$ifNull": [
            "$s0.duration_ms",
            null
          ]
        },
        "run_id": {
          "$ifNull": [
            "$s0.run_id",
            null
          ]
        }
      },
      "o0": {
        "$ifNull": [
          "$s0.release_name",
          null
        ]
      },
      "o1": {
        "$ifNull": [
          "$s0.slow_rank",
          null
        ]
      },
      "o2": {
        "$ifNull": [
          "$s0.run_id",
          null
        ]
      }
    }
  },
  {
    "$sort": {
      "o0": -1,
      "o1": 1,
      "o2": 1
    }
  },
  {
    "$replaceWith": "$result"
  }
]
