def ranking(request, group=0): """ This function generates the station ranks based on active orders in the DB """ # Connect to memcache mc = get_memcache_client() # Check if we already have a stored copy if (mc.get("e43-station-ranking") is not None): # Get values from mc if existent ranking = mc.get("e43-station-ranking") rank_list = ranking['rank_list'] generated_at = ranking['generated_at'] else: # Generate numbers if there is no cached version present in memcache (anymore) rank_list = Orders.active.values('stastation__id').annotate(ordercount=Count('id')).order_by('-ordercount')[:50] for rank in rank_list: station = StaStation.objects.get(id=rank['stastation__id']) rank.update({'system': station.solar_system, 'name': station.name, 'region': station.region, 'id': station.id}) generated_at = datetime.datetime.now() # Expire after an hour mc.set("e43-station-ranking", {'rank_list': rank_list, 'generated_at': generated_at}, time=3600) rcontext = RequestContext(request, {'rank_list': rank_list, 'generated_at': generated_at}) return render_to_response('station/ranking.haml', rcontext)
def home(request): """ Returns our static home template with a CSRF protection for our search as well as the stats layout. """ type_ids = [34, 35, 36, 37, 38, 39, 40, 29668] region = 10000002 types = InvType.objects.filter(id__in=type_ids) # # Preload cached stats # # Connect to memcache mc = get_memcache_client() typestats = {} if (mc.get("e43-fullstats") is not None): initial_stats = ujson.loads(mc.get("e43-fullstats")) else: # Load empty values for invtype in types: stats = {'bid_max': 0, 'ask_min': 0, 'bid_median': 0, 'bid_median_move': 0, 'ask_median': 0, 'ask_median_move': 0} if stats: typestats[invtype] = stats initial_stats = {'active_orders': 0, 'archived_orders': 0, 'history_records': 0, 'new_orders': 0, 'updated_orders': 0, 'old_orders': 0, 'history_messages': 0, 'typestats': typestats} # Create context for CSRF protection rcontext = RequestContext(request, {'type_ids': type_ids, 'types': types, 'region': region, 'stats': initial_stats}) return render_to_response('home.haml', rcontext)
def home(request): """ Returns our static home template with a CSRF protection for our search as well as the stats layout. """ type_ids = [34, 35, 36, 37, 38, 39, 40, 29668] region = 10000002 types = InvType.objects.filter(id__in=type_ids) # # Preload cached stats # # Connect to memcache mc = get_memcache_client() typestats = {} if (mc.get("e43-fullstats") is not None): initial_stats = ujson.loads(mc.get("e43-fullstats")) else: # Load empty values for invtype in types: stats = { 'bid_median': 0, 'bid_median_move': 0, 'ask_median': 0, 'ask_median_move': 0 } if stats: typestats[invtype] = stats initial_stats = {'typestats': typestats} # Create context for CSRF protection rcontext = RequestContext( request, { 'type_ids': type_ids, 'types': types, 'region': region, 'stats': initial_stats }) return render_to_response('home.haml', rcontext)
def stats_json(request, region_id): """ Returns stat JSON for the front page """ # Connect to memcache mc = get_memcache_client() # Collect stats # 1. Platform stats # these is a disconnect between history and history messages/min -- history is orderhistory table which is all rows # message per min is based on emdr which is multiple rows in one message. if (mc.get("e43-stats-activeorders") is not None): active_orders = mc.get("e43-stats-activeorders") else: active_orders = Orders.active.count() mc.set("e43-stats-activeorders", active_orders, time=3600) if (mc.get("e43-stats-archivedorders") is not None): archived_orders = mc.get("e43-stats-archivedorders") else: archived_orders = Orders.archived.count() mc.set("e43-stats-archivedorders", archived_orders, time=3600) if (mc.get("e43-stats-history") is not None): history = mc.get("e43-stats-history") else: history = OrderHistory.objects.count() mc.set("e43-stats-history", history, time=3600) new_orders_per_minute = EmdrStats.objects.filter( status_type=1).order_by("-message_timestamp")[:1][0].status_count / 5 updated_orders_per_minute = EmdrStats.objects.filter( status_type=2).order_by("-message_timestamp")[:1][0].status_count / 5 old_orders_per_minute = EmdrStats.objects.filter( status_type=3).order_by("-message_timestamp")[:1][0].status_count / 5 history_messages_per_minute = EmdrStats.objects.filter( status_type=4).order_by("-message_timestamp")[:1][0].status_count / 5 # 2. Minerals and PLEX types = request.GET.getlist('type') new_types = [] for item in types: new_types.append(int(item)) types = new_types typestats = {} cache_item = {} buymedian = 0 sellmedian = 0 for item in types: # Still works if we have no data for that item try: # check to see if it's in the cache, if so use those values if (mc.get("e43-stats" + str(item)) is not None): cache_item = ujson.loads(mc.get("e43-stats" + str(item))) buymedian = cache_item['buymedian'] sellmedian = cache_item['sellmedian'] # otherwise go to the DB for it else: # Catch error if we don't have any data for that type try: region_stats = ItemRegionStat.objects.filter( mapregion_id=region_id, invtype_id=item)[:2][1] buymedian = region_stats.buymedian sellmedian = region_stats.sellmedian except: buymedian = 0 sellmedian = 0 region_stats_history = ItemRegionStatHistory.objects.filter(mapregion_id=region_id, invtype_id=item).order_by("-date")[:1][0] # Get Jita prices buy = Orders.active.filter(mapsolarsystem=30000142, invtype=item, is_bid=True).order_by("-price")[:1][0].price sell = Orders.active.filter(mapsolarsystem=30000142, invtype=item, is_bid=False).order_by("price")[:1][0].price stats = {'bid_max': buy, 'ask_min': sell, 'bid_median': buymedian, 'bid_median_move': region_stats_history.buymedian - buymedian, 'ask_median': sellmedian, 'ask_median_move': region_stats_history.sellmedian - sellmedian} if stats: typestats[item] = stats except pylibmc.Error as e: print e # Create JSON stat_json = simplejson.dumps({'active_orders': active_orders, 'archived_orders': archived_orders, 'history_records': history, 'new_orders': new_orders_per_minute, 'updated_orders': updated_orders_per_minute, 'old_orders': old_orders_per_minute, 'history_messages': history_messages_per_minute, 'typestats': typestats}) # Return JSON without using any template return HttpResponse(stat_json, mimetype='application/json')
def ranking(request, group=0): """ This function generates the station ranks based on active orders in the DB """ # Connect to memcache mc = get_memcache_client() # Check if we already have a stored copy if (mc.get("e43-station-ranking") is not None): # Get values from mc if existent ranking = mc.get("e43-station-ranking") rank_list = ranking['rank_list'] generated_at = ranking['generated_at'] else: # # Generate numbers if there is no cached version present in memcache (anymore) # # Get top stations by number of orders rank_list = Orders.active.values('stastation__id').annotate( ordercount=Count('id')).order_by('-ordercount')[:50] for rank in rank_list: # Get station station = StaStation.objects.get(id=rank['stastation__id']) # # Get group values # # Get ask/bid ISK volumes in that station by marketgroup in descending order volumes_by_group = group_volume(station.id)[:10] # Total volume in ISK total_volume = 0 # Get group objects from DB and calculate total volume for group in volumes_by_group: try: # Get market group group['group'] = InvMarketGroup.objects.get( id=group['market_group_id']) except InvMarketGroup.DoesNotExist: # Sometimes invTypes do not have a valid group - add pseudo-group group['group'] = {} group['group']['id'] = 0 group['group']['name'] = "No group" total_volume += group['group_total'] # # Get type values # # Get ask/bid ISK volumes in that station by marketgroup in descending order volumes_by_type = type_volume(station.id)[:10] # Get gtype objects from DB for invType in volumes_by_type: # Get market group invType['type'] = InvType.objects.get(id=invType['invtype_id']) rank.update({ 'station': station, 'volume': total_volume, 'volumes_by_group': volumes_by_group, 'volumes_by_type': volumes_by_type }) generated_at = pytz.utc.localize(datetime.datetime.utcnow()) # Expire after an hour mc.set("e43-station-ranking", { 'rank_list': rank_list, 'generated_at': generated_at }, time=3600) rcontext = RequestContext(request, { 'rank_list': rank_list, 'generated_at': generated_at }) return render_to_response('station/ranking.haml', rcontext)
def stats_json(request, region_id): """ Returns stat JSON for the front page """ # Connect to memcache mc = get_memcache_client() # Minerals and PLEX types = request.GET.getlist('type') new_types = [] for item in types: new_types.append(int(item)) types = new_types typestats = {} cache_item = {} buymedian = 0 sellmedian = 0 for item in types: # Still works in case we have no data for that item try: # check to see if it's in the cache, if so use those values if (mc.get("e43-stats" + str(item)) is not None): cache_item = ujson.loads(mc.get("e43-stats" + str(item))) buymedian = cache_item['buymedian'] sellmedian = cache_item['sellmedian'] # otherwise go to the DB for it else: # Catch error if we don't have any data for that type try: region_stats = ItemRegionStat.objects.filter( mapregion_id=region_id, invtype_id=item)[:1][0] buymedian = region_stats.buymedian sellmedian = region_stats.sellmedian except: buymedian = 0 sellmedian = 0 region_stats_history = ItemRegionStatHistory.objects.filter( mapregion_id=region_id, invtype_id=item).order_by("-date")[:1][0] stats = { 'bid_median': buymedian, 'bid_median_move': region_stats_history.buymedian - buymedian, 'ask_median': sellmedian, 'ask_median_move': region_stats_history.sellmedian - sellmedian } if stats: typestats[item] = stats except pylibmc.Error as e: print e # Create JSON stat_json = json.dumps({'typestats': typestats}) # Save complete stats to memcached mc.set("e43-fullstats", stat_json) # Return JSON without using any template return HttpResponse(stat_json, content_type='application/json')
def ranking(request, group=0): """ This function generates the station ranks based on active orders in the DB """ # Connect to memcache mc = get_memcache_client() # Check if we already have a stored copy if (mc.get("e43-station-ranking") is not None): # Get values from mc if existent ranking = mc.get("e43-station-ranking") rank_list = ranking['rank_list'] generated_at = ranking['generated_at'] else: # # Generate numbers if there is no cached version present in memcache (anymore) # # Get top stations by number of orders rank_list = Orders.active.values('stastation__id').annotate(ordercount=Count('id')).order_by('-ordercount')[:50] for rank in rank_list: # Get station station = StaStation.objects.get(id=rank['stastation__id']) # # Get group values # # Get ask/bid ISK volumes in that station by marketgroup in descending order volumes_by_group = group_volume(station.id)[:10] # Total volume in ISK total_volume = 0 # Get group objects from DB and calculate total volume for group in volumes_by_group: try: # Get market group group['group'] = InvMarketGroup.objects.get(id=group['market_group_id']) except InvMarketGroup.DoesNotExist: # Sometimes invTypes do not have a valid group - add pseudo-group group['group'] = {} group['group']['id'] = 0 group['group']['name'] = "No group" total_volume += group['group_total'] # # Get type values # # Get ask/bid ISK volumes in that station by marketgroup in descending order volumes_by_type = type_volume(station.id)[:10] # Get gtype objects from DB for invType in volumes_by_type: # Get market group invType['type'] = InvType.objects.get(id=invType['invtype_id']) rank.update({'station': station, 'volume': total_volume, 'volumes_by_group': volumes_by_group, 'volumes_by_type': volumes_by_type}) generated_at = pytz.utc.localize(datetime.datetime.utcnow()) # Expire after an hour mc.set("e43-station-ranking", {'rank_list': rank_list, 'generated_at': generated_at}, time=3600) rcontext = RequestContext(request, {'rank_list': rank_list, 'generated_at': generated_at}) return render_to_response('station/ranking.haml', rcontext)
def stats_json(request, region_id): """ Returns stat JSON for the front page """ # Connect to memcache mc = get_memcache_client() # Minerals and PLEX types = request.GET.getlist('type') new_types = [] for item in types: new_types.append(int(item)) types = new_types typestats = {} cache_item = {} buymedian = 0 sellmedian = 0 for item in types: # Still works in case we have no data for that item try: # check to see if it's in the cache, if so use those values if (mc.get("e43-stats" + str(item)) is not None): cache_item = ujson.loads(mc.get("e43-stats" + str(item))) buymedian = cache_item['buymedian'] sellmedian = cache_item['sellmedian'] # otherwise go to the DB for it else: # Catch error if we don't have any data for that type try: region_stats = ItemRegionStat.objects.filter(mapregion_id=region_id, invtype_id=item)[:1][0] buymedian = region_stats.buymedian sellmedian = region_stats.sellmedian except: buymedian = 0 sellmedian = 0 region_stats_history = ItemRegionStatHistory.objects.filter(mapregion_id=region_id, invtype_id=item).order_by("-date")[:1][0] stats = {'bid_median': buymedian, 'bid_median_move': region_stats_history.buymedian - buymedian, 'ask_median': sellmedian, 'ask_median_move': region_stats_history.sellmedian - sellmedian} if stats: typestats[item] = stats except pylibmc.Error as e: print e # Create JSON stat_json = json.dumps({'typestats': typestats}) # Save complete stats to memcached mc.set("e43-fullstats", stat_json) # Return JSON without using any template return HttpResponse(stat_json, mimetype='application/json')