func (ws *WebService) manageAnalytics(url *storage.URL, request engine.Request, longURL bool, shortURL bool) { log.Printf("[INFO] [abraracourcix] Analytics for URL : %v %s %s", url, request.UserAgent(), request.RemoteAddress()) key := storage.GetAnalyticsKey(url.Key) stat, err := ws.retrieveAnalytics([]byte(key)) if err != nil { log.Printf("[WARN] [abraracourcix] Can't decode Analytics %v", err) return } log.Printf("[INFO] [abraracourcix] Analytics find : %v", stat) ua := request.UserAgent() if stat.UserAgents != nil { stat.UserAgents[ua] = stat.UserAgents[ua] + 1 } else { stat.UserAgents = make(map[string]int64) stat.UserAgents[ua] = 1 } if longURL { stat.LongURLClicks = stat.LongURLClicks + 1 } if shortURL { stat.ShortURLClicks = stat.ShortURLClicks + 1 } err = ws.storeAnalytics([]byte(key), stat) if err != nil { log.Printf("[WARN] [abraracourcix] Can't store analytics URL %s %v", url, stat) } log.Printf("[INFO] [abraracourcix] Analytics updated : %v", stat) }
func (ws *WebService) createAnalytics(url *storage.URL) { log.Printf("[INFO] [abraracourcix] Analytics for URL : %v", url) stat := storage.NewAnalytics() key := storage.GetAnalyticsKey(url.Key) err := ws.storeAnalytics([]byte(key), stat) if err != nil { log.Printf("[WARN] [abraracourcix] Can't store analytics URL %s %v", url, stat) } log.Printf("[INFO] [abraracourcix] Analytics added : %s -> %v", key, stat) }
// URLStats send the url analytics using the key func (ws *WebService) URLStats() echo.HandlerFunc { return func(c echo.Context) error { url := c.Param("url") log.Printf("[INFO] [abraracourcix] Retrieve URL analytics using key: %v", url) stat, err := ws.retrieveAnalytics([]byte(storage.GetAnalyticsKey(url))) if err != nil { return c.JSON(http.StatusInternalServerError, &APIErrorResponse{Error: err.Error()}) } log.Printf("[INFO] [abraracourcix] Find Analytics : %v", stat) return c.JSON(http.StatusOK, stat) } }