MCPcopy Create free account
hub / github.com/GitbookIO/micro-analytics / formatURLForCache

Function formatURLForCache

database/sqlite/sharded.go:650–687  ·  view source on GitHub ↗

Format URL for a specific shard Basically, remove start/end if is is before/after shard time

(uRL *url.URL, shardName int, startMonth int, endMonth int, timeRange *database.TimeRange)

Source from the content-addressed store, hash-verified

648// Format URL for a specific shard
649// Basically, remove start/end if is is before/after shard time
650func formatURLForCache(uRL *url.URL, shardName int, startMonth int, endMonth int, timeRange *database.TimeRange) (string, error) {
651 // Extract URL query parameters
652 queryParams := uRL.Query()
653
654 // Remove start
655 if startMonth < shardName {
656 queryParams.Del("start")
657 }
658
659 // Remove start if timeRange.Start is the start of current month
660 if startMonth == shardName && isStartOfMonth(timeRange.Start) {
661 queryParams.Del("start")
662 }
663
664 // Remove end
665 if endMonth > shardName {
666 queryParams.Del("end")
667 }
668
669 // Remove cache for months before current month
670 currentMonth, err := shardNameToInt(timeToShardName(time.Now().UTC()))
671 if err != nil {
672 return "", err
673 }
674
675 if shardName < currentMonth {
676 queryParams.Del("cache")
677 }
678
679 // Add shard=shardName query parameter
680 queryParams.Add("shard", strconv.Itoa(shardName))
681
682 // Create new modified URL
683 cacheURL := *uRL
684 cacheURL.RawQuery = queryParams.Encode()
685
686 return cacheURL.String(), nil
687}
688
689func isStartOfMonth(t time.Time) bool {
690 y := t.Year()

Callers 4

QueryMethod · 0.85
CountMethod · 0.85
GroupByMethod · 0.85
SeriesMethod · 0.85

Calls 5

isStartOfMonthFunction · 0.85
shardNameToIntFunction · 0.85
timeToShardNameFunction · 0.85
StringMethod · 0.80
QueryMethod · 0.65

Tested by

no test coverage detected