<commit_before>package main

import (
	"app/admin"
	"app/config"
	"app/counter"
	"app/email"
	"bytes"
	"encoding/gob"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"path"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/gorilla/mux"
	"github.com/hoisie/mustache"

	"google.golang.org/appengine/log"

	"google.golang.org/appengine/memcache"

	"google.golang.org/appengine"
	"google.golang.org/appengine/datastore"
	"google.golang.org/appengine/taskqueue"
	"google.golang.org/appengine/urlfetch"
	"google.golang.org/appengine/user"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	var (
		dir    = path.Join(os.Getenv("PWD"), "templates")
		index  = path.Join(dir, "index.html")
		layout = path.Join(dir, "layout.html")
		c      = appengine.NewContext(r)
		cnfg   = config.Load(c)
		data   = map[string]interface{}{
			"title":   PageTitle,
			"captcha": cnfg["captchaPublic"],
		}
		err error
	)

	for _, k := range []string{"TotalLoops", "TotalPosts", "TotalVerified", "24hLoops", "24hPosts", "24hUsers"} {
		if data[k], err = counter.Count(c, k); err != nil {
			log.Errorf(c, "Error getting %s: %v", k, err)
		}
	}

	if popusers, err := memcache.Get(c, "popusers"); err == nil {
		var users []*VineUser
		var dec *gob.Decoder
		userIDs := strings.Split(string(popusers.Value[:]), ",")
		for len(users) < 6 {
			rand.Seed(time.Now().Unix())
			r := rand.Intn(len(userIDs))

			key, err := memcache.Get(c, userIDs[r])
			dec = gob.NewDecoder(bytes.NewReader(key.Value))
			var u *VineUser
			err = dec.Decode(&u)
			if err == nil {
				users = append(users, u)
				userIDs = append(userIDs[:r], userIDs[r+1:]...)
				//above removes already chosen user from userID array
			}
		}
		data["popusers"] = users
	} else {
		log.Errorf(c, "popusers memcache err: %v", err)
	}

	if featuredUser, err := memcache.Get(c, "featuredUser"); err == nil {
		var user *VineUser
		r := strings.Split(string(featuredUser.Value[:]), ";")
		u, _ := memcache.Get(c, r[0])
		dec := gob.NewDecoder(bytes.NewReader(u.Value))
		dec.Decode(&user)

		data["featuredUser"] = user
		data["featuredPost"] = r[1]
	}<commit_msg>Fix null pointer if key from popusers exits cache<commit_after>package main

import (
	"app/admin"
	"app/config"
	"app/counter"
	"app/email"
	"bytes"
	"encoding/gob"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"path"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/gorilla/mux"
	"github.com/hoisie/mustache"

	"google.golang.org/appengine/log"

	"google.golang.org/appengine/memcache"

	"google.golang.org/appengine"
	"google.golang.org/appengine/datastore"
	"google.golang.org/appengine/taskqueue"
	"google.golang.org/appengine/urlfetch"
	"google.golang.org/appengine/user"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	var (
		dir    = path.Join(os.Getenv("PWD"), "templates")
		index  = path.Join(dir, "index.html")
		layout = path.Join(dir, "layout.html")
		c      = appengine.NewContext(r)
		cnfg   = config.Load(c)
		data   = map[string]interface{}{
			"title":   PageTitle,
			"captcha": cnfg["captchaPublic"],
		}
		err error
	)

	for _, k := range []string{"TotalLoops", "TotalPosts", "TotalVerified", "24hLoops", "24hPosts", "24hUsers"} {
		if data[k], err = counter.Count(c, k); err != nil {
			log.Errorf(c, "Error getting %s: %v", k, err)
		}
	}

	if popusers, err := memcache.Get(c, "popusers"); err == nil {
		var users []*VineUser
		var dec *gob.Decoder
		userIDs := strings.Split(string(popusers.Value[:]), ",")
		for len(users) < 6 {
			rand.Seed(time.Now().Unix())
			r := rand.Intn(len(userIDs))

			key, err := memcache.Get(c, userIDs[r])
			if err == nil {
				dec = gob.NewDecoder(bytes.NewReader(key.Value))
				var u *VineUser
				err = dec.Decode(&u)
				if err == nil {
					users = append(users, u)
					userIDs = append(userIDs[:r], userIDs[r+1:]...)
				}
			}
		}
		data["popusers"] = users
	} else {
		log.Errorf(c, "popusers memcache err: %v", err)
	}

	if featuredUser, err := memcache.Get(c, "featuredUser"); err == nil {
		var user *VineUser
		r := strings.Split(string(featuredUser.Value[:]), ";")
		u, _ := memcache.Get(c, r[0])
		dec := gob.NewDecoder(bytes.NewReader(u.Value))
		dec.Decode(&user)

		data["featuredUser"] = user
		data["featuredPost"] = r[1]
	}
{'fraction_non_alphanumeric': 0.18485472431937772, 'fraction_numerical': 0.004575611988103409, 'mean_word_length': 3.752173913043478, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '51644', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1738, 'n_words': 325}