<commit_before># Template and context-related imports
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template import RequestContext

# JSON for the live search
from django.utils import simplejson

# Models
from eve_db.models import InvType
from apps.market_data.models import Orders, OrdersWarehouse, History, ItemRegionStat, ItemRegionStatHistory, EmdrStats

# Utils
import datetime

"""
Those are our views. We have to use the RequestContext for CSRF protection, 
since we have a form (search) in every single of our views, as they extend 'base.haml'.
"""

def home(request):
	
	"""
	Returns our static home template with a CSRF protection for our search as well as the stats.
	"""
	
	# Create context for CSRF protection
	rcontext = RequestContext(request, {})
	
	return render_to_response('home.haml', rcontext)
	
def stats(request):
	"""
	Returns stats page
	"""
	
	# Collect stats
	
	# 1. Platform stats
	active_orders = Orders.objects.count()
	archived_orders = OrdersWarehouse.objects.count()
	history = History.objects.count()
	
	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 = 1).order_by("message_timestamp")[:1][0].status_count / 5
	
	# 2. Minerals and PLEX
	types = [34,35,36,37,38,39,40,29668]
	region = 10000002
	typestats = []
	
	for item in types:
		
		# Still works if we have no data for that item
		try:
			region_stats = ItemRegionStat.objects.filter(mapregion_id = region, invtype_id = item).order_by("lastupdate")[:1][0]
			region_stats_history = ItemRegionStatHistory.objects.filter(mapregion_id = region, invtype_id = item).order_by("date")[:1][0]
			buy = Orders.objects.filter(mapregion = region, invtype = item, is_bid = True).order_by("-price")[:1][0].price
			sell = Orders.objects.filter(mapregion = region, invtype = item, is_bid = False).order_by("price")[:1][0].price
			
			stats = {'buy': buy,
					 'sell': sell,
					 'avg_buy': region_stats.buyavg,
					 'avg_buy_move': region_stats_history.buyavg - region_stats.buyavg,
					 'avg_sell': region_stats.sellavg,
					 'avg_sell_move': region_stats_history.sellavg - region_stats.sellavg,
					 'mean': (region_stats.buyavg + region_stats.sellavg) / 2,
					 'mean_move': ((region_stats_history.buyavg + region_stats_history.sellavg) / 2) - ((region_stats.buyavg + region_stats.sellavg) / 2)}
		
			typestats.append({'type':InvType.objects.get(id = item), 'stats':stats})
		
		except:
			pass
	<commit_msg>started moving stats data to memcache, still have a couple problems<commit_after># Template and context-related imports
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template import RequestContext

# JSON for the live search
from django.utils import simplejson

# Models
from eve_db.models import InvType
from apps.market_data.models import Orders, OrdersWarehouse, History, ItemRegionStat, ItemRegionStatHistory, EmdrStats

# Utils
import datetime
import pylibmc
import ujson as json
import logging

"""
Those are our views. We have to use the RequestContext for CSRF protection, 
since we have a form (search) in every single of our views, as they extend 'base.haml'.
"""

def home(request):
	
	"""
	Returns our static home template with a CSRF protection for our search as well as the stats.
	"""
	
	# Create context for CSRF protection
	rcontext = RequestContext(request, {})
	
	return render_to_response('home.haml', rcontext)
	
def stats(request):
	"""
	Returns stats page
	"""
        
        logger = logging.getLogger(__name__)

        #connect to memcache
        mc = pylibmc.Client('127.0.0.1', binary=True, behaviors={"tcp_nodelay": True, "ketama": True})
        # need to clean that up and put that in a config file (memcache server location)

	# Collect stats
	
	# 1. Platform stats
	active_orders = Orders.objects.count()
	archived_orders = OrdersWarehouse.objects.count()
	history = History.objects.count()
	
	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 = 1).order_by("message_timestamp")[:1][0].status_count / 5
	
	# 2. Minerals and PLEX
	types = [34,35,36,37,38,39,40,29668]
	region = 10000002
	typestats = []
        cache_item = {}
        buyavg = 0
        sellavg = 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 "e43-stats"+str(item) in mc:
                            cache_item = json.loads(mc.get("e43-stats"+str(item)))
                            #print "Item: ", item, " cache: ", cache_item
                            buyavg = cache_item['buyavg']
                            sellavg = cache_item['sellavg']
                        # otherwise go to the DB for it
                        else:
                            region_stats = ItemRegionStat.objects.filter(mapregion_id = region, invtype_id = item)[:1][0]
                            buyavg = region_stats.buyavg
                            sellavg = region_stats.sellavg
			region_stats_history = ItemRegionStatHistory.objects.filter(mapregion_id = region, invtype_id = item).order_by("-date")[:1][0]
			buy = Orders.objects.filter(mapregion = region, invtype = item, is_bid = True).order_by("-price")[:1][0].price
			sell = Orders.objects.filter(mapregion = region, invtype = item, is_bid = False).order_by("price")[:1][0].price
			
			stats = {'buy': buy,
					 'sell': sell,
					 'avg_buy': buyavg,
					 'avg_buy_move': region_stats_history.buyavg - buyavg,
					 'avg_sell': sellavg,
					 'avg_sell_move': region_stats_history.sellavg - sellavg,
					 'mean': (buyavg + sellavg) / 2,
					 'mean_move': ((region_stats_history.buyavg + region_stats_history.sellavg) / 2) - ((buyavg + sellavg) / 2)}
		
			typestats.append({'type':InvType.objects.get(id = item), 'stats':stats})
		
		except:
			pass
	
{'fraction_non_alphanumeric': 0.11180586571386035, 'fraction_numerical': 0.018460622301622747, 'mean_word_length': 3.6750173973556017, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643769', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2039, 'n_words': 522}