1 /***********************************************************************
2  * Copyright (c) 2013-2024 Commonwealth Computer Research, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Apache License, Version 2.0
5  * which accompanies this distribution and is available at
6  * http://www.opensource.org/licenses/apache2.0.php.
7  ***********************************************************************/
8 
9 package org.locationtech.geomesa.gt.partition.postgis.dialect
10 package functions
11 
12 import org.locationtech.geomesa.gt.partition.postgis.dialect.procedures.{AnalyzePartitions, PartitionMaintenance, RollWriteAheadLog}
13 
14 /**
15  * Deletes history older than 7 days
16  */
17 object LogCleaner extends LogCleaner with AdvisoryLock {
18   override protected val lockId: Long = 6743164310736814350L
19 }
20 
21 class LogCleaner extends SqlFunction with CronSchedule {
22 
23   override def name(info: TypeInfo): FunctionName = FunctionName("cron_log_cleaner")
24 
25   override def jobName(info: TypeInfo): SqlLiteral = SqlLiteral(s"${info.typeName}-cron-log-cleaner")
26 
27   override protected def createStatements(info: TypeInfo): Seq[String] =
28     Seq(function()) ++ super.createStatements(info)
29 
30   override protected def dropStatements(info: TypeInfo): Seq[String] = Seq.empty // function is shared between types
31 
32   override protected def schedule(info: TypeInfo): SqlLiteral = SqlLiteral("30 1 * * *") // 01:30 every day
33 
34   override protected def invocation(info: TypeInfo): SqlLiteral =
35     SqlLiteral(s"SELECT cron_log_cleaner(${info.tables.view.name.asLiteral}, INTERVAL '7 DAYS')")
36 
37   private def function(): String = {
38     val info = TypeInfo(SchemaName("", ""), "", null, null, null)
39     val maintenanceSuffix = PartitionMaintenance.jobName(info).quoted
40     val rollSuffix = RollWriteAheadLog.jobName(info).quoted
41     val analyzeSuffix = AnalyzePartitions.jobName(info).quoted
42     val logsSuffix = jobName(info).quoted
43 
44     s"""CREATE OR REPLACE FUNCTION cron_log_cleaner(name text, tokeep interval) RETURNS void LANGUAGE plpgsql AS
45        |  $$BODY$$
46        |    DECLARE
47        |      maintenance_name text;
48        |      roll_name text;
49        |      analyze_name text;
50        |      logs_name text;
51        |    BEGIN
52        |      maintenance_name := name || $maintenanceSuffix;
53        |      roll_name := name || $rollSuffix;
54        |      analyze_name := name || $analyzeSuffix;
55        |      logs_name := name || $logsSuffix;
56        |      DELETE FROM cron.job_run_details
57        |        WHERE end_time < now() - tokeep
58        |        AND jobid IN (
59        |          SELECT jobid FROM cron.job
60        |            WHERE jobname IN (maintenance_name, roll_name, analyze_name, logs_name)
61        |        );
62        |    END;
63        |  $$BODY$$;""".stripMargin
64   }
65 }
Line Stmt Id Pos Tree Symbol Tests Code
18 86538 822 - 842 Literal <nosymbol> 6743164310736814350L
23 86539 956 - 988 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.FunctionName.apply dialect.this.`package`.FunctionName.apply("cron_log_cleaner")
25 86541 1072 - 1090 Literal <nosymbol> "-cron-log-cleaner"
25 86540 1056 - 1057 Literal <nosymbol> ""
25 86543 1054 - 1090 Apply scala.StringContext.s scala.StringContext.apply("", "-cron-log-cleaner").s(info.typeName)
25 86542 1058 - 1071 Select org.locationtech.geomesa.gt.partition.postgis.dialect.TypeInfo.typeName info.typeName
25 86544 1043 - 1091 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.apply dialect.this.`package`.SqlLiteral.apply(scala.StringContext.apply("", "-cron-log-cleaner").s(info.typeName))
28 86545 1174 - 1184 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.functions.LogCleaner.function LogCleaner.this.function()
28 86547 1186 - 1186 TypeApply scala.collection.Seq.canBuildFrom collection.this.Seq.canBuildFrom[String]
28 86546 1189 - 1217 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.CronSchedule.createStatements LogCleaner.super.createStatements(info)
28 86548 1170 - 1217 ApplyToImplicitArgs scala.collection.TraversableLike.++ scala.collection.Seq.apply[String](LogCleaner.this.function()).++[String, Seq[String]](LogCleaner.super.createStatements(info))(collection.this.Seq.canBuildFrom[String])
30 86549 1290 - 1299 TypeApply scala.collection.generic.GenericCompanion.empty scala.collection.Seq.empty[Nothing]
32 86550 1401 - 1425 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.apply dialect.this.`package`.SqlLiteral.apply("30 1 * * *")
35 86551 1529 - 1554 Literal <nosymbol> "SELECT cron_log_cleaner("
35 86553 1555 - 1586 Select org.locationtech.geomesa.gt.partition.postgis.dialect.SqlIdentifier.asLiteral info.tables.view.name.asLiteral
35 86552 1587 - 1608 Literal <nosymbol> ", INTERVAL \'7 DAYS\')"
35 86555 1516 - 1609 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.apply dialect.this.`package`.SqlLiteral.apply(scala.StringContext.apply("SELECT cron_log_cleaner(", ", INTERVAL \'7 DAYS\')").s(info.tables.view.name.asLiteral))
35 86554 1527 - 1608 Apply scala.StringContext.s scala.StringContext.apply("SELECT cron_log_cleaner(", ", INTERVAL \'7 DAYS\')").s(info.tables.view.name.asLiteral)
38 86557 1692 - 1694 Literal <nosymbol> ""
38 86556 1672 - 1690 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.SchemaName.apply dialect.this.`package`.SchemaName.apply("", "")
38 86559 1702 - 1706 Literal <nosymbol> null
38 86558 1696 - 1700 Literal <nosymbol> null
38 86561 1663 - 1713 Apply org.locationtech.geomesa.gt.partition.postgis.dialect.TypeInfo.apply dialect.this.`package`.TypeInfo.apply(dialect.this.`package`.SchemaName.apply("", ""), "", null, null, null, dialect.this.`package`.TypeInfo.apply$default$6)
38 86560 1708 - 1712 Literal <nosymbol> null
39 86562 1742 - 1783 Select org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.quoted org.locationtech.geomesa.gt.partition.postgis.dialect.procedures.PartitionMaintenance.jobName(info).quoted
40 86563 1805 - 1843 Select org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.quoted org.locationtech.geomesa.gt.partition.postgis.dialect.procedures.RollWriteAheadLog.jobName(info).quoted
41 86564 1868 - 1906 Select org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.quoted org.locationtech.geomesa.gt.partition.postgis.dialect.procedures.AnalyzePartitions.jobName(info).quoted
42 86565 1928 - 1948 Select org.locationtech.geomesa.gt.partition.postgis.dialect.SqlLiteral.quoted LogCleaner.this.jobName(info).quoted
44 86566 1954 - 2783 Apply scala.StringContext.s scala.StringContext.apply("CREATE OR REPLACE FUNCTION cron_log_cleaner(name text, tokeep interval) RETURNS void LANGUAGE plpgsql AS\n | $BODY$\n | DECLARE\n | maintenance_name text;\n | roll_name text;\n | analyze_name text;\n | logs_name text;\n | BEGIN\n | maintenance_name := name || ", ";\n | roll_name := name || ", ";\n | analyze_name := name || ", ";\n | logs_name := name || ", ";\n | DELETE FROM cron.job_run_details\n | WHERE end_time < now() - tokeep\n | AND jobid IN (\n | SELECT jobid FROM cron.job\n | WHERE jobname IN (maintenance_name, roll_name, analyze_name, logs_name)\n | );\n | END;\n | $BODY$;").s(maintenanceSuffix, rollSuffix, analyzeSuffix, logsSuffix)
63 86567 1954 - 2795 Select scala.collection.immutable.StringLike.stripMargin scala.Predef.augmentString(scala.StringContext.apply("CREATE OR REPLACE FUNCTION cron_log_cleaner(name text, tokeep interval) RETURNS void LANGUAGE plpgsql AS\n | $BODY$\n | DECLARE\n | maintenance_name text;\n | roll_name text;\n | analyze_name text;\n | logs_name text;\n | BEGIN\n | maintenance_name := name || ", ";\n | roll_name := name || ", ";\n | analyze_name := name || ", ";\n | logs_name := name || ", ";\n | DELETE FROM cron.job_run_details\n | WHERE end_time < now() - tokeep\n | AND jobid IN (\n | SELECT jobid FROM cron.job\n | WHERE jobname IN (maintenance_name, roll_name, analyze_name, logs_name)\n | );\n | END;\n | $BODY$;").s(maintenanceSuffix, rollSuffix, analyzeSuffix, logsSuffix)).stripMargin