|
| 1 | +from ...storage.storage import count_docs |
| 2 | +from ...storage.query import find_max, find_min, find_many_ascending_order |
| 3 | +from ...ticketscraping import constants |
| 4 | +from ..models.pick import Pick |
| 5 | + |
| 6 | +# metric 1 |
| 7 | + |
| 8 | + |
| 9 | +def percent_of_change_metric(pick: Pick, scraping_id: str) -> float: |
| 10 | + # Find the % of change |
| 11 | + max_seat = find_max(constants.DATABASE['BEST_HISTORY_SEATS'], { |
| 12 | + "scraping_id": scraping_id}, 'price') |
| 13 | + min_seat = find_min(constants.DATABASE['BEST_HISTORY_SEATS'], { |
| 14 | + "scraping_id": scraping_id}, 'price') |
| 15 | + max_price = max_seat.get('price', 0) |
| 16 | + min_price = min_seat.get('price', 0) |
| 17 | + |
| 18 | + # min and max are identical - abort |
| 19 | + if max_price == min_price: |
| 20 | + raise Exception('min and max prices are identical') |
| 21 | + |
| 22 | + percent_change = (pick.price - min_price) / (max_price - min_price) |
| 23 | + |
| 24 | + # price of change exceeds the metric value - abort |
| 25 | + if percent_change > constants.PERCENT_OF_CHANGE: |
| 26 | + raise Exception( |
| 27 | + f'price of change ({percent_change}) exceeds the metric value') |
| 28 | + |
| 29 | + return percent_change |
| 30 | + |
| 31 | +# metric 2 |
| 32 | + |
| 33 | + |
| 34 | +def percentile_metric(pick: Pick, scraping_id: str) -> float: |
| 35 | + rank = count_docs(constants.DATABASE['BEST_HISTORY_SEATS'], |
| 36 | + {"scraping_id": scraping_id, "price": {"$lte": pick.price}}) |
| 37 | + total_count = count_docs(constants.DATABASE['BEST_HISTORY_SEATS'], |
| 38 | + { |
| 39 | + "scraping_id": scraping_id}) |
| 40 | + |
| 41 | + # no history seats data - abort |
| 42 | + if total_count == 0: |
| 43 | + raise Exception('no history seats data') |
| 44 | + |
| 45 | + percentile = rank / total_count |
| 46 | + |
| 47 | + # percentile of history prices exceeds the metric value - abort |
| 48 | + if percentile > constants.PERCENTILE_HISTORY_PRICES: |
| 49 | + raise Exception( |
| 50 | + 'percentile of history prices ({percentile}) exceeds the metric value') |
| 51 | + |
| 52 | + return percentile |
| 53 | + |
| 54 | + |
| 55 | +def get_exact_same_seats(pick: Pick, scraping_id: str): |
| 56 | + return find_many_ascending_order(constants.DATABASE['BEST_HISTORY_SEATS'], |
| 57 | + {"scraping_id": scraping_id, "section": pick.section, |
| 58 | + "row": pick.row, "seat_columns": pick.seat_columns}, |
| 59 | + 'last_modified') |
| 60 | + |
| 61 | + |
| 62 | +def run_async_task(pick: Pick, scraping_id: str): |
| 63 | + try: |
| 64 | + # Find the % of change |
| 65 | + percent_change = percent_of_change_metric(pick, scraping_id) |
| 66 | + # Find the percentile of the seat based on some criteria(e.g. rank or price). |
| 67 | + percentile = percentile_metric(pick, scraping_id) |
| 68 | + # If found the exact same seat based on(sec, row?, seat?), get the history price(s) of the seat. |
| 69 | + same_seats = get_exact_same_seats(pick, scraping_id) |
| 70 | + |
| 71 | + print(f"percent change: {percent_change*100}") |
| 72 | + print(f"percentile: {percentile*100}") |
| 73 | + print(f"same seats in chronological order") |
| 74 | + print(f"new seat price: {pick.price}") |
| 75 | + print(f"history seat prices:") |
| 76 | + print(list(map(lambda seat: seat.get('price', -1), same_seats))) |
| 77 | + |
| 78 | + # TODO |
| 79 | + # Alert the user based on alert conditions |
| 80 | + |
| 81 | + except Exception as ex: |
| 82 | + print(ex) |
| 83 | + pass |
0 commit comments