code: def is_idle(request_address, mode):
      try:
          kernels = requests.get(
              request_address + "/api/kernels", verify=False
          ).json()
          terminals = requests.get(
              request_address + "/api/terminals", verify=False
          ).json()
          sessions = requests.get(
              request_address + "/api/sessions", verify=False
          ).json()
      except Exception:
          logging.warning(
              "Cannot get notebook kernel status", exc_info=True
          )
          return False
  
      if mode == IdleType.KERNELS_OR_TERMINALS:
          return (
              len(kernels) == 0
              and len(terminals) == 0
              and len(sessions) == 0
          )
      elif mode == IdleType.KERNEL_CONNECTIONS:
          # Unfortunately, the terminals API doesn't return a connection count.
          return all(k["connections"] == 0 for k in kernels)
      elif mode == IdleType.ACTIVITY:
          global last_activity
  
          old_last_activity = last_activity
          if kernels or terminals:
              last_activity = max(
                  x["last_activity"] for x in kernels + terminals
              )
          no_busy_kernels = all(
              k["execution_state"] != "busy" for k in kernels
          )
  
          return no_busy_kernels and (
              last_activity == old_last_activity
          )
  
file_path: determined/master/static/srv/check_idle.py
imports: import enum
  import logging
  import os
  import socket
  import time
  import requests
  from determined.common import api
  from determined.common.api import certs
label_desc: Use image node_id 2 for calling a global function with example usage: is_idle(request_address, mode) and returns: unknown, False, all, unknown