Skip to content

Commit

Permalink
fixup! snippet-service: Introduce Conditions API
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jun 6, 2024
1 parent 04d92e3 commit a809956
Show file tree
Hide file tree
Showing 20 changed files with 767 additions and 482 deletions.
5 changes: 3 additions & 2 deletions snippet-service/pb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ TODO Naming
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id_filter | [string](#string) | repeated | |
| configuration_id_filter | [IDFilter](#snippetservice-pb-IDFilter) | repeated | |
| configuration_id_filter | [string](#string) | repeated | |
| device_id_filter | [string](#string) | repeated | |
| condition_id_filter | [IDFilter](#snippetservice-pb-IDFilter) | repeated | |
| condition_id_filter | [string](#string) | repeated | |



Expand All @@ -275,6 +275,7 @@ TODO Naming
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id_filter | [IDFilter](#snippetservice-pb-IDFilter) | repeated | |
| configuration_id_fitler | [string](#string) | repeated | |



Expand Down
11 changes: 9 additions & 2 deletions snippet-service/pb/doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ <h3 id="snippetservice.pb.GetAppliedDeviceConfigurationsRequest">GetAppliedDevic

<tr>
<td>configuration_id_filter</td>
<td><a href="#snippetservice.pb.IDFilter">IDFilter</a></td>
<td><a href="#string">string</a></td>
<td>repeated</td>
<td><p> </p></td>
</tr>
Expand All @@ -798,7 +798,7 @@ <h3 id="snippetservice.pb.GetAppliedDeviceConfigurationsRequest">GetAppliedDevic

<tr>
<td>condition_id_filter</td>
<td><a href="#snippetservice.pb.IDFilter">IDFilter</a></td>
<td><a href="#string">string</a></td>
<td>repeated</td>
<td><p> </p></td>
</tr>
Expand Down Expand Up @@ -827,6 +827,13 @@ <h3 id="snippetservice.pb.GetConditionsRequest">GetConditionsRequest</h3>
<td><p> </p></td>
</tr>

<tr>
<td>configuration_id_fitler</td>
<td><a href="#string">string</a></td>
<td>repeated</td>
<td><p> </p></td>
</tr>

</tbody>
</table>

Expand Down
632 changes: 319 additions & 313 deletions snippet-service/pb/service.pb.go

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions snippet-service/pb/service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
}
}
},
"parameters": [
{
"name": "configurationIdFitler",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
],
"tags": [
"Conditions"
]
Expand Down Expand Up @@ -282,6 +294,16 @@
},
"collectionFormat": "multi"
},
{
"name": "configurationIdFilter",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "deviceIdFilter",
"in": "query",
Expand All @@ -291,6 +313,16 @@
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "conditionIdFilter",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
],
"tags": [
Expand Down
22 changes: 19 additions & 3 deletions snippet-service/service/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,26 @@ func errCannotGetConfigurations(err error) error {
func sendConfigurations(ctx context.Context, srv pb.SnippetService_GetConfigurationsServer, iter store.Iterator[store.Configuration]) error {
storedCfg := store.Configuration{}
for iter.Next(ctx, &storedCfg) {
var lastVersion *store.ConfigurationVersion
for i := range storedCfg.Versions {
errS := srv.Send(storedCfg.GetConfiguration(i))
if errS != nil {
return errS
err := srv.Send(storedCfg.GetConfiguration(i))
if err != nil {
return err
}
lastVersion = &storedCfg.Versions[i]
}
if storedCfg.Latest != nil {
latest, err := storedCfg.GetLatest()
if err != nil {
return err
}
if lastVersion != nil && lastVersion.Version == latest.GetVersion() {
// already sent when iterating over versions array
continue
}
err = srv.Send(latest)
if err != nil {
return err
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion snippet-service/service/http/getConditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestRequestHandlerGetConditions(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), config.TEST_TIMEOUT*100)
ctx, cancel := context.WithTimeout(context.Background(), config.TEST_TIMEOUT)
defer cancel()

shutDown := service.SetUpServices(context.Background(), t, service.SetUpServicesOAuth)
Expand Down
3 changes: 3 additions & 0 deletions snippet-service/service/http/updateConfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestRequestHandlerUpdateConfiguration(t *testing.T) {
id: conf.GetId(),
conf: &snippetPb.Configuration{
Version: 1,
Name: "updated1",
Resources: []*snippetPb.Configuration_Resource{
makeTestResource(t, "/test/1", 42),
makeTestResource(t, "/test/2", 52),
Expand All @@ -90,6 +91,7 @@ func TestRequestHandlerUpdateConfiguration(t *testing.T) {
conf: &snippetPb.Configuration{
Version: 2,
Owner: oauthService.DeviceUserID,
Name: "updated2",
Resources: []*snippetPb.Configuration_Resource{
makeTestResource(t, "/test/3", 62),
},
Expand All @@ -105,6 +107,7 @@ func TestRequestHandlerUpdateConfiguration(t *testing.T) {
conf: &snippetPb.Configuration{
Id: uuid.NewString(), // this ID will get overwritten by the ID in the query
Version: 3,
Name: "updated3",
Resources: []*snippetPb.Configuration_Resource{
makeTestResource(t, "/test/4", 72),
makeTestResource(t, "/test/5", 82),
Expand Down
92 changes: 70 additions & 22 deletions snippet-service/store/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,93 @@ func ValidateAndNormalizeConfiguration(c *pb.Configuration, isUpdate bool) error
}

type ConfigurationVersion struct {
Name string `bson:"name,omitempty"`
Version uint64 `bson:"version"`
Resources []*pb.Configuration_Resource `bson:"resources"`
Timestamp int64 `bson:"timestamp"`
}

func (cv *ConfigurationVersion) Copy() ConfigurationVersion {
c := ConfigurationVersion{
Name: cv.Name,
Version: cv.Version,
Timestamp: cv.Timestamp,
}
for _, r := range cv.Resources {
c.Resources = append(c.Resources, r.Clone())
}
return c
}

func MakeConfigurationVersion2(c *pb.Configuration) ConfigurationVersion {
return ConfigurationVersion{
Name: c.GetName(),
Version: c.GetVersion(),
Resources: c.GetResources(),
Timestamp: c.GetTimestamp(),
}
}

type Configuration struct {
Id string `bson:"_id"`
Name string `bson:"name,omitempty"`
Owner string `bson:"owner"`
Timestamp int64 `bson:"timestamp"`
Versions []ConfigurationVersion `bson:"versions,omitempty"`
Id string `bson:"_id"`
Owner string `bson:"owner"`
Latest *ConfigurationVersion `bson:"latest,omitempty"`
Versions []ConfigurationVersion `bson:"versions,omitempty"`
}

func MakeConfiguration(c *pb.Configuration) Configuration {
return Configuration{
Id: c.GetId(),
func MakeFirstConfiguration2(c *pb.Configuration) Configuration {
version := ConfigurationVersion{
Name: c.GetName(),
Owner: c.GetOwner(),
Timestamp: GetTimestampOrNow(c.GetTimestamp()),
Versions: []ConfigurationVersion{{Version: c.GetVersion(), Resources: c.GetResources()}},
Version: c.GetVersion(),
Resources: c.GetResources(),
Timestamp: c.GetTimestamp(),
}

return Configuration{
Id: c.GetId(),
Owner: c.GetOwner(),
Latest: &version,
Versions: []ConfigurationVersion{version},
}
}

func (c *Configuration) Clone() *Configuration {
return &Configuration{
func (c *Configuration) GetLatest() (*pb.Configuration, error) {
if c.Latest == nil {
return nil, errors.New("latest configuration not set")
}
return &pb.Configuration{
Id: c.Id,
Name: c.Name,
Owner: c.Owner,
Timestamp: c.Timestamp,
Versions: slices.Clone(c.Versions),
}
Version: c.Latest.Version,
Name: c.Latest.Name,
Resources: c.Latest.Resources,
Timestamp: c.Latest.Timestamp,
}, nil
}

func (c *Configuration) GetConfiguration(version int) *pb.Configuration {
func (c *Configuration) GetConfiguration(index int) *pb.Configuration {
return &pb.Configuration{
Id: c.Id,
Name: c.Name,
Owner: c.Owner,
Timestamp: c.Timestamp,
Version: c.Versions[version].Version,
Resources: c.Versions[version].Resources,
Name: c.Versions[index].Name,
Timestamp: c.Versions[index].Timestamp,
Version: c.Versions[index].Version,
Resources: c.Versions[index].Resources,
}
}

func (c *Configuration) Clone() *Configuration {
c2 := &Configuration{
Id: c.Id,
Owner: c.Owner,
}
if c.Latest != nil {
latest := c.Latest.Copy()
c2.Latest = &latest
}

for _, v := range c.Versions {
c2.Versions = append(c2.Versions, v.Copy())
}
return c2
}
8 changes: 4 additions & 4 deletions snippet-service/store/cqldb/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ func (s *Store) CreateConfiguration(context.Context, *pb.Configuration) (*pb.Con
return nil, store.ErrNotSupported
}

func (s *Store) DeleteConfigurations(context.Context, string, *pb.DeleteConfigurationsRequest) (int64, error) {
return 0, store.ErrNotSupported
func (s *Store) UpdateConfiguration(context.Context, *pb.Configuration) (*pb.Configuration, error) {
return nil, store.ErrNotSupported
}

func (s *Store) GetConfigurations(context.Context, string, *pb.GetConfigurationsRequest, store.ProcessConfigurations) error {
return store.ErrNotSupported
}

func (s *Store) UpdateConfiguration(context.Context, *pb.Configuration) (*pb.Configuration, error) {
return nil, store.ErrNotSupported
func (s *Store) DeleteConfigurations(context.Context, string, *pb.DeleteConfigurationsRequest) (int64, error) {
return 0, store.ErrNotSupported
}
4 changes: 2 additions & 2 deletions snippet-service/store/mongodb/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (s *Store) getConditionsByFind(ctx context.Context, owner string, idfAlls [

func (s *Store) getConditionsByAggregation(ctx context.Context, owner, id string, vf pb.VersionFilter, p store.ProcessConditions) error {
pl := mongo.Pipeline{bson.D{{Key: "$match", Value: addMatchCondition(owner, id)}}}
pl = getVersionsPipeline(pl, vf, false)
pl = getVersionsPipelineObsolete(pl, vf, false)
cur, err := s.Collection(conditionsCol).Aggregate(ctx, pl)
if err != nil {
return err
Expand All @@ -218,5 +218,5 @@ func (s *Store) GetConditions(ctx context.Context, owner string, query *pb.GetCo
}

func (s *Store) DeleteConditions(ctx context.Context, owner string, query *pb.DeleteConditionsRequest) (int64, error) {
return s.delete(ctx, conditionsCol, owner, query.GetIdFilter())
return s.deleteObsolete(ctx, conditionsCol, owner, query.GetIdFilter())
}
Loading

0 comments on commit a809956

Please sign in to comment.