1193
1193
return [ True, has_errors ]
1195
def _smart_remove_keep_all_( self, snapshots, keep_snapshots, min_date ):
1195
def _smart_remove_keep_all_( self, snapshots, keep_snapshots, min_date, max_date ):
1196
1196
min_id = self.get_snapshot_id( min_date )
1197
max_id = self.get_snapshot_id( max_date )
1198
logger.info( "[smart remove] keep all >= %s" % min_id )
1199
logger.info( "[smart remove] keep all >= %s and < %s" % ( min_id, max_id ) )
1200
1201
for snapshot_id in snapshots:
1201
if snapshot_id >= min_id:
1202
if snapshot_id >= min_id and snapshot_id < max_id:
1202
1203
if snapshot_id not in keep_snapshots:
1203
1204
keep_snapshots.append( snapshot_id )
1205
1206
return keep_snapshots
1207
1208
def _smart_remove_keep_first_( self, snapshots, keep_snapshots, min_date, max_date ):
1208
max_id = self.get_snapshot_id( max_date + datetime.timedelta( days = 1 ) )
1209
1209
min_id = self.get_snapshot_id( min_date )
1210
max_id = self.get_snapshot_id( max_date )
1211
1212
logger.info( "[smart remove] keep first >= %s and < %s" % ( min_id, max_id ) )
1219
1220
return keep_snapshots
1221
def smart_remove( self, now_full = None ):
1222
def inc_month( self, date ):
1228
return datetime.date( y, m, 1 )
1230
def dec_month( self, date ):
1236
return datetime.date( y, m, 1 )
1238
def smart_remove( self, now_full, keep_all, keep_one_per_day, keep_one_per_week, keep_one_per_month ):
1222
1239
snapshots = self.get_snapshots_list()
1223
1240
logger.info( "[smart remove] considered: %s" % snapshots )
1224
1241
if len( snapshots ) <= 1:
1228
1245
if now_full is None:
1229
1246
now_full = datetime.datetime.today()
1231
now = datetime.datetime( now_full.year, now_full.month, now_full.day )
1248
now = now_full.date()
1233
1250
#keep the last snapshot
1234
1251
keep_snapshots = [ snapshots[0] ]
1236
#keep all from today and yesterday
1237
keep_snapshots = self._smart_remove_keep_all_( snapshots, keep_snapshots, now - datetime.timedelta( days = 1 ) )
1240
max_date = now - datetime.timedelta( days = now.weekday() + 1 )
1241
min_date = max_date - datetime.timedelta( days = 6 )
1242
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1245
max_date = max_date - datetime.timedelta( days = 7 )
1246
min_date = min_date - datetime.timedelta( days = 7 )
1247
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1249
#one per month for all months of this year and last year
1252
for month in xrange( 1, now.month ):
1253
#print "month: %s" % month
1254
min_date = datetime.date( now.year, month, 1 )
1255
max_date = datetime.date( now.year, month + 1, 1 ) - datetime.timedelta( days = 1 )
1256
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1259
for month in xrange( 1, 12 ):
1260
#print "month: %s" % month
1261
min_date = datetime.date( now.year - 1, month, 1 )
1262
max_date = datetime.date( now.year - 1, month + 1, 1 ) - datetime.timedelta( days = 1 )
1263
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1265
min_date = datetime.date( now.year - 1, 12, 1 )
1266
max_date = datetime.date( now.year, 1, 1 ) - datetime.timedelta( days = 1 )
1267
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1269
#one per year for all previous years
1270
min_year = int( snapshots[ -1 ][ :4 ] )
1271
for year in xrange( min_year, now.year - 1 ):
1272
#print "year: %s" % year
1273
min_date = datetime.date( year, 1, 1 )
1274
max_date = datetime.date( year + 1, 1, 1 ) - datetime.timedelta( days = 1 )
1275
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, min_date, max_date )
1253
#keep all for the last keep_all days
1255
keep_snapshots = self._smart_remove_keep_all_( snapshots, keep_snapshots, now - datetime.timedelta( days=keep_all-1), now + datetime.timedelta(days=1) )
1257
#keep one per days for the last keep_one_per_day days
1258
if keep_one_per_day > 0:
1260
for i in xrange( 0, keep_one_per_day ):
1261
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d, d + datetime.timedelta(days=1) )
1262
d = d - datetime.timedelta(days=1)
1264
#keep one per week for the last keep_one_per_week weeks
1265
if keep_one_per_week > 0:
1266
d = now - datetime.timedelta( days = now.weekday() + 1 )
1267
for i in xrange( 0, keep_one_per_week ):
1268
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d, d + datetime.timedelta(days=8) )
1269
d = d - datetime.timedelta(days=7)
1271
#keep one per month for the last keep_one_per_month months
1272
if keep_one_per_month > 0:
1273
d1 = datetime.date( now.year, now.month, 1 )
1274
d2 = self.inc_date( d1 )
1275
for i in xrange( 0, keep_one_per_month ):
1276
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, d1, d2 )
1278
d1 = self.dec_month(1)
1280
#keep one per year for all years
1281
first_year = int(snapshots[-1][ : 4])
1282
for i in xrange( first_year, now.year()+1 ):
1283
keep_snapshots = self._smart_remove_keep_first_( snapshots, keep_snapshots, datetime.date(i,1,1), datetime.date(i+1,1,1) )
1277
1285
logger.info( "[smart remove] keep snapshots: %s" % keep_snapshots )