Skip to content

Commit

Permalink
WIP: ptime negotiation (#1524)
Browse files Browse the repository at this point in the history
* add codec group ptime config
* update rexml

---------

Co-authored-by: Denis Talakevich <senid231@gmail.com>
  • Loading branch information
dmitry-sinina and senid231 authored Aug 6, 2024
1 parent f675c0a commit e63282a
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ GEM
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.3.2)
rexml (3.3.4)
strscan
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand Down
8 changes: 6 additions & 2 deletions app/admin/equipment/codec_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
acts_as_clone duplicates: [:codec_group_codecs]
acts_as_safe_destroy

acts_as_export :id, :name
acts_as_export :id, :name, :ptime

permit_params :name,
permit_params :name, :ptime,
codec_group_codecs_attributes: %i[
id codec_id priority dynamic_payload_type format_parameters _destroy
]
Expand All @@ -25,6 +25,7 @@ def scoped_collection
id_column
actions
column :name
column :ptime
column :codecs do |row|
codec_names = row.codec_names

Expand All @@ -40,11 +41,13 @@ def scoped_collection

filter :id
filter :name
filter :ptime

form do |f|
f.semantic_errors *f.object.errors.attribute_names
f.inputs form_title do
f.input :name
f.input :ptime, hint: "Allowed values: #{CodecGroup::ALLOWED_PTIMES.join(', ')}. Leave empty to use value announced by other leg"
end

f.inputs 'Codecs' do
Expand All @@ -63,6 +66,7 @@ def scoped_collection
attributes_table do
row :id
row :name
row :ptime
end

panel 'Codecs' do
Expand Down
9 changes: 7 additions & 2 deletions app/models/codec_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#
# Table name: codec_groups
#
# id :integer(4) not null, primary key
# name :string not null
# id :integer(4) not null, primary key
# name :string not null
# ptime :integer(2)
#
# Indexes
#
Expand All @@ -15,13 +16,17 @@
class CodecGroup < ApplicationRecord
include WithPaperTrail

ALLOWED_PTIMES = [10, 20, 30, 40].freeze

has_many :codec_group_codecs, inverse_of: :codec_group, dependent: :destroy
has_many :codecs, through: :codec_group_codecs

accepts_nested_attributes_for :codec_group_codecs, allow_destroy: true

validates :name, uniqueness: { allow_blank: false }
validates :name, presence: true

validates :ptime, inclusion: { in: ALLOWED_PTIMES, message: 'Ptime %<value>s is not allowed' }, allow_nil: true
validate :check_uniqueness_of_codecs

def codec_names
Expand Down
79 changes: 79 additions & 0 deletions db/migrate/20240805121644_codec_group_ptime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class CodecGroupPtime < ActiveRecord::Migration[7.0]
def up
execute %q{
alter table class4.codec_groups add ptime smallint;
CREATE FUNCTION switch21.load_codec_groups() RETURNS TABLE(id integer, ptime smallint, codecs json)
LANGUAGE plpgsql COST 10
AS $$
BEGIN
RETURN QUERY
SELECT
cg.id,
cg.ptime,
json_agg(json_build_object(
'id', c.id,
'name', c.name,
'priority', cgc.priority,
'dynamic_payload_type', cgc.dynamic_payload_type,
'format_parameters', cgc.format_parameters
)) as codecs
FROM class4.codec_groups cg
LEFT JOIN class4.codec_group_codecs cgc ON cg.id = cgc.codec_group_id
LEFT JOIN class4.codecs c ON cgc.codec_id=c.id
GROUP BY cg.id;
END;
$$;
CREATE OR REPLACE FUNCTION switch21.load_codecs() RETURNS TABLE(o_id integer, o_codec_group_id integer, o_codec_name character varying, o_priority integer, o_dynamic_payload_id integer, o_format_params character varying)
LANGUAGE plpgsql COST 10
AS $$
BEGIN
-- TODO: this function replaced by load_codec_groups and should be deleted in future
RETURN
QUERY SELECT
cgc.id,
cgc.codec_group_id,
c.name ,
cgc.priority,
cgc.dynamic_payload_type,
cgc.format_parameters
from class4.codec_group_codecs cgc
JOIN class4.codecs c ON c.id=cgc.codec_id
order by cgc.codec_group_id,cgc.priority desc ,c.name;
END;
$$;
}
end

def down
execute %q{
alter table class4.codec_groups drop column ptime;
DROP FUNCTION switch21.load_codec_groups();
CREATE OR REPLACE FUNCTION switch21.load_codecs() RETURNS TABLE(o_id integer, o_codec_group_id integer, o_codec_name character varying, o_priority integer, o_dynamic_payload_id integer, o_format_params character varying)
LANGUAGE plpgsql COST 10
AS $$
BEGIN
RETURN
QUERY SELECT
cgc.id,
cgc.codec_group_id,
c.name ,
cgc.priority,
cgc.dynamic_payload_type,
cgc.format_parameters
from class4.codec_group_codecs cgc
JOIN class4.codecs c ON c.id=cgc.codec_id
order by cgc.codec_group_id,cgc.priority desc ,c.name;
END;
$$;
}
end

end
35 changes: 33 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22701,6 +22701,33 @@ END;
$$;


