Exemplo n.º 1
0
func updateView(pid, uid string) {
	if isRelevantTo(pid, uid) {
		now := time.Now()
		p := firego.New(userPostsURL + "/" + uid + "/" + pid)
		p.Set(now)
		p = firego.New(postUsersURL + "/" + pid + "/" + uid)
		p.Set(now)
	}
}
Exemplo n.º 2
0
func putPostNode(id string, p *PostNode) error {
	fb := firego.New(postsURL + "/" + id)
	if err := fb.Set(p); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
// SignIn creates the user's posts view and starts the user's notification
// channel.
func SignIn(uid, name, imageUrl string) error {
	un := UserNode{Name: name, ImageURL: imageUrl}
	if err := putUserNode(uid, &un); err != nil {
		return err
	}

	// for right now, every user is watching every post
	fb := firego.New(userPostsURL + "/" + uid)
	fb.Set(nil)
	qns, err := getPostNodes(QuestionType)
	if err != nil {
		return err
	}

	for pid := range qns {
		updateView(pid, uid)
	}

	c := make(chan firego.Event)
	if err := fb.Watch(c); err != nil {
		return err
	}

	// TODO:  this needs to be protected by a mutex
	userNotifications[uid] = c
	return nil
}
Exemplo n.º 4
0
func ExampleFirebase_IncludePriority() {
	fb := firego.New("https://someapp.firebaseio.com")
	// turn on
	fb.IncludePriority(true)
	// turn off
	fb.IncludePriority(false)
}
Exemplo n.º 5
0
func ExampleFirebase_Shallow() {
	fb := firego.New("https://someapp.firebaseio.com")
	// turn on
	fb.Shallow(true)
	// turn off
	fb.Shallow(false)
}
Exemplo n.º 6
0
func CustomerHandler(response http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	customer := vars["customer"]
	orderId := vars["order"]

	f := firego.New("https://example-app-123.firebaseio.com")
	f.Auth("<FIREBASE KEY>")

	var profileBuffer bytes.Buffer
	profileBuffer.WriteString("/customers/")
	profileBuffer.WriteString(customer)
	profileBuffer.WriteString("/profile/")

	var orderBuffer bytes.Buffer
	orderBuffer.WriteString("/customers/")
	orderBuffer.WriteString(customer)
	orderBuffer.WriteString("/orders/")
	orderBuffer.WriteString(orderId)

	profile := f.Child(profileBuffer.String())
	var p Profile
	if err := profile.Value(&p); err != nil {
		log.Fatal(err)
	}

	order := f.Child(orderBuffer.String())
	var o Order
	if err := order.Value(&o); err != nil {
		log.Fatal(err)
	}

	if _, err := Mailer(p, o); err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 7
0
func getPostNode(id string) (*PostNode, error) {
	fb := firego.New(postsURL + "/" + id)
	v := &PostNode{}
	if err := fb.Value(v); err != nil {
		return nil, err
	}
	return v, nil
}
Exemplo n.º 8
0
func getCommentNode(id string) (*CommentNode, error) {
	fb := firego.New(commentsURL + "/" + id)
	v := &CommentNode{}
	if err := fb.Value(v); err != nil {
		return nil, err
	}
	return v, nil
}
Exemplo n.º 9
0
func NewAPI(fbURL, fbSecret string) (*api, error) {
	if fbURL == "" || fbSecret == "" {
		return nil, errors.New("fbURL or fbSecret are empty")
	}
	fb := firego.New(fbURL)
	fb.Auth(fbSecret)
	return &api{fb: fb}, nil
}
Exemplo n.º 10
0
func getQuestionNode(id string) (*QuestionNode, error) {
	fb := firego.New(questionsURL + "/" + id)
	v := &QuestionNode{}
	if err := fb.Value(v); err != nil {
		return nil, err
	}
	return v, nil
}
Exemplo n.º 11
0
// getUser returns a User given a UID.  Note that it never returns an error;
// if a user isn't found, the User returned will have the zero value.
func getUser(uid string) User {
	fb := firego.New(usersURL + "/" + uid)
	var v UserNode
	if err := fb.Value(&v); err != nil {
		return User{}
	}
	return User{UID: uid, Name: v.Name, ImageURL: v.ImageURL}
}
Exemplo n.º 12
0
func getAnswerNode(id string) (*AnswerNode, error) {
	fb := firego.New(answersURL + "/" + id)
	v := &AnswerNode{}
	if err := fb.Value(v); err != nil {
		return nil, err
	}
	return v, nil
}
Exemplo n.º 13
0
func ExampleFirebase_Value() {
	fb := firego.New("https://someapp.firebaseio.com/some/value")
	var v interface{}
	if err := fb.Value(v); err != nil {
		log.Fatal(err)
	}

	log.Printf("My value %v\n", v)
}
Exemplo n.º 14
0
func ExampleFirebase_Push() {
	fb := firego.New("https://someapp.firebaseio.com")
	newRef, err := fb.Push("my-value")
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("My new ref %s\n", newRef)
}
Exemplo n.º 15
0
func addPostNode(uid string, t PostType) (string, error) {
	fb := firego.New(postsURL)

	p := PostNode{Type: t, Added: time.Now(), AddedBy: uid}
	n, err := fb.Push(p)
	if err != nil {
		return "", err
	}
	path := strings.Split(n.String(), "/")
	return path[len(path)-1], nil
}
Exemplo n.º 16
0
func TestFirebase(t *testing.T) {
	f := firego.New("https://peekapp.firebaseio.com/testnode")
	err := f.Set(struct {
		Status  string `json:"status"`
		Message string `json:"message"`
	}{"success", "sadasf"})

	if err != nil {
		t.Fatal(err)
	}
}
Exemplo n.º 17
0
func touchPostNode(id string) error {
	// find the users that are watching this post
	fb := firego.New(postUsersURL + "/" + id)
	var v map[string]interface{}
	if err := fb.Value(&v); err != nil {
		return err
	}

	now := time.Now()
	for uid := range v {
		// notify users that this post has changed.
		fb = firego.New(userPostsURL + "/" + uid + "/" + id)
		fb.Set(now)
		// TODO (rrossney):  is it really necessary to keep a last-modified
		// timestamp under postUsers?
		fb = firego.New(postUsersURL + "/" + id + "/" + uid)
		fb.Set(now)
	}
	return nil
}
Exemplo n.º 18
0
func ExampleFirebase_Set() {
	fb := firego.New("https://someapp.firebaseio.com")

	v := map[string]interface{}{
		"foo": "bar",
		"bar": 1,
		"bez": []string{"hello", "world"},
	}
	if err := fb.Set(v); err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 19
0
func getPostNodes(t PostType) (map[string]PostNode, error) {
	fb := firego.New(postsURL)
	var v map[string]PostNode
	if err := fb.Value(&v); err != nil {
		return nil, err
	}
	result := make(map[string]PostNode, 0)
	for pk, pn := range v {
		if pn.Type == t {
			result[pk] = pn
		}
	}
	return result, nil
}
Exemplo n.º 20
0
func ExampleFirebase_StopWatching() {
	fb := firego.New("https://someapp.firebaseio.com/some/value")
	notifications := make(chan firego.Event)
	if err := fb.Watch(notifications); err != nil {
		log.Fatal(err)
	}

	go func() {
		for _ = range notifications {
		}
		log.Println("Channel closed")
	}()
	time.Sleep(10 * time.Millisecond) // let go routine start

	fb.StopWatching()
}
Exemplo n.º 21
0
func ExampleFirebase_Watch() {
	fb := firego.New("https://someapp.firebaseio.com/some/value")
	notifications := make(chan firego.Event)
	if err := fb.Watch(notifications); err != nil {
		log.Fatal(err)
	}

	for event := range notifications {
		log.Println("Event Received")
		log.Printf("Type: %s\n", event.Type)
		log.Printf("Path: %s\n", event.Path)
		log.Printf("Data: %v\n", event.Data)
		if event.Type == firego.EventTypeError {
			log.Print("Error occurred, loop ending")
		}
	}
}
Exemplo n.º 22
0
Arquivo: main.go Projeto: jcbeaury/fd
func main() {
	// open the csv file
	csvfile, err := os.Open("players-complete.csv")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer csvfile.Close()

	// read the csv file
	reader := csv.NewReader(csvfile)
	rawCSVdata, err := reader.ReadAll()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// save csv data into array of structs
	var players []models.Player
	for _, player := range rawCSVdata {
		// convert strings to numbers
		fppg, err := strconv.ParseFloat(player[4], 64)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		projection, err := strconv.ParseFloat(player[12], 64)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		gp, err := strconv.Atoi(player[5])
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		salary, err := strconv.Atoi(player[6])
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		// save data in struct
		players = append(players, models.Player{
			player[0],
			player[1],
			player[2],
			player[3],
			fppg,
			gp,
			salary,
			player[7],
			player[8],
			player[9],
			player[10],
			player[11],
			projection,
		})
	}

	// organize players by position
	for _, player := range players {
		// skip injured mafks
		if player.Injury == "Q" || player.Injury == "O" || player.Projection < 5 {
			continue
		}
		switch player.Position {
		case "QB":
			if len(qbs) < 4 {
				qbs = append(qbs, player)
			}
		case "RB":
			if len(rbs) < 8 {
				rbs = append(rbs, player)
			}
		case "WR":
			if len(wrs) < 16 {
				wrs = append(wrs, player)
			}
		case "TE":
			if len(tes) < 4 {
				tes = append(tes, player)
			}
		case "K":
			if len(ks) < 4 {
				ks = append(ks, player)
			}
		case "D":
			if len(ds) < 4 {
				ds = append(ds, player)
			}
		default:
			fmt.Printf("player with unreadable position: %s", player)
			os.Exit(1)
		}
	}
	fmt.Printf("%d qb\n", len(qbs))
	fmt.Printf("%d rb\n", len(rbs))
	fmt.Printf("%d wr\n", len(wrs))
	fmt.Printf("%d te\n", len(tes))
	fmt.Printf("%d k\n", len(ks))
	fmt.Printf("%d d\n", len(ds))

	// find all rb combos
	for i, rb := range rbs {
		for j := i + 1; j < len(rbs); j++ {
			rbCombos = append(rbCombos, models.RbCombo{rb, rbs[j]})
		}
	}
	// find all qb-rb combos
	for _, qb := range qbs {
		for _, rbCombo := range rbCombos {
			qbRbCombos = append(qbRbCombos, models.QbRbCombo{qb, rbCombo.Rb1, rbCombo.Rb2})
		}
	}
	// find all wr combos
	for i, wr := range wrs {
		for j := i + 1; j < len(wrs); j++ {
			for k := j + 1; k < len(wrs); k++ {
				wrCombos = append(wrCombos, models.WrCombo{wr, wrs[j], wrs[k]})
			}
		}
	}
	// find all te-k-d combos
	for _, te := range tes {
		for _, k := range ks {
			for _, d := range ds {
				teKDCombos = append(teKDCombos, models.TeKDCombo{te, k, d})
			}
		}
	}
	fmt.Printf("%d qb-rb combos\n", len(qbRbCombos))
	fmt.Printf("%d wr combos\n", len(wrCombos))
	fmt.Printf("%d te-k-d combos\n", len(teKDCombos))

	// find number of combos
	allCombos := len(qbRbCombos) * len(teKDCombos) * len(wrCombos)
	fmt.Printf("%d total combos\n", allCombos)

	// connect to firebase
	firebase := firego.New("https://fanduel.firebaseio.com/")
	firebase.Auth("C3utgR1QlRSKOup1MjVAr6rMIu5h8DP5WaeXlEc4")
	functions.ClearFirebase(firebase)

	// construct all rosters
	var wg sync.WaitGroup
	wg.Add(len(qbRbCombos))
	for _, qbRbCombo := range qbRbCombos {
		go qbRbRosterCombos(qbRbCombo, firebase, &wg)
	}
	wg.Wait()
	wg.Add(len(wrCombos))
	for _, wrCombo := range wrCombos {
		go wrRosterCombos(wrCombo, firebase, &wg)
	}
	wg.Wait()
	wg.Add(len(teKDCombos))
	for _, teKDCombo := range teKDCombos {
		go teKDRosterCombos(teKDCombo, firebase, &wg)
	}
	wg.Wait()

	// get all the uploaded rosters
	var rMap map[string]models.Roster
	var rosters []models.Roster
	if err := firebase.Value(&rMap); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	for _, roster := range rMap {
		rosters = append(rosters, roster)
	}
	sort.Sort(ByProjection(rosters))

	// write rosters to a csv file
	newcsvfile, err := os.Create("rosters.csv")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer newcsvfile.Close()
	writer := csv.NewWriter(newcsvfile)
	err = writer.Write([]string{"fppg", "projection", "qb", "rb1", "rb2", "wr1", "wr2", "wr3", "te", "k", "d"})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	for _, roster := range rosters {
		err = writer.Write([]string{
			fmt.Sprintf("%g", roster.Fppg),
			fmt.Sprintf("%g", roster.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Qb.FirstName, roster.Qb.LastName, roster.Qb.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Rb1.FirstName, roster.Rb1.LastName, roster.Rb1.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Rb2.FirstName, roster.Rb2.LastName, roster.Rb2.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Wr1.FirstName, roster.Wr1.LastName, roster.Wr1.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Wr2.FirstName, roster.Wr2.LastName, roster.Wr2.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Wr3.FirstName, roster.Wr3.LastName, roster.Wr3.Projection),
			fmt.Sprintf("%s %s (%g)", roster.Te.FirstName, roster.Te.LastName, roster.Te.Projection),
			fmt.Sprintf("%s %s (%g)", roster.K.FirstName, roster.K.LastName, roster.K.Projection),
			fmt.Sprintf("%s %s (%g)", roster.D.FirstName, roster.D.LastName, roster.D.Projection),
		})
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}
	writer.Flush()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Exemplo n.º 23
0
Arquivo: main.go Projeto: thinkhike/ci
func build(commit *hookserve.Event) {
	key := strings.Join([]string{commit.Owner, commit.Repo, commit.Commit}, ",")
	if _, ok := mymap.Get(key); ok {
		return
	} else {
		mymap.Set(key, "pending")
	}
	defer mymap.Remove(key)
	fmt.Println("Building: ", commit.Owner, commit.Repo, commit.Branch, commit.Commit)

	currentStatus := PENDING

	buildURL := fmt.Sprintf("https://builds.firebaseio.com/%s/%s/%s/%s", commit.Owner, commit.Repo, commit.Branch, commit.Commit)
	f := firego.New(buildURL)
	f.Auth(os.Getenv("FIREBASE_SECRET"))
	v := map[string]string{
		"status": currentStatus,
	}
	pushedFirego, err := f.Push(v)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", pushedFirego)

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: os.Getenv("GH_TOKEN")},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client := github.NewClient(tc)
	repoStatus, _, err := client.Repositories.CreateStatus(commit.Owner, commit.Repo, commit.Commit,
		&github.RepoStatus{
			State:       sPtr(currentStatus),
			TargetURL:   sPtr(buildURL),
			Description: sPtr("The build started"),
			Context:     sPtr("ci/builds"),
		})

	if err != nil {
		fmt.Println(err)
	}
	jsonBytes := prepare.Main(commit.Owner, commit.Repo)
	var statusVal map[string]string
	json.Unmarshal(jsonBytes, &statusVal)
	if statusVal != nil {
		fmt.Println("Got the following status: ", statusVal["status"])
		currentStatus = statusVal["status"]
	}
	wait()
	pushedFirego.Update(map[string]string{"status": currentStatus})
	repoStatus, _, err = client.Repositories.CreateStatus(commit.Owner, commit.Repo, commit.Commit,
		&github.RepoStatus{
			State:       sPtr(currentStatus),
			TargetURL:   sPtr(buildURL),
			Description: sPtr("The build succeeded"),
			Context:     sPtr("ci/builds"),
		})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(repoStatus)
}
Exemplo n.º 24
0
func putQuestionNode(id string, q *QuestionNode) error {
	fb := firego.New(questionsURL + "/" + id)
	return fb.Set(q)
}
Exemplo n.º 25
0
func NewFirebaseTracker() *FirebaseTracker {
	return &FirebaseTracker{firego.New(firebaseRoot)}
}
Exemplo n.º 26
0
func ExampleFirebase_Update() {
	fb := firego.New("https://someapp.firebaseio.com/some/value")
	if err := fb.Update("new-value"); err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 27
0
func putCommentNode(id string, c *CommentNode) error {
	fb := firego.New(commentsURL + "/" + id)
	return fb.Set(c)
}
Exemplo n.º 28
0
func ExampleFirebase_Auth() {
	fb := firego.New("https://someapp.firebaseio.com")
	fb.Auth("my-token")
}
Exemplo n.º 29
0
func putAnswerNode(id string, a *AnswerNode) error {
	fb := firego.New(answersURL + "/" + id)
	return fb.Set(a)
}
Exemplo n.º 30
0
func putUserNode(uid string, u *UserNode) error {
	fb := firego.New(usersURL + "/" + uid)
	return fb.Set(u)
}