Skip to content

Commit

Permalink
feat: added worst performing goods
Browse files Browse the repository at this point in the history
  • Loading branch information
AshminJayson committed Sep 23, 2023
1 parent 9284e47 commit 28b067c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ async def top_items_by_store(shop_id):
return {'top_sold_items': res, 'shop_details': shop_details }


@app.get('/worst_items_by_store')
async def worst_items_by_store(shop_id):
transactions = db.get_all_transactions_of_shop(shop_id)
res = frequency_metric.get_bottom_k_most_sold_items(3, transactions)
shop_details = db.get_shop_details(shop_id)
return {'bottom_sold_items': res, 'shop_details': shop_details }


@app.get('/top_items_by_time')
async def get_top_items_by_time(time: str):
year, month, date = time.split('-')
Expand Down
21 changes: 21 additions & 0 deletions mltoolkit/frequency_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ def get_top_k_most_sold_items(k, transactions):
return res


def get_bottom_k_most_sold_items(k, transactions):
dataset = []
for transaction in transactions:
item_list = transaction['item_list']
items = item_list.split(',')
cleaned_items = [item.strip() for item in items]
dataset.extend(cleaned_items)

item_counter = Counter(dataset)
item_counter_list = list(item_counter.items())
item_counter_list.sort(key = lambda x: x[1])

k = min(k, len(dataset))

res = []
for i in range(k):
res.append(item_counter_list[i][0])

return res





Expand Down

0 comments on commit 28b067c

Please sign in to comment.