Skip to content

Commit

Permalink
index node versions (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: James Kwon <96548424+hongil0316@users.noreply.github.com>
  • Loading branch information
james03160927 and james03160927 authored Aug 28, 2024
1 parent f8de615 commit 318b5d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions integration-tests/registry_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"registry-backend/ent"
"registry-backend/ent/nodeversion"
"registry-backend/ent/schema"
drip_logging "registry-backend/logging"
"registry-backend/mock/gateways"
"registry-backend/server/implementation"
drip_authorization "registry-backend/server/middleware/authorization"
Expand Down Expand Up @@ -727,6 +728,13 @@ func TestRegistryNodeVersion(t *testing.T) {
}
})

t.Run("Index Nodes", func(t *testing.T) {
ctx := drip_logging.SetupLogger().WithContext(ctx)
res, err := withMiddleware(authz, impl.ReindexNodes)(ctx, drip.ReindexNodesRequestObject{})
require.NoError(t, err, "should not return error")
assert.IsType(t, drip.ReindexNodes200Response{}, res)
})

t.Run("Node Installation", func(t *testing.T) {
resIns, err := withMiddleware(authz, impl.InstallNode)(ctx, drip.InstallNodeRequestObject{NodeId: *node.Id})
require.NoError(t, err, "should not return error")
Expand Down
24 changes: 23 additions & 1 deletion services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,16 +849,38 @@ func (s *RegistryService) AssertPublisherBanned(ctx context.Context, client *ent

func (s *RegistryService) ReindexAllNodes(ctx context.Context, client *ent.Client) error {
log.Ctx(ctx).Info().Msgf("reindexing nodes")
nodes, err := client.Node.Query().All(ctx)
nodes, err := client.Node.Query().
WithVersions(func(q *ent.NodeVersionQuery) {
q.Modify(func(s *sql.Selector) {
s.Where(sql.ExprP(
`(node_id, create_time) IN (
SELECT node_id, MAX(create_time)
FROM node_versions
GROUP BY node_id
)`,
))
})
}).All(ctx)
if err != nil {
return fmt.Errorf("failed to fetch all nodes: %w", err)
}

nvs := []*ent.NodeVersion{}
for _, node := range nodes {
nvs = append(nvs, node.Edges.Versions...)
}

log.Ctx(ctx).Info().Msgf("reindexing %d number of nodes", len(nodes))
err = s.algolia.IndexNodes(ctx, nodes...)
if err != nil {
return fmt.Errorf("failed to reindex all nodes: %w", err)
}

log.Ctx(ctx).Info().Msgf("reindexing %d number of nodes version", len(nvs))
err = s.algolia.IndexNodeVersions(ctx, nvs...)
if err != nil {
return fmt.Errorf("failed to reindex all nodes: %w", err)
}
return nil
}

Expand Down

0 comments on commit 318b5d3

Please sign in to comment.