iowrappers

package
v0.0.0-...-8f783ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GoogleNearbySearchDelay             = time.Second
	GoogleMapsSearchTimeout             = time.Second * 10
	GoogleMapsSearchCallMaxCount        = 5
	GoogleNearbySearchMaxRadiusInMeters = 50000
)
View Source
const (
	ValidPrefix        = "https://lh3.googleusercontent.com/places/"
	UnknownImageFormat = "unknown image format"
)
View Source
const (
	MaxSearchRadius              = 16000               // 10 miles
	MinMapsResultRefreshDuration = time.Hour * 24 * 14 // 14 days
	GoogleSearchHomePageURL      = "https://www.google.com/"
	ContextRequestIdKey          = ContextKey("request_id")
)
View Source
const (
	PlanningSolutionsExpirationTime = 24 * time.Hour
	PlanningStatExpirationTime      = 24 * time.Hour
	CityInfoExpirationTime          = 0

	NumVisitorsPlanningAPI         = "visitor_count:planning_APIs"
	NumVisitorsPrefix              = "visitor_count"
	TravelPlansRedisCacheKeyPrefix = "travel_plans"
	TravelPlanRedisCacheKeyPrefix  = "travel_plan"
	CityRedisKeyPrefix             = "city"
	CitiesRedisKey                 = "known_cities_ids"
	KnownCitiesHashMapRedisKey     = "known_cities_name_to_id"
	MapsLastSearchTimeRedisKey     = "MapsLastSearchTime"
	// AnnouncementsRedisKey is a Redis Hash that maps ID to announcement details
	AnnouncementsRedisKey      = "announcements"
	PlaceDetailsRedisKeyPrefix = "place_details:place_ID:"
)
View Source
const (
	PlaceDetailsKeyPrefix = "place_details"
	PlaceIDsKeyPrefix     = "placeIDs"
)
View Source
const (
	UserKeyPrefix       = "user"
	UserPlanWorkerCount = 5
)
View Source
const (
	UserSavedTravelPlansPrefix = "user_saved_travel_plans"
	UserSavedTravelPlanPrefix  = "user_saved_travel_plan"

	//UserNamesKey maps usernames to IDs
	UserNamesKey = "user_names"

	//UserEmailsKey maps emails to IDs
	UserEmailsKey = "user_emails"

	//EmailVerificationCodes maps email to verification code
	EmailVerificationCodes = "email_verification_codes"

	FindUserByName  FindUserBy = "FindUserByName"
	FindUserByID    FindUserBy = "FindUserByID"
	FindUserByEmail FindUserBy = "FindUserByEmail"
)
View Source
const (
	BatchSize = 300
)
View Source
const ReverseIndexStringPrefix string = "reverse_index"

Variables

View Source
var RedisClientDefaultBlankContext context.Context

Functions

func CreateLogger

func CreateLogger() error

func CreateMapSearchRequest

func CreateMapSearchRequest(reqIn *PlaceSearchRequest, placeType POI.LocationType, token string) (reqOut maps.NearbySearchRequest)

CreateMapSearchRequest creates a NearbySearchRequest for maps NearbySearch, adjust key settings such as radius and price levels

func DestroyLogger

func DestroyLogger()

func Filter

func Filter[T any](places []T, condition func(place T) bool) []T

Filter places that meet certain condition

func PlaceDetailsSearchWrapper

func PlaceDetailsSearchWrapper(context context.Context, mapsClient *MapsClient, idx int, placeId string, fields []string, detailSearchRes *PlaceDetailsSearchResult, wg *sync.WaitGroup)

Types

type City

type City struct {
	ID         string  `json:"id"`
	GeonameID  int64   `json:"geonameId"`
	Name       string  `json:"name"`
	Latitude   float64 `json:"lat"`
	Longitude  float64 `json:"lng"`
	Population int64   `json:"population"`
	AdminArea1 string  `json:"adminArea1"`
	Country    string  `json:"country"`
}

type ContextKey

type ContextKey string

type EmailType

