# Copyright (c) 2015, JINGROW
# License: MIT. See LICENSE

import jingrow
from jingrow.model.page import Page
from jingrow.query_builder import Interval
from jingrow.query_builder.functions import Now


class ErrorLog(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 | None
		method: DF.Data | None
		reference_pagetype: DF.Link | None
		reference_name: DF.Data | None
		seen: DF.Check
		trace_id: DF.Data | None

	# end: auto-generated types
	def onload(self):
		if not self.seen and not jingrow.flags.read_only:
			self.db_set("seen", 1, update_modified=0)
			jingrow.db.commit()

	@staticmethod
	def clear_old_logs(days=30):
		table = jingrow.qb.PageType("Error Log")
		jingrow.db.delete(table, filters=(table.modified < (Now() - Interval(days=days))))


@jingrow.whitelist()
def clear_error_logs():
	"""Flush all Error Logs"""
	jingrow.only_for("System Manager")
	jingrow.db.truncate("Error Log")
