iowrappers

package
v0.0.0-...-3c848be Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 41 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GoogleMapsSearchTimeout             = time.Second * 10
	GoogleMapsSearchCallMaxCount        = 5
	GoogleNearbySearchMaxRadiusInMeters = 50000
	MaxConcurrentAPIRequests            = 5
)
View Source
const (
	ValidPrefix        = "https://lh3.googleusercontent.com/places/"
	UnknownImageFormat = "unknown image format"
)
View Source
const (
	MaxSearchRadius              = 16000               // 10 miles, upper bound for radius requested by callers
	ColdStartSearchRadius        = 8000                // 5 miles, radius used for external maps searches that populate the cache
	MinMapsResultRefreshDuration = time.Hour * 24 * 14 // 14 days
	// MinEmptyResultRefreshDuration is the refresh window for a search that came back with
	// nothing. Shorter than MinMapsResultRefreshDuration so an area that has just been built
	// out is retried within a day, rather than being frozen for the full two weeks.
	MinEmptyResultRefreshDuration = time.Hour * 24 // 1 day
	// PlaceDetailsRefreshDuration is how long a stored place's Details-sourced fields (opening
	// hours, formatted address, URL, editorial summary) are trusted before a cold search buys
	// them again. Business status arrives with every Nearby Search, so closures are still caught
	// by the Operational filter regardless of this window.
	PlaceDetailsRefreshDuration = time.Hour * 24 * 90 // 90 days
	GoogleSearchHomePageURL     = "https://www.google.com/"
	ContextRequestIdKey         = ContextKey("request_id")
	ContextRequestUserId        = ContextKey("user_id")
)
View Source
const (
	PlanningSolutionsExpirationTime = 24 * time.Hour
	CityInfoExpirationTime          = 0

	NumVisitorsPlanningAPI = "visitor_count:planning_api"

	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 (
	// PlaceSearchCandidateRedisKeyPrefix stores the full POI.Place a text search returned, keyed
	// by place ID, so a later confirm can resolve it without a second Google call and without
	// trusting any place data the confirm request itself might carry.
	PlaceSearchCandidateRedisKeyPrefix = "place_search_candidate:place_ID:"
	// PlaceSearchCandidateTTL bounds how long a search result stays confirmable. Long enough for
	// a user to review results and pick one, short enough that stale candidates do not accumulate.
	PlaceSearchCandidateTTL = 30 * time.Minute
	// PlaceTextSearchMaxResults is one legacy Text Search page; the legacy API does not support
	// paging through more without a page token round trip, which this feature does not need.
	PlaceTextSearchMaxResults = 20
)
View Source
const (
	UserKeyPrefix       = "user"
	UserPlanWorkerCount = 5
)
View Source
const (
	UserSavedTravelPlansPrefix = "user_saved_travel_plans"
	UserSavedTravelPlanPrefix  = "user_saved_travel_plan"
	UserSearchHistoryPrefix    = "users:search_history"

	//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 JobRedisKeyPrefix = "job:"
View Source
const (
	PlaceDetailsKeyPrefix = "place_details"
)
View Source
const ReverseGeocodeExpiration = 30 * 24 * time.Hour

ReverseGeocodeExpiration bounds how long a cached reverse-geocode result is served. City-level info is effectively static, but Google's Geocoding terms only allow temporary caching — 30 days keeps the cache self-cleaning and policy-friendly.

View Source
const ReverseIndexStringPrefix string = "reverse_index"

Variables

View Source
var (
	// ErrSearchCandidateNotFound means the place ID either was never searched or its stash entry
	// expired (see PlaceSearchCandidateTTL).
	ErrSearchCandidateNotFound = errors.New("place search candidate not found or expired")
	// ErrUnsupportedPlaceType means the candidate's primary Google type does not map to any
	// POI.PlaceCategory. The insert refuses entirely on this path: writing the place would force
	// a guess at which geo bucket it belongs in, which is exactly how the fast_food_restaurant
	// incident poisoned the eatery bucket with hotels (see POI/categories.go).
	ErrUnsupportedPlaceType = errors.New("place type does not map to a supported category")
)

Functions

func CreateLogger

func CreateLogger() error

func CreateMapSearchRequest

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

CreateMapSearchRequest creates a NearbySearchRequest for maps NearbySearch, adjust key settings such as radius and price levels. It rejects any place type the legacy Places API does not define: POI.LocationType is cast straight to maps.PlaceType and forwarded as ?type=, and Google responds to an unknown value by IGNORING the filter and returning prominence-ranked establishments rather than erroring. Those results are then stamped with the queried type, so an unvalidated type silently poisons the cache. maps.ParsePlaceType is the SDK's own list of legal values.

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 MatchesBrandName

func MatchesBrandName(placeName, keyword string) bool

MatchesBrandName reports whether a place name matches a brand keyword after normalization, e.g. "Dunkin' Donuts #1234" matches keyword "Dunkin'"

func PlaceDetailsSearchWrapper

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

func SortPlacesByDistance

func SortPlacesByDistance(places []POI.Place, lat, lng float64)

SortPlacesByDistance orders places ascending by distance from (lat, lng).

Callers that truncate a candidate list to a limit MUST sort first. Only the Redis cache path returns places in distance order; the fresh path appends one place type's results after another in Google prominence order, so an unsorted slice[:limit] drops the last place types wholesale and can rank a 3km result above a 250m one.

The sort is stable so places at equal distance keep their existing relative order.

func TravelPlansCacheKey

func TravelPlansCacheKey(req *PlanningSolutionsSaveRequest) (string, error)

Types

type AddSearchedPlaceResult

type AddSearchedPlaceResult struct {
	Place         POI.Place         `json:"place"`
	Category      POI.PlaceCategory `json:"category"`
	AlreadyCached bool              `json:"alreadyCached"`
}

AddSearchedPlaceResult is the outcome of confirming a search candidate into the shared cache.

type BucketCleanupReport

type BucketCleanupReport struct {
	BucketSizes   map[string]int64 `json:"bucket_sizes"`
	TotalMembers  int64            `json:"total_members"`
	Scanned       int              `json:"scanned"`
	Misclassified int              `json:"misclassified"`
	Removed       int              `json:"removed"`
	RemovedIDs    []string         `json:"removed_ids"`
}

BucketCleanupReport summarizes a RemoveMisclassifiedPlacesFromCategoryBuckets run. BucketSizes/TotalMembers are measured with ZCARD before the scan begins so a dry-run report states the scale of the job even for buckets an operator has never sized.

type CachedPlaceLookup

type CachedPlaceLookup func(ctx context.Context, placeIDs []string) (map[string]POI.Place, error)

CachedPlaceLookup resolves already-stored place records by ID, returning only the IDs it found. It is injected as a function rather than a RedisClient reference so MapsClient keeps no dependency on the cache and stays constructible without one; a nil lookup means every candidate gets a Place Details call, which is the behaviour before caching was consulted.

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 EateryBucketUnionReport

type EateryBucketUnionReport struct {
	SourceKeys    []string         `json:"source_keys"`
	SourceSizes   map[string]int64 `json:"source_sizes"`
	SourceTotal   int64            `json:"source_total"`
	TargetKey     string           `json:"target_key"`
	TargetBefore  int64            `json:"target_before"`
	ExpectedAfter int64            `json:"expected_after"`
	TargetAfter   int64            `json:"target_after"`
}

EateryBucketUnionReport describes a run of UnionEateryPriceBucketsIntoCategoryBucket. ExpectedAfter is computed by reading members rather than by writing, so a dry run states the exact resulting size without touching anything.

type EmailType

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

type Favorites

type Favorites struct {
	MostFrequentSearch string `json:"most_frequent_search"`
}

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

func (*GeocodeQuery) String

func (gq *GeocodeQuery) String() string

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 Job

type Job struct {
	ID          string      `json:"id"`
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Parameters  interface{} `json:"parameters"`
	Status      JobStatus   `json:"status"`
	Priority    JobPriority `json:"priority"`
	CreatedAt   time.Time   `json:"created_at"`
	UpdatedAt   time.Time   `json:"updated_at"`
}

type JobExecution

type JobExecution struct {
	JobID     string    `json:"job_id"`
	Status    JobStatus `json:"status"`
	ExpiresAt time.Time `json:"expires_at"`
}

JobExecution helps the service to determine if the job result is still valid

type JobPriority

type JobPriority int
const (
	JobPriorityLow    JobPriority = 0
	JobPriorityNormal JobPriority = 1
	JobPriorityHigh   JobPriority = 2
)

type JobStatus

type JobStatus string
const (
	JobStatusNew        JobStatus = "new"
	JobStatusCreated    JobStatus = "created"
	JobStatusDuplicated JobStatus = "duplicated"
	JobStatusRunning    JobStatus = "running"
	JobStatusFailed     JobStatus = "failed"
	JobStatusCompleted  JobStatus = "completed"
	JobStatusUnknown    JobStatus = "unknown"

	JobExpirationTime = 24 * time.Hour
)

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) SetCachedPlaceLookup

func (c *MapsClient) SetCachedPlaceLookup(lookup CachedPlaceLookup)

SetCachedPlaceLookup wires in the cache so a nearby search can skip buying Place Details for places it already has. Place Details is the dominant cost of a cold search — one call per place — so on an area we have already covered this takes that spend to near zero.

func (*MapsClient) SetDetailedSearchFields

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

func (*MapsClient) TextSearchPlaces

func (c *MapsClient) TextSearchPlaces(ctx context.Context, req *TextSearchRequest) ([]POI.Place, error)

TextSearchPlaces issues a Google Places Text Search and parses the response into POI.Place values. The request deliberately sets nothing beyond Query, Location and Radius: no Type, no OpenNow, no MinPrice/MaxPrice/Language/Region. A Type filter is especially dangerous here — Google silently ignores an unenforceable type on the legacy API rather than erroring, which is how hotels once got written into the eatery cache (see POI/categories.go's GetPlaceCategory doc). Text search has no type to filter on in the first place; the category is decided entirely after the fact, server-side, from what Google actually returns.

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 NewPATResponse

type NewPATResponse struct {
	TokenID   string `json:"token_id"`
	TokenHash string `json:"token_hash"` // Only shown once!
	Name      string `json:"name"`
	CreatedAt string `json:"created_at"` // Human-readable format
	ExpiresAt string `json:"expires_at"` // Human-readable format
	ExpiresIn string `json:"expires_in"` // Human-readable duration (e.g., "24 hours")
}

NewPATResponse contains the token information shown once during creation

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 PlaceSearchCandidate

type PlaceSearchCandidate struct {
	Place      POI.Place         `json:"place"`
	Category   POI.PlaceCategory `json:"category"`
	Insertable bool              `json:"insertable"`
}

PlaceSearchCandidate is one text-search result, annotated with the category it would be filed under if confirmed. Category is "" and Insertable is false when the place's primary Google type is not one POI.GetPlaceCategory recognizes; such candidates are still returned and stashed so a confirm attempt gets an accurate 422 instead of a confusing 404 (see AddSearchedPlaceToCache).

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

	// IncludeClosedPlaces opts a caller INTO receiving non-Operational places. The zero value
	// filters them from both cache reads and cold-search responses: closed places are persisted
	// in the cache (their closure is the signal that retires them), so a request that forgot to
	// ask for filtering must never serve one — planners consume these results directly. This
	// replaces the old BusinessStatus field, whose zero value was the UNFILTERED behavior and
	// therefore one forgotten assignment away from leaking closures.
	IncludeClosedPlaces bool
	// true if using precise geolocation instead of using a grander administrative area
	UsePreciseLocation bool

	PriceLevel POI.PriceLevel

	// Keyword is a brand or merchant keyword (e.g. "Dunkin'"). When set, the search is
	// keyword-based instead of place-type-based, and results are cached under a
	// brand-scoped Redis key (see POI.EncodeBrandNearbySearchRedisKey).
	Keyword string

	// StrictNameMatch only keeps results whose names match Keyword (after normalization).
	// It applies before results are cached so brand-scoped Redis buckets stay brand-pure.
	StrictNameMatch bool

	// DetailsLimit caps how many places get the expensive Place Details API call, chosen
	// by proximity to the request location. Zero means no cap (previous behavior).
	DetailsLimit int
}

type PlanningEvent

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

type PlanningSolutionRecord

type PlanningSolutionRecord struct {
	ID              string              `json:"id"`
	PlaceIDs        []string            `json:"place_ids"`
	Score           float64             `json:"score"`
	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"`
	Weekdays        []string            `json:"weekdays"`
	TimeSlots       []string            `json:"time_slots"`
	Destination     POI.Location        `json:"destination"`
	PlanSpec        string              `json:"plan_spec"`
}

type PlanningSolutionsResponse

type PlanningSolutionsResponse struct {
	PlanningSpec            string                   `json:"planning_spec"`
	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
	NumPlans                int64
}

type PoiSearcher

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

func CreatePoiSearcher

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

func (*PoiSearcher) AddSearchedPlaceToCache

func (s *PoiSearcher) AddSearchedPlaceToCache(ctx context.Context, placeID string) (AddSearchedPlaceResult, error)

AddSearchedPlaceToCache confirms a previously text-searched candidate into the shared Redis geo cache. See addSearchedPlaceToCache for the step-by-step behavior; this exported form supplies the real Google Place Details enricher.

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) RemoveMisclassifiedPlacesFromCategoryBuckets