type EmailType string
const EmailVerification EmailType = "Email Verification"
const PasswordReset EmailType = "Password Reset"

type FindUserBy

type FindUserBy string

type GeocodeQuery

type GeocodeQuery struct {
	City              string `json:"city"`
	AdminAreaLevelOne string `json:"admin_area_level_one"`
	Country           string `json:"country"`
}

GeocodeQuery can also be used as the result of reverse geocoding

type GoogleOAuthResponse

type GoogleOAuthResponse struct {
	ID            string `json:"id"`
	Email         string `json:"email"`
	VerifiedEmail bool   `json:"verified_email"`
	PictureURL    string `json:"picture"`
}

type IndexMetadata

type IndexMetadata struct {
	Location POI.Location
	Weekday  POI.Weekday
	Interval POI.TimeInterval
}

type Mailer

type Mailer struct {
	// contains filtered or unexported fields
}

func (*Mailer) Broadcast

func (m *Mailer) Broadcast(ctx context.Context, subject, message, environment string) error

Broadcast sends a message to all users

func (*Mailer) Init

func (m *Mailer) Init(redisClient *RedisClient) error

func (*Mailer) Send

func (m *Mailer) Send(ctx context.Context, t EmailType, recipient user.View, environment string) error

type MapsClient

type MapsClient struct {
	DetailedSearchFields []string
	// contains filtered or unexported fields
}

func CreateMapsClient

func CreateMapsClient(apiKey string) *MapsClient

CreateMapsClient is a factory method for MapsClient

func (*MapsClient) Geocode

func (c *MapsClient) Geocode(ctx context.Context, query *GeocodeQuery) (lat float64, lng float64, err error)

func (*MapsClient) GoogleMapsNearbySearchWrapper

func (c *MapsClient) GoogleMapsNearbySearchWrapper(ctx context.Context, mapsReq maps.NearbySearchRequest) (resp maps.PlacesSearchResponse, err error)

func (*MapsClient) NearbySearch

func (c *MapsClient) NearbySearch(ctx context.Context, request *PlaceSearchRequest) ([]POI.Place, error)

func (*MapsClient) PlaceDetailedSearch

func (c *MapsClient) PlaceDetailedSearch(context context.Context, placeId string, fields []string) (maps.PlaceDetailsResult, error)

func (*MapsClient) ReverseGeocode

func (c *MapsClient) ReverseGeocode(context context.Context, latitude, longitude float64) (*GeocodeQuery, error)

func (*MapsClient) SetDetailedSearchFields

func (c *MapsClient) SetDetailedSearchFields(fields []string)

type MapsPhotoClient

type MapsPhotoClient struct {
	// contains filtered or unexported fields
}

func (*MapsPhotoClient) GetPhotoURL

func (c *MapsPhotoClient) GetPhotoURL(ctx context.Context, photoRef string, placeId string) (PhotoURL, error)

type NearbyCityRequest

type NearbyCityRequest struct {
	ApiKey   string                  `json:"apiKey"`
	Location POI.Location            `json:"location"`
	Radius   float64                 `json:"radius"`
	Filter   gogeonames.SearchFilter `json:"filter"`
}

type NearbyCityResponse

type NearbyCityResponse struct {
	Cities []City `json:"cities"`
}

type PhotoClient

type PhotoClient interface {
	GetPhotoURL(context.Context, string, string) (PhotoURL, error)
}

func CreatePhotoClient

func CreatePhotoClient(apiKey string, baseURL string, enableMapPhotoClient bool, placeDetailsFields []string, redisClient *RedisClient) (PhotoClient, error)

CreatePhotoClient is a factory method for PhotoClient

type PhotoHttpClient

type PhotoHttpClient struct {
	// contains filtered or unexported fields
}

func (*PhotoHttpClient) GetPhotoURL

func (photoClient *PhotoHttpClient) GetPhotoURL(ctx context.Context, photoRef string, s string) (PhotoURL, error)

type PhotoURL

type PhotoURL string

type PlaceDetailsFields

