Skip to content

Commit

Permalink
Merge pull request #9 from safe-distance/interaction_debouncing
Browse files Browse the repository at this point in the history
interaction debouncing window implemented and working
  • Loading branch information
mattbonnell authored May 22, 2020
2 parents 1f2b344 + ff74d24 commit d19a3de
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ clean-scoring:
rm -f ./scoring/$(BUILD_DIR)/${BINARY_NAME}
clean: clean-circle clean-proximity clean-scoring
token:
./auth/cmd/tokengen/tokengen.out -u $(uid)
./auth/cmd/tokengen/tokengen.out -u $(uid) | pbcopy
run:
./$(PACKAGE)$(BUILD_DIR)/$(EXEC) $(ARGS)
push-deps:
Expand All @@ -54,4 +54,4 @@ build: build-deps

stop:
docker-compose down ${service}
@echo Services torn down
@echo Services torn down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ services:
PORT: $PORT
KAFKA_PEERS: kafka:9092
GOOGLE_APPLICATION_CREDENTIALS: /run/secrets/google_application_credentials
INTERACTION_DEBOUNCING_PERIOD_SECONDS: $INTERACTION_DEBOUNCING_PERIOD_SECONDS
expose:
- $PORT
secrets:
Expand Down
16 changes: 16 additions & 0 deletions proximity/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import (
"os"
"strconv"

"github.com/gorilla/mux"
"github.com/safe-distance/socium-infra/auth"
)
Expand All @@ -18,3 +21,16 @@ var Middleware = []mux.MiddlewareFunc{

// ProductionTopic is the Kafka topic this service produces to
const ProductionTopic = "interaction_added"

const debouncingPeriodEnvVariableName = "INTERACTION_DEBOUNCING_PERIOD_SECONDS"

// InteractionDebouncingPeriod is the number of seconds after receiving an interaction between two users for which subsequent interactions should be ignored
func InteractionDebouncingPeriod() int {
period, err := strconv.Atoi(os.Getenv(debouncingPeriodEnvVariableName))
if err != nil {
return defaultInteractionDebouncingPeriod
}
return period
}

const defaultInteractionDebouncingPeriod = 60
21 changes: 20 additions & 1 deletion proximity/pkg/handlers/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package handlers

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"

"github.com/safe-distance/socium-infra/auth"
"github.com/safe-distance/socium-infra/common"
"github.com/safe-distance/socium-infra/proximity/config"
"github.com/safe-distance/socium-infra/proximity/pkg/models"
)

Expand Down Expand Up @@ -38,14 +41,30 @@ func AddInteraction(s *common.Service) http.Handler {
json.NewEncoder(w).Encode(response)
return
}
// Check if we've already registered an interaction for these users in the debouncing window
startOfDebouncingPeriod := interaction.Timestamp.Add(-1 * time.Duration(config.InteractionDebouncingPeriod()) * time.Second)
fmt.Printf("start of debouncing period: %v\n", startOfDebouncingPeriod)
var mostRecentInteraction models.Interaction
query := s.DB.Where(models.Interaction{UID: user.ID, OtherUID: otherUserUID}).Or(models.Interaction{UID: otherUserUID, OtherUID: user.ID}).Order("timestamp desc")
query = query.Attrs(models.Interaction{UID: "not_found"}).FirstOrInit(&mostRecentInteraction)
if query.Error != nil {
common.ThrowError(w, fmt.Errorf("error retrieving most recent interaction between %v and %v: %v", user.ID, otherUserUID, err), http.StatusAlreadyReported)
return
}
if mostRecentInteraction.UID != "not_found" && mostRecentInteraction.Timestamp.After(startOfDebouncingPeriod) {
msg := fmt.Sprintf("error: interaction between these two users recorded at %v", mostRecentInteraction.Timestamp)
common.ThrowError(w, errors.New(msg), http.StatusAlreadyReported)
return
}

// Add the user's UID from the auth token to the interaction
interaction.UID = user.ID
interaction.OtherUID = otherUserUID
s.DB.Create(&interaction)
json.NewEncoder(w).Encode(&interaction)
fmt.Printf("created interaction: %+v\n", interaction)

// Log a new interaction (send msg to kafka)
// Publish a new interaction (send msg to kafka)
common.LogObject(s.Producer, string(interaction.ID), interaction, s.ProductionTopic)

})
Expand Down

0 comments on commit d19a3de

Please sign in to comment.