func (s *PoiSearcher) RemoveMisclassifiedPlacesFromCategoryBuckets(ctx context.Context, cat POI.PlaceCategory, dryRun bool) (BucketCleanupReport, error)

RemoveMisclassifiedPlacesFromCategoryBuckets forwards to the RedisClient method so the admin handler can call it through the concrete PoiSearcher (p.Solver.Searcher).

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)

ReverseGeocode resolves city-level info for a coordinate, consulting the per-cell Redis cache before Google. This runs on EVERY nearby scan (processLocation), and on a warm place cache it was the only Google call left — caching it takes a warm scan's Google spend to zero.

func (*PoiSearcher) TextSearchPlaces

func (s *PoiSearcher) TextSearchPlaces(ctx context.Context, req *TextSearchRequest) ([]PlaceSearchCandidate, error)

TextSearchPlaces runs a free-text Google search and stashes every result — insertable or not — as a confirmable candidate keyed by place ID. Non-insertable candidates are stashed too: doing so is cheap, and it lets a later confirm attempt on that ID report an accurate 422 (unsupported type) instead of a confusing 404 (candidate not found).

func (*PoiSearcher) UnionEateryPriceBucketsIntoCategoryBucket

func (s *PoiSearcher) UnionEateryPriceBucketsIntoCategoryBucket(ctx context.Context, dryRun bool) (EateryBucketUnionReport, error)

