Search in sources :

Example 21 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleAuditService method executeCleanupAuditMaxRecords.

private void executeCleanupAuditMaxRecords(int maxRecords) {
    long opHandle = registerOperationStart(OP_CLEANUP_AUDIT_MAX_RECORDS);
    long start = System.currentTimeMillis();
    long deletedCount = 0;
    try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
        logger.info("Audit cleanup, deleting to leave only {} records.", maxRecords);
        QAuditEventRecord qae = QAuditEventRecordMapping.get().defaultAlias();
        Long deleteFromId = jdbcSession.newQuery().select(qae.id).from(qae).orderBy(qae.id.desc()).offset(maxRecords).fetchFirst();
        if (deleteFromId == null) {
            logger.info("Nothing to delete from audit, {} entries allowed.", maxRecords);
            return;
        }
        deletedCount = jdbcSession.newDelete(qae).where(qae.id.loe(deleteFromId)).execute();
        jdbcSession.commit();
    } finally {
        registerOperationFinish(opHandle);
        logger.info("Audit cleanup based on record count finished; deleted {} entries in {} seconds.", deletedCount, (System.currentTimeMillis() - start) / 1000L);
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Example 22 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleAuditService method executeCleanupAuditMaxAge.

private void executeCleanupAuditMaxAge(Duration maxAge) {
    long opHandle = registerOperationStart(OP_CLEANUP_AUDIT_MAX_AGE);
    if (maxAge.getSign() > 0) {
        maxAge = maxAge.negate();
    }
    // This is silly, it mutates the Date, but there seems to be no other way to do it!
    Date minValue = new Date();
    maxAge.addTo(minValue);
    Instant olderThan = Instant.ofEpochMilli(minValue.getTime());
    long start = System.currentTimeMillis();
    long deletedCount = 0;
    try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
        logger.info("Audit cleanup, deleting records older than {}.", olderThan);
        QAuditEventRecord qae = QAuditEventRecordMapping.get().defaultAlias();
        deletedCount = jdbcSession.newDelete(qae).where(qae.timestamp.lt(olderThan)).execute();
        jdbcSession.commit();
    } finally {
        registerOperationFinish(opHandle);
        logger.info("Audit cleanup based on age finished; deleted {} entries in {} seconds.", deletedCount, (System.currentTimeMillis() - start) / 1000L);
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) Instant(java.time.Instant)

Example 23 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleAuditService method executeAudit.

private void executeAudit(AuditEventRecord record) {
    long opHandle = registerOperationStart(OP_AUDIT);
    try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
        // we want DB to assign the ID
        record.setRepoId(null);
        MAuditEventRecord auditRow = insertAuditEventRecord(jdbcSession, record);
        record.setRepoId(auditRow.id);
        insertAuditDeltas(jdbcSession, auditRow);
        insertReferences(jdbcSession, auditRow, record.getReferences());
        jdbcSession.commit();
    } finally {
        registerOperationFinish(opHandle);
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Example 24 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class SqaleAuditService method executeAudit.

private void executeAudit(AuditEventRecordType record) {
    long opHandle = registerOperationStart(OP_AUDIT);
    try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
        // plenty of parameters, but it's better to have a short-lived stateful worker for it
        new AuditInsertion(record, jdbcSession, sqlRepoContext, escapeIllegalCharacters, logger).execute();
        jdbcSession.commit();
    } finally {
        registerOperationFinish(opHandle);
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Example 25 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class CleanupTest method cleanup.

@AfterMethod
public void cleanup() {
    try (JdbcSession jdbcSession = createJdbcSession().startTransaction()) {
        jdbcSession.newDelete(QAuditDeltaMapping.get().defaultAlias()).execute();
        jdbcSession.newDelete(QAuditItemMapping.get().defaultAlias()).execute();
        jdbcSession.newDelete(QAuditPropertyValueMapping.get().defaultAlias()).execute();
        jdbcSession.newDelete(QAuditResourceMapping.get().defaultAlias()).execute();
        jdbcSession.newDelete(QAuditRefValueMapping.get().defaultAlias()).execute();
        jdbcSession.newDelete(QAuditEventRecordMapping.get().defaultAlias()).execute();
        jdbcSession.commit();
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) AfterMethod(org.testng.annotations.AfterMethod)

Aggregations

JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)27 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)4 MExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem)4 QExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 NotNull (org.jetbrains.annotations.NotNull)4 MUri (com.evolveum.midpoint.repo.sqale.qmodel.common.MUri)3 QUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser)3 Test (org.testng.annotations.Test)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)2 ItemName (com.evolveum.midpoint.prism.path.ItemName)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 RepoModifyOptions (com.evolveum.midpoint.repo.api.RepoModifyOptions)2 Jsonb (com.evolveum.midpoint.repo.sqale.jsonb.Jsonb)2 QUri (com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)2 MUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser)2 QUserMapping (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUserMapping)2 Instant (java.time.Instant)2 QName (javax.xml.namespace.QName)2