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

import influxframework
from influxframework.model.document import Document
from influxframework.query_builder import Interval
from influxframework.query_builder.functions import Now


class ErrorLog(Document):
	def onload(self):
		if not self.seen and not influxframework.flags.read_only:
			self.db_set("seen", 1, update_modified=0)
			influxframework.db.commit()

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


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