UnionEateryPriceBucketsIntoCategoryBucket forwards to the RedisClient method so the admin handler can call it through the concrete PoiSearcher.

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) AddGeoLocation

func (r *RedisClient) AddGeoLocation(ctx context.Context, key string, place POI.Place) error

AddGeoLocation adds a place to a geo bucket under an explicit key. Exported for migrations and tests that need to write buckets the normal write path would reject.

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) CachedPlaces

func (r *RedisClient) CachedPlaces(ctx context.Context, placeIDs []string) (map[string]POI.Place, error)

CachedPlaces resolves stored place records for the given IDs in one round trip, omitting IDs with no usable record. Used by the maps client to avoid re-buying Place Details for places we already have; see MapsClient.SetCachedPlaceLookup.

func (*RedisClient) CollectPlanningAPIStats

func (r *RedisClient) CollectPlanningAPIStats(ctx context.Context, event PlanningEvent, workerIdx int)

CollectPlanningAPIStats updates the number of calls to the planning APIs per hour. It also updates API call stats for each city per hour. These keys expire after 30 days.

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) DeleteJob

func (r *RedisClient) DeleteJob(ctx context.Context, id 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) FetchSingleRecordTypeSet

func (r *RedisClient) FetchSingleRecordTypeSet(context context.Context, redisKey string) ([]string, 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) GetBrandMapsLastSearchTime

