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

Method Query

database/sqlite/sharded.go:48–143  ·  view source on GitHub ↗
(params database.Params)

Source from the content-addressed store, hash-verified

46}
47
48func (driver *Sharded) Query(params database.Params) (*database.Analytics, error) {
49 // Construct DBPath
50 dbPath := manager.DBPath{
51 Name: params.DBName,
52 Directory: driver.directory,
53 }
54
55 // Check if DB file exists
56 dbExists, err := driver.DBManager.DBExists(dbPath)
57 if err != nil {
58 driver.DBManager.Logger.Error("Error executing Query/DBExists on DB %s: %v\n", dbPath, err)
59 return nil, &errors.InternalError
60 }
61
62 // DB doesn't exist
63 if !dbExists {
64 return nil, &errors.InvalidDatabaseName
65 }
66
67 // At this point, there should be shards to query
68 // Get list of shards by reading directory
69 shards := listShards(dbPath)
70 analytics := database.Analytics{}
71 cachedRequest := cachedRequest(params.URL)
72
73 // Read from each shard
74 for _, shardName := range shards {
75
76 // Don't include shard if not in timerange
77 shardInt, err := shardNameToInt(shardName)
78 if err != nil {
79 return nil, err
80 }
81
82 startInt, endInt := timeRangeToInt(params.TimeRange)
83 if shardInt < startInt || shardInt > endInt {
84 continue
85 }
86
87 // Get result if is cached
88 var shardAnalytics *database.Analytics
89
90 cacheURL, err := formatURLForCache(params.URL, shardInt, startInt, endInt, params.TimeRange)
91 if err != nil {
92 return nil, err
93 }
94
95 cached, inCache := driver.cache.Get(cacheURL)
96 if inCache {
97 err = json.Unmarshal(cached, &shardAnalytics)
98 if err != nil {
99 driver.DBManager.Logger.Error("Error unmarshaling from cache: %v\n", err)
100 return nil, err
101 }
102 } else {
103 // Else query shard
104 // Construct each shard DBPath
105 shardPath := manager.DBPath{

Callers

nothing calls this directly

Calls 11

StringMethod · 0.95
listShardsFunction · 0.85
cachedRequestFunction · 0.85
shardNameToIntFunction · 0.85
timeRangeToIntFunction · 0.85
formatURLForCacheFunction · 0.85
DBExistsMethod · 0.80
AcquireMethod · 0.80
ReleaseMethod · 0.80
QueryMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected