revamped decision logic
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Thu, 10 Dec 2020 01:26:29 +0000 (12:26 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Thu, 10 Dec 2020 01:26:29 +0000 (12:26 +1100)
sss.sh

diff --git a/sss.sh b/sss.sh
index af393fb6b5522494c08e19046f6888a16a5fe454..fbda5db03cfe17bef2206a056316e81da71212cc 100755 (executable)
--- a/sss.sh
+++ b/sss.sh
@@ -31,125 +31,37 @@ MONTHLY="$(date -d '-1 month' '+%Y-%m-%d %H:%M:%S')"
 WEEKLY="$(date -d '-7 days' '+%Y-%m-%d %H:%M:%S')"
 YEARLY="$(date -d '-1 year' '+%Y-%m-%d %H:%M:%S')"
 
-function older_than_a_year() {
-    #echo "--- $CNO $DATE $TIME" >&2
-    [[ ! "$TS" < "$YEARLY" ]] && return 1
-    #echo "# keep the first snapshot of older years: $CNO $DATE $TIME" >&2
-    if [ "$KEEPYY" = "${DATE:0:4}" ] ; then
-       echo "$DEV: discard year duplicate $CNO $DATE $TIME" >&2
-       chcp cp $DEV $CNO
-    else
-       KEEPYY="${DATE:0:4}"
-    fi
-    return 0
-}
-
-function older_than_a_month() {
-    [[ ! "$TS" < "$MONTHLY" ]] && return 1
-    #echo "# keep the first snapshot of older months" >&2
-    if [ "$KEEPMM" = "${DATE:0:7}" ] ; then
-       echo "$DEV: discard month duplicate $CNO $DATE $TIME" >&2
-       chcp cp $DEV $CNO
-    else
-       KEEPMM="${DATE:0:7}"
-    fi
-    return 0
-}
-
-function older_than_a_week() {
-    [[ ! "$TS" < "$WEEKLY" ]] && return 1
-    if [[ "${DATE:8:2}" < "$KEEPDD" ]] ; then
-       #echo "# keep latest when before choice date: $CNO $DATE $TIME" >&2
-       if [ -n "$KEEPWW0" ] ; then
-           echo "$DEV: discard outed $KEEPWW0" >&2
-           chcp cp $DEV $KEEPWW0
-       fi
-       KEEPWW0=( $CNO $DATE $TIME ) 
-    else
-       #echo "# keep first when at or after choice date: $CNO $DATE $TIME" >&2
-       if [ -z "$KEEPWW" ] ; then
-           if [ -n "$KEEPWW0" ] ; then
-               echo "$DEV: discard outed $KEEPWW0" >&2
-               chcp cp $DEV $KEEPWW0
-           fi
-           KEEPWW=( $CNO $DATE $TIME )
-           continue
-       else
-           echo "$DEV: discard monthly extras: $CNO $DATE $TIME" >&2
-           chcp cp $DEV $CNO
-       fi
-    fi
-    return 0
-}
-
-function older_than_a_day() {
-    [[ ! "$TS" < "$DAILY" ]] && return 1
-    if [[ "${TIME:0:2}" < "$KEEPHH" ]] ; then
-       #echo "# keep latest when before choice hour: $CNO $DATE $TIME" >&2
-       if [ -n "$KEPTHH0" ] ; then
-            echo "$DEV: discard outed $KEPTHH0" >&2
-            chcp cp $DEV $KEPTHH0
-        fi
-        KEPTHH0=( $CNO $DATE $TIME )
-    else
-       #echo "# keep first when at/after choice hour: $CNO $DATE $TIME" >&2
-       if [ -z "$KEPTHH" ] ; then
-           if [ -n "$KEPTHH0" ] ; then
-               echo "$DEV: discard outed $KEPTHH0" >&2
-               chcp cp $DEV $KEPTHH0
-               KEPTHH0=
-            fi
-            KEPTHH=( $CNO $DATE $TIME )
-       else
-           echo "$DEV: discard daily extras: $CNO $DATE $TIME" >&2
-           chcp cp $DEV $CNO
-       fi
+MARK=( )
+function marksnap() {
+    if [[ "${MARK[0]}" == "$1" ]] && [[ "${MARK[1]}" = "$2" ]] ; then
+       chcp cp $DEV ${MARKYEAR[2]}
     fi
-}
-
-function older_than_an_hour() {
-    [[ ! "$TS" < "$HOURLY" ]] && return 1
-    #echo "# keep first of each hour: $CNO $DATE $TIME" >&2
-    if [[ "$KEPT55"  != "${TIME:0:2}" ]] ; then
-       KEPT55="${TIME:0:2}"
-    else
-       echo "$DEV: discard hourly duplicate: $CNO $DATE $TIME" >&2
-       chcp cp $DEV $CNO
-    fi
-    return 0
-}
-
-function older_than_5_minutes() {
-    if [[ ! "$TS" < "$FIVELY" ]] ; then
-       echo "$CNO $DATE $TIME is within last five minutes"
-    else
-       : # no noise here
-       if [[ "$TS" < "$NEXT" ]] ; then
-           echo "$DEV: discard 5-minutely duplicate: $CNO $DATE $TIME" >&2
-           chcp cp $DEV $CNO
-       else
-           T=${TS:0:15}$(( ( ${TS:15:1} / 5 ) * 5 )):00
-           NEXT="$(date -d "5 minutes $T" '+%Y-%m-%d %H:%M:%S')"
-           #echo "NEXT=$NEXT" >&2
-       fi
-    fi
-}
+    MARK=( "$1" "$2" $3 )
+fi
 
 { flock 9
 
 date "+$DEV: %Y-%m-%d %H:%M:%S ---- checking" >&2
 lscp -s $DEV | while read CNO DATE TIME REST ; do
     TS="$DATE $TIME"
-    [ "$CNO" = "CNO" ] ||
-       older_than_a_year ||
-       older_than_a_month ||
-       older_than_a_week ||
-       older_than_a_day ||
-       older_than_an_hour ||
-       older_than_5_minutes
+    [ "$CNO" = "CNO" ] && continue
+    if [[ "$TS" > "$YEARLY" ]] ; then
+       marksnap YEAR "${DATE:0:4}" $CNO
+    elif [[ "$TS" > "$MONTHLY" ]]
+        marksnap MONTH "${DATE:5:2}" $CNO
+    elif [[ "$TS" > "$WEEKLY" ]]
+        # For older than most recent week, keep last snapshot each week
+        marksnap WEEK "$(( 7${DATE:8:2} / 7 - 100 ))" $CNO
+    elif [[ "$TS" > "$DAILY" ]]
+        marksnap DAY "${DATE:8:2}" $CNO
+    elif [[ "$TS" > "$HOURLY" ]]
+        marksnap HOUR "${DATE:8:2}-${TIME:0:2}" $CNO
+    else
+       echo "$CNO $DATE $TIME within last hour"
+    fi
 done | if read X ; then
-    :
-elif [[ $(( $(date +%M|sed 's/^0//') % 5 )) = 0 ]] ; then
+    : # snapshot within last hour
+else
     date "+$DEV: new snapshot at %Y-%m-%d %H:%M:%S" >&2
     mkcp -s $DEV
 fi