func (r *RedisClient) GetBrandMapsLastSearchTime(context context.Context, lat, lng float64, keyword string) (lastSearchTime time.Time, err error)

GetBrandMapsLastSearchTime returns the last time an external maps search ran for a brand keyword in the location cell containing (lat, lng)

func (*RedisClient) GetCities

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

func (*RedisClient) GetJob

func (r *RedisClient) GetJob(ctx context.Context, id string) (*Job, error)

func (*RedisClient) GetLocationPhotoIDs

func (r *RedisClient) GetLocationPhotoIDs(ctx context.Context, location *POI.Location) ([]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, lat, lng float64, category POI.PlaceCategory, priceLevel POI.PriceLevel) (lastSearchTime time.Time, err error)

GetMapsLastSearchTime reports when an external maps search last covered the location cell containing (lat, lng) for this category and price level. The field is keyed on the cell rather than the city because the geo buckets it guards are read from arbitrary coordinates — see POI.EncodeLastSearchTimeField. There is no longer a per-category exception: the price segment is derived from PriceyEatery, so Visit needing none is a consequence of the rule rather than a special case that the newer categories were never added to.

func (*RedisClient) GetPlaceCountByCategory

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

GetPlaceCountByCategory returns how many places sit in a category's geo bucket. It goes through POI.EncodeNearbySearchRedisKey rather than assembling the key locally: the hand-rolled version produced "placeIDs:eatery" while the write path used "placeIDs:eatery:level<N>", so this reported zero eateries no matter how many were stored.

