# -*- coding: utf-8 -*-
import re

from ppms.migration.packet import *

VALUERANGES = {'003394': ('e25bdd8435872c57dd15f1cd0829423d', '0d2393c63622b9fb9be79d059f02fc83')}

SUB = lambda code: re.sub('.+','''def computeSqlValueRange(dt_name):
    val = """case
                 when DI001510 NOT BETWEEN -0.0009 AND 0.0009 then 1
              end"""
    return(str(val))''', code, 0, re.DOTALL)

class MemoryAllocationBookedActHours(MigratePythonPacket):
    '''Reduces the amount of memory that is allocated when opening module 009AVM




    This simply replaces a inefficient SQL statement with a more efficient one.
    The packet will fail if the SQL statement was individualized.
    '''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9225
        
    def fix(self, runner):
        for di_nr, (before_hash, after_hash) in VALUERANGES.items():
            if self.migrate_python(before_hash, after_hash, self.CU_TYPE_DATAITEM, di_nr, SUB):
                return self.success()
             
            return self.fail("could not replace the value range")