示例#1
0
文件: commands.py 项目: mgear/frepple
  def run(cls, database=DEFAULT_DB_ALIAS, **kwargs):
    import frepple

    if cls.filter:
      filter_and = "and %s " % cls.filter
      filter_where = "where %s " % cls.filter
    else:
      filter_and = ""
      filter_where = ""

    with connections[database].cursor() as cursor:
      cnt = 0
      starttime = time()

      # Preprocessing step
      # Make sure any routing has the produced item of its last step populated in the operation table
      cursor.execute('''
        update operation
        set item_id = t.item_id
        from (
              select operation.name operation_id, min(operationmaterial.item_id) item_id
               from operation
               inner join suboperation s1 on s1.operation_id = operation.name
               inner join operationmaterial on operationmaterial.operation_id = s1.suboperation_id and quantity > 0
               where operation.type = 'routing'
               and not exists
                  (select 1 from suboperation s2 where s1.operation_id = s2.operation_id and s1.priority < s2.priority)
               group by operation.name
               having count(operationmaterial.item_id) = 1
             ) t
        where operation.type = 'routing'
          and operation.name = t.operation_id
        ''')

      # Preprocessing step
      # Make sure any regular operation (i.e. that has no suboperation and is not a suboperation)
      # has its item_id field populated
      # That should cover 90% of the cases
      cursor.execute('''
        update operation
        set item_id = t.item_id
        from (
              select operation.name operation_id, min(operationmaterial.item_id) item_id
              from operation
              inner join operationmaterial on operationmaterial.operation_id = operation.name and quantity > 0
              where not exists
                    (select 1 from suboperation
                    where suboperation.operation_id = operation.name
                          or suboperation.suboperation_id = operation.name)
                and operation.type not in ('routing', 'alternate', 'split')
              group by operation.name
              having count(operationmaterial.item_id) = 1
             ) t
        where operation.type not in ('routing', 'alternate', 'split')
          and t.operation_id = operation.name
        ''')

      # Preprocessing step
      # Operations that are suboperation of a parent operation shouldn't have
      # the item field set. It is the parent operation that should have it set.
      cursor.execute('''
        update operation
        set item_id = null
        from suboperation
        where operation.name = suboperation.suboperation_id
        and operation.item_id is not null
        ''')

    with connections[database].chunked_cursor() as cursor:

      cursor.execute('''
        SELECT
          name, fence, posttime, sizeminimum, sizemultiple, sizemaximum,
          type, duration, duration_per, location_id, cost, search, description,
          category, subcategory, source, item_id, priority, effective_start,
          effective_end, available_id
        FROM operation %s
        ''' % filter_where)
      for i in cursor:
        cnt += 1
        try:
          if not i[6] or i[6] == "fixed_time":
            x = frepple.operation_fixed_time(
              name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
              )
            if i[7]:
              x.duration = i[7].total_seconds()
          elif i[6] == "time_per":
            x = frepple.operation_time_per(
              name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
              )
            if i[7]:
              x.duration = i[7].total_seconds()
            if i[8]:
              x.duration_per = i[8].total_seconds()
          elif i[6] == "alternate":
            x = frepple.operation_alternate(
              name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
              )
          elif i[6] == "split":
            x = frepple.operation_split(
              name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
              )
          elif i[6] == "routing":
            x = frepple.operation_routing(
              name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
              )
          else:
            raise ValueError("Operation type '%s' not recognized" % i[6])
          if i[1]:
            x.fence = i[1].total_seconds()
          if i[2]:
            x.posttime = i[2].total_seconds()
          if i[3] is not None:
            x.size_minimum = i[3]
          if i[4]:
            x.size_multiple = i[4]
          if i[5]:
            x.size_maximum = i[5]
          if i[9]:
            x.location = frepple.location(name=i[9])
          if i[10]:
            x.cost = i[10]
          if i[11]:
            x.search = i[11]
          if i[16]:
            x.item = frepple.item(name=i[16])
          if i[17] is not None:
            x.priority = i[17]
          if i[18]:
            x.effective_start = i[18]
          if i[19]:
            x.effective_end = i[19]
          if i[20]:
            x.available = frepple.calendar(name=i[20])
        except Exception as e:
          logger.error("**** %s ****" % e)
      logger.info('Loaded %d operations in %.2f seconds' % (cnt, time() - starttime))
