#!/bin/sh -e

## hb -e - Enables checking of all commands. 
##         If a command exits with an error and the caller does not check such error, the script aborts immediately

## hb. /etc/spdirs

GraceDays=14
GraceDays=$(/opt/allot/bin/psql-query -c "select public.get_int_param('Purge','capture_history')")
/opt/allot/bin/psql-exec <<EOF
BEGIN;

-- Chop bodies off old 'traffic-sample' and 'flow-capture' documents.
-- Does not alter documents which belong to a flood or capture which
-- has 'high' importance.
--
-- NB: The temporary table to save the list of entity_id's to expire
--     MUST be used.
--
--     There is a rule on entity_data which alters 'modified' to now.
--     This rule is executed before the UPDATE, which then causes the
--     'modified' condition to match none of the desired rows.
--
-- NB: This could be rewritten to use an intermediate temporary table
--     with the results of the initial pass over fast.entities.
--     Then a merge join with fast.entity_data could be used.
--
--     However during testing there was some skew in the test data set
--     which meant the entity_id's spanned a very large range, causing
--     the merge join to perform badly.

     SELECT entity_id
       INTO TEMP TABLE to_expire
       FROM fast.entities         e
       JOIN fast.entity_data      ed USING (entity_id)
       JOIN fast.types            t  USING (type_id)
  LEFT JOIN flood.samples         fs ON (fs.sample_id = e.entity_id)
  LEFT JOIN flood.importance      fi USING (flood_id)
  LEFT JOIN quarantine.samples    qs ON (qs.sample_id = e.entity_id)
  LEFT JOIN quarantine.captures   qc ON (qc.fast_id = e.entity_id)
  LEFT JOIN quarantine.importance qi ON (qi.capture_id IN (qs.capture_id, qc.capture_id))
      WHERE e.modified BETWEEN CURRENT_DATE - ($GraceDays+3) AND CURRENT_DATE - ($GraceDays)
        AND t.name   IN ('traffic-sample', 'flow-capture')
        AND ed.error IS NULL
        AND ed.body  IS NOT NULL
        AND (fi.importance IS NULL OR fi.importance <= 2)
        AND (qi.importance IS NULL OR qi.importance <= 2)
;

UPDATE fast.entity_data
   SET body = NULL, expired = TRUE
  FROM to_expire
 WHERE fast.entity_data.entity_id = to_expire.entity_id
;

COMMIT;
EOF
