diff --git a/CHANGELOG.md b/CHANGELOG.md index 7676b7b..d46bc40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pkg/dbs/globaldb/instances_test.go b/pkg/dbs/globaldb/instances_test.go index 7443d30..7f9348a 100644 --- a/pkg/dbs/globaldb/instances_test.go +++ b/pkg/dbs/globaldb/instances_test.go @@ -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)) } }) diff --git a/pkg/grpc/service/study-management_endpoints.go b/pkg/grpc/service/study-management_endpoints.go index 4740776..ed4e1d9 100644 --- a/pkg/grpc/service/study-management_endpoints.go +++ b/pkg/grpc/service/study-management_endpoints.go @@ -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 { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 2933d0b..5e6901f 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -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 } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index d1e6e94..b6fa4b6 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -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) } } }