<commit_before>// +build appengine

package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"sort"
	"sync"

	"golang.org/x/net/context"

	"github.com/hsluo/slack-bot"

	"google.golang.org/appengine"
	l "google.golang.org/appengine/log"
	"google.golang.org/appengine/urlfetch"
)

type VoteResult map[string]StringSet

func (vr VoteResult) hasVoted(user string) bool {
	for _, v := range vr {
		if v.contains(user) {
			return true
		}
	}
	return false
}

func (vr VoteResult) String() string {
	options := make([]string, 0, len(vr))
	for k, _ := range vr {
		options = append(options, k)
	}
	sort.Strings(options)
	var buf bytes.Buffer
	buf.WriteString("Result:\n")
	for i := range options {
		buf.WriteString(fmt.Sprintf("%s: %v\n", options[i], vr[options[i]].toSlice()))
	}
	return buf.String()
}

var (
	votes      = newStringSet()
	voteResult = VoteResult{}
	m          sync.Mutex
)

func vote(rw http.ResponseWriter, req *http.Request) {
	var (
		c         = appengine.NewContext(req)
		channelId = req.PostFormValue("channel_id")
		text      = req.PostFormValue("text")
		userId    = req.PostFormValue("user_id")
	)
	m.Lock()
	if text == "start" {
		if startVote(channelId) {
			err := annouce(c, channelId, fmt.Sprintf("<@%s> just starts a vote!", userId))
			if err != nil {
				fmt.Fprintln(rw, err)
			} else {
				fmt.Fprintln(rw, "vote starts now")
			}
		} else {
			fmt.Fprintln(rw, "we're voting")
		}
	} else if text == "done" {
		annouce(c, channelId, voteResult.String())
		fmt.Fprintln(rw, "vote ends")
		delete(votes.set, channelId)
	} else if votes.contains(channelId) {
		userName := req.PostFormValue("user_name")
		if voters, ok := voteResult[text]; ok {
			if !voteResult.hasVoted(userName) {
				voters.add(userName)
			}
		} else {
			voters = newStringSet()
			voters.add(userName)
			voteResult[text] = voters
		}<commit_msg>add log for vote calls<commit_after>// +build appengine

package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"sort"
	"sync"

	"golang.org/x/net/context"

	"github.com/hsluo/slack-bot"

	"google.golang.org/appengine"
	l "google.golang.org/appengine/log"
	"google.golang.org/appengine/urlfetch"
)

type VoteResult map[string]StringSet

func (vr VoteResult) hasVoted(user string) bool {
	for _, v := range vr {
		if v.contains(user) {
			return true
		}
	}
	return false
}

func (vr VoteResult) String() string {
	options := make([]string, 0, len(vr))
	for k, _ := range vr {
		options = append(options, k)
	}
	sort.Strings(options)
	var buf bytes.Buffer
	buf.WriteString("Result:\n")
	for i := range options {
		buf.WriteString(fmt.Sprintf("%s: %v\n", options[i], vr[options[i]].toSlice()))
	}
	return buf.String()
}

var (
	votes      = newStringSet()
	voteResult = VoteResult{}
	m          sync.Mutex
)

func vote(rw http.ResponseWriter, req *http.Request) {
	var (
		c         = appengine.NewContext(req)
		channelId = req.PostFormValue("channel_id")
		text      = req.PostFormValue("text")
		userId    = req.PostFormValue("user_id")
	)
	l.Infof(c, "%v", req.PostForm)
	m.Lock()
	if text == "start" {
		if startVote(channelId) {
			err := annouce(c, channelId, fmt.Sprintf("<@%s> just starts a vote!", userId))
			if err != nil {
				fmt.Fprintln(rw, err)
			} else {
				fmt.Fprintln(rw, "vote starts now")
			}
		} else {
			fmt.Fprintln(rw, "we're voting")
		}
	} else if text == "done" {
		annouce(c, channelId, voteResult.String())
		fmt.Fprintln(rw, "vote ends")
		delete(votes.set, channelId)
	} else if votes.contains(channelId) {
		userName := req.PostFormValue("user_name")
		if voters, ok := voteResult[text]; ok {
			if !voteResult.hasVoted(userName) {
				voters.add(userName)
			}
		} else {
			voters = newStringSet()
			voters.add(userName)
			voteResult[text] = voters
		}
{'fraction_non_alphanumeric': 0.15814569536423842, 'fraction_numerical': 0.0005298013245033113, 'mean_word_length': 3.4898929845422115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1598643', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1458, 'n_words': 307}