Skip to content

Commit df7fed1

Browse files
committed
speeds and technology now Enum
1 parent 4e59dc0 commit df7fed1

6 files changed

Lines changed: 129 additions & 144 deletions

File tree

lib/nbn/resources/nbn_ethernet.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
1818

1919
alias DiffoExample.Nbn
2020
alias DiffoExample.Nbn.Util
21+
alias DiffoExample.Nbn.Speeds
2122

2223
use Ash.Resource,
2324
fragments: [BaseInstance],
@@ -144,7 +145,7 @@ defmodule DiffoExample.Nbn.NbnEthernet do
144145
# calculate the speeds from the extracted technology and bandwidth_profile
145146
speeds =
146147
{:speeds,
147-
Util.speeds(
148+
Speeds.speeds(
148149
Keyword.get(pri_updates, :bandwidth_profile),
149150
Keyword.get(pri_updates, :technology)
150151
)}

lib/nbn/resources/types/bandwidth_profile.ex

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@ defmodule DiffoExample.Nbn.BandwidthProfile do
88
BandwidthProfile type for NBN domain
99
"""
1010

11-
require Ash.Type.NewType
12-
13-
use Ash.Type.NewType,
14-
subtype_of: :atom,
15-
constraints: [one_of: bandwidth_profiles()]
16-
17-
def default do
18-
:home_fast
19-
end
20-
21-
def bandwidth_profiles do
22-
[
11+
use Ash.Type.Enum,
12+
values: [
2313
:D12_U1,
2414
:D25_U5,
2515
:D25_U10,
@@ -36,5 +26,6 @@ defmodule DiffoExample.Nbn.BandwidthProfile do
3626
:home_ultrafast,
3727
:home_hyperfast
3828
]
39-
end
29+
30+
def default, do: :home_fast
4031
end

lib/nbn/resources/types/speeds.ex

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule DiffoExample.Nbn.Speeds do
99
"""
1010

1111
require Ash.Type.NewType
12+
alias DiffoExample.Nbn.Technology
1213

1314
use Ash.Type.NewType,
1415
subtype_of: :tuple,
@@ -65,4 +66,123 @@ defmodule DiffoExample.Nbn.Speeds do
6566
|> Jason.encode!()
6667
end
6768
end
69+
70+
@doc """
71+
Returns a tuple of maximum downstream and upstream speeds in Mbps
72+
given the bandwidth_profile and technology, or :error
73+
74+
## Examples
75+
iex> DiffoExample.Nbn.Speeds.speeds(:D12_U1, :Satellite)
76+
{12, 1}
77+
iex> DiffoExample.Nbn.Speeds.speeds(:home_fast, :FTTP)
78+
{500, 50}
79+
iex> DiffoExample.Nbn.Speeds.speeds(:home_hyperfast, :HFC)
80+
{2000, 100}
81+
iex> DiffoExample.Nbn.Speeds.speeds(:home_fast, :FixedWireless)
82+
:error
83+
"""
84+
def speeds(:D12_U1, technology) when is_atom(technology) do
85+
if technology in Technology.values() do
86+
{12, 1}
87+
else
88+
:error
89+
end
90+
end
91+
92+
def speeds(:D25_U5, technology) when is_atom(technology) do
93+
if technology in Technology.values() do
94+
{25, 5}
95+
else
96+
:error
97+
end
98+
end
99+
100+
def speeds(:D25_U10, technology) when is_atom(technology) do
101+
if technology in [:FTTP, :HFC, :FTTC] do
102+
{25, 10}
103+
else
104+
:error
105+
end
106+
end
107+
108+
def speeds(:D50_U20, technology) when is_atom(technology) do
109+
if technology in [:FTTP, :HFC, :FTTC] do
110+
{50, 20}
111+
else
112+
:error
113+
end
114+
end
115+
116+
def speeds(bandwidth_profile, :FixedWireless) do
117+
case bandwidth_profile do
118+
:wireless_plus ->
119+
{100, 20}
120+
121+
:wireless_fast ->
122+
{250, 20}
123+
124+
:wireless_superfast ->
125+
{400, 40}
126+
127+
_ ->
128+
:error
129+
end
130+
end
131+
132+
def speeds(bandwidth_profile, :HFC) do
133+
case bandwidth_profile do
134+
:home_fast ->
135+
{500, 50}
136+
137+
:home_superfast ->
138+
{750, 50}
139+
140+
:home_ultrafast ->
141+
{1000, 100}
142+
143+
:home_hyperfast ->
144+
{2000, 100}
145+
146+
:U100_D40 ->
147+
{100, 40}
148+
149+
_ ->
150+
:error
151+
end
152+
end
153+
154+
def speeds(bandwidth_profile, :FTTP) do
155+
case bandwidth_profile do
156+
:home_fast ->
157+
{500, 50}
158+
159+
:home_superfast ->
160+
{750, 50}
161+
162+
:home_ultrafast ->
163+
{1000, 100}
164+
165+
:home_hyperfast ->
166+
{2000, 200}
167+
168+
:D100_U40 ->
169+
{100, 40}
170+
171+
:D250_U100 ->
172+
{250, 100}
173+
174+
:D500_200 ->
175+
{500, 200}
176+
177+
:D1000_400 ->
178+
{1000, 400}
179+
180+
_ ->
181+
:error
182+
end
183+
end
184+
185+
def speeds(_bandwidth, _technology) do
186+
:error
187+
end
68188
end

lib/nbn/resources/types/technology.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ defmodule DiffoExample.Nbn.Technology do
88
Technology type for NBN domain
99
"""
1010

11-
require Ash.Type.NewType
12-
13-
use Ash.Type.NewType,
14-
subtype_of: :atom,
15-
constraints: [one_of: technology()]
11+
use Ash.Type.Enum,
12+
values: [:FTTP, :FTTN, :FTTB, :FTTC, :HFC, :FixedWireless, :Satellite]
1613

1714
def default do
1815
:FTTP
1916
end
20-
21-
def technology do
22-
[:FTTP, :FTTN, :FTTB, :FTTC, :HFC, :FixedWireless, :Satellite]
23-
end
2417
end

lib/nbn/util.ex

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ defmodule DiffoExample.Nbn.Util do
99
Util - various utilities for NBN domain
1010
"""
1111

12-
alias DiffoExample.Nbn.Technology
13-
1412
@doc """
1513
Generates a new random NBN identifier with the prefix
1614
@@ -54,123 +52,4 @@ defmodule DiffoExample.Nbn.Util do
5452
%{value: value} -> value |> Diffo.Unwrap.unwrap() |> Map.get(field)
5553
end
5654
end
57-
58-
@doc """
59-
Returns a tuple of maximum downstream and upstream speeds in Mbps
60-
given the bandwidth_profile and technology, or :error
61-
62-
## Examples
63-
iex> DiffoExample.Nbn.Util.speeds(:D12_U1, :Satellite)
64-
{12, 1}
65-
iex> DiffoExample.Nbn.Util.speeds(:home_fast, :FTTP)
66-
{500, 50}
67-
iex> DiffoExample.Nbn.Util.speeds(:home_hyperfast, :HFC)
68-
{2000, 100}
69-
iex> DiffoExample.Nbn.Util.speeds(:home_fast, :FixedWireless)
70-
:error
71-
"""
72-
def speeds(:D12_U1, technology) when is_atom(technology) do
73-
if technology in Technology.technology() do
74-
{12, 1}
75-
else
76-
:error
77-
end
78-
end
79-
80-
def speeds(:D25_U5, technology) when is_atom(technology) do
81-
if technology in Technology.technology() do
82-
{25, 5}
83-
else
84-
:error
85-
end
86-
end
87-
88-
def speeds(:D25_U10, technology) when is_atom(technology) do
89-
if technology in [:FTTP, :HFC, :FTTC] do
90-
{25, 10}
91-
else
92-
:error
93-
end
94-
end
95-
96-
def speeds(:D50_U20, technology) when is_atom(technology) do
97-
if technology in [:FTTP, :HFC, :FTTC] do
98-
{50, 20}
99-
else
100-
:error
101-
end
102-
end
103-
104-
def speeds(bandwidth_profile, :FixedWireless) do
105-
case bandwidth_profile do
106-
:wireless_plus ->
107-
{100, 20}
108-
109-
:wireless_fast ->
110-
{250, 20}
111-
112-
:wireless_superfast ->
113-
{400, 40}
114-
115-
_ ->
116-
:error
117-
end
118-
end
119-
120-
def speeds(bandwidth_profile, :HFC) do
121-
case bandwidth_profile do
122-
:home_fast ->
123-
{500, 50}
124-
125-
:home_superfast ->
126-
{750, 50}
127-
128-
:home_ultrafast ->
129-
{1000, 100}
130-
131-
:home_hyperfast ->
132-
{2000, 100}
133-
134-
:U100_D40 ->
135-
{100, 40}
136-
137-
_ ->
138-
:error
139-
end
140-
end
141-
142-
def speeds(bandwidth_profile, :FTTP) do
143-
case bandwidth_profile do
144-
:home_fast ->
145-
{500, 50}
146-
147-
:home_superfast ->
148-
{750, 50}
149-
150-
:home_ultrafast ->
151-
{1000, 100}
152-
153-
:home_hyperfast ->
154-
{2000, 200}
155-
156-
:D100_U40 ->
157-
{100, 40}
158-
159-
:D250_U100 ->
160-
{250, 100}
161-
162-
:D500_200 ->
163-
{500, 200}
164-
165-
:D1000_400 ->
166-
{1000, 400}
167-
168-
_ ->
169-
:error
170-
end
171-
end
172-
173-
def speed(_bandwidth, _technology) do
174-
:error
175-
end
17655
end

test/diffo_example_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ defmodule DiffoExampleTest do
77
use ExUnit.Case
88
doctest DiffoExample.Access.Util
99
doctest DiffoExample.Nbn.Util
10+
doctest DiffoExample.Nbn.Speeds
1011
end

0 commit comments

Comments
 (0)