func (*RedisClient) GetPlaceCountInRedis

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

func (*RedisClient) ListUserPATMetadata

func (r *RedisClient) ListUserPATMetadata(ctx context.Context, userId string) ([]*TokenMetadata, error)

ListUserPATMetadata retrieves metadata (no hash) for all user's tokens

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) NewPAT

func (r *RedisClient) NewPAT(ctx context.Context, name, userId, token string, valid time.Duration) (*NewPATResponse, error)

func (*RedisClient) PlaceSearchCandidate

func (r *RedisClient) PlaceSearchCandidate(ctx context.Context, placeID string) (POI.Place, error)

PlaceSearchCandidate resolves a stashed text-search result by place ID. A miss (never stashed, or the TTL expired) wraps ErrSearchCandidateNotFound so callers can distinguish it from other Redis failures.

func (*RedisClient) PlanningSolutions

func (*RedisClient) RemoveKeys

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

func (*RedisClient) RemoveMisclassifiedPlacesFromCategoryBuckets

func (r *RedisClient) RemoveMisclassifiedPlacesFromCategoryBuckets(ctx context.Context, cat POI.PlaceCategory, dryRun bool) (BucketCleanupReport, error)

RemoveMisclassifiedPlacesFromCategoryBuckets removes places from cat's geo buckets whose PRIMARY Google type does not belong to cat. It repairs the fast_food_restaurant incident: two Places-API-(New)-only types were searched against the legacy Nearby Search, which ignored the unenforceable type filter and returned prominence-ranked establishments, and those were stamped with the queried type and written into placeIDs:eatery:level*.

The removal rule is the exact inverse of the WRITE rule. SetPlacesAddGeoLocations files a place under GetPlaceCategory(place.LocationType) and refuses to write anything whose type maps to no category, so this migration removes a member only when its primary type positively maps to a DIFFERENT category (lodging -> Lodging, supermarket -> Shopping). An UNMAPPED primary type is deliberately kept: types like "university", "airport", and "real_estate_agency" are legal legacy types placeTypeToCategory (POI/categories.go) does not classify, as are records with no Types at all (cached before Types was captured). Those places carry a stamped LocationType the write path maps straight back to this category, so removing them would only delete rows the next cold search re-creates — while shrinking the trip-planning candidate pool for up to MinMapsResultRefreshDuration, because the trip-planning path (planner/solver.go -> matching.NearbySearchForCategory) reads these buckets with no reclassification at all.

Since the place-text-search PR unified POI.ReclassifyForCategory (the merchant-endpoint read filter) onto the same placeTypeToCategory table via GetPlaceCategory, both this migration and that read filter now agree on the case that matters: a primary type that positively resolves to a different category is excluded by both. They are not identical in every case — the read filter also drops a place whose primary type is present but still unmapped, while this migration treats that same case as "not evidence of misclassification" and leaves it in the bucket (see docs/migrations/reclassify-buckets.md) — but that divergence is intentional: one function decides what to show in a single response, this one decides what may exist in the shared cache.

dryRun reports what would be removed without deleting anything. Always dry-run first.

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(ctx context.Context, lat, lng float64) (*GeocodeQuery, error)

func (*RedisClient) RevokePAT

func (r *RedisClient) RevokePAT(ctx context.Context, userId, tokenId string) error

func (*RedisClient) RevokePATByName

func (r *RedisClient) RevokePATByName(ctx context.Context, userId, tokenName string) error

RevokePATByName revokes a personal access token by name (user-friendly approach)

func (*RedisClient) SaveAnnouncement

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

func (*RedisClient) SaveLocationPhotoID

func (r *RedisClient) SaveLocationPhotoID(ctx context.Context, location *POI.Location, id 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) SetBrandMapsLastSearchTime

func (r *RedisClient) SetBrandMapsLastSearchTime(context context.Context, lat, lng float64, keyword string, requestTime string) (err error)

SetBrandMapsLastSearchTime records the last time an external maps search ran for a brand keyword in the location cell containing (lat, lng)

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, lat, lng float64, 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) SetPlace

func (r *RedisClient) SetPlace(ctx context.Context, place POI.Place) error

SetPlace stores a single place record. Exported wrapper over setPlace for migrations and tests.

func (*RedisClient) SetPlaceSearchCandidate

func (r *RedisClient) SetPlaceSearchCandidate(ctx context.Context, place POI.Place, ttl time.Duration) error