示例#2
0
 def loadOperations(self):
   print('Importing operations...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     SELECT
       name, fence, posttime, sizeminimum, sizemultiple, sizemaximum,
       type, duration, duration_per, location_id, cost, search, description,
       category, subcategory, source, item_id, priority, effective_start,
       effective_end
     FROM operation %s
     ''' % self.filter_where)
   for i in self.cursor.fetchall():
     cnt += 1
     try:
       if not i[6] or i[6] == "fixed_time":
         x = frepple.operation_fixed_time(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
         if i[7]:
           x.duration = i[7].total_seconds()
       elif i[6] == "time_per":
         x = frepple.operation_time_per(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
         if i[7]:
           x.duration = i[7].total_seconds()
         if i[8]:
           x.duration_per = i[8].total_seconds()
       elif i[6] == "alternate":
         x = frepple.operation_alternate(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       elif i[6] == "split":
         x = frepple.operation_split(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       elif i[6] == "routing":
         x = frepple.operation_routing(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       else:
         raise ValueError("Operation type '%s' not recognized" % i[6])
       if i[1]:
         x.fence = i[1].total_seconds()
       if i[2]:
         x.posttime = i[2].total_seconds()
       if i[3] is not None:
         x.size_minimum = i[3]
       if i[4]:
         x.size_multiple = i[4]
       if i[5]:
         x.size_maximum = i[5]
       if i[9]:
         x.location = frepple.location(name=i[9])
       if i[10]:
         x.cost = i[10]
       if i[11]:
         x.search = i[11]
       if i[16]:
         x.item = frepple.item(name=i[16])
       if i[17] is not None:
         x.priority = i[17]
       if i[18]:
         x.effective_start = i[18]
       if i[19]:
         x.effective_end = i[19]
     except Exception as e:
       print("Error:", e)
   print('Loaded %d operations in %.2f seconds' % (cnt, time() - starttime))
示例#3
0
 def loadOperations(self):
   print('Importing operations...')
   cnt = 0
   starttime = time()
   self.cursor.execute('''
     SELECT
       name, fence, posttime, sizeminimum, sizemultiple, sizemaximum,
       type, duration, duration_per, location_id, cost, search, description,
       category, subcategory, source
     FROM operation %s
     ''' % self.filter_where)
   for i in self.cursor.fetchall():
     cnt += 1
     try:
       if not i[6] or i[6] == "fixed_time":
         x = frepple.operation_fixed_time(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
         if i[7]:
           x.duration = i[7]
       elif i[6] == "time_per":
         x = frepple.operation_time_per(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
         if i[7]:
           x.duration = i[7]
         if i[8]:
           x.duration_per = i[8]
       elif i[6] == "alternate":
         x = frepple.operation_alternate(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       elif i[6] == "split":
         x = frepple.operation_split(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       elif i[6] == "routing":
         x = frepple.operation_routing(
           name=i[0], description=i[12], category=i[13], subcategory=i[14], source=i[15]
           )
       else:
         raise ValueError("Operation type '%s' not recognized" % i[6])
       if i[1]:
         x.fence = i[1]
       if i[2]:
         x.posttime = i[2]
       if i[3] is not None:
         x.size_minimum = i[3]
       if i[4]:
         x.size_multiple = i[4]
       if i[5]:
         x.size_maximum = i[5]
       if i[9]:
         x.location = frepple.location(name=i[9])
       if i[10]:
         x.cost = i[10]
       if i[11]:
         x.search = i[11]
     except Exception as e:
       print("Error:", e)
   print('Loaded %d operations in %.2f seconds' % (cnt, time() - starttime))