Skip to content

Commit

Permalink
removing the new-demand-aggregation logic: unused and unmaintained
Browse files Browse the repository at this point in the history
  • Loading branch information
jdetaeye committed Jan 22, 2025
1 parent 57a6150 commit 53d8bb1
Showing 1 changed file with 0 additions and 185 deletions.
185 changes: 0 additions & 185 deletions freppledb/forecast/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,191 +388,6 @@ def getWeight(cls, database=DEFAULT_DB_ALIAS, **kwargs):

@classmethod
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs):
if (
Parameter.getValue(
"forecast.new_demand_aggregation", database, "false"
).lower()
== "true"
):
cls.run_new(database=database, **kwargs)
else:
cls.run_old(database=database, **kwargs)

@classmethod
def run_new(cls, database=DEFAULT_DB_ALIAS, **kwargs):
with connections[database].cursor() as cursor:
fcst_calendar = Parameter.getValue("forecast.calendar", database, None)
horizon_future = int(
Parameter.getValue("forecast.Horizon_future", database, 365)
)
horizon_history = int(
Parameter.getValue("forecast.Horizon_history", database, 10000)
)
currentdate = getCurrentDate(database)

# Delete forecastplan records for invalid dates
starttime = time()

# Creating temp tables
cursor.execute(
"""
create temporary table item_hierarchy on commit preserve rows as
select parent.name parent, child.name child from item parent
inner join item child
on child.lft between parent.lft and parent.rght
"""
)
cursor.execute(
"""
create index item_hierarchy_idx on item_hierarchy (child)
"""
)
cursor.execute(
"""
create temporary table location_hierarchy on commit preserve rows as
select parent.name parent, child.name child from location parent
inner join location child
on child.lft between parent.lft and parent.rght
"""
)
cursor.execute(
"create index location_hierarchy_idx on location_hierarchy (child)"
)
cursor.execute(
"""
create temporary table customer_hierarchy on commit preserve rows as
select parent.name parent, child.name child from customer parent
inner join customer child
on child.lft between parent.lft and parent.rght
"""
)
cursor.execute(
"create index customer_hierarchy_idx on customer_hierarchy (child)"
)
cursor.execute(
"""
drop table if exists forecasthierarchy;
create table forecasthierarchy as
select distinct
item_hierarchy.parent item_id, location_hierarchy.parent location_id,
customer_hierarchy.parent customer_id
from forecast
inner join item_hierarchy on forecast.item_id = item_hierarchy.child
inner join customer_hierarchy on forecast.customer_id = customer_hierarchy.child
inner join location_hierarchy on forecast.location_id = location_hierarchy.child
where coalesce(method, 'automatic') != 'aggregate'
"""
)
cursor.execute(
"create unique index nodes on forecasthierarchy (item_id, location_id, customer_id)"
)
cursor.execute(
"drop table item_hierarchy, location_hierarchy, customer_hierarchy"
)

cursor.execute(
"""
delete from forecastplan
where (startdate, enddate) not in (
select startdate, enddate
from common_bucketdetail
where bucket_id = %s
and startdate >= %s
and startdate < %s
and enddate > least((select coalesce(min(due),'2000-01-01 00:00:00'::timestamp) from demand),
%s)
)
""",
(
fcst_calendar,
currentdate - timedelta(days=horizon_history),
currentdate + timedelta(days=horizon_future),
currentdate,
),
)
transaction.commit(using=database)
logger.info(
"Aggregate - deleted %d obsolete forecast buckets in %.2f seconds"
% (cursor.rowcount, time() - starttime)
)

# Main aggregation
starttime = time()
cursor.execute(
"""
begin;
call aggregatedemand(%s, %s, %s);
end;
""",
(
fcst_calendar,
currentdate - timedelta(days=horizon_history),
currentdate + timedelta(days=horizon_future),
),
)
logger.info(
"Aggregate - aggregated demand information in %.2f seconds"
% (time() - starttime)
)

# Pruning empty records
starttime = time()
cursor.execute(
"""
delete from forecastplan
where value = '{}'::jsonb or value is null
"""
)
transaction.commit(using=database)
logger.info(
"Aggregate - pruned %d empty records in %.2f seconds"
% (cursor.rowcount, time() - starttime)
)

# Pruning dangling records, ie records that have no child any longer
# in the forecast table
starttime = time()
cursor.execute(
"""
with cte as (
select distinct item_id, location_id, customer_id
from forecastplan
)
delete from forecastplan
where (item_id, location_id, customer_id) in (
select
item_id, location_id, customer_id
from cte
where not exists (
select 1
from forecast
inner join item
on forecast.item_id = item.name
inner join location
on forecast.location_id = location.name
inner join customer
on forecast.customer_id = customer.name
inner join item as fitem on
cte.item_id = fitem.name
inner join location as flocation
on cte.location_id = flocation.name
inner join customer as fcustomer
on cte.customer_id = fcustomer.name
where item.lft between fitem.lft and fitem.rght
and location.lft between flocation.lft and flocation.rght
and customer.lft between fcustomer.lft and fcustomer.rght
)
)
"""
)
transaction.commit(using=database)
logger.info(
"Aggregate - pruned %d dangling records in %.2f seconds"
% (cursor.rowcount, time() - starttime)
)

@classmethod
def run_old(cls, database=DEFAULT_DB_ALIAS, **kwargs):
cursor = connections[database].cursor()

fcst_calendar = Parameter.getValue("forecast.calendar", database, None)
Expand Down

0 comments on commit 53d8bb1

Please sign in to comment.