Skip to content

Commit

Permalink
WIP - Slack API migrated. Missing checkpoint for update time
Browse files Browse the repository at this point in the history
  • Loading branch information
thoeni committed Apr 29, 2017
1 parent d4e69ce commit cba7f55
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 95 deletions.
107 changes: 18 additions & 89 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"github.com/thoeni/slack-tube-service/service"
)

const minStatusPollPeriod = 2

var statuses []tfl.Report
var reportMap map[string]tfl.Report
var client tfl.Client = &service.InMemoryCachedClient{
tfl.NewClient(),
[]tfl.Report{},
Expand Down Expand Up @@ -63,44 +59,6 @@ func lineStatusHandler(w http.ResponseWriter, r *http.Request) {
}
}

func lineStatusHandler2(w http.ResponseWriter, r *http.Request) {

var response []tfl.Report

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

if isUpdateNeeded() {
if err := updateStatusInformation(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode("There was an error getting information from TFL"); err != nil {
log.Panic(err)
}
}
}

vars := mux.Vars(r)
tubeLine, lineIsPresentInPath := vars["line"]

if !lineIsPresentInPath {
response = statuses
} else {
report, found := reportMap[strings.ToLower(tubeLine)]
if !found {
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode("Not a recognised line."); err != nil {
log.Panic(err)
}
return
}
response = append(response, report)
}

w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Panic(err)
}
}

func slackRequestHandler(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Expand All @@ -122,33 +80,30 @@ func slackRequestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
slackResp.Text = "Unauthorised"
} else {
if isUpdateNeeded() {
if err := updateStatusInformation(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
slackResp.Text = "There was an error getting information from TFL"
}
}

tubeLine := strings.Join(slackReq.Text, " ")

w.WriteHeader(http.StatusOK)
slackResp.ResponseType = "ephemeral"
slackResp.Text = fmt.Sprintf("Slack Tube Service - last updated at %s", lastStatusCheck.Format("15:04:05"))

if tubeLine == "" {
for _, line := range statuses {
attachments = append(attachments, mapTflLineToSlackAttachment(line))
}
} else {
for _, line := range statuses {
if strings.ToLower(line.Name) == strings.ToLower(tubeLine) {
attachments = append(attachments, mapTflLineToSlackAttachment(line))
}
}
if len(attachments) == 0 {
w.WriteHeader(http.StatusNotFound)
slackResp.Text = "Not a recognised line."
}
var lines []string
if tubeLine != "" {
lines = []string{tubeLine}
}

reportsMap, err := tubeService.GetStatusFor(lines)
log.Printf("Client address is %p", &(tubeService.Client))

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
slackResp.Text = "Error while retrieving information from TFL"
} else if len(reportsMap) == 0 {
w.WriteHeader(http.StatusNotFound)
slackResp.Text = "Not a recognised line."
}

for _, v := range reportsMap {
attachments = append(attachments, mapTflLineToSlackAttachment(v))
}

slackResp.Attachments = attachments
Expand All @@ -174,29 +129,3 @@ func slackTokenRequestHandler(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusAccepted)
}

func updateStatusInformation() error {
var client tfl.Client = tfl.NewClient()
reports, err := client.GetTubeStatus()
if err != nil {
log.Print("Error while retrieving Tube statuses")
return err
}
statuses = reports
reportMap = tfl.ReportArrayToMap(reports)
lastStatusCheck = time.Now()
return nil
}

func isUpdateNeeded() bool {
return time.Since(lastStatusCheck).Minutes() > minStatusPollPeriod
}

func getReports() ([]tfl.Report, error) {
if isUpdateNeeded() {
if err := updateStatusInformation(); err != nil {
return nil, err
}
}
return statuses, nil
}
6 changes: 3 additions & 3 deletions service/tube-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ type TubeService struct {
type InMemoryCachedClient struct {
Client tfl.Client
TubeStatus []tfl.Report
LastRetrieved time.Time
LastUpdated time.Time
InvalidateIntervalInSeconds float64
}

func (c *InMemoryCachedClient) GetTubeStatus() ([]tfl.Report, error) {
log.Printf("Called GetTubeStatus on InMemoryCachedClient!")
if time.Since(c.LastRetrieved).Seconds() > c.InvalidateIntervalInSeconds {
if time.Since(c.LastUpdated).Seconds() > c.InvalidateIntervalInSeconds {
log.Printf("Calling TFL...")
r, e := c.Client.GetTubeStatus()
c.TubeStatus = r
c.LastRetrieved = time.Now()
c.LastUpdated = time.Now()
return c.TubeStatus, e
}
log.Printf("Returning cached value...")
Expand Down
6 changes: 3 additions & 3 deletions service/tube-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestInMemoryCachedClient_WhenTimeLessThanInvalidate_ThenDoesNotCallTflClien
c := InMemoryCachedClient{
Client: mockTflClient,
InvalidateIntervalInSeconds: 10,
LastRetrieved: time.Now().Add(-5 * time.Second),
LastUpdated: time.Now().Add(-5 * time.Second),
TubeStatus: validTflClientResponse,
}
c.GetTubeStatus()
Expand All @@ -88,7 +88,7 @@ func TestInMemoryCachedClient_WhenTimeLessThanInvalidate_ThenDoesCallTflClientTo
c := InMemoryCachedClient{
Client: mockTflClient,
InvalidateIntervalInSeconds: 10,
LastRetrieved: time.Now().Add(-15 * time.Second),
LastUpdated: time.Now().Add(-15 * time.Second),
TubeStatus: validTflClientResponse,
}
c.GetTubeStatus()
Expand All @@ -101,7 +101,7 @@ func TestInMemoryCachedClient_WhenSetUrl_ThenChangesTheUnderlyingClientURL(t *te
c := InMemoryCachedClient{
Client: mockTflClient,
InvalidateIntervalInSeconds: 10,
LastRetrieved: time.Now().Add(-15 * time.Second),
LastUpdated: time.Now().Add(-15 * time.Second),
TubeStatus: validTflClientResponse,
}
c.SetBaseURL("newUrl")
Expand Down

0 comments on commit cba7f55

Please sign in to comment.