type PlaceDetailsFields string
const (
	PlaceDetailsFieldURL              PlaceDetailsFields = "URL"
	PlaceDetailsFieldPhoto            PlaceDetailsFields = "photo"
	PlaceDetailsFieldUserRatingsCount PlaceDetailsFields = "ratings_count"
)

type PlaceDetailsSearchResult

type PlaceDetailsSearchResult struct {
	// contains filtered or unexported fields
}

type PlaceSearchRequest

type PlaceSearchRequest struct {
	// "visit", "eatery", etc...
	PlaceCat POI.PlaceCategory

	Location POI.Location
	// search radius
	Radius uint
	// minimum number of results, set this lower limit for reducing risk of zero result in upper-layer computations.
	// suppose a location has more places established over time, this field would help trigger new searches to get those new establishments.
	MinNumResults uint

	BusinessStatus POI.BusinessStatus
	// true if using precise geolocation instead of using a grander administrative area
	UsePreciseLocation bool

	PriceLevel POI.PriceLevel
}

type PlanningEvent

type PlanningEvent struct {
	User      string `json:"user"`
	City      string `json:"city"`
	Country   string `json:"country"`
	Timestamp string `json:"timestamp"`
}

type PlanningSolutionRecord

type PlanningSolutionRecord struct {
	ID              string              `json:"id"`
	PlaceIDs        []string            `json:"place_ids"`
	Score           float64             `json:"score"`
	ScoreOld        float64             `json:"score_old"`
	PlaceNames      []string            `json:"place_names"`
	PlaceLocations  [][2]float64        `json:"place_locations"`
	PlaceAddresses  []string            `json:"place_addresses"`
	PlaceURLs       []string            `json:"place_urls"`
	PlaceCategories []POI.PlaceCategory `json:"place_categories"`
	Destination     POI.Location        `json:"destination"`
}

type PlanningSolutionsResponse

type PlanningSolutionsResponse struct {
	PlanningSolutionRecords []PlanningSolutionRecord `json:"cached_planning_solutions"`
}

type PlanningSolutionsSaveRequest

type PlanningSolutionsSaveRequest struct {
	Location                POI.Location
	PriceLevel              POI.PriceLevel
	PlaceCategories         []POI.PlaceCategory
	Intervals               []POI.TimeInterval
	Weekdays                []POI.Weekday
	PlanningSolutionRecords []PlanningSolutionRecord
}

type PoiSearcher

type PoiSearcher struct {
	// contains filtered or unexported fields
}

func CreatePoiSearcher

func CreatePoiSearcher(mapsApiKey string, redisUrl *url.URL) *PoiSearcher

func (*PoiSearcher) AddUrl

func (s *PoiSearcher) AddUrl(context context.Context) error

func (*PoiSearcher) AddUserRatingsTotal

func (s *PoiSearcher) AddUserRatingsTotal(context context.Context) error

func (*PoiSearcher) Geocode

func (s *PoiSearcher) Geocode(context context.Context, query *GeocodeQuery) (lat float64, lng float64, err error)

Geocode performs geocoding, mapping city and country to latitude and longitude

func (*PoiSearcher) GetMapsClient

func (s *PoiSearcher) GetMapsClient() *MapsClient

func (*PoiSearcher) GetRedisClient

func (s *PoiSearcher) GetRedisClient() *RedisClient

func (*PoiSearcher) NearbyCities

func (s *PoiSearcher) NearbyCities(ctx context.Context, req *NearbyCityRequest) (NearbyCityResponse, error)

func (*PoiSearcher) NearbySearch

func (s *PoiSearcher) NearbySearch(context context.Context, request *PlaceSearchRequest) ([]POI.Place, error)

func (*PoiSearcher) RemovePlaces

func (s *PoiSearcher) RemovePlaces(context context.Context, nonEmptyFields []PlaceDetailsFields) error

func (*PoiSearcher) ReverseGeocode

func (s *PoiSearcher) ReverseGeocode(ctx context.Context, lat, lng float64) (*GeocodeQuery, error)

func (*PoiSearcher) UpdateRedis

func (s *PoiSearcher) UpdateRedis(context context.Context, places []POI.Place)

type RedisClient

type RedisClient struct {
	// contains filtered or unexported fields
}

func CreateRedisClient

func CreateRedisClient(url *url.URL) *RedisClient

CreateRedisClient is a factory method for RedisClient

func (*RedisClient) AddCities

func (r *RedisClient) AddCities(ctx context.Context, cities []City) error

func (*RedisClient) Authenticate

func (r *RedisClient) Authenticate(context context.Context, credential user.Credential) (user.View, string, time.Time, error)

func (*RedisClient) CacheLocationAlias

func (r *RedisClient) CacheLocationAlias(context context.Context, query GeocodeQuery, correctedQuery GeocodeQuery) (err error)

CacheLocationAlias caches the mapping from user input location name to geo-coding-corrected location name correct location name is an alias of itself

func (*RedisClient) CollectPlanningAPIStats

func (r *RedisClient) CollectPlanningAPIStats(event PlanningEvent)

CollectPlanningAPIStats generates analytics of total number of unique visitors to the planning APIs in the last 24 hours analytics of number of unique users planning for each city

func (*RedisClient) CreateReverseIndex

func (r *RedisClient) CreateReverseIndex(ctx context.Context, index *ReverseIndex) error

func (*RedisClient) CreateUser

func (r *RedisClient) CreateUser(context context.Context, userView user.View, skipPasswordGeneration bool) (user.View, error)

func (*RedisClient) CreateUserOnEmailVerified

func (r *RedisClient) CreateUserOnEmailVerified(ctx context.Context, tmpUserID string) error

func (*RedisClient) DeleteUserPlan

func (r *RedisClient) DeleteUserPlan(context context.Context, userView user.View, planView user.TravelPlanView) error

func (*RedisClient) Destroy

func (r *RedisClient) Destroy()

Destroy closes Redis connection from the client

func (*RedisClient) FetchSingleRecord

func (r *RedisClient) FetchSingleRecord(context context.Context, redisKey string, response interface{}) error

func (*RedisClient) FindUser

func (r *RedisClient) FindUser(context context.Context, findUserBy FindUserBy, userView user.View) (user.View, error)

func (*RedisClient) FindUserPlans

func (r *RedisClient) FindUserPlans(ctx context.Context, userView user.View) []user.TravelPlanView

func (*RedisClient) Geocode

func (r *RedisClient) Geocode(context context.Context, query *GeocodeQuery) (lat float64, lng float64, err error)

func (*RedisClient) Get

func (r *RedisClient) Get() *redis.Client

func (*RedisClient) GetCities

func (r *RedisClient) GetCities(context context.Context) (map[string]string, error)

func (*RedisClient) GetLocationWithAlias

func (r *RedisClient) GetLocationWithAlias(context context.Context, query *GeocodeQuery) (string, error)

func (*RedisClient) GetMapsLastSearchTime

func (r *RedisClient) GetMapsLastSearchTime(context context.Context, location POI.Location, category POI.PlaceCategory, priceLevel POI.PriceLevel) (lastSearchTime time.Time, err error)

func (*RedisClient) GetPlaceCountByCategory

func (r *RedisClient) GetPlaceCountByCategory(context context.Context, category POI.PlaceCategory) (int64, error)

func (*RedisClient) GetPlaceCountInRedis

func (r *RedisClient) GetPlaceCountInRedis(context context.Context) (placeKeys []string, count int, err error)

func (*RedisClient) NearbyCities

func (r *RedisClient) NearbyCities(ctx context.Context, lat, lng, radius float64, filter gogeonames.SearchFilter) ([]City, error)

func (*RedisClient) NearbySearch

func (r *RedisClient) NearbySearch(ctx context.Context, req *PlaceSearchRequest) ([]POI.Place, error)

func (*RedisClient) PlanningSolutions

func (*RedisClient) RemoveKeys

