def exportForecastDemands(cursor): # Detect whether the forecast module is available if not 'demand_forecast' in [ a[0] for a in inspect.getmembers(frepple) ]: return print("Exporting forecast demands...") starttime = time() cursor.execute("SELECT forecast_id, startdate, enddate FROM forecastdemand") primary_keys = set([ i for i in cursor.fetchall() ]) cursor.executemany( '''insert into forecastdemand (forecast_id,startdate,enddate,quantity,lastmodified) values(%s,%s,%s,%s,%s)''', [( i.owner.name, str(i.startdate.date()), str(i.enddate.date()), round(i.total,settings.DECIMAL_PLACES), timestamp ) for i in frepple.demands() if isinstance(i,frepple.demand_forecastbucket) and (i.owner.name,i.startdate.date(),i.enddate.date()) not in primary_keys ]) cursor.executemany( '''update forecastdemand set quantity=%s, lastmodified=%s where forecast_id=%s and startdate=%s and enddate=%s''', [( round(i.total,settings.DECIMAL_PLACES), timestamp, i.owner.name, str(i.startdate.date()), str(i.enddate.date()), ) for i in frepple.demands() if isinstance(i,frepple.demand_forecastbucket) and (i.owner.name,i.startdate.date(),i.enddate.date()) in primary_keys ]) transaction.commit(using=database) print('Exported forecast demands in %.2f seconds' % (time() - starttime))
def exportForecasts(cursor): # Detect whether the forecast module is available if not 'demand_forecast' in [ a[0] for a in inspect.getmembers(frepple) ]: return print("Exporting forecast...") starttime = time() cursor.execute("SELECT name FROM forecast") primary_keys = set([ i[0] for i in cursor.fetchall() ]) cursor.executemany( '''insert into forecast (name,customer_id,item_id,priority,operation_id,minshipment, calendar_id,discrete,maxlateness,category,subcategory,lastmodified) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', [( i.name, i.customer and i.customer.name or None, i.item.name, i.priority, i.operation and i.operation.name or None, round(i.minshipment,settings.DECIMAL_PLACES), i.calendar.name, i.discrete, round(i.maxlateness,settings.DECIMAL_PLACES), i.category, i.subcategory, timestamp ) for i in frepple.demands() if i.name not in primary_keys and isinstance(i,frepple.demand_forecast) ]) cursor.executemany( '''update forecast set customer_id=%s, item_id=%s, priority=%s, operation_id=%s, minshipment=%s, calendar_id=%s, discrete=%s,maxlateness=%s, category=%s, subcategory=%s, lastmodified=%s where name=%s''', [( i.customer and i.customer.name or None, i.item.name, i.priority, i.operation and i.operation.name or None, round(i.minshipment,settings.DECIMAL_PLACES), i.calendar.name, i.discrete, round(i.maxlateness,settings.DECIMAL_PLACES), i.category, i.subcategory, timestamp, i.name, ) for i in frepple.demands() if i.name in primary_keys and isinstance(i,frepple.demand_forecast) ]) transaction.commit(using=database) print('Exported forecasts in %.2f seconds' % (time() - starttime))
def exportDemands(self, cursor): with transaction.atomic(using=self.database, savepoint=False): print("Exporting demands...") starttime = time() cursor.execute("SELECT name FROM demand") primary_keys = set([ i[0] for i in cursor.fetchall() ]) cursor.executemany( "insert into demand \ (name,due,quantity,priority,item_id,location_id,operation_id,customer_id, \ minshipment,maxlateness,category,subcategory,source,lastmodified,status) \ values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s * interval '1 second',%s,%s,%s,%s,%s)", [ ( i.name, str(i.due), round(i.quantity, 6), i.priority, i.item.name, i.location.name if i.location else None, i.operation.name if i.operation and not i.operation.hidden else None, i.customer.name if i.customer else None, round(i.minshipment, 6), i.maxlateness, i.category, i.subcategory, i.source, self.timestamp, i.status ) for i in frepple.demands() if i.name not in primary_keys and isinstance(i, frepple.demand_default) and not i.hidden and (not self.source or self.source == i.source) ]) cursor.executemany( "update demand \ set due=%s, quantity=%s, priority=%s, item_id=%s, location_id=%s, \ operation_id=%s, customer_id=%s, minshipment=%s, maxlateness=%s * interval '1 second', \ category=%s, subcategory=%s, source=%s, lastmodified=%s, status=%s \ where name=%s", [ ( str(i.due), round(i.quantity, 6), i.priority, i.item.name, i.location.name if i.location else None, i.operation.name if i.operation and not i.operation.hidden else None, i.customer.name if i.customer else None, round(i.minshipment, 6), i.maxlateness, i.category, i.subcategory, i.source, self.timestamp, i.status, i.name ) for i in frepple.demands() if i.name in primary_keys and isinstance(i, frepple.demand_default) and not i.hidden and (not self.source or self.source == i.source) ]) cursor.executemany( "update demand set owner_id=%s where name=%s", [ (i.owner.name, i.name) for i in frepple.demands() if i.owner and isinstance(i, frepple.demand_default) and (not self.source or self.source == i.source) ]) print('Exported demands in %.2f seconds' % (time() - starttime))
def exportConstraints(self): if self.verbosity: logger.info("Exporting constraints...") starttime = time() cursor = connections[self.database].cursor() with tempfile.TemporaryFile(mode="w+t", encoding='utf-8') as tmp: for d in frepple.demands(): if self.cluster != -1 and self.cluster != d.cluster: continue for i in d.constraints: print(("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % ( d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and i.owner.operation.name or i.owner.name, i.description, str(i.start), str(i.end), round(i.weight, 8) )),file=tmp) tmp.seek(0) cursor.copy_from( tmp, 'out_constraint', columns=('demand','entity','name','owner','description','startdate','enddate','weight') ) tmp.close() if self.verbosity: logger.info('Exported constraints in %.2f seconds' % (time() - starttime))
def exportPegging(cursor): print("Exporting pegging...") starttime = time() cnt = 0 for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging cursor.executemany( "insert into out_demandpegging \ (demand,depth,cons_operationplan,cons_date,prod_operationplan,prod_date, \ buffer,item,quantity_demand,quantity_buffer) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [( n, str(j.level), j.consuming and j.consuming.id or '0', str(j.consuming_date), j.producing and j.producing.id or '0', str(j.producing_date), j.buffer and j.buffer.name or '', (j.buffer and j.buffer.item and j.buffer.item.name) or '', round(j.quantity_demand, settings.DECIMAL_PLACES), round(j.quantity_buffer, settings.DECIMAL_PLACES) ) for j in i.pegging] ) cnt += 1 if cnt % 500 == 0: transaction.commit(using=database) transaction.commit(using=database) cursor.execute("select count(*) from out_demandpegging") print('Exported %d pegging in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def exportConstraints(): print("Exporting constraints...") starttime = time() writer = csv.writer( open("constraints.csv", "w", newline="", encoding="utf-8"), quoting=csv.QUOTE_ALL, ) writer.writerow(( "#demand", "entity", "name", "owner", "description", "start date", "end date", "weight", )) for d in frepple.demands(): for i in d.constraints: writer.writerow(( d.name, i.entity, i.name, i.owner.name, i.description, i.start, i.end, i.weight, )) print("Exported constraints in %.2f seconds" % (time() - starttime))
def exportDemand(): def deliveries(d): cumplanned = 0 n = d while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield (n.encode(encoding,"ignore"), d.item.name.encode(encoding,"ignore"), d.customer and d.customer.name.encode(encoding,"ignore") or None, d.due, cur, i.end, i.quantity, i.id) # Extra record if planned short if cumplanned < d.quantity: yield (n.encode(encoding,"ignore"), d.item.name.encode(encoding,"ignore"), d.customer and d.customer.name.encode(encoding,"ignore") or None, d.due, d.quantity - cumplanned, None, None, None) print("Exporting demand plans...") starttime = time() writer = csv.writer(open("demands.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow(('#demand','item','customer','due date','requested quantity', 'plan date','plan quantity','operationplan id')) for i in frepple.demands(): if i.quantity == 0: continue for j in deliveries(i): writer.writerow(j) print('Exported demand plans in %.2f seconds' % (time() - starttime))
def exportPegging(cursor): print("Exporting pegging...") starttime = time() cnt = 0 for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging cursor.executemany( "insert into out_demandpegging \ (demand,depth,cons_operationplan,cons_date,prod_operationplan,prod_date, \ buffer,item,quantity_demand,quantity_buffer) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [( n, str(j.level), j.consuming and j.consuming.id or '0', str(j.consuming_date), j.producing and j.producing.id or '0', str(j.producing_date), j.buffer and j.buffer.name or '', (j.buffer and j.buffer.item and j.buffer.item.name) or '', round(j.quantity_demand,settings.DECIMAL_PLACES), round(j.quantity_buffer,settings.DECIMAL_PLACES) ) for j in i.pegging ]) cnt += 1 if cnt % 500 == 0: transaction.commit(using=database) transaction.commit(using=database) cursor.execute("select count(*) from out_demandpegging") print('Exported %d pegging in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def exportDemand(process): def deliveries(d): cumplanned = 0 # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield (d.name, d.item.name, d.customer and d.customer.name or "\\N", str(d.due), round(cur, 4), str(i.end), round(i.quantity, 4), i.id) # Extra record if planned short if cumplanned < d.quantity: yield (d.name, d.item.name, d.customer and d.customer.name or "\\N", str(d.due), round(d.quantity - cumplanned, 4), "\\N", "\\N", "\\N") print("Exporting demand plans...") starttime = time() process.stdin.write( 'COPY out_demand (demand,item,customer,due,quantity,plandate,planquantity,operationplan) FROM STDIN;\n' .encode(encoding)) for i in frepple.demands(): if i.quantity == 0: continue for j in deliveries(i): process.stdin.write( ("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % j).encode(encoding)) process.stdin.write('\\.\n'.encode(encoding)) print('Exported demand plans in %.2f seconds' % (time() - starttime))
def exportPegging(process): print("Exporting pegging...") starttime = time() process.stdin.write( "COPY out_demandpegging (demand,depth,cons_operationplan,cons_date,prod_operationplan,prod_date, buffer,item,quantity_demand,quantity_buffer) FROM STDIN;\n" ) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or "unspecified" # Export pegging for j in i.pegging: process.stdin.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( n.encode(encoding), str(j.level), j.consuming and j.consuming.id or "0", str(j.consuming_date), j.producing and j.producing.id or "0", str(j.producing_date), j.buffer and j.buffer.name.encode(encoding) or "", (j.buffer and j.buffer.item and j.buffer.item.name.encode(encoding)) or "", round(j.quantity_demand, settings.DECIMAL_PLACES), round(j.quantity_buffer, settings.DECIMAL_PLACES), ) ) process.stdin.write("\\.\n") print("Exported pegging in %.2f seconds" % (time() - starttime))
def exportConstraints(process): print("Exporting constraints...") starttime = time() process.stdin.write( "COPY out_constraint (demand,entity,name,owner,description,startdate,enddate,weight) FROM STDIN;\n" ) for d in frepple.demands(): for i in d.constraints: process.stdin.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( d.name.encode(encoding), i.entity.encode(encoding), i.name.encode(encoding), isinstance(i.owner, frepple.operationplan) and i.owner.operation.name.encode(encoding) or i.owner.name.encode(encoding), i.description.encode(encoding)[0 : settings.NAMESIZE + 20], str(i.start), str(i.end), round(i.weight, settings.DECIMAL_PLACES), ) ) process.stdin.write("\\.\n") print("Exported constraints in %.2f seconds" % (time() - starttime))
def exportDemand(process): def deliveries(d): cumplanned = 0 # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield ( d.name.encode(encoding), d.item.name.encode(encoding), d.customer and d.customer.name.encode(encoding) or "\\N", str(d.due), round(cur, settings.DECIMAL_PLACES), str(i.end), round(i.quantity, settings.DECIMAL_PLACES), i.id ) # Extra record if planned short if cumplanned < d.quantity: yield ( d.name.encode(encoding), d.item.name.encode(encoding), d.customer and d.customer.name.encode(encoding) or "\\N", str(d.due), round(d.quantity - cumplanned, settings.DECIMAL_PLACES), "\\N", "\\N", "\\N" ) print("Exporting demand plans...") starttime = time() process.stdin.write('COPY out_demand (demand,item,customer,due,quantity,plandate,planquantity,operationplan) FROM STDIN;\n') for i in frepple.demands(): if i.quantity == 0: continue for j in deliveries(i): process.stdin.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % j) process.stdin.write('\\.\n') print('Exported demand plans in %.2f seconds' % (time() - starttime))
def exportConstraints(self): if self.verbosity: logger.info("Exporting constraints...") starttime = time() cursor = connections[self.database].cursor() with tempfile.TemporaryFile(mode="w+t", encoding='utf-8') as tmp: for d in frepple.demands(): if self.cluster != -1 and self.cluster != d.cluster: continue for i in d.constraints: print(("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and i.owner.operation.name or i.owner.name, i.description, str( i.start), str(i.end), round(i.weight, 8))), file=tmp) tmp.seek(0) cursor.copy_from(tmp, 'out_constraint', columns=('demand', 'entity', 'name', 'owner', 'description', 'startdate', 'enddate', 'weight')) tmp.close() if self.verbosity: logger.info('Exported constraints in %.2f seconds' % (time() - starttime))
def exportPegging(process): print("Exporting pegging...") starttime = time() process.stdin.write( 'COPY out_demandpegging (demand,depth,cons_operationplan,cons_date,prod_operationplan,prod_date, buffer,item,quantity_demand,quantity_buffer) FROM STDIN;\n' ) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: process.stdin.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (n.encode(encoding), str(j.level), j.consuming and j.consuming.id or '0', str(j.consuming_date), j.producing and j.producing.id or '0', str(j.producing_date), j.buffer and j.buffer.name.encode(encoding) or '', (j.buffer and j.buffer.item and j.buffer.item.name.encode(encoding)) or '', round(j.quantity_demand, settings.DECIMAL_PLACES), round(j.quantity_buffer, settings.DECIMAL_PLACES))) process.stdin.write('\\.\n') print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportPegging(): print("Exporting pegging...") starttime = time() writer = csv.writer(open("demand_pegging.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow( ('#demand', 'level', 'consuming operationplan id', 'consuming date', 'producing operationplan id', 'producing date', 'buffer', 'item', 'quantity demand', 'quantity buffer')) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: writer.writerow( (n.encode(encoding, "ignore"), j.level, j.consuming and j.consuming.id or '', j.consuming_date, j.producing and j.producing.id or '', j.producing_date, j.buffer and j.buffer.name.encode(encoding, "ignore") or '', (j.buffer and j.buffer.item and j.buffer.item.name.encode(encoding, "ignore")) or '', j.quantity_demand, j.quantity_buffer)) print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportDemand(): def deliveries(d): cumplanned = 0 n = d while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield (n, d.item.name, d.customer and d.customer.name or None, d.due, cur, i.end, i.quantity, i.id) # Extra record if planned short if cumplanned < d.quantity: yield (n, d.item.name, d.customer and d.customer.name or None, d.due, d.quantity - cumplanned, None, None, None) print("Exporting demand plans...") starttime = time() writer = csv.writer(open("demands.csv", "w", newline="", encoding="utf-8"), quoting=csv.QUOTE_ALL) writer.writerow( ('#demand', 'item', 'customer', 'due date', 'requested quantity', 'plan date', 'plan quantity', 'operationplan')) for i in frepple.demands(): if i.quantity == 0: continue for j in deliveries(i): writer.writerow(j) print('Exported demand plans in %.2f seconds' % (time() - starttime))
def exportPegging(): print("Exporting pegging...") starttime = time() writer = csv.writer(open("demand_pegging.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow(( '#demand', 'level', 'consuming operationplan id', 'consuming date', 'producing operationplan id', 'producing date', 'buffer', 'item', 'quantity demand', 'quantity buffer' )) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: writer.writerow(( n.encode(encoding, "ignore"), j.level, j.consuming and j.consuming.id or '', j.consuming_date, j.producing and j.producing.id or '', j.producing_date, j.buffer and j.buffer.name.encode(encoding, "ignore") or '', (j.buffer and j.buffer.item and j.buffer.item.name.encode(encoding, "ignore")) or '', j.quantity_demand, j.quantity_buffer )) print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportDemands(self, cursor): print("Exporting demands...") starttime = time() cursor.execute("SELECT name FROM demand") primary_keys = set([ i[0] for i in cursor.fetchall() ]) cursor.executemany( '''insert into demand (name,due,quantity,priority,item_id,operation_id,customer_id, minshipment,maxlateness,category,subcategory,source,lastmodified) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', [ ( i.name, str(i.due), round(i.quantity, settings.DECIMAL_PLACES), i.priority, i.item.name, i.operation and i.operation.name or None, i.customer and i.customer.name or None, round(i.minshipment, settings.DECIMAL_PLACES), round(i.maxlateness, settings.DECIMAL_PLACES), i.category, i.subcategory, i.source, self.timestamp ) for i in frepple.demands() if i.name not in primary_keys and isinstance(i, frepple.demand_default) and not i.hidden and (not self.source or self.source == i.source) ]) cursor.executemany( '''update demand set due=%s, quantity=%s, priority=%s, item_id=%s, operation_id=%s, customer_id=%s, minshipment=%s, maxlateness=%s, category=%s, subcategory=%s, source=%s, lastmodified=%s where name=%s''', [ ( str(i.due), round(i.quantity, settings.DECIMAL_PLACES), i.priority, i.item.name, i.operation and i.operation.name or None, i.customer and i.customer.name or None, round(i.minshipment, settings.DECIMAL_PLACES), round(i.maxlateness, settings.DECIMAL_PLACES), i.category, i.subcategory, i.source, self.timestamp, i.name ) for i in frepple.demands() if i.name in primary_keys and isinstance(i, frepple.demand_default) and not i.hidden and (not self.source or self.source == i.source) ]) cursor.executemany( "update demand set owner_id=%s where name=%s", [ (i.owner.name, i.name) for i in frepple.demands() if i.owner and isinstance(i, frepple.demand_default) and (not self.source or self.source == i.source) ]) transaction.commit(using=self.database) print('Exported demands in %.2f seconds' % (time() - starttime))
def exportData(filename): with open(filename, "wt") as output: print("param timerate := 0.97;\n", file=output) print("param : demands : reqqty prio due :=", file=output) for b in frepple.demands(): if b.quantity > 0: # @todo Export of due date works for monthly buckets and a maximum horizon of 1 year only print(b.name.replace(' ', '').replace(':', ''), b.quantity, b.priority, b.due.month, file=output) print(";\n", file=output) print("param numbuckets := 12;", file=output) print("set buckets := 1 2 3 4 5 6 7 8 9 10 11 12;", file=output) print("set resources := ", file=output) for b in frepple.resources(): print(b.name.replace(' ', '').replace(':', ''), file=output) print(";\n", file=output) print("param availablecapacity", file=output) print(": 1 2 3 4 5 6 7 8 9 10 11 12 := ", file=output) res = [] for b in frepple.resources(): # @todo need a more correct way to extract the capacity per bucket. res.append(b.name.replace(' ', '').replace(':', '')) print(b.name.replace(' ', '').replace(':', ''), "120 120 120 120 120 120 120 120 120 120 120 120", file=output) print(";\n", file=output) print("param : loads : loadfactor :=", file=output) for b in frepple.demands(): if b.quantity > 0: oper = b.operation or b.item.operation if oper: for fl in oper.flows: if fl.quantity < 0: findResources(output, b, fl) print(";\n", file=output) print("end;\n", file=output)
def exportConstraints(): print("Exporting constraints...") starttime = time() writer = csv.writer(open("constraints.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow(('#demand','entity','name','owner','description','start date','end date','weight')) for d in frepple.demands(): for i in d.constraints: writer.writerow(( d.name.encode(encoding,"ignore"), i.entity, i.name.encode(encoding,"ignore"), i.owner.name.encode(encoding,"ignore"), i.description.encode(encoding,"ignore"), i.start, i.end, i.weight )) print('Exported constraints in %.2f seconds' % (time() - starttime))
def exportConstraints(process): print("Exporting constraints...") starttime = time() process.stdin.write('COPY out_constraint (demand,entity,name,owner,description,startdate,enddate,weight) FROM STDIN;\n'.encode(encoding)) for d in frepple.demands(): for i in d.constraints: process.stdin.write(("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and i.owner.operation.name or i.owner.name, i.description, str(i.start), str(i.end), round(i.weight, 4) )).encode(encoding)) process.stdin.write('\\.\n'.encode(encoding)) print('Exported constraints in %.2f seconds' % (time() - starttime))
def getDemandPlan(): for i in frepple.demands(): if self.cluster != -1 and self.cluster != i.cluster: continue if i.hidden or not isinstance(i, frepple.demand_default): continue peg = [] for j in i.pegging: peg.append({ "level": j.level, "opplan": j.operationplan.reference, "quantity": j.quantity, }) yield (json.dumps({"pegging": peg}), i.name)
def getDemandPlan(): for i in frepple.demands(): if self.cluster != -1 and self.cluster != i.cluster: continue if i.hidden or not isinstance(i, frepple.demand_default): continue peg = [] for j in i.pegging: peg.append({ 'level': j.level, 'opplan': j.operationplan.id, 'quantity': j.quantity }) yield (json.dumps({'pegging': peg}), i.name)
def exportData(filename): with open(filename,"wt") as output: print("param timerate := 0.97;\n", file=output) print("param : demands : reqqty prio due :=", file=output) for b in frepple.demands(): if b.quantity > 0: # @todo Export of due date works for monthly buckets and a maximum horizon of 1 year only print(b.name.replace(' ','').replace(':',''), b.quantity, b.priority, b.due.month, file=output) print(";\n", file=output) print("param numbuckets := 12;", file=output) print("set buckets := 1 2 3 4 5 6 7 8 9 10 11 12;", file=output) print("set resources := ", file=output) for b in frepple.resources(): print(b.name.replace(' ','').replace(':',''), file=output) print(";\n", file=output) print("param availablecapacity", file=output) print(": 1 2 3 4 5 6 7 8 9 10 11 12 := ", file=output) res = [] for b in frepple.resources(): # @todo need a more correct way to extract the capacity per bucket. res.append(b.name.replace(' ','').replace(':','')) print(b.name.replace(' ','').replace(':',''), "120 120 120 120 120 120 120 120 120 120 120 120", file=output) print(";\n", file=output) print("param : loads : loadfactor :=", file=output) for b in frepple.demands(): if b.quantity > 0: oper = b.operation or b.item.operation if oper: for fl in oper.flows: if fl.quantity < 0: findResources(output, b, fl) print(";\n", file=output) print("end;\n", file=output)
def exportConstraints(): print("Exporting constraints...") starttime = time() writer = csv.writer(open("constraints.csv", "w", newline="", encoding="utf-8"), quoting=csv.QUOTE_ALL) writer.writerow(('#demand', 'entity', 'name', 'owner', 'description', 'start date', 'end date', 'weight')) for d in frepple.demands(): for i in d.constraints: writer.writerow((d.name, i.entity, i.name, i.owner.name, i.description, i.start, i.end, i.weight)) print('Exported constraints in %.2f seconds' % (time() - starttime))
def exportForecast(): # Detect whether the forecast module is available if not 'demand_forecast' in [a[0] for a in inspect.getmembers(frepple)]: return print("Exporting forecast plans...") starttime = time() writer = csv.writer(open("forecast.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow(('#forecast', 'start date', 'end date', 'total quantity', 'net quantity', 'consumed quantity')) for i in frepple.demands(): if not isinstance(i, frepple.demand_forecastbucket) or i.total <= 0.0: continue writer.writerow((i.name.encode(encoding, "ignore"), i.startdate, i.enddate, i.total, i.quantity, i.consumed)) print('Exported forecast plans in %.2f seconds' % (time() - starttime))
def exportPegging(process): print("Exporting pegging...") starttime = time() process.stdin.write("COPY out_demandpegging (demand,level,operationplan,quantity) FROM STDIN;\n".encode(encoding)) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or "unspecified" # Export pegging for j in i.pegging: process.stdin.write( ("%s\t%s\t%s\t%s\n" % (n, str(j.level), j.operationplan.id, round(j.quantity, 4))).encode(encoding) ) process.stdin.write("\\.\n".encode(encoding)) print("Exported pegging in %.2f seconds" % (time() - starttime))
def exportForecast(): # Detect whether the forecast module is available if not 'demand_forecast' in [ a[0] for a in inspect.getmembers(frepple) ]: return print("Exporting forecast plans...") starttime = time() writer = csv.writer(open("forecast.csv", "wb"), quoting=csv.QUOTE_ALL) writer.writerow(('#forecast','start date','end date','total quantity', 'net quantity','consumed quantity')) for i in frepple.demands(): if not isinstance(i, frepple.demand_forecastbucket) or i.total <= 0.0: continue writer.writerow(( i.name.encode(encoding,"ignore"), i.startdate, i.enddate, i.total, i.quantity, i.consumed )) print('Exported forecast plans in %.2f seconds' % (time() - starttime))
def getData(): for d in frepple.demands(): if self.cluster != -1 and self.cluster != d.cluster: continue for i in d.constraints: yield "%s\v%s\v%s\v%s\v%s\v%s\v%s\v%s\n" % ( clean_value(d.name), clean_value(i.entity), clean_value(i.name), isinstance(i.owner, frepple.operationplan) and clean_value(i.owner.operation.name) or clean_value(i.owner.name), clean_value(i.description), str(i.start), str(i.end), round(i.weight, 8), )
def exportConstraints(process): print("Exporting constraints...") starttime = time() process.stdin.write( 'COPY out_constraint (demand,entity,name,owner,description,startdate,enddate,weight) FROM STDIN;\n' .encode(encoding)) for d in frepple.demands(): for i in d.constraints: process.stdin.write( ("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and i.owner.operation.name or i.owner.name, i.description, str(i.start), str( i.end), round(i.weight, 4))).encode(encoding)) process.stdin.write('\\.\n'.encode(encoding)) print('Exported constraints in %.2f seconds' % (time() - starttime))
def exportPegging(): print("Exporting pegging...") starttime = time() writer = csv.writer( open("demand_pegging.csv", "w", newline="", encoding="utf-8"), quoting=csv.QUOTE_ALL, ) writer.writerow(("#demand", "level", "operationplan", "quantity")) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or "unspecified" # Export pegging for j in i.pegging: writer.writerow((n, j.level, j.operationplan.reference, j.quantity)) print("Exported pegging in %.2f seconds" % (time() - starttime))
def exportConstraints(cursor): print("Exporting constraints...") starttime = time() cnt = 0 for d in frepple.demands(): cursor.executemany( "insert into out_constraint \ (demand,entity,name,owner,description,startdate,enddate,weight) \ values(%s,%s,%s,%s,%s,%s,%s,%s)", [( d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and str(i.owner.operation) or str(i.owner), i.description[0:settings.NAMESIZE + 20], str(i.start), str(i.end), round(i.weight, settings.DECIMAL_PLACES) ) for i in d.constraints] ) cnt += 1 cursor.execute("select count(*) from out_constraint") print('Exported %d constraints in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def exportPegging(process): print("Exporting pegging...") starttime = time() process.stdin.write( 'COPY out_demandpegging (demand,level,operationplan,quantity) FROM STDIN;\n' .encode(encoding)) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: process.stdin.write( ("%s\t%s\t%s\t%s\n" % (n, str(j.level), j.operationplan.id, round(j.quantity, 4))).encode(encoding)) process.stdin.write('\\.\n'.encode(encoding)) print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportPegging(): print("Exporting pegging...") starttime = time() writer = csv.writer(open("demand_pegging.csv", "w", newline="", encoding="utf-8"), quoting=csv.QUOTE_ALL) writer.writerow(( '#demand', 'level', 'operationplan', 'quantity' )) for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: writer.writerow(( n, j.level, j.operationplan.id, j.quantity )) print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportDemand(cursor): def deliveries(d): cumplanned = 0 # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield ( d.name, d.item.name, d.customer and d.customer.name or None, str(d.due), round(cur, settings.DECIMAL_PLACES), str(i.end), round(i.quantity, settings.DECIMAL_PLACES), i.id ) # Extra record if planned short if cumplanned < d.quantity: yield ( d.name, d.item.name, d.customer and d.customer.name or None, str(d.due), round(d.quantity - cumplanned, settings.DECIMAL_PLACES), None, None, None ) print("Exporting demand plans...") starttime = time() cnt = 0 for i in frepple.demands(): if i.quantity == 0: continue cursor.executemany( "insert into out_demand \ (demand,item,customer,due,quantity,plandate,planquantity,operationplan) \ values (%s,%s,%s,%s,%s,%s,%s,%s)", [ j for j in deliveries(i) ] ) cnt += 1 if cnt % 500 == 0: transaction.commit(using=database) transaction.commit(using=database) cursor.execute("select count(*) from out_demand") print('Exported %d demand plans in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def exportPegging(self, process): print("Exporting pegging...") starttime = time() process.stdin.write('COPY out_demandpegging (demand,level,operationplan,quantity) FROM STDIN;\n'.encode(self.encoding)) for i in frepple.demands(): if self.cluster != -1 and self.cluster != i.cluster: continue # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging for j in i.pegging: process.stdin.write(("%s\t%s\t%s\t%s\n" % ( n, str(j.level), j.operationplan.id, round(j.quantity, 4) )).encode(self.encoding)) process.stdin.write('\\.\n'.encode(self.encoding)) if self.verbosity: print('Exported pegging in %.2f seconds' % (time() - starttime))
def exportConstraints(self, process): if self.verbosity: print("Exporting constraints...") starttime = time() process( 'COPY out_constraint (demand,entity,name,owner,description,startdate,enddate,weight) FROM STDIN;\n' ) for d in frepple.demands(): if self.cluster != -1 and self.cluster != d.cluster: continue for i in d.constraints: process( ("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (d.name, i.entity, i.name, isinstance(i.owner, frepple.operationplan) and i.owner.operation.name or i.owner.name, i.description, str(i.start), str(i.end), round(i.weight, 6)))) process('\\.\n') if self.verbosity: print('Exported constraints in %.2f seconds' % (time() - starttime))
def exportPegging(cursor): print("Exporting pegging...") starttime = time() cnt = 0 for i in frepple.demands(): # Find non-hidden demand owner n = i while n.hidden and n.owner: n = n.owner n = n and n.name or 'unspecified' # Export pegging cursor.executemany( "insert into out_demandpegging \ (demand,level,operationplan,quantity) values (%s,%s,%s,%s)", [( n, j.level, j.operationplan.id, round(j.quantity, settings.DECIMAL_PLACES) ) for j in i.pegging] ) cnt += 1 cursor.execute("select count(*) from out_demandpegging") print('Exported %d pegging in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def exportDemand(cursor): def deliveries(d): cumplanned = 0 # Loop over all delivery operationplans for i in d.operationplans: cumplanned += i.quantity cur = i.quantity if cumplanned > d.quantity: cur -= cumplanned - d.quantity if cur < 0: cur = 0 yield ( d.name, d.item.name, d.customer and d.customer.name or None, str(d.due), round(cur, settings.DECIMAL_PLACES), str(i.end), round(i.quantity, settings.DECIMAL_PLACES), i.id ) # Extra record if planned short if cumplanned < d.quantity: yield ( d.name, d.item.name, d.customer and d.customer.name or None, str(d.due), round(d.quantity - cumplanned, settings.DECIMAL_PLACES), None, None, None ) print("Exporting demand plans...") starttime = time() cnt = 0 for i in frepple.demands(): if i.quantity == 0: continue cursor.executemany( "insert into out_demand \ (demand,item,customer,due,quantity,plandate,planquantity,operationplan) \ values (%s,%s,%s,%s,%s,%s,%s,%s)", [ j for j in deliveries(i) ] ) cnt += 1 cursor.execute("select count(*) from out_demand") print('Exported %d demand plans in %.2f seconds' % (cursor.fetchone()[0], time() - starttime))
def getData(cluster=-1): import frepple for d in frepple.demands(): if cluster != -1 and cluster != d.cluster: continue for i in d.constraints: yield "%s\v%s\v%s\v%s\v%s\v%s\v%s\v%s\v%s\v%s\n" % ( clean_value(d.name) if isinstance(d, frepple.demand_default) else "\\N", "\\N" if isinstance(d, frepple.demand_default) else clean_value(d.owner.name), clean_value(d.item.name), clean_value(i.entity), clean_value(i.name), isinstance(i.owner, frepple.operationplan) and clean_value( i.owner.operation.name) or clean_value(i.owner.name), clean_value(i.description), str(i.start), str(i.end), round(i.weight, 8), )
def DISABLED_nextDemand(cls, cluster): # Build the list of demands to plan when accessed for the first time if not hasattr(cls, "demandlist"): import frepple cls.demandlist = {} for d in frepple.demands(): if d.quantity > 0 and d.status in ("open", "quote"): if d.cluster in cls.demandlist: cls.demandlist[d.cluster].append(d) else: cls.demandlist[d.cluster] = [d] for cl in cls.demandlist: cls.demandlist[cl].sort( key=lambda d: (d.priority, d.due, d.quantity, d.name)) for d in cls.demandlist[cl]: print("sorted", cl, d.priority, d.due, d.quantity, d.name) # Pop a demand from the list on every call print("asked next demand for", cls.demandlist[cluster]) if cls.demandlist.get(cluster, None): return cls.demandlist[cluster].pop(0) else: return None
def printModel(filename): ''' A function that prints out all models to a file. ''' # Open the output file output = open(filename,"wt") # Global settings print("Echoing global settings", file=output) print("Plan name:", frepple.settings.name, file=output) print("Plan description:", frepple.settings.description.encode('utf-8'), file=output) print("Plan current:", frepple.settings.current, file=output) # Solvers print("\nEchoing solvers:", file=output) for b in frepple.solvers(): print(" Solver:", b.name, b.loglevel, getattr(b,'constraints',None), file=output) # Calendars print("\nEchoing calendars:", file=output) for b in frepple.calendars(): print(" Calendar:", b.name, getattr(b,'default',None), file=output) for j in b.buckets: print(" Bucket:", getattr(j,'value',None), j.start, j.end, j.priority, file=output) # Customers print("\nEchoing customers:", file=output) for b in frepple.customers(): print(" Customer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Locations print("\nEchoing locations:", file=output) for b in frepple.locations(): print(" Location:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Items print("\nEchoing items:", file=output) for b in frepple.items(): print(" Item:", b.name, b.description, b.category, b.subcategory, b.owner, b.operation, file=output) # Resources print("\nEchoing resources:", file=output) for b in frepple.resources(): print(" Resource:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.loads: print(" Load:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.loadplans: print(" Loadplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.startdate, l.enddate, file=output) # Buffers print("\nEchoing buffers:", file=output) for b in frepple.buffers(): print(" Buffer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.flows: print(" Flow:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flowplans: print(" Flowplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.date, file=output) # Operations print("\nEchoing operations:", file=output) for b in frepple.operations(): print(" Operation:", b.name, b.description, b.category, b.subcategory, file=output) for l in b.loads: print(" Load:", l.resource.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flows: print(" Flow:", l.buffer.name, l.quantity, l.effective_start, l.effective_end, file=output) if isinstance(b, frepple.operation_alternate): for l in b.alternates: print(" Alternate:", l.name, file=output) if isinstance(b, frepple.operation_routing): for l in b.steps: print(" Step:", l.name, file=output) # Demands print("\nEchoing demands:", file=output) for b in frepple.demands(): print(" Demand:", b.name, b.due, b.item.name, b.quantity, file=output) for i in b.operationplans: print(" Operationplan:", i.id, i.operation.name, i.quantity, i.end, file=output) # Operationplans print("\nEchoing operationplans:", file=output) for b in frepple.operationplans(): print(" Operationplan:", b.operation.name, b.quantity, b.start, b.end, file=output) for s in b.operationplans: print(" ", s.operation.name, s.quantity, s.start, s.end, file=output) # Problems print("\nPrinting problems", file=output) for i in frepple.problems(): print(" Problem:", i.entity, i.name, i.description, i.start, i.end, i.weight, file=output)
def printModel(filename): ''' A function that prints out all models to a file. ''' # Open the output file with open(filename, "wt", encoding='utf-8') as output: # Global settings print("Echoing global settings", file=output) print("Plan name:", frepple.settings.name, file=output) print("Plan description:", frepple.settings.description, file=output) print("Plan current:", frepple.settings.current, file=output) # Solvers print("\nEchoing solvers:", file=output) for b in frepple.solvers(): print(" Solver:", b.name, b.loglevel, getattr(b, 'constraints', None), file=output) # Calendars print("\nEchoing calendars:", file=output) for b in frepple.calendars(): print(" Calendar:", b.name, getattr(b, 'default', None), file=output) for j in b.buckets: print(" Bucket:", getattr(j, 'value', None), j.start, j.end, j.priority, file=output) # Customers print("\nEchoing customers:", file=output) for b in frepple.customers(): print(" Customer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Locations print("\nEchoing locations:", file=output) for b in frepple.locations(): print(" Location:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Items print("\nEchoing items:", file=output) for b in frepple.items(): print(" Item:", b.name, b.description, b.category, b.subcategory, b.owner, b.operation, file=output) # Resources print("\nEchoing resources:", file=output) for b in frepple.resources(): print(" Resource:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.loads: print(" Load:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.loadplans: print(" Loadplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.startdate, l.enddate, file=output) # Buffers print("\nEchoing buffers:", file=output) for b in frepple.buffers(): print(" Buffer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.flows: print(" Flow:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flowplans: print(" Flowplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.date, file=output) # Operations print("\nEchoing operations:", file=output) for b in frepple.operations(): print(" Operation:", b.name, b.description, b.category, b.subcategory, file=output) for l in b.loads: print(" Load:", l.resource.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flows: print(" Flow:", l.buffer.name, l.quantity, l.effective_start, l.effective_end, file=output) if isinstance(b, frepple.operation_alternate): for l in b.alternates: print(" Alternate:", l[0].name, l[1], l[2], l[3], file=output) if isinstance(b, frepple.operation_routing): for l in b.steps: print(" Step:", l.name, file=output) # Demands print("\nEchoing demands:", file=output) for b in frepple.demands(): print(" Demand:", b.name, b.due, b.item.name, b.quantity, file=output) for i in b.operationplans: print(" Operationplan:", i.id, i.operation.name, i.quantity, i.end, file=output) # Operationplans print("\nEchoing operationplans:", file=output) for b in frepple.operationplans(): print(" Operationplan:", b.operation.name, b.quantity, b.start, b.end, file=output) for s in b.operationplans: print(" ", s.operation.name, s.quantity, s.start, s.end, file=output) # Problems print("\nPrinting problems", file=output) for i in frepple.problems(): print(" Problem:", i.entity, i.name, i.description, i.start, i.end, i.weight, file=output)