# Copyright (c) 2024, JINGROW
# For license information, please see license.txt

import jingrow
import requests
from jingrow.model.page import Page

from jcloud.agent import Agent
from jcloud.utils import log_error


class AgentRequestFailure(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		error: DF.Code
		failure_count: DF.Int
		server: DF.DynamicLink
		server_type: DF.Link
		traceback: DF.Code
	# end: auto-generated types

	pass

	def before_insert(self):
		# TODO: Remove once tests pass
		if jingrow.flags.in_test:
			print(jingrow.get_traceback(with_context=True))


def is_server_archived(failure):
	# Server was archived more than an hour ago
	server = jingrow.db.get_value(failure.server_type, failure.server, ["status", "modified"], as_dict=True)
	if (server.status == "Archived") and (server.modified < jingrow.utils.add_to_date(None, hours=-1)):
		return True
	return False


def remove_old_failures():
	failures = jingrow.get_all(
		"Agent Request Failure",
		["name", "server_type", "server", "failure_count"],
		order_by="creation ASC",
	)
	for failure in failures:
		delta = 0
		try:
			agent = Agent(failure.server, failure.server_type)
			agent.raw_request("GET", "ping", raises=True, timeout=(1, 5))
		except (requests.ConnectTimeout, requests.ReadTimeout, requests.ConnectionError):
			# Server is still down, either because
			# 1. Couldn't connect
			# 2. Couldn't respond in time,
			# increment the failure count
			delta = 1
		except requests.RequestException:
			# Something still wrong with the connection, ignore for now.
			pass
		except Exception:
			# Something weird happened, probably not related to requests
			log_error("Agent Status Check Failure", failure=failure)
		else:
			# Server responded, aggressively decrement the failure count
			# Aggressively
			delta = -100

		if delta:
			if failure.failure_count + delta <= 0 or is_server_archived(failure):
				jingrow.delete_pg("Agent Request Failure", failure.name)
			else:
				jingrow.db.set_value(
					"Agent Request Failure",
					failure.name,
					"failure_count",
					failure.failure_count + delta,
				)
