Exemple #1
0
func (driver *SQLite) Count(params database.Params) (*database.Count, error) {
	// Construct DBPath
	dbPath := manager.DBPath{
		Name:      params.DBName,
		Directory: driver.directory,
	}

	// Check if DB file exists
	dbExists, err := driver.DBManager.DBExists(dbPath)
	if err != nil {
		return nil, &errors.InternalError
	}

	// DB doesn't exist
	if !dbExists {
		return nil, &errors.InvalidDatabaseName
	}

	// Get DB from manager
	db, err := driver.DBManager.Acquire(dbPath)
	if err != nil {
		return nil, &errors.InternalError
	}
	defer driver.DBManager.Release(db)

	// Return query result
	analytics, err := query.Count(db.DB, params.TimeRange)
	if err != nil {
		return nil, &errors.InternalError
	}

	return analytics, nil
}
Exemple #2
0
func (driver *Sharded) Count(params database.Params) (*database.Count, error) {
	// Construct DBPath
	dbPath := manager.DBPath{
		Name:      params.DBName,
		Directory: driver.directory,
	}

	// Check if DB file exists
	dbExists, err := driver.DBManager.DBExists(dbPath)
	if err != nil {
		driver.DBManager.Logger.Error("Error executing Count/DBExists on DB %s: %v\n", dbPath, err)
		return nil, &errors.InternalError
	}

	// DB doesn't exist
	if !dbExists {
		return nil, &errors.InvalidDatabaseName
	}

	// At this point, there should be shards to query
	// Get list of shards by reading directory
	shards := listShards(dbPath)

	// Aggregated query result
	analytics := database.Count{}

	cachedRequest := cachedRequest(params.URL)

	// Read from each shard
	for _, shardName := range shards {
		// Don't include shard if not in timerange
		shardInt, err := shardNameToInt(shardName)
		if err != nil {
			return nil, err
		}

		startInt, endInt := timeRangeToInt(params.TimeRange)
		if shardInt < startInt || shardInt > endInt {
			continue
		}

		// Get result if is cached
		var shardAnalytics *database.Count

		cacheURL, err := formatURLForCache(params.URL, shardInt, startInt, endInt, params.TimeRange)
		if err != nil {
			return nil, err
		}

		cached, inCache := driver.cache.Get(cacheURL)
		if inCache {
			err = json.Unmarshal(cached, &shardAnalytics)
			if err != nil {
				driver.DBManager.Logger.Error("Error unmarshaling from cache: %v\n", err)
				return nil, err
			}
		} else {
			// Else query shard
			// Construct each shard DBPath
			shardPath := manager.DBPath{
				Name:      shardName,
				Directory: dbPath.String(),
			}

			// Get DB shard from manager
			db, err := driver.DBManager.Acquire(shardPath)
			if err != nil {
				driver.DBManager.Logger.Error("Error executing Count/Acquire on DB %s: %v\n", shardPath, err)
				return nil, &errors.InternalError
			}
			defer driver.DBManager.Release(db)

			// Launch query
			shardAnalytics, err = query.Count(db.DB, params.TimeRange)
			if err != nil {
				driver.DBManager.Logger.Error("Error executing Count on DB %s: %v\n", shardPath, err)
				return nil, &errors.InternalError
			}

			// Set shard result in cache if asked
			if cachedRequest {
				if data, err := json.Marshal(shardAnalytics); err == nil {
					err = driver.cache.Set(cacheURL, data)
					if err != nil {
						driver.DBManager.Logger.Error("Error adding to cache: %v\n", err)
					}
				}
			}
		}

		// Add shard result to main result
		analytics.Total += shardAnalytics.Total
		analytics.Unique += shardAnalytics.Unique
	}

	return &analytics, nil
}