Skip to content

Commit

Permalink
change how survey version is generated to a shorter format
Browse files Browse the repository at this point in the history
  • Loading branch information
phev8 committed Apr 20, 2021
1 parent b5f8fc4 commit b023df5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v0.14.1]

### Changed

- Changed how survey version ID is generated, to use a shorter format (YY-MM-counter).

## [v0.14.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion pkg/dbs/globaldb/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDbInterfaceMethods(t *testing.T) {
t.Errorf(err.Error())
return
}
if len(instances) != 2 {
if len(instances) < 1 {
t.Errorf("unexpected number of instances: %d", len(instances))
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/grpc/service/study-management_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *studyServiceServer) SaveSurveyToStudy(ctx context.Context, req *api.Add

newSurvey := types.SurveyFromAPI(req.Survey)
if newSurvey.Current.VersionID == "" {
newSurvey.Current.VersionID = utils.GenerateSurveyVersionID()
newSurvey.Current.VersionID = utils.GenerateSurveyVersionID(newSurvey.History)
}
createdSurvey, err := s.studyDBservice.SaveSurvey(req.Token.InstanceId, req.StudyKey, newSurvey)
if err != nil {
Expand Down
32 changes: 24 additions & 8 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package utils

import (
"encoding/base32"
"encoding/binary"
"fmt"
"time"

"github.com/influenzanet/study-service/pkg/types"
)

func GenerateSurveyVersionID() string {
func GenerateSurveyVersionID(oldVersions []types.SurveyVersion) string {
t := time.Now()
ms := uint64(t.UnixNano())

b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, ms)
date := t.Format("06-01")

counter := 1
newID := fmt.Sprintf("%s-%d", date, counter)
for {
idAlreadyPresent := false
for _, v := range oldVersions {
if v.VersionID == newID {
idAlreadyPresent = true
break
}
}
if !idAlreadyPresent {
break
} else {
counter += 1
newID = fmt.Sprintf("%s-%d", date, counter)
}
}

str := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)
return str
return newID
}
18 changes: 9 additions & 9 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package utils

import (
"testing"
"time"

"github.com/influenzanet/study-service/pkg/types"
)

func TestGenerateSurveyVersionID(t *testing.T) {
t.Run("test id generation for uniqueness", func(t *testing.T) {
ids := []string{}
oldVersions := []types.SurveyVersion{}

for i := 0; i < 100; i++ {
id := GenerateSurveyVersionID()
ids = append(ids, id)
time.Sleep(time.Millisecond)
id := GenerateSurveyVersionID(oldVersions)
oldVersions = append(oldVersions, types.SurveyVersion{VersionID: id})
}

for i, id_1 := range ids {
for j, id_2 := range ids {
if i != j && id_1 == id_2 {
t.Errorf("duplicate key present: i: %d - %s j: %d - %s ", i, id_1, j, id_2)
for i, id_1 := range oldVersions {
for j, id_2 := range oldVersions {
if i != j && id_1.VersionID == id_2.VersionID {
t.Errorf("duplicate key present: i: %d - %s j: %d - %s ", i, id_1.VersionID, j, id_2.VersionID)
}
}
}
Expand Down

0 comments on commit b023df5

Please sign in to comment.