--
-- Name: load_codec_groups(); Type: FUNCTION; Schema: switch21; Owner: -
--

CREATE FUNCTION switch21.load_codec_groups() RETURNS TABLE(id integer, ptime smallint, codecs json)
LANGUAGE plpgsql COST 10
AS $$
BEGIN
RETURN QUERY
SELECT
cg.id,
cg.ptime,
json_agg(json_build_object(
'id', c.id,
'name', c.name,
'priority', cgc.priority,
'dynamic_payload_type', cgc.dynamic_payload_type,
'format_parameters', cgc.format_parameters
)) as codecs
FROM class4.codec_groups cg
LEFT JOIN class4.codec_group_codecs cgc ON cg.id = cgc.codec_group_id
LEFT JOIN class4.codecs c ON cgc.codec_id=c.id
GROUP BY cg.id;
END;
$$;


--
-- Name: load_codecs(); Type: FUNCTION; Schema: switch21; Owner: -
--
Expand All @@ -22709,6 +22736,8 @@ CREATE FUNCTION switch21.load_codecs() RETURNS TABLE(o_id integer, o_codec_group
LANGUAGE plpgsql COST 10
AS $$
BEGIN

-- TODO: this function replaced by load_codec_groups and should be deleted in future
RETURN
QUERY SELECT
cgc.id,
Expand Down Expand Up @@ -31792,7 +31821,8 @@ ALTER SEQUENCE class4.codec_group_codecs_id_seq OWNED BY class4.codec_group_code

CREATE TABLE class4.codec_groups (
id integer NOT NULL,
name character varying NOT NULL
name character varying NOT NULL,
ptime smallint
);


Expand Down Expand Up @@ -41166,6 +41196,7 @@ INSERT INTO "public"."schema_migrations" (version) VALUES
('20240704100545'),
('20240721201110'),
('20240725132743'),
('20240725135654');
('20240725135654'),
('20240805121644');


5 changes: 3 additions & 2 deletions spec/factories/codec_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#
# Table name: codec_groups
#
# id :integer(4) not null, primary key
# name :string not null
# id :integer(4) not null, primary key
# name :string not null
# ptime :integer(2)
#
# Indexes
#
Expand Down
3 changes: 2 additions & 1 deletion spec/features/equipment/codec_groups/new_codec_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
visit new_codec_group_path

fill_in 'Name', with: 'test codec group'
fill_in 'Ptime', with: '30'

click_link 'Add New Codec group codec'
within_form_has_many 'codec_group_codecs', index: 0 do
Expand All @@ -31,7 +32,7 @@
record = CodecGroup.last!
expect(page).to have_current_path codec_group_path(record.id)

expect(record).to have_attributes(name: 'test codec group')
expect(record).to have_attributes(name: 'test codec group', ptime: 30)
expect(record.codec_group_codecs.size).to eq(1)
expect(record.codec_group_codecs.first).to have_attributes(
codec_id: codec.id,
Expand Down
23 changes: 23 additions & 0 deletions spec/models/system/network_prefix_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: sys.network_prefixes
#
# id :integer(4) not null, primary key
# number_max_length :integer(2) default(100), not null
# number_min_length :integer(2) default(0), not null
# prefix :string not null
# uuid :uuid not null
# country_id :integer(4)
# network_id :integer(4)
#
# Indexes
#
# network_prefixes_prefix_key (prefix,number_min_length,number_max_length) UNIQUE
# network_prefixes_prefix_range_idx (((prefix)::prefix_range)) USING gist
# network_prefixes_uuid_key (uuid) UNIQUE
#
# Foreign Keys
#
# network_prefixes_country_id_fkey (country_id => countries.id)
# network_prefixes_network_id_fkey (network_id => networks.id)
#
RSpec.describe System::NetworkPrefix, type: :model do
describe '#validate_prefix_uniqueness' do
subject { network_prefix.errors.messages }
Expand Down
49 changes: 49 additions & 0 deletions spec/sql/switch21/load_codec_groups_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

RSpec.describe 'switch21.load_codec_groups' do
subject do
SqlCaller::Yeti.select_all(sql).map(&:deep_symbolize_keys)
end

let(:sql) do
'SELECT * FROM switch21.load_codec_groups()'
end
let!(:codec_groups) do
[
CodecGroup.find(1),
create(:codec_group, ptime: nil),
create(:codec_group, ptime: 20)
]
end

it 'responds with correct codec_groups' do
expect(subject).to match_array(
codec_groups.map do |c|
{
id: c.id,
ptime: c.ptime,
codecs: a_kind_of(String)
}
end
)
end

it 'responds with correct codecs inside codec_groups', :aggregate_failures do
subject.each do |row|
codec_group = codec_groups.detect { |c| c.id == row[:id] }
actual_codecs = JSON.parse(row[:codecs], symbolize_names: true)
expect(actual_codecs).to match_array(
codec_group.codec_group_codecs.map do |codec_group_codec|
codec = codec_group_codec.codec
{
id: codec.id,
name: codec.name,
priority: codec_group_codec.priority,
dynamic_payload_type: codec_group_codec.dynamic_payload_type,
format_parameters: codec_group_codec.format_parameters
}
end
)
end
end
end

0 comments on commit e63282a

Please sign in to comment.