Skip to content

Commit

Permalink
Kalimat search (#614)
Browse files Browse the repository at this point in the history
* fix rub el hizb

* kalimat search

* sync api

* include action view rendering for v3
  • Loading branch information
naveed-ahmad authored Mar 15, 2023
1 parent f77231b commit 6b45989
Show file tree
Hide file tree
Showing 53 changed files with 2,710 additions and 215 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ gem 'graphql', '= 1.11.4'
gem 'graphql-schema_comparator'
#gem 'graphql-playground', github: 'naveed-ahmad/graphql-playground-rails'

gem 'cld3', '= 3.4.4'

#gem 'cld3', '= 3.4.4'
gem 'cld3', '= 3.4.3'

# Elasticsearch
gem 'elastic-transport'
Expand Down Expand Up @@ -86,3 +88,4 @@ group :test, :development do
gem 'rubocop-rspec', '>= 2.1.0'
gem 'json-schema-rspec'
end
gem "kredis", "~> 1.3"
15 changes: 12 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ GEM
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
cld3 (3.4.4)
cld3 (3.4.3)
ffi (>= 1.1.0, < 1.16.0)
coderay (1.1.3)
concurrent-ruby (1.1.10)
connection_pool (2.3.0)
crass (1.0.6)
dead_end (3.1.1)
derailed_benchmarks (2.1.1)
Expand Down Expand Up @@ -176,6 +177,9 @@ GEM
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
kredis (1.3.0.1)
activesupport (>= 6.0.0)
redis (>= 4.2, < 6)
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -264,6 +268,10 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
redis (5.0.6)
redis-client (>= 0.9.0)
redis-client (0.14.0)
connection_pool
regexp_parser (2.2.1)
reverse_markdown (2.1.1)
nokogiri
Expand Down Expand Up @@ -381,14 +389,15 @@ DEPENDENCIES
benchmark-ips
bullet
byebug
cld3 (= 3.4.4)
cld3 (= 3.4.3)
derailed_benchmarks
elastic-transport
elasticsearch-model
factory_bot_rails (>= 6.2.0)
graphql (= 1.11.4)
graphql-schema_comparator
json-schema-rspec
kredis (~> 1.3)
oj
oj_mimic_json
pagy
Expand All @@ -400,7 +409,7 @@ DEPENDENCIES
rack-brotli
rack-cors
rails (~> 7.0.2, >= 7.0.2.3)
rails-html-sanitizer (>= 1.4.2)
rails-html-sanitizer (>= 1.4.4)
rspec-rails (= 5.0.2)
rubocop (>= 1.7.0)
rubocop-rails_config (>= 1.9.1)
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/api/qdc/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def chapter_reciters

def translations
load_translations

render
end

# TODO: deprecated, moved the filters to /resources/translations api
def filter
translation_ids = params[:translations].to_s.split(',')
@translations = load_translations.where(id: translation_ids)
Expand Down Expand Up @@ -116,6 +118,10 @@ def load_translations
.approved
.order('priority ASC')

if params[:ids].present? || params[:name].present?
list = list.filter_by(ids: params[:ids], name: params[:name])
end

@translations = eager_load_translated_name(list)
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v3/api_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Api::V3
class ApiController < ApplicationController
include ActionView::Rendering
end
end
2 changes: 2 additions & 0 deletions app/controllers/api/v4/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Api::V4
class ApiController < ApplicationController
include ActionView::Rendering

protected
def fetch_translation_resource
approved = ResourceContent
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v4/hizbs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def index
end

def show
@hizb = Hizb.find_by(id: params[:id])
@hizb = Hizb.find_by(hizb_number: params[:id])

if @juz.nil?
if @hizb.nil?
render_404("Hizb not found. Please select valid hizb number from 1-60")
else
render
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v4/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ def languages
def changes
if time = after_timestamp
@resources = ResourceContent
.changes(after: time)
.change_log(after: time)
.filter_subtype(params[:type])
.approved

render
else
render_422("Pass in valid datetime")
render_422("after_timestamp is invalid or missing. Please use a valid date or unix timestamp(in seconds)")
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class ApplicationController < ActionController::API
include ActionController::Caching
include QuranUtils::StrongMemoize
include ActionView::Rendering

before_action :set_cache_headers
before_action :set_default_response_format
Expand All @@ -23,10 +22,18 @@ def render_404(error=nil)
render partial: "api/errors/404", locals: { message: error.to_s }, status: :not_found
end

def render_bad_request(error=nil, status=:bad_request)
render partial: "api/errors/bad_request", locals: { message: error.to_s }, status: status
end

def render_422(error=nil)
render partial: "api/errors/422", locals: { message: error.to_s }, status: :unprocessable_entity
end

def render_request_error(error, status)
render partial: "api/errors/requst_error", locals: { message: error.to_s, status: status }, status: status
end

def fetch_locale
params[:language].presence || params[:locale].presence || 'en'
end
Expand Down
63 changes: 63 additions & 0 deletions app/controllers/kalimat/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Kalimat
class SearchController < ApplicationController
include ActionController::Renderers::All
use_renderers :json

before_action :init_kalimat_api_client
before_action :authorize_api_access

def search
render json: @api.search(search_params)
end

def suggest
render json: @api.suggest(suggest_params)
end

protected
def authorize_api_access
qdc_client = qdc_api_client

if qdc_client.blank?
render_bad_request("Bad request. Api key is missing.")
elsif qdc_client.rate_limited?
render_request_error("too many requests", 429)
else
qdc_client.track_api_call(query: query) if query.present?
end
end

def init_kalimat_api_client
@api = KalimatApi.new(qdc_api_client)
end

def qdc_api_client
@api_client ||= ApiClient.where(active: true).find_by(api_key: request.headers['api-key'])
end

def query
params[:query].strip.presence if params[:query]
end

def search_params
params.permit(
:query,
:exact_match,
:include_text,
:page,
:per_page
).compact_blank
end

def suggest_params
params.permit(
:query,
:debug,
:highlight,
:include_text,
:page,
:per_page
).compact_blank
end
end
end
40 changes: 40 additions & 0 deletions app/models/api_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# == Schema Information
# Schema version: 20230313013539
#
# Table name: api_clients
#
# id :bigint not null, primary key
# active :boolean default(TRUE)
# api_key :string not null
# current_period_ends_at :datetime
# current_period_requests_count :integer
# name :string
# requests_count :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_api_clients_on_active (active)
# index_api_clients_on_api_key (api_key)
#
class ApiClient < ApplicationRecord
def self.sync(attrs)
client = ApiClient.where(id: attrs[:id]).first_or_initialize
client.attributes = attrs

client.save(validate: false)
client
end

def rate_limited?
return false if internal_api?

current_period_requests_count.to_i > request_quota.to_i
end

def track_api_call(query:)
list = Kredis.list("api_client:#{id}-requests")
list.append(Oj.dump({ query: query, timestamp: Time.now.to_i }))
end
end
19 changes: 19 additions & 0 deletions app/models/api_client_request_stat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# == Schema Information
# Schema version: 20230313013539
#
# Table name: api_client_request_stats
#
# id :bigint not null, primary key
# date :date
# requests_count :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
# api_client_id :integer
#
# Indexes
#
# index_api_client_request_stats_on_api_client_id (api_client_id)
# index_api_client_request_stats_on_date (date)
#
class ApiClientRequestStat < ApplicationRecord
end
5 changes: 1 addition & 4 deletions app/models/audio/chapter_audio_file.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
# == Schema Information
# Schema version: 20220123232023
# Schema version: 20230313013539
#
# Table name: audio_chapter_audio_files
#
Expand All @@ -22,16 +22,13 @@
# updated_at :datetime not null
# audio_recitation_id :integer
# chapter_id :integer
# resource_content_id :integer
#
# Indexes
#
# index_audio_chapter_audio_files_on_audio_recitation_id (audio_recitation_id)
# index_audio_chapter_audio_files_on_chapter_id (chapter_id)
# index_audio_chapter_audio_files_on_format (format)
# index_audio_chapter_audio_files_on_resource_content_id (resource_content_id)
#

class Audio::ChapterAudioFile < ApplicationRecord
belongs_to :audio_recitation, class_name: 'Audio::Recitation'
belongs_to :chapter
Expand Down
9 changes: 5 additions & 4 deletions app/models/audio/recitation.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# frozen_string_literal: true
# == Schema Information
# Schema version: 20220123232023
# Schema version: 20230313013539
#
# Table name: audio_recitations
#
# id :bigint not null, primary key
# approved :boolean
# approved :boolean default(FALSE)
# arabic_name :string
# description :text
# files_count :integer
# files_size :float
# format :string
# home :integer
# lock_segments :boolean default(FALSE)
# name :string
# priority :integer
# relative_path :string
# segment_locked :boolean default(FALSE)
# segments_count :integer
# segment_locked :integer
# created_at :datetime not null
# updated_at :datetime not null
# qirat_type_id :integer
Expand All @@ -30,6 +30,7 @@
# index_audio_recitations_on_approved (approved)
# index_audio_recitations_on_name (name)
# index_audio_recitations_on_priority (priority)
# index_audio_recitations_on_qirat_type_id (qirat_type_id)
# index_audio_recitations_on_recitation_style_id (recitation_style_id)
# index_audio_recitations_on_reciter_id (reciter_id)
# index_audio_recitations_on_relative_path (relative_path)
Expand Down
3 changes: 1 addition & 2 deletions app/models/audio/segment.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true
# == Schema Information
# Schema version: 20220123232023
# Schema version: 20230313013539
#
# Table name: audio_segments
#
# id :bigint not null, primary key
# duration :integer
# duration_ms :integer
# percentile :float
# relative_segments :jsonb
# relative_silent_duration :integer
Expand Down
Loading

0 comments on commit 6b45989

Please sign in to comment.