def calc_orders_data_csv(recipient, batch_size, offset=0, csv_bytestring=u"", calc_cost=False): link_domain = "www.waybetter.com" logging.info("querying computations %s->%s" % (offset, offset + batch_size)) start_dt = set_default_tz_time(datetime(2012, 1, 1)) end_dt = set_default_tz_time(datetime.now()) station, station_cost_rules = None, [] computations = [] #TODO_WB: no computations any more... fix if needed for computation in computations: if computation.debug: continue rides = computation.rides.all() total_interval_orders = sum([ride.orders.count() for ride in rides]) for ride in rides: orders = ride.orders.all() count_orders = len(orders) for order in orders: depart_day = order.depart_time.date().isoformat() if order.depart_time else "" depart_time = order.depart_time.time().strftime("%H:%M") if order.depart_time else "" arrive_day = order.arrive_time.date().isoformat() if order.arrive_time else "" arrive_time = order.arrive_time.time().strftime("%H:%M") if order.arrive_time else "" hotspot_type = computation.get_hotspot_type_display() ordering_td = (order.depart_time or order.arrive_time) - order.create_date ordering_td_format = str(ordering_td).split(".")[0] # trim microseconds passenger_name = order.passenger.full_name shared = "yes" if count_orders > 1 else "" price = order.get_billing_amount() cost = 0 if calc_cost: if ride.station and ride.station != station: # store the rules in memory to reduce queries station = ride.station station_cost_rules = list(ride.station.fixed_prices.all()) logging.info("got new prices from station %s (was %s)" % (ride.station, station)) for rule in station_cost_rules: if rule.is_active(order.from_lat, order.from_lon, order.to_lat, order.to_lon, ride.depart_time): cost = rule.price link = "http://%s/%s" % (link_domain , reverse(ride_page, args=[ride.id])) order_data = [depart_day, depart_time, arrive_day, arrive_time, ordering_td_format, passenger_name, order.from_raw, order.from_lat, order.from_lon, order.to_raw, order.to_lat, order.to_lon, hotspot_type, shared, order.computation_id, total_interval_orders, price, cost, link] csv_bytestring += u";".join([unicode(i).replace(";", "").replace('"', '') for i in order_data]) csv_bytestring += u"\n" if computations: deferred.defer(calc_orders_data_csv, recipient, batch_size, offset=offset + batch_size + 1, csv_bytestring=csv_bytestring, calc_cost=calc_cost) else: logging.info("all done, sending data...") timestamp = date.today() send_mail_as_noreply(recipient, "Orders data %s" % timestamp, attachments=[("orders_data_%s.csv" % timestamp, csv_bytestring)])
def feb_statistics(): sharing_launched = set_default_tz_time( datetime.datetime(2011, 10, 1, 0, 0, 0)) feb_start = datetime.date(2012, 2, 1) feb_end = datetime.date(2012, 3, 1) - datetime.timedelta(days=1) rides = filter( lambda ride: ride.debug == False, SharedRide.objects.filter(create_date__gte=sharing_launched)) orders = [order for ride in rides for order in ride.orders.all()] sharing_passengers = set([order.passenger for order in orders]) feb_rides = [] pre_feb_sharing_passengers = set() for ride in rides: if feb_start <= ride.create_date.date() <= feb_end: feb_rides.append(ride) feb_orders = [order for ride in feb_rides for order in ride.orders.all()] for order in feb_orders: if order.passenger.create_date.date() < feb_start: pre_feb_sharing_passengers.add(order.passenger) msg = """ total sharing passengers: %s total orders: %s orders in february: %s passengers registered before feb 1st and ordered in feb: %s """ % (len(sharing_passengers), len(orders), len(feb_orders), len(pre_feb_sharing_passengers)) send_mail_as_noreply("*****@*****.**", "feb stats", msg)
def first_ride_stats(): csv = u"" sharing_launched = set_default_tz_time( datetime.datetime(2011, 10, 1, 0, 0, 0)) rides = filter( lambda ride: ride.debug == False, SharedRide.objects.filter(create_date__gte=sharing_launched)) orders = [order for ride in rides for order in ride.orders.all()] sharing_passengers = set([order.passenger for order in orders]) for passenger in sharing_passengers: my_orders = filter(lambda o: o.passenger == passenger, orders) if my_orders: my_first_order = sorted(my_orders, key=lambda o: o.create_date)[0] try: csv += u",".join([ unicode(v) for v in [ my_first_order.depart_time.date().isoformat(), passenger.full_name, passenger.phone ] ]) csv += u"\n" except Exception, e: logging.error("order[%s]: %s" % (my_first_order.id, e.message))
def create_ride(address, comments, passenger_phone, first_name, last_name, start_time, finish_time, station_id, as_raw_output): from common.tz_support import set_default_tz_time from geo.coder import geocode if not address: return "Please choose a valid street address: street, house number and city" lat, lon, city, street, house_number = None, None, None, None, None results = geocode(address, lang_code="he") if results: result = results[0] if "street_address" in result["types"]: lat = result["geometry"]["location"]["lat"] lon = result["geometry"]["location"]["lng"] for component in result["address_components"]: if "street_number" in component["types"]: house_number = component["short_name"] if "route" in component["types"]: street = component["short_name"] if "locality" in component["types"]: city_name = component["short_name"] city = City.objects.get(name=city_name) if not all([lat, lon, city, street, house_number]): return "Please choose a valid street address: street, house number and city" user = User() user.first_name = first_name user.last_name = last_name passenger = Passenger() passenger.user = user passenger.phone = passenger_phone passenger.id = random.randrange(1, 999999) order = Order() order.id = random.randrange(1, 999999) order.from_raw = address order.from_city = city order.from_street_address = street order.from_house_number = house_number order.from_lat = lat order.from_lon = lon order.comments = comments order.depart_time = set_default_tz_time(datetime.datetime.strptime(start_time, "%Y-%m-%dT%H:%M:%S")) if start_time else None order.arrive_time = set_default_tz_time(datetime.datetime.strptime(finish_time, "%Y-%m-%dT%H:%M:%S")) if finish_time else None order.passenger = passenger station = Station() station.fleet_manager = isr_fm station.fleet_station_id = station_id or 8 # waybetter station operator id ride = FakeSharedRide([order]) ride.id = random.randrange(1, 999999) ride.station = station ride.dn_fleet_manager_id = isr_fm.id ride.status = ASSIGNED DEV_WB_ONGOING_RIDES.append(ride) if as_raw_output: ex_order = ISR._create_external_order(order, station.fleet_station_id) reply = ISR._get_client().service.Insert_External_Order(ISR._get_login_token(), ex_order) return reply return "%s ride id=%s" % (fleet_manager.create_ride(ride), ride.id)
def get_ride_events(request): def _add_shared_ride(ride_id): ride = SharedRide.by_id(ride_id) shared_rides[ride_id] = { "id": ride.id, "type": "sharing", "taxi": ride.taxi_number, "stops": [ p.serialize_for_status_page() for p in sorted(ride.points.all(), key=lambda p: p.stop_time) ], "events": [] } return ride def _add_pickmeapp_ride(ride_id): ride = PickMeAppRide.by_id(ride_id) order = ride.order stops = [{ "lon": order.from_lon, "lat": order.from_lat, "address": order.from_raw, "time": order.create_date.strftime("%d/%m/%y %H:%M") }] if order.to_lon and order.to_lat: stops.append({ "lon": order.to_lon, "lat": order.to_lat, "address": order.to_raw, "time": order.create_date.strftime("%d/%m/%y %H:%M") }) pickmeapp_rides[ride_id] = { "id": ride.id, "type": "pickmeapp", "taxi": ride.taxi_number, "stops": stops, "events": [] } return ride import dateutil.parser ride_id = request.GET.get("ride_id") shared_rides = {} pickmeapp_rides = {} if ride_id: ride = _add_shared_ride(ride_id) if not ride: ride = _add_pickmeapp_ride(ride_id) events = ride.events.all() if ride else [] else: from_date = dateutil.parser.parse(request.GET.get("from_date")) to_date = dateutil.parser.parse(request.GET.get("to_date")) from_date = datetime.datetime.combine( from_date, set_default_tz_time(datetime.time.min)) to_date = datetime.datetime.combine( to_date, set_default_tz_time(datetime.time.max)) logging.info("get_ride_events: %s, %s" % (from_date, to_date)) events = RideEvent.objects.filter(create_date__gt=from_date, create_date__lt=to_date) for e in events: taxi = e.taxi_id if e.shared_ride_id: key = str(e.shared_ride_id) if key in shared_rides: shared_rides[key]["events"].append( e.serialize_for_status_page()) if taxi and not shared_rides[key]['taxi']: shared_rides[key]['taxi'] = taxi else: logging.error(u"miss configured events! %s not found in %s" % (e.shared_ride_id, shared_rides)) elif e.pickmeapp_ride_id: key = str(e.pickmeapp_ride_id) if key in pickmeapp_rides: pickmeapp_rides[key]["events"].append( e.serialize_for_status_page()) if taxi and not pickmeapp_rides[key]['taxi']: pickmeapp_rides[key]['taxi'] = taxi else: logging.error(u"miss configured events! %s not found in %s" % (e.pickmeapp_ride_id, pickmeapp_rides)) result = shared_rides.values() + pickmeapp_rides.values() return JSONResponse(result)
def create_ride(address, comments, passenger_phone, first_name, last_name, start_time, finish_time, station_id, as_raw_output): from common.tz_support import set_default_tz_time from geo.coder import geocode if not address: return "Please choose a valid street address: street, house number and city" lat, lon, city, street, house_number = None, None, None, None, None results = geocode(address, lang_code="he") if results: result = results[0] if "street_address" in result["types"]: lat = result["geometry"]["location"]["lat"] lon = result["geometry"]["location"]["lng"] for component in result["address_components"]: if "street_number" in component["types"]: house_number = component["short_name"] if "route" in component["types"]: street = component["short_name"] if "locality" in component["types"]: city_name = component["short_name"] city = City.objects.get(name=city_name) if not all([lat, lon, city, street, house_number]): return "Please choose a valid street address: street, house number and city" user = User() user.first_name = first_name user.last_name = last_name passenger = Passenger() passenger.user = user passenger.phone = passenger_phone passenger.id = random.randrange(1, 999999) order = Order() order.id = random.randrange(1, 999999) order.from_raw = address order.from_city = city order.from_street_address = street order.from_house_number = house_number order.from_lat = lat order.from_lon = lon order.comments = comments order.depart_time = set_default_tz_time( datetime.datetime.strptime( start_time, "%Y-%m-%dT%H:%M:%S")) if start_time else None order.arrive_time = set_default_tz_time( datetime.datetime.strptime( finish_time, "%Y-%m-%dT%H:%M:%S")) if finish_time else None order.passenger = passenger station = Station() station.fleet_manager = isr_fm station.fleet_station_id = station_id or 8 # waybetter station operator id ride = FakeSharedRide([order]) ride.id = random.randrange(1, 999999) ride.station = station ride.dn_fleet_manager_id = isr_fm.id ride.status = ASSIGNED DEV_WB_ONGOING_RIDES.append(ride) if as_raw_output: ex_order = ISR._create_external_order(order, station.fleet_station_id) reply = ISR._get_client().service.Insert_External_Order( ISR._get_login_token(), ex_order) return reply return "%s ride id=%s" % (fleet_manager.create_ride(ride), ride.id)
def get_ride_events(request): def _add_shared_ride(ride_id): ride = SharedRide.by_id(ride_id) shared_rides[ride_id] = { "id" : ride.id, "type" : "sharing", "taxi" : ride.taxi_number, "stops" : [p.serialize_for_status_page() for p in sorted(ride.points.all(), key=lambda p: p.stop_time)], "events" : [] } return ride def _add_pickmeapp_ride(ride_id): ride = PickMeAppRide.by_id(ride_id) order = ride.order stops = [{"lon": order.from_lon, "lat": order.from_lat, "address": order.from_raw, "time": order.create_date.strftime("%d/%m/%y %H:%M")}] if order.to_lon and order.to_lat: stops.append({ "lon": order.to_lon, "lat": order.to_lat, "address": order.to_raw, "time": order.create_date.strftime("%d/%m/%y %H:%M")}) pickmeapp_rides[ride_id] = { "id" : ride.id, "type" : "pickmeapp", "taxi" : ride.taxi_number, "stops" : stops, "events" : [] } return ride import dateutil.parser ride_id = request.GET.get("ride_id") shared_rides = {} pickmeapp_rides = {} if ride_id: ride = _add_shared_ride(ride_id) if not ride: ride = _add_pickmeapp_ride(ride_id) events = ride.events.all() if ride else [] else: from_date = dateutil.parser.parse(request.GET.get("from_date")) to_date = dateutil.parser.parse(request.GET.get("to_date")) from_date = datetime.datetime.combine(from_date, set_default_tz_time(datetime.time.min)) to_date = datetime.datetime.combine(to_date, set_default_tz_time(datetime.time.max)) logging.info("get_ride_events: %s, %s" % (from_date, to_date)) events = RideEvent.objects.filter(create_date__gt=from_date, create_date__lt=to_date) for e in events: taxi = e.taxi_id if e.shared_ride_id: key = str(e.shared_ride_id) if key in shared_rides: shared_rides[key]["events"].append(e.serialize_for_status_page()) if taxi and not shared_rides[key]['taxi']: shared_rides[key]['taxi'] = taxi else: logging.error(u"miss configured events! %s not found in %s" % (e.shared_ride_id, shared_rides)) elif e.pickmeapp_ride_id: key = str(e.pickmeapp_ride_id) if key in pickmeapp_rides: pickmeapp_rides[key]["events"].append(e.serialize_for_status_page()) if taxi and not pickmeapp_rides[key]['taxi']: pickmeapp_rides[key]['taxi'] = taxi else: logging.error(u"miss configured events! %s not found in %s" % (e.pickmeapp_ride_id, pickmeapp_rides)) result = shared_rides.values() + pickmeapp_rides.values() return JSONResponse(result)
def calc_orders_data_csv(recipient, batch_size, offset=0, csv_bytestring=u"", calc_cost=False): link_domain = "www.waybetter.com" logging.info("querying computations %s->%s" % (offset, offset + batch_size)) start_dt = set_default_tz_time(datetime(2012, 1, 1)) end_dt = set_default_tz_time(datetime.now()) station, station_cost_rules = None, [] computations = [] #TODO_WB: no computations any more... fix if needed for computation in computations: if computation.debug: continue rides = computation.rides.all() total_interval_orders = sum([ride.orders.count() for ride in rides]) for ride in rides: orders = ride.orders.all() count_orders = len(orders) for order in orders: depart_day = order.depart_time.date().isoformat( ) if order.depart_time else "" depart_time = order.depart_time.time().strftime( "%H:%M") if order.depart_time else "" arrive_day = order.arrive_time.date().isoformat( ) if order.arrive_time else "" arrive_time = order.arrive_time.time().strftime( "%H:%M") if order.arrive_time else "" hotspot_type = computation.get_hotspot_type_display() ordering_td = (order.depart_time or order.arrive_time) - order.create_date ordering_td_format = str(ordering_td).split(".")[ 0] # trim microseconds passenger_name = order.passenger.full_name shared = "yes" if count_orders > 1 else "" price = order.get_billing_amount() cost = 0 if calc_cost: if ride.station and ride.station != station: # store the rules in memory to reduce queries station = ride.station station_cost_rules = list( ride.station.fixed_prices.all()) logging.info( "got new prices from station %s (was %s)" % (ride.station, station)) for rule in station_cost_rules: if rule.is_active(order.from_lat, order.from_lon, order.to_lat, order.to_lon, ride.depart_time): cost = rule.price link = "http://%s/%s" % (link_domain, reverse(ride_page, args=[ride.id])) order_data = [ depart_day, depart_time, arrive_day, arrive_time, ordering_td_format, passenger_name, order.from_raw, order.from_lat, order.from_lon, order.to_raw, order.to_lat, order.to_lon, hotspot_type, shared, order.computation_id, total_interval_orders, price, cost, link ] csv_bytestring += u";".join([ unicode(i).replace(";", "").replace('"', '') for i in order_data ]) csv_bytestring += u"\n" if computations: deferred.defer(calc_orders_data_csv, recipient, batch_size, offset=offset + batch_size + 1, csv_bytestring=csv_bytestring, calc_cost=calc_cost) else: logging.info("all done, sending data...") timestamp = date.today() send_mail_as_noreply(recipient, "Orders data %s" % timestamp, attachments=[("orders_data_%s.csv" % timestamp, csv_bytestring)])