Skip to content

Commit

Permalink
Merge pull request #253 from brave/master
Browse files Browse the repository at this point in the history
Release 2024/05/13 #2
  • Loading branch information
DJAndries authored May 13, 2024
2 parents f8705ba + 1103ea0 commit 91a01be
Showing 1 changed file with 51 additions and 49 deletions.
100 changes: 51 additions & 49 deletions datastore/item_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,58 @@ func (counts *ClientItemCounts) SumHistoryCounts() int {

func (dynamo *Dynamo) initRealCountsAndUpdateHistoryCounts(counts *ClientItemCounts) error {
now := time.Now().Unix()
if counts.ItemCount > 0 && counts.LastPeriodChangeTime == 0 {
// If last period change tiem is 0, assume that the old count
// exists in ItemCount, which may include history items that have expired
// Query the DB to get updated counts
pkCond := expression.Key(clientIDDataTypeMtimeIdxPk).Equal(expression.Value(counts.ClientID))
filterCond := expression.And(
expression.Name(dataTypeAttrName).In(expression.Value(HistoryTypeID), expression.Value(HistoryDeleteDirectiveTypeID)),
expression.Name(deletedAttrName).Equal(expression.Value(false)),
)
expr, err := expression.NewBuilder().WithKeyCondition(pkCond).WithFilter(filterCond).Build()
if err != nil {
return fmt.Errorf("error building history item count query: %w", err)
}
selectCount := dynamodb.SelectCount
historyCountInput := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
FilterExpression: expr.Filter(),
TableName: aws.String(Table),
Select: &selectCount,
}
out, err := dynamo.Query(historyCountInput)
if err != nil {
return fmt.Errorf("error querying history item count: %w", err)
}
counts.HistoryItemCountPeriod4 = int(*out.Count)
filterCond = expression.And(
expression.AttributeExists(expression.Name(dataTypeAttrName)),
expression.Name(dataTypeAttrName).NotEqual(expression.Value(HistoryTypeID)),
expression.Name(dataTypeAttrName).NotEqual(expression.Value(HistoryDeleteDirectiveTypeID)),
expression.Name(deletedAttrName).Equal(expression.Value(false)),
)
expr, err = expression.NewBuilder().WithKeyCondition(pkCond).WithFilter(filterCond).Build()
if err != nil {
return fmt.Errorf("error building normal item count query: %w", err)
}
normalCountInput := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
FilterExpression: expr.Filter(),
TableName: aws.String(Table),
Select: &selectCount,
}
out, err = dynamo.Query(normalCountInput)
if err != nil {
return fmt.Errorf("error querying history item count: %w", err)
if counts.LastPeriodChangeTime == 0 {
if counts.ItemCount > 0 {
// If last period change tiem is 0, assume that the old count
// exists in ItemCount, which may include history items that have expired
// Query the DB to get updated counts
pkCond := expression.Key(clientIDDataTypeMtimeIdxPk).Equal(expression.Value(counts.ClientID))
filterCond := expression.And(
expression.Name(dataTypeAttrName).In(expression.Value(HistoryTypeID), expression.Value(HistoryDeleteDirectiveTypeID)),
expression.Name(deletedAttrName).Equal(expression.Value(false)),
)
expr, err := expression.NewBuilder().WithKeyCondition(pkCond).WithFilter(filterCond).Build()
if err != nil {
return fmt.Errorf("error building history item count query: %w", err)
}
selectCount := dynamodb.SelectCount
historyCountInput := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
FilterExpression: expr.Filter(),
TableName: aws.String(Table),
Select: &selectCount,
}
out, err := dynamo.Query(historyCountInput)
if err != nil {
return fmt.Errorf("error querying history item count: %w", err)
}
counts.HistoryItemCountPeriod4 = int(*out.Count)
filterCond = expression.And(
expression.AttributeExists(expression.Name(dataTypeAttrName)),
expression.Name(dataTypeAttrName).NotEqual(expression.Value(HistoryTypeID)),
expression.Name(dataTypeAttrName).NotEqual(expression.Value(HistoryDeleteDirectiveTypeID)),
expression.Name(deletedAttrName).Equal(expression.Value(false)),
)
expr, err = expression.NewBuilder().WithKeyCondition(pkCond).WithFilter(filterCond).Build()
if err != nil {
return fmt.Errorf("error building normal item count query: %w", err)
}
normalCountInput := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
FilterExpression: expr.Filter(),
TableName: aws.String(Table),
Select: &selectCount,
}
out, err = dynamo.Query(normalCountInput)
if err != nil {
return fmt.Errorf("error querying history item count: %w", err)
}
counts.ItemCount = int(*out.Count)
}
counts.ItemCount = int(*out.Count)
counts.LastPeriodChangeTime = now
} else {
timeSinceLastChange := now - counts.LastPeriodChangeTime
Expand Down

0 comments on commit 91a01be

Please sign in to comment.