func (r *RedisClient) RemoveKeys(context context.Context, keys []string) (err error)

func (*RedisClient) RemovePlaces

func (r *RedisClient) RemovePlaces(context context.Context, nonEmptyFields []PlaceDetailsFields) error

func (*RedisClient) RetrieveReverseIndex

func (r *RedisClient) RetrieveReverseIndex(ctx context.Context, metadata *IndexMetadata, count int64) (*ReverseIndex, error)

func (*RedisClient) ReverseGeocode

func (r *RedisClient) ReverseGeocode(context.Context, float64, float64) (*GeocodeQuery, error)

func (*RedisClient) SaveAnnouncement

func (r *RedisClient) SaveAnnouncement(ctx context.Context, id, data string) error

func (*RedisClient) SavePlanningSolutions

func (r *RedisClient) SavePlanningSolutions(ctx context.Context, request *PlanningSolutionsSaveRequest) error

func (*RedisClient) SaveUserPlan

func (r *RedisClient) SaveUserPlan(context context.Context, userView user.View, planView *user.TravelPlanView) error

func (*RedisClient) SetGeocode

func (r *RedisClient) SetGeocode(context context.Context, query GeocodeQuery, lat float64, lng float64, originalQuery GeocodeQuery)

func (*RedisClient) SetMapsLastSearchTime

func (r *RedisClient) SetMapsLastSearchTime(context context.Context, location POI.Location, category POI.PlaceCategory, priceLevel POI.PriceLevel, requestTime string) (err error)

func (*RedisClient) SetPassword

func (r *RedisClient) SetPassword(ctx context.Context, req *user.PasswordResetRequest) error

func (*RedisClient) SetPlacesAddGeoLocations

func (r *RedisClient) SetPlacesAddGeoLocations(c context.Context, places []POI.Place)

SetPlacesAddGeoLocations stores two types of information in redis 1. key-value pair, {placeID: POI.place} 2. add place to the correct bucket in geohashing for nearby search

func (*RedisClient) StorePlacesForLocation

func (r *RedisClient) StorePlacesForLocation(context context.Context, geocodeInString string, places []POI.Place) error

StorePlacesForLocation is deprecated, but it is still a primitive implementation that might have faster search time compared with all places stored under one key store places obtained from database or external API in Redis places for a location are stored in separate sorted sets based on category

func (*RedisClient) StreamsLogging

func (r *RedisClient) StreamsLogging(streamName string, data map[string]string) string

StreamsLogging returns redis streams ID if XADD command execution is successful

func (*RedisClient) UpdatePlace

func (r *RedisClient) UpdatePlace(ctx context.Context, id string, data map[string]interface{}) error

func (*RedisClient) UpdateSearchHistory

func (r *RedisClient) UpdateSearchHistory(ctx context.Context, location string, userView *user.View) error

func (*RedisClient) UpdateUser

func (r *RedisClient) UpdateUser(ctx context.Context, view *user.View) error

UpdateUser should only accept a complete view of the user so that no user information is lost after update.

func (*RedisClient) VerifyPasswordResetRequest

func (r *RedisClient) VerifyPasswordResetRequest(ctx context.Context, req *user.PasswordResetRequest) VerificationResult

type ReverseIndex

type ReverseIndex struct {
	Metadata *IndexMetadata
	Scores   []float64
	PlaceIDs []string
}

type SearchClient

type SearchClient interface {
	Geocode(context.Context, *GeocodeQuery) (float64, float64, error)        // translate a textual location to latitude and longitude
	ReverseGeocode(context.Context, float64, float64) (*GeocodeQuery, error) // look up a textual location based on latitude and longitude
	NearbySearch(context.Context, *PlaceSearchRequest) ([]POI.Place, error)  // search nearby places in a category around a central location
}

SearchClient defines an interface of a client that performs location-based operations such as nearby search

type VerificationResult

type VerificationResult struct {
	Message    error
	HttpStatus int
}

type View

type View interface {
	user.View | user.TravelPlanView
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL