Class: Typesensual::RakeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/typesensual/rake_helper.rb

Constant Summary collapse

HEADER_FORMAT =
Paint["==> %s\n", :bold]
LIST_ROW_FORMAT =
"%<prefix>4s %<version>-20s %<created_at>-20s %<documents>-20s\n"
LIST_HEADER =
Paint[format(
  LIST_ROW_FORMAT,
  prefix: '', version: 'Version', created_at: 'Created At', documents: 'Documents'
), :bold, :underline]

Class Method Summary collapse

Class Method Details

.drop_version(index:, version:, output: $stdout) ⇒ Object

Drop a version of an index

Examples:

rake typesensual:drop_version[FooIndex,1]

Parameters:

  • index (String)

    The name of the index to remove a version from

  • version (String)

    The version to remove



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/typesensual/rake_helper.rb', line 141

def drop_version(index:, version:, output: $stdout)
  index = index.safe_constantize
  collection = index.collection_for(version: version)

  collection.delete!

  output.printf(
    "==> Dropped version %<version>s of %<index>s\n",
    version: version,
    index: index.name
  )
end

.index(index:, model:, output: $stdout) ⇒ Object

Index all records from a model into an index

Examples:

rake typesensual:index[FooIndex,Foo]

Parameters:

  • index (String)

    The name of the index to index into

  • model (String)

    The name of the model to index from



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/typesensual/rake_helper.rb', line 50

def index(index:, model:, output: $stdout)
  index = index.safe_constantize
  model = model.safe_constantize

  collection = index.create!
  output.printf(
    Paint["==> Indexing %<model>s into %<index>s (Version %<version>s)\n", :bold],
    model: model.name,
    index: index.name,
    version: collection.version
  )
  failures = index.index_many(
    model.ids,
    collection: collection
  )

  failures.each do |failure|
    output.puts(failure.to_json)
  end
end

.list(output: $stdout) ⇒ Object

List the collections in all Index descendants

Examples:

rake typesensual:list

Parameters:

  • output (IO) (defaults to: $stdout)

    The output stream to write to



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/typesensual/rake_helper.rb', line 20

def list(output: $stdout)
  # Build up a hash of indices and their (sorted) collections
  indices = Index.descendants.to_h do |index|
    [index, index.collections.sort_by(&:created_at).reverse]
  end

  indices.each do |index, collections|
    alias_name = index.collection.name

    output.printf(HEADER_FORMAT, index.name.titleize)
    output.printf(LIST_HEADER)

    collections.each do |collection|
      output.printf(LIST_ROW_FORMAT,
        prefix: collection.name == alias_name ? '->' : '',
        version: collection.version,
        created_at: collection.created_at.strftime('%Y-%m-%d %H:%M:%S'),
        documents: collection.num_documents)
    end

    output.printf("\n")
  end
end

.reindex(index:, model:, output: $stdout) ⇒ Object

Index all records from a model into a new collection, then update the alias to point to it.

Examples:

take typesensual:reindex[FooIndex,Foo]

Parameters:

  • index (String)

    The name of the index to index into

  • model (String)

    The name of the model to index from



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/typesensual/rake_helper.rb', line 77

def reindex(index:, model:, output: $stdout)
  index = index.safe_constantize
  model = model.safe_constantize

  collection = index.create!
  output.printf(
    Paint["==> Reindexing %<model>s into %<index>s (Version %<version>s)\n", :bold],
    model: model.name,
    index: index.name,
    version: collection.version
  )
  failures = index.index_many(
    model.ids,
    collection: collection
  )

  index.update_alias!(collection)

  failures.each do |failure|
    output.puts(failure.to_json)
  end
end

.update_alias(index:, version:, output: $stdout) ⇒ Object

Update the alias for an index to point to a specific version

Examples:

rake typesensual:update_alias[FooIndex,1]

Parameters:

  • index (String)

    The name of the index to update

  • version (String)

    The version to update the alias to



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/typesensual/rake_helper.rb', line 106

def update_alias(index:, version:, output: $stdout)
  index = index.safe_constantize
  old_coll = begin
    index.collection
  rescue Typesense::Error::ObjectNotFound
    nil
  end
  new_coll = index.collection_for(version: version)

  unless new_coll
    output.puts(Paint["--> No such version #{version} for #{index.name}", :bold])
    return
  end

  output.puts(Paint["==> Alias for #{index.name}", :bold])
  output.printf(
    "Old: %<version>s (%<created_at>s)\n",
    version: old_coll&.version || 'None',
    created_at: old_coll&.created_at&.strftime('%Y-%m-%d %H:%M:%S') || 'N/A'
  )
  index.update_alias!(new_coll)

  output.printf(
    "New: %<version>s (%<created_at>s)\n",
    version: new_coll.version,
    created_at: new_coll.created_at.strftime('%Y-%m-%d %H:%M:%S')
  )
end