Advertisement
Guest User

bix nood muhfugga

a guest
Jun 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 10.90 KB | None | 0 0
  1. %%%-------------------------------------------------------------------
  2. %%% @author Jarosław Bieniek
  3. %%% @copyright (C) 2018, linuxutopia.net
  4. %%% @doc
  5. %%%
  6. %%% @end
  7. %%% Created : 01. kwi 2018 21:13
  8. %%%-------------------------------------------------------------------
  9. -module(pollution).
  10. -author("Jarosław Bieniek").
  11.  
  12. %% API
  13. -export([
  14.   createMonitor/0,
  15.   addStation/3,
  16.   addValue/5,
  17.   getStationMean/3,
  18.   removeValue/4,
  19.   getOneValue/4,
  20.   getDailyMean/3,
  21.   getHourlyMean/4,
  22.   getHighestHour/2
  23. ]).
  24.  
  25. createMonitor() -> #{stations => [], measurements => []}.
  26.  
  27. addStation(Name, Coordinates, Monitor) ->
  28.   case lists:keymember(Name, 1, maps:get(stations, Monitor)) or
  29.     lists:keymember(Coordinates, 2, maps:get(stations, Monitor)) of
  30.     false -> {ok, maps:update(stations, maps:get(stations, Monitor) ++ [{Name, Coordinates}], Monitor)};
  31.     _ -> {error, io_lib:format("There already exists a station with name ~s and coordinates ~w.~n", [Name, Coordinates]),
  32.       Monitor}
  33.   end.
  34.  
  35. addValue(Name, Date, Type, Value, Monitor) when is_list(Name) ->
  36.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
  37.     [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name]),
  38.     Monitor};
  39.     _ -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <- maps:get(measurements, Monitor),
  40.       NameGet == Name, Date == DateGet, TypeGet == Type] of
  41.            [] -> {ok, maps:update(measurements,
  42.              maps:get(measurements, Monitor) ++ [{Name, Date, Type, Value}], Monitor)};
  43.            _ -> {error, io_lib:format("The measurement ~w has already been registered within station ~s.~n",
  44.              [{Date, Type, Value}, Name]),
  45.              Monitor}
  46.            end
  47.   end;
  48.  
  49. addValue({X, Y}, Date, Type, Value, Monitor) ->
  50.   case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
  51.     {value, Station} -> {Name, _} = Station,
  52.       case [NameGet || {NameGet, DateGet, TypeGet, _} <- maps:get(measurements, Monitor),
  53.         NameGet == Name, DateGet == Date, TypeGet == Type] of
  54.         [] -> {ok, maps:update(measurements,
  55.           maps:get(measurements, Monitor) ++ [{Name, Date, Type, Value}], Monitor)};
  56.         _ -> {error, io_lib:format("The measurement ~w has already been registered within station ~s.~n",
  57.           [{Date, Type, Value}, Name]),
  58.           Monitor}
  59.       end;
  60.     _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}]),
  61.       Monitor}
  62.   end.
  63.  
  64. removeValue(Name, Date, Type, Monitor) when is_list(Name) ->
  65.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
  66.     [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name]),
  67.     Monitor};
  68.     _ -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
  69.            maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
  70.            [] -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station ~s.~n",
  71.              [{Date, Type}, Name]),
  72.            Monitor};
  73.            [{NameGet, DateGet, TypeGet, ValueGet}] -> {ok, maps:update(measurements,
  74.              lists:delete({NameGet, DateGet, TypeGet, ValueGet}, maps:get(measurements, Monitor)), Monitor)}
  75.          end
  76.   end;
  77.  
  78. removeValue({X, Y}, Date, Type, Monitor) ->
  79.   case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
  80.     {value, Station} -> {Name, _} = Station,
  81.       case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
  82.         maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
  83.         [] -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station with coordinates ~w.~n",
  84.           [{Date, Type}, {X, Y}]),
  85.         Monitor};
  86.         [{NameGet, DateGet, TypeGet, ValueGet}] -> {ok, maps:update(measurements,
  87.           lists:delete({NameGet, DateGet, TypeGet, ValueGet}, maps:get(measurements, Monitor)), Monitor)}
  88.       end;
  89.     _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}]),
  90.       Monitor}
  91.   end.
  92.  
  93. getOneValue(Name, Date, Type, Monitor) when is_list(Name) ->
  94.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
  95.     [Name] -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
  96.                 maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
  97.                 [{Name, Date, Type, ValueGet}] -> {ok, ValueGet};
  98.                 _ -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station ~s.~n",
  99.                   [{Date, Type}, Name])}
  100.               end;
  101.     _ -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name])}
  102.   end;
  103.  
  104. getOneValue({X, Y}, Date, Type, Monitor) ->
  105.   case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
  106.     {value, Station} -> {Name, _} = Station,
  107.       case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
  108.         maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
  109.         [{Name, Date, Type, ValueGet}] -> {ok, ValueGet};
  110.         _ -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station with coordinates ~w.~n",
  111.           [{Date, Type}, {X, Y}])}
  112.       end;
  113.     _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}])}
  114.   end.
  115.  
  116. getStationMean(Name, Type, Monitor) when is_list(Name) ->
  117.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
  118.     [] -> {error, io_lib:format("Station ~s doesn't exist.~n", Name)};
  119.     _ -> case MeasurementSet = [Measurement || Measurement = {NameGet, _, TypeGet, _} <-
  120.       maps:get(measurements, Monitor), NameGet == Name, TypeGet == Type] of
  121.            [] -> {error, io_lib:format("Station ~s doesn't have any values measured.~n", Name)};
  122.            _ -> MeanInfo = lists:foldl(fun ({_, _, _, ValueArg}, {Sum, Count}) ->
  123.              {ValueArg + Sum, Count + 1} end, {0, 0}, MeasurementSet),
  124.              {ok, element(1, MeanInfo) / element(2, MeanInfo)}
  125.          end
  126.   end;
  127.  
  128. getStationMean({X, Y}, Type, Monitor) ->
  129.   case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
  130.     {value, Station} -> {Name, _} = Station,
  131.       case MeasurementSet = [Measurement || Measurement = {NameGet, _, TypeGet, _} <-
  132.         maps:get(measurements, Monitor), NameGet == Name, TypeGet == Type] of
  133.         [] -> {error, io_lib:format("Station ~s doesn't have any values measured.~n", [Name])};
  134.         _ -> MeanInfo = lists:foldl(fun ({_, _, _, ValueArg}, {Sum, Count}) ->
  135.           {ValueArg + Sum, Count + 1} end, {0, 0}, MeasurementSet),
  136.           {ok, element(1, MeanInfo) / element(2, MeanInfo)}
  137.       end;
  138.     _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}])}
  139.   end.
  140.  
  141. getDailyMean(Date, Type, Monitor) ->
  142.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor)] of
  143.     [] -> {error, io_lib:format("This monitor doesn't have any stations added.~n", [])};
  144.     _ -> case ValueSet = [ValueGet || {_, DateGet, TypeGet, ValueGet} <-
  145.       maps:get(measurements, Monitor), element(1, DateGet) == Date, TypeGet == Type] of
  146.            [] -> {error, io_lib:format("On ~w, there have been no recorded measurements of ~s.~n", [Date, Type])};
  147.            _ -> MeanInfo = lists:foldl(fun (ValueArg, {Sum, Count}) ->
  148.              {ValueArg + Sum, Count + 1} end, {0, 0}, ValueSet),
  149.              {ok, element(1, MeanInfo) / element(2, MeanInfo)}
  150.          end
  151.   end.
  152.  
  153. getHourlyMean(Name, Hour, Type, Monitor) when Hour >= 0, Hour =< 23 ->
  154.   case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
  155.     [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name])};
  156.     _ -> case ValueSet = [ValueGet || {StationNameGet, DateGet, TypeGet, ValueGet} <-
  157.       maps:get(measurements, Monitor), StationNameGet == Name, element(1, element(2, DateGet)) == Hour, TypeGet == Type] of
  158.            [] -> {error,
  159.              io_lib:format("At ~w, there have been no recorded measurements of ~s on station ~s.~n", [Hour, Type, Name])};
  160.            _ -> MeanInfo = lists:foldl(fun (ValueArg, {Sum, Count}) ->
  161.              {ValueArg + Sum, Count + 1} end, {0, 0}, ValueSet),
  162.              {ok, element(1, MeanInfo) / element(2, MeanInfo)}
  163.          end
  164.   end.
  165.  
  166. getHighestHour(Type, Monitor) ->
  167.   case [{element(1, element(2, DateGet)), ValueGet} ||
  168.     {_, DateGet, TypeGet, ValueGet} <- maps:get(measurements, Monitor), Type == TypeGet] of
  169.     [] -> {error, io_lib:format("There are no measurements of type ~s.~n", [Type])};
  170.     Pairs -> Amounts = [lists:foldl(fun ({_, ValueArg}, {Hour, Sum, Count}) ->
  171.       {Hour, Sum + ValueArg, Count + 1} end, {HourGetOuter, 0, 0}, [{HourGet, ValueGet} ||
  172.       {HourGet, ValueGet} <- Pairs, HourGet == HourGetOuter]) || HourGetOuter <- lists:seq(0, 23)],
  173.       AmountsMeans = [case Count of
  174.                         0 -> {Hour, 0};
  175.                         _ -> {Hour, Sum / Count}
  176.                       end || {Hour, Sum, Count} <- Amounts],
  177.       AmountsMeansSorted = lists:reverse(lists:keysort(2, AmountsMeans)),
  178.       {ok, element(1, lists:nth(1, AmountsMeansSorted))}
  179.   end.
  180.  
  181. prepareMonitorForTesting() ->
  182.   P = createMonitor(),
  183.   P1 = addStation("Wisniewo", {1.2, 3.4}, P),
  184.   P2 = addStation("Zurominek", {3.4, 5.6}, P1),
  185.   P3 = addStation("Podkrajewo", {5.6, 7.8}, P2),
  186.   P4 = addValue("Zurominek", {{2018, 4, 3}, {15, 50, 13}}, "PM10", 8.0, P3),
  187.   P5 = addValue("Wisniewo", {{2018, 4, 4}, {15, 20, 10}}, "PM10", 1.2, P4),
  188.   P6 = addValue({1.2, 3.4}, {{2018, 4, 3}, {15, 12, 45}}, "PM10", 2.4, P5),
  189.   P7 = addValue("Podkrajewo", {{2018, 4, 3}, {15, 20, 10}}, "PM10", 104, P6),
  190.   P8 = removeValue({5.6, 7.8}, {{2018, 4, 3}, {15, 20, 10}}, "PM10", P7),
  191.   P9 = removeValue("Wisniewo", {{2018, 4, 4}, {15, 20, 10}}, "PM10", P8),
  192.   P10 = addValue("Wisniewo", {{2018, 4, 3}, {16, 43, 12}}, "PM10", 4.6, P9),
  193.   P11 = addValue({1.2, 3.4}, {{2018, 4, 4}, {15, 23, 47}}, "PM2.5", 7, P10),
  194.   addValue("Wisniewo", {{2018, 4, 4}, {15, 23, 47}}, "PM10", 2, P11).
  195.  
  196. getOneValue_test() ->
  197.   P12 = prepareMonitorForTesting(),
  198.   (getOneValue({3.4, 5.6}, {{2018, 4, 3}, {15, 50, 13}}, "PM10", P12) == 8.0) and
  199.     (getOneValue("Wisniewo", {{2018, 4, 4}, {15, 23, 47}}, "PM2.5", P12) == 7).
  200.  
  201. getStationMean_test() ->
  202.   P12 = prepareMonitorForTesting(),
  203.   getStationMean("Wisniewo", "PM10", P12) == 4.0.
  204.  
  205. getDailyMean_test() ->
  206.   P12 = prepareMonitorForTesting(),
  207.   getDailyMean({2018, 4, 3}, "PM10", P12) == 5.0.
  208.  
  209. getHourlyMean_test() ->
  210.   P12 = prepareMonitorForTesting(),
  211.   getHourlyMean("Wisniewo", 15, "PM10", P12) == 2.2.
  212.  
  213. getFromNonexistentStation_test() ->
  214.   P12 = prepareMonitorForTesting(),
  215.   getOneValue("Bogurzyn", {{2018, 4, 3}, {15, 50, 13}}, "PM10", P12) == P12.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement