Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
[build] Fix line endings; Travis config.
Browse files Browse the repository at this point in the history
  • Loading branch information
elithrar committed Feb 14, 2018
1 parent 17d04fb commit 74d669b
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 477 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Expand Up @@ -12,6 +12,7 @@ matrix:

before_install:
- go get github.com/mitchellh/gox

install:
- # skip

Expand All @@ -20,17 +21,16 @@ script:
- diff -u <(echo -n) <(gofmt -d .)
- go vet $(go list ./... | grep -v /vendor/)
- go test -v -race ./...
- if [ "${LATEST}" = "true" ]; then gox -os="linux darwin" -arch="amd64" -output="centimentd.."
-ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./...; fi
- if [ "${LATEST}" = "true" ]; then gox -os="linux darwin" -arch="amd64" -output="centimentd.{{.OS}}.{{.Arch}}" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./...; fi

deploy:
provider: releases
skip_cleanup: true
api_key:
secure: i3RaRoIL+BYxBHemfLF4m8yMtGn7W/r12+jXyJlLCOvTV22Z2bZLXGeyxnHz/gj4W9xN5g/nyPLOIY9ak1/IsYajKQXZRmmDljLyLQnCh7DYGvWfLxDnsE0C5BUyTtn5a2c8hK+9kudpK77OMYwuw1HMfRt/JyYo/i8RHdaK3TzMCvp+c+58DhwpJm5YU+8iO3LKwb0lQGaF5JJBNJd1nR3K8L/tkWC9oZ7mv5528RhxtpIYgU4Lsk70RpvcwgSYcjwIb0M4wZgNlXFCuO2WDb3TY7CVzO6hoUymE65rW2iTQrc4EP7U4ulxjUgF6QcLblyQZ8Oz+kzJf/D4BkdNg4pu6MrQLmeMt7DEjliafMqPgWJmKBch/5W0BSkIp5ksDFNHck3qIK79KcCsCbnBYXyOYYfA+Pl6uZXgpeZ+ti7N/Z+eKrCT2FemSkxEzbQ65bWzINC+26Hy6/tpNzzWvmKgM91DvNi3axEn+IuvzzTfKohSvd03Ti3zHBOUwxkG6Tu7VNAHODghbIiyGCOFikcvRyGjYDzLIE8sdgG9WfpNSg9lDCoY+dj2uc73v5Y8NnGp9pfot3A8dBYwXeVcftrwTYVuZ0o3Kd7lHMgqcNtQCNn9eoy9xsQGOC11Pgi7vXGSwEQOvxebfUksseevdMceO4+ZgD4gxt5e92saFjc=
file:
- centimentd.darwin.amd64
- centimentd.linux.amd64
- "centimentd.darwin.amd64"
- "centimentd.linux.amd64"
on:
repo: elithrar/centiment
tags: true
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2017, Matt Silverlock
Copyright (c) 2017-2018, Matt Silverlock
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
206 changes: 103 additions & 103 deletions aggregate.go
@@ -1,103 +1,103 @@
package centiment

import (
"context"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)

// Aggregator aggregates results from an analysis run.
type Aggregator struct {
logger log.Logger
db DB
}

// NewAggregator creates a new Aggregator: call Run to collect results and save
// them to the given DB.
func NewAggregator(logger log.Logger, db DB) (*Aggregator, error) {
agg := &Aggregator{
db: db,
logger: logger,
}

return agg, nil
}

// Run an aggregatation on the provided results.
func (ag *Aggregator) Run(ctx context.Context, results <-chan *AnalyzerResult) error {
var sentiments = make(map[string]*Sentiment)

// TODO(matt): Handle cancellation. Use for-select here with two cases.
for res := range results {
topic := res.SearchTerm.Topic
if sentiments[topic] == nil {
sentiments[topic] = &Sentiment{}
}

// Update the rolling aggregate for each topic.
sentiments[topic] = ag.updateAggregate(
res.Score,
res.Magnitude,
res.TweetID,
sentiments[topic],
)

sentiments[topic].populateWithSearch(res.SearchTerm)
}

for topic, sentiment := range sentiments {
sentiment.finalize()
id, err := ag.db.SaveSentiment(ctx, *sentiment)
if err != nil {
// TODO(matt): Implement retry logic w/ back-off.
ag.logger.Log(
"err", errors.Wrap(err, "failed to save topic"),
"topic", topic,
)
continue
}

ag.logger.Log(
"state", "saved",
"topic", sentiment.Topic,
"slug", sentiment.Slug,
"id", id,
"score", sentiment.Score,
"count", sentiment.Count,
"stddev", sentiment.StdDev,
"variance", sentiment.Variance,
)
}

return nil
}

func (ag *Aggregator) updateAggregate(score float32, magnitude float32, tweetID int64, sentiment *Sentiment) *Sentiment {
sentiment.Count++
oldAverage := sentiment.Score
sentiment.Score = updateAverage(score, sentiment.Score, sentiment.Count)
sentiment.Variance = updateVariance(
score,
sentiment.Variance,
oldAverage,
sentiment.Score,
sentiment.Count,
)

// Record the largest (newest) Tweet ID we've seen across our results for this
// topic, as the checkpoint for future searches.
if tweetID > sentiment.LastSeenID {
sentiment.LastSeenID = tweetID
}

return sentiment
}

func updateAverage(value float32, currentAverage float64, count int64) float64 {
return currentAverage + ((float64(value) - currentAverage) / float64(count))
}

func updateVariance(value float32, variance float64, oldAverage float64, newAverage float64, count int64) float64 {
return variance + (float64(value)-oldAverage)*(float64(value)-newAverage)
}
package centiment

import (
"context"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)

// Aggregator aggregates results from an analysis run.
type Aggregator struct {
logger log.Logger
db DB
}

// NewAggregator creates a new Aggregator: call Run to collect results and save
// them to the given DB.
func NewAggregator(logger log.Logger, db DB) (*Aggregator, error) {
agg := &Aggregator{
db: db,
logger: logger,
}

return agg, nil
}

// Run an aggregatation on the provided results.
func (ag *Aggregator) Run(ctx context.Context, results <-chan *AnalyzerResult) error {
var sentiments = make(map[string]*Sentiment)

// TODO(matt): Handle cancellation. Use for-select here with two cases.
for res := range results {
topic := res.SearchTerm.Topic
if sentiments[topic] == nil {
sentiments[topic] = &Sentiment{}
}

// Update the rolling aggregate for each topic.
sentiments[topic] = ag.updateAggregate(
res.Score,
res.Magnitude,
res.TweetID,
sentiments[topic],
)

sentiments[topic].populateWithSearch(res.SearchTerm)
}

for topic, sentiment := range sentiments {
sentiment.finalize()
id, err := ag.db.SaveSentiment(ctx, *sentiment)
if err != nil {
// TODO(matt): Implement retry logic w/ back-off.
ag.logger.Log(
"err", errors.Wrap(err, "failed to save topic"),
"topic", topic,
)
continue
}

ag.logger.Log(
"state", "saved",
"topic", sentiment.Topic,
"slug", sentiment.Slug,
"id", id,
"score", sentiment.Score,
"count", sentiment.Count,
"stddev", sentiment.StdDev,
"variance", sentiment.Variance,
)
}

return nil
}

func (ag *Aggregator) updateAggregate(score float32, magnitude float32, tweetID int64, sentiment *Sentiment) *Sentiment {
sentiment.Count++
oldAverage := sentiment.Score
sentiment.Score = updateAverage(score, sentiment.Score, sentiment.Count)
sentiment.Variance = updateVariance(
score,
sentiment.Variance,
oldAverage,
sentiment.Score,
sentiment.Count,
)

// Record the largest (newest) Tweet ID we've seen across our results for this
// topic, as the checkpoint for future searches.
if tweetID > sentiment.LastSeenID {
sentiment.LastSeenID = tweetID
}

return sentiment
}

func updateAverage(value float32, currentAverage float64, count int64) float64 {
return currentAverage + ((float64(value) - currentAverage) / float64(count))
}

func updateVariance(value float32, variance float64, oldAverage float64, newAverage float64, count int64) float64 {
return variance + (float64(value)-oldAverage)*(float64(value)-newAverage)
}

0 comments on commit 74d669b

Please sign in to comment.