Class: Gromit::GromitController

Inherits:
ApplicationController show all
Defined in:
app/controllers/gromit/gromit_controller.rb

Constant Summary collapse

IM_A_TEAPOT =
418

Instance Method Summary collapse

Instance Method Details

#healthcheckObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/gromit/gromit_controller.rb', line 9

def healthcheck
  gromit = Gromit::Search.new

  begin
    # Send a PING command to Redis
    redis_ping_response = gromit.redis.ping

    # Check if the Redis server responded with "PONG"
    if redis_ping_response == 'PONG'

      # create the index if it's not already there
      gromit.create_index
      render json: { status: 'healthy', message: 'Redis connection is healthy' }
    else
      render json: { status: 'unhealthy', message: 'Redis connection is unhealthy' }, status: IM_A_TEAPOT
    end
  rescue Redis::CommandError, Redis::CannotConnectError => e
    if e.is_a?(Redis::CommandError)
      if e.message == "Index already exists" 
        render json: { status: 'healthy', message: 'Redis connection is healthy' }
      else
        render json: { status: 'unhealthy', message: "Unknown command error" }, status: IM_A_TEAPOT
      end
    else
      render json: { status: 'unhealthy', message: "Redis connection error: #{e.message}" }, status: IM_A_TEAPOT
    end
  end
end

#searchObject



38
39
40
41
42
# File 'app/controllers/gromit/gromit_controller.rb', line 38

def search
  gromit = Gromit::Search.new
  result = gromit.find_by_embedding(params[:embedding])
  render json: { data: result }
end

#upsertObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/gromit/gromit_controller.rb', line 44

def upsert
  gromit = Gromit::Search.new

  # Hopefully don't have to do this
  ## json_params = JSON.parse(request.raw_post) 

  # Extract the key and value from the request data
  id = params[:id]

  data = params.to_unsafe_h.deep_stringify_keys

  # Upsert the record into the Redis database
  gromit.redis.json_set("item:#{id}", Rejson::Path.root_path.str_path, data.except("action", "controller"))

  # Return a success response
  render json: { status: 'success', message: "Record upserted successfully", key: "item:#{id}" }
end