Documentation ¶
Index ¶
- Variables
- func AddHealthCheckEndpoints(r *mux.Router, env *Env) *mux.Router
- func AddIndexEndpoints(r *mux.Router, env *Env) *mux.Router
- func AddMetricEndpoints(r *mux.Router, env *Env) *mux.Router
- func AddSentimentEndpoints(r *mux.Router, env *Env) *mux.Router
- func LogRequest(logger log.Logger) func(http.Handler) http.Handler
- func RunServer(srv *http.Server) func() error
- type Aggregator
- type Analyzer
- type AnalyzerResult
- type DB
- type Endpoint
- type Env
- type Firestore
- func (fs *Firestore) GetSentimentByID(ctx context.Context, id string) (*Sentiment, error)
- func (fs *Firestore) GetSentimentsBySlug(ctx context.Context, topicSlug string, limit int) ([]*Sentiment, error)
- func (fs *Firestore) GetSentimentsByTopic(ctx context.Context, topic string, limit int) ([]*Sentiment, error)
- func (fs *Firestore) SaveSentiment(ctx context.Context, sentiment Sentiment) (string, error)
- type HTTPError
- type SearchResult
- type SearchTerm
- type Searcher
- type Sentiment
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoResultsFound is returned when a DB cannot find a result in the store. ErrNoResultsFound = errors.New("store: no results found") // ErrInvalidSlug is returned when a URL slug does not match expected slug format (via slug.IsSlug) ErrInvalidSlug = errors.New("store: bad slug format") )
Functions ¶
func AddHealthCheckEndpoints ¶
AddHealthCheckEndpoints adds the health check endpoints to the given router, and returns an instance of the Subrouter.
func AddIndexEndpoints ¶
AddIndexEndpoints adds the entrypoint/index handlers to the given router.
func AddMetricEndpoints ¶
AddMetricEndpoints adds the metric/debugging endpoints to the given router, and returns an instance of the Subrouter.
func AddSentimentEndpoints ¶
AddSentimentEndpoints adds the sentiment endpoints to the given router, and returns an instance of the Subrouter.
func LogRequest ¶
LogRequest logs each HTTP request, using the given logger.
Types ¶
type Aggregator ¶
type Aggregator struct {
// contains filtered or unexported fields
}
Aggregator aggregates results from an analysis run.
func NewAggregator ¶
func NewAggregator(logger log.Logger, db DB) (*Aggregator, error)
NewAggregator creates a new Aggregator: call Run to collect results and save them to the given DB.
func (*Aggregator) Run ¶
func (ag *Aggregator) Run(ctx context.Context, results <-chan *AnalyzerResult) error
Run an aggregatation on the provided results.
type Analyzer ¶
type Analyzer struct {
// contains filtered or unexported fields
}
Analyzer holds the configuration for running analyses against a Natural Language API. An Analyzer should only be initialized via NewAnalyzer.
func NewAnalyzer ¶
NewAnalyzer instantiates an Analyzer. Call the Run method to start an analysis.
func (*Analyzer) Run ¶
func (az *Analyzer) Run(ctx context.Context, searched <-chan *SearchResult, analyzed chan<- *AnalyzerResult) error
Run passes the values from searched to the Natural Language API, performs analysis concurrently, and returns the results on the analyzed channel.
Run returns when analyses have completed, and can be cancelled by wrapping the provided context with context.WithCancel and calling the provided CancelFunc.
type AnalyzerResult ¶
type AnalyzerResult struct { TweetID int64 Score float32 Magnitude float32 SearchTerm *SearchTerm }
AnalyzerResult is the result from natural language analysis of a tweet.
type DB ¶
type DB interface { SaveSentiment(ctx context.Context, sentiment Sentiment) (string, error) GetSentimentByID(ctx context.Context, id string) (*Sentiment, error) GetSentimentsBySlug(ctx context.Context, slug string, limit int) ([]*Sentiment, error) GetSentimentsByTopic(ctx context.Context, topic string, limit int) ([]*Sentiment, error) }
DB represents a database for storing & retrieving Sentiments.
type Endpoint ¶
Endpoint represents a application server endpoint. It bundles a error-returning handler and injects our application dependencies.
type Firestore ¶
type Firestore struct { Store *firestore.Client // The name of the collection. CollectionName string }
Firestore is an implementation of DB that uses Google Cloud Firestore.
func (*Firestore) GetSentimentByID ¶
GetSentimentByID fetches an existing Sentiment by its ID. It will return a nil value and no error if no record was found.
func (*Firestore) GetSentimentsBySlug ¶
func (fs *Firestore) GetSentimentsBySlug(ctx context.Context, topicSlug string, limit int) ([]*Sentiment, error)
GetSentimentsBySlug fetches all historical sentiments for the given slug ("slugified" topic name) up to limit records. Providing a limit of 0 (or less) will fetch all records. Records are ordered from most recent to least recent.
An error (ErrNoResultsFound) will be returned if no records were found.
func (*Firestore) GetSentimentsByTopic ¶
func (fs *Firestore) GetSentimentsByTopic(ctx context.Context, topic string, limit int) ([]*Sentiment, error)
GetSentimentsByTopic fetches all historical sentiments for the given topic, up to limit records. Providing a limit of 0 (or less) will fetch all records. Records are ordered from most recent to least recent.
An error (ErrNoResultsFound) will be returned if no records were found.
type SearchResult ¶
type SearchResult struct {
// contains filtered or unexported fields
}
SearchResult represents the result of a search against Twitter, and encapsulates a Tweet.
type SearchTerm ¶
type SearchTerm struct { // The human-readable topic of the search. Topic string // The Twitter search query // Ref: https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators Query string }
SearchTerm represents the term or phrase to search for a given topic.
type Searcher ¶
type Searcher struct {
// contains filtered or unexported fields
}
Searcher is a worker pool that searches Twitter for the given set of search terms. Call NewSearcher to configure a new pool. Pools are safe to use concurrently.
The "Run" method on Searcher should be used to begin a search.
func NewSearcher ¶
func NewSearcher(logger log.Logger, terms []*SearchTerm, minResults int, maxAge time.Duration, client *anaconda.TwitterApi, db DB) (*Searcher, error)
NewSearcher creates a new Searcher with the given search terms. It will attempt to fetch minResults per search term and return tweets newer than maxAge.
func (*Searcher) Run ¶
func (sr *Searcher) Run(ctx context.Context, searched chan<- *SearchResult) error
Run performs a concurrent search against the configured terms, and returns results onto the provided searched channel.
Run returns when searches have completed, and can be cancelled by wrapping the provided context with context.WithCancel and calling the provided CancelFunc.
type Sentiment ¶
type Sentiment struct { ID string `json:"id" firestore:"id,omitempty"` Topic string `json:"topic" firestore:"topic"` Slug string `json:"slug" firestore:"slug"` Query string `json:"query" firestore:"query"` Count int64 `json:"count" firestore:"count"` Score float64 `json:"score" firestore:"score"` StdDev float64 `json:"stdDev" firestore:"stdDev"` Variance float64 `json:"variance" firestore:"variance"` FetchedAt time.Time `json:"fetchedAt" firestore:"fetchedAt"` LastSeenID int64 `json:"-" firestore:"lastSeenID"` }
Sentiment represents the aggregated result of performing sentiment analysis against a number (Count) of tweets for a given topic.