SetPlaceSearchCandidate stashes a text-search result under its place ID with a TTL, so a later confirm (PoiSearcher.AddSearchedPlaceToCache) can resolve it by ID alone without trusting any place data an HTTP caller might send back. Plain SET with expiration, mirroring setPlace's style but never overwriting the permanent place_details record it lives beside.

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 Batches places into groups to reduce goroutine overhead

func (*RedisClient) SetPlacesAddGeoLocationsForBrand

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

SetPlacesAddGeoLocationsForBrand stores place details and adds places to a brand-scoped geo index (placeIDs:brand:{brandKey}) so cached brand searches stay brand-pure. Brand result sets are small, so a single pipeline is used without batching.

func (*RedisClient) SetReverseGeocode

func (r *RedisClient) SetReverseGeocode(ctx context.Context, lat, lng float64, query GeocodeQuery)

SetReverseGeocode caches a reverse-geocode result for the cell containing (lat, lng). Best-effort like SetGeocode: a write failure only means the next request buys the geocode again.

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(ctx context.Context, streamName string, data map[string]string) string

StreamsLogging returns redis streams ID if XADD command execution is successful

func (*RedisClient) UnionEateryPriceBucketsIntoCategoryBucket

func (r *RedisClient) UnionEateryPriceBucketsIntoCategoryBucket(ctx context.Context, dryRun bool) (EateryBucketUnionReport, error)

UnionEateryPriceBucketsIntoCategoryBucket merges the legacy per-price eatery geo indexes (placeIDs:eatery:level0..4) into the single placeIDs:eatery bucket that POI.EncodeNearbySearchRedisKey now names.

Run this BEFORE deploying the code that reads the collapsed key. It is purely additive and invisible to the running code, whereas deploying first would point every eatery read at a key that does not exist yet and trigger a global cold-search burst.

The legacy key format is spelled out literally here on purpose: the encoder no longer emits it, and a migration is the one place a retired format belongs.

AGGREGATE MIN is mandatory. redis.ZStore.Aggregate defaults to SUM, and a GEO member's score IS its 52-bit geohash — summing the scores of a place that appears in two source buckets would silently relocate it, in our case to somewhere in the ocean. MIN keeps a real geohash, and since a place's coordinates are identical across buckets, which one survives is immaterial.

The target is included in the union sources so the migration is re-runnable and cannot drop members that new code has already written to the collapsed key.

func (*RedisClient) UpdateJob

func (r *RedisClient) UpdateJob(ctx context.Context, job *Job) error

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, preciseLocation bool) 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) UserFavorites

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

func (*RedisClient) UserFeedback

func (r *RedisClient) UserFeedback(ctx context.Context, fb *UserFeedback) error

func (*RedisClient) ValidatePATByHash

func (r *RedisClient) ValidatePATByHash(ctx context.Context, tokenHash string) (*TokenRecord, error)

ValidatePATByHash validates a personal access token by hash for API authentication

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 TextSearchRequest

type TextSearchRequest struct {
	Query    string
	Location POI.Location
	Radius   uint
	// Limit caps the number of results returned. <= 0 means PlaceTextSearchMaxResults.
	Limit int
}

TextSearchRequest is the input to MapsClient.TextSearchPlaces / PoiSearcher.TextSearchPlaces.

type TokenMetadata

type TokenMetadata struct {
	Id        string     `json:"id"`
	Name      string     `json:"name"`
	UserId    string     `json:"user_id"`
	Scopes    []string   `json:"scopes"`
	CreatedAt time.Time  `json:"created_at"`
	ExpiresAt *time.Time `json:"expires_at"`
	RevokedAt *time.Time `json:"revoked_at"`
	IsActive  bool       `json:"is_active"`
}

TokenMetadata contains non-sensitive token information for user management

type TokenRecord

type TokenRecord struct {
	Id        string     `json:"id"`
	Name      string     `json:"name"`
	Hash      string     `json:"hash"`
	UserId    string     `json:"user_id"`
	Scopes    []string   `json:"scopes"`
	CreatedAt time.Time  `json:"created_at"`
	ExpiresAt *time.Time `json:"expires_at"`
	RevokedAt *time.Time `json:"revoked_at"`
}

func (*TokenRecord) Valid

func (tr *TokenRecord) Valid() bool

type UserFeedback

type UserFeedback struct {
	UserId   string `json:"user_id"`
	PlanSpec string `json:"plan_spec"`
	PlanId   string `json:"plan_id"`
	Like     bool   `json:"like"`
}

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
⌂ 换网址