Exemplo n.º 1
0
func (d *DB) RecentSuggestionSessions(n uint64) ([]tokenData, error) {
	psql := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
	q := psql.
		Select("token", "created_at", "data::json->'URL'").
		From("samples").
		Where(squirrel.Eq{"type": "NewClientToken"}).
		OrderBy("ID desc").
		Limit(n)

	rows, err := q.RunWith(d.cache).Query()
	if err != nil {
		lg.Fatal(err)
	}
	defer rows.Close()
	var tokens []tokenData
	for rows.Next() {
		var token tokenData
		var ID string
		err := rows.Scan(&ID, &token.CreatedAt, &token.URL)
		if err != nil {
			lg.Fatal(err)
		}
		token.ID = shared.SuggestionToken(ID)
		tokens = append(tokens, token)
	}
	return tokens, nil
}
Exemplo n.º 2
0
func (s *sessionTokenStore) New(URL string) shared.SuggestionToken {
	idstr, err := shared.SecureRandomString(32)
	if err != nil {
		panic(err)
	}
	id := shared.SuggestionToken(idstr)
	s.Lock()
	s.sessions[id] = tokenData{
		ID:        id,
		CreatedAt: time.Now(),
		URL:       URL,
	}
	s.Unlock()
	tokenSessionsActive.Add(1)
	tokenSessionsTotal.Add(1)
	return id
}
Exemplo n.º 3
0
func (d *DB) GetSessionSamples(token shared.SuggestionToken) ([]Sample, error) {
	psql := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)

	//	fromID := 0
	i := psql.
		Select("id", "host", "country_code", "asn", "created_at", "origin", "type", "token", "data", "extra_data").
		From("samples").
		Where(squirrel.Eq{"token": string(token)})

	rows, err := i.RunWith(d.cache).Query()

	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var results []Sample

	for rows.Next() {
		var sample Sample
		var token string
		err := rows.Scan(
			&sample.ID,
			&sample.Host,
			&sample.CountryCode,
			&sample.ASN,
			&sample.CreatedAt,
			&sample.Origin,
			&sample.Type,
			&token,
			&sample.Data,
			&sample.ExtraData,
		)
		if err != nil {
			lg.Warning(err)
			continue
		}
		sample.Token = shared.SuggestionToken(token)
		results = append(results, sample)
	}

	return results, nil
}
Exemplo n.º 4
0
func (d *DB) GetSamples(fromID uint64, sampleType string) (chan Sample, error) {
	psql := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)

	i := psql.
		Select("id", "host", "country_code", "asn", "created_at", "origin", "type", "token", "data", "extra_data").
		From("samples").
		Where("id > ?", fromID)
	rows, err := i.RunWith(d.cache).Query()
	if err != nil {
		return nil, err
	}

	results := make(chan Sample, 0)
	go func(rows *sql.Rows) {
		defer rows.Close()
		defer close(results)
		for rows.Next() {
			var token string
			var sample Sample
			err := rows.Scan(
				&sample.ID,
				&sample.Host,
				&sample.CountryCode,
				&sample.ASN,
				&sample.CreatedAt,
				&sample.Origin,
				&sample.Type,
				&token,
				&sample.Data,
				&sample.ExtraData,
			)
			if err != nil {
				lg.Warning(err)
				continue
			}
			sample.Token = shared.SuggestionToken(token)
			results <- sample
		}
	}(rows)
	return results, nil
}