-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
views,usage: Move query specs to their own file
- Loading branch information
Showing
6 changed files
with
111 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package usage | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type FromToQuerySpec struct { | ||
From, To *time.Time | ||
} | ||
|
||
var allowedTimeSteps = map[string]bool{ | ||
"hour": true, | ||
"day": true, | ||
} | ||
|
||
type QueryFilter struct { | ||
CreatorID string | ||
UserID string | ||
} | ||
|
||
type QuerySpec struct { | ||
TimeStep string | ||
From, To *time.Time | ||
Filter QueryFilter | ||
BreakdownBy []string | ||
} | ||
|
||
var usageBreakdownFields = map[string]string{ | ||
"creatorId": "creator_id", | ||
} | ||
|
||
func (q *QuerySpec) HasAnyBreakdown() bool { | ||
return len(q.BreakdownBy) > 0 || q.TimeStep != "" | ||
} | ||
|
||
func (q *QuerySpec) hasBreakdownBy(e string) bool { | ||
// callers always set `e` as a string literal so we can panic if it's not valid | ||
if usageBreakdownFields[e] == "" { | ||
panic(fmt.Sprintf("unknown breakdown field %q", e)) | ||
} | ||
|
||
for _, a := range q.BreakdownBy { | ||
if strings.EqualFold(a, e) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package views | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type QueryFilter struct { | ||
PlaybackID string | ||
CreatorID string | ||
UserID string | ||
} | ||
|
||
type QuerySpec struct { | ||
From, To *time.Time | ||
TimeStep string | ||
Filter QueryFilter | ||
BreakdownBy []string | ||
Detailed bool | ||
} | ||
|
||
var viewershipBreakdownFields = map[string]string{ | ||
"playbackId": "playback_id", | ||
"dStorageUrl": "d_storage_url", | ||
"deviceType": "device_type", | ||
"device": "device", | ||
"cpu": "cpu", | ||
"os": "os", | ||
"browser": "browser", | ||
"browserEngine": "browser_engine", | ||
"continent": "playback_continent_name", | ||
"country": "playback_country_name", | ||
"subdivision": "playback_subdivision_name", | ||
"timezone": "playback_timezone", | ||
"geohash": "playback_geo_hash", | ||
"viewerId": "viewer_id", | ||
"creatorId": "creator_id", | ||
} | ||
|
||
var allowedTimeSteps = map[string]bool{ | ||
"hour": true, | ||
"day": true, | ||
"week": true, | ||
"month": true, | ||
"year": true, | ||
} | ||
|
||
func (q *QuerySpec) hasBreakdownBy(e string) bool { | ||
// callers always set `e` as a string literal so we can panic if it's not valid | ||
if viewershipBreakdownFields[e] == "" { | ||
panic(fmt.Sprintf("unknown breakdown field %q", e)) | ||
} | ||
|
||
for _, a := range q.BreakdownBy { | ||
if strings.EqualFold(a, e) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |