Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%-------------------------------------------------------------------
- %%% @author Jarosław Bieniek
- %%% @copyright (C) 2018, linuxutopia.net
- %%% @doc
- %%%
- %%% @end
- %%% Created : 01. kwi 2018 21:13
- %%%-------------------------------------------------------------------
- -module(pollution).
- -author("Jarosław Bieniek").
- %% API
- -export([
- createMonitor/0,
- addStation/3,
- addValue/5,
- getStationMean/3,
- removeValue/4,
- getOneValue/4,
- getDailyMean/3,
- getHourlyMean/4,
- getHighestHour/2
- ]).
- createMonitor() -> #{stations => [], measurements => []}.
- addStation(Name, Coordinates, Monitor) ->
- case lists:keymember(Name, 1, maps:get(stations, Monitor)) or
- lists:keymember(Coordinates, 2, maps:get(stations, Monitor)) of
- false -> {ok, maps:update(stations, maps:get(stations, Monitor) ++ [{Name, Coordinates}], Monitor)};
- _ -> {error, io_lib:format("There already exists a station with name ~s and coordinates ~w.~n", [Name, Coordinates]),
- Monitor}
- end.
- addValue(Name, Date, Type, Value, Monitor) when is_list(Name) ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
- [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name]),
- Monitor};
- _ -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <- maps:get(measurements, Monitor),
- NameGet == Name, Date == DateGet, TypeGet == Type] of
- [] -> {ok, maps:update(measurements,
- maps:get(measurements, Monitor) ++ [{Name, Date, Type, Value}], Monitor)};
- _ -> {error, io_lib:format("The measurement ~w has already been registered within station ~s.~n",
- [{Date, Type, Value}, Name]),
- Monitor}
- end
- end;
- addValue({X, Y}, Date, Type, Value, Monitor) ->
- case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
- {value, Station} -> {Name, _} = Station,
- case [NameGet || {NameGet, DateGet, TypeGet, _} <- maps:get(measurements, Monitor),
- NameGet == Name, DateGet == Date, TypeGet == Type] of
- [] -> {ok, maps:update(measurements,
- maps:get(measurements, Monitor) ++ [{Name, Date, Type, Value}], Monitor)};
- _ -> {error, io_lib:format("The measurement ~w has already been registered within station ~s.~n",
- [{Date, Type, Value}, Name]),
- Monitor}
- end;
- _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}]),
- Monitor}
- end.
- removeValue(Name, Date, Type, Monitor) when is_list(Name) ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
- [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name]),
- Monitor};
- _ -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
- [] -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station ~s.~n",
- [{Date, Type}, Name]),
- Monitor};
- [{NameGet, DateGet, TypeGet, ValueGet}] -> {ok, maps:update(measurements,
- lists:delete({NameGet, DateGet, TypeGet, ValueGet}, maps:get(measurements, Monitor)), Monitor)}
- end
- end;
- removeValue({X, Y}, Date, Type, Monitor) ->
- case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
- {value, Station} -> {Name, _} = Station,
- case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
- [] -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station with coordinates ~w.~n",
- [{Date, Type}, {X, Y}]),
- Monitor};
- [{NameGet, DateGet, TypeGet, ValueGet}] -> {ok, maps:update(measurements,
- lists:delete({NameGet, DateGet, TypeGet, ValueGet}, maps:get(measurements, Monitor)), Monitor)}
- end;
- _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}]),
- Monitor}
- end.
- getOneValue(Name, Date, Type, Monitor) when is_list(Name) ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
- [Name] -> case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
- [{Name, Date, Type, ValueGet}] -> {ok, ValueGet};
- _ -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station ~s.~n",
- [{Date, Type}, Name])}
- end;
- _ -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name])}
- end;
- getOneValue({X, Y}, Date, Type, Monitor) ->
- case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
- {value, Station} -> {Name, _} = Station,
- case [MeasurementGet || MeasurementGet = {NameGet, DateGet, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, DateGet == Date, TypeGet == Type] of
- [{Name, Date, Type, ValueGet}] -> {ok, ValueGet};
- _ -> {error, io_lib:format("The measurement with specified parameters ~w doesn't exist in station with coordinates ~w.~n",
- [{Date, Type}, {X, Y}])}
- end;
- _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}])}
- end.
- getStationMean(Name, Type, Monitor) when is_list(Name) ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
- [] -> {error, io_lib:format("Station ~s doesn't exist.~n", Name)};
- _ -> case MeasurementSet = [Measurement || Measurement = {NameGet, _, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, TypeGet == Type] of
- [] -> {error, io_lib:format("Station ~s doesn't have any values measured.~n", Name)};
- _ -> MeanInfo = lists:foldl(fun ({_, _, _, ValueArg}, {Sum, Count}) ->
- {ValueArg + Sum, Count + 1} end, {0, 0}, MeasurementSet),
- {ok, element(1, MeanInfo) / element(2, MeanInfo)}
- end
- end;
- getStationMean({X, Y}, Type, Monitor) ->
- case lists:keysearch({X, Y}, 2, maps:get(stations, Monitor)) of
- {value, Station} -> {Name, _} = Station,
- case MeasurementSet = [Measurement || Measurement = {NameGet, _, TypeGet, _} <-
- maps:get(measurements, Monitor), NameGet == Name, TypeGet == Type] of
- [] -> {error, io_lib:format("Station ~s doesn't have any values measured.~n", [Name])};
- _ -> MeanInfo = lists:foldl(fun ({_, _, _, ValueArg}, {Sum, Count}) ->
- {ValueArg + Sum, Count + 1} end, {0, 0}, MeasurementSet),
- {ok, element(1, MeanInfo) / element(2, MeanInfo)}
- end;
- _ -> {error, io_lib:format("The station with coordinates ~w doesn't exist.~n", [{X, Y}])}
- end.
- getDailyMean(Date, Type, Monitor) ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor)] of
- [] -> {error, io_lib:format("This monitor doesn't have any stations added.~n", [])};
- _ -> case ValueSet = [ValueGet || {_, DateGet, TypeGet, ValueGet} <-
- maps:get(measurements, Monitor), element(1, DateGet) == Date, TypeGet == Type] of
- [] -> {error, io_lib:format("On ~w, there have been no recorded measurements of ~s.~n", [Date, Type])};
- _ -> MeanInfo = lists:foldl(fun (ValueArg, {Sum, Count}) ->
- {ValueArg + Sum, Count + 1} end, {0, 0}, ValueSet),
- {ok, element(1, MeanInfo) / element(2, MeanInfo)}
- end
- end.
- getHourlyMean(Name, Hour, Type, Monitor) when Hour >= 0, Hour =< 23 ->
- case [StationNameGet || {StationNameGet, _} <- maps:get(stations, Monitor), StationNameGet == Name] of
- [] -> {error, io_lib:format("The station ~s doesn't exist.~n", [Name])};
- _ -> case ValueSet = [ValueGet || {StationNameGet, DateGet, TypeGet, ValueGet} <-
- maps:get(measurements, Monitor), StationNameGet == Name, element(1, element(2, DateGet)) == Hour, TypeGet == Type] of
- [] -> {error,
- io_lib:format("At ~w, there have been no recorded measurements of ~s on station ~s.~n", [Hour, Type, Name])};
- _ -> MeanInfo = lists:foldl(fun (ValueArg, {Sum, Count}) ->
- {ValueArg + Sum, Count + 1} end, {0, 0}, ValueSet),
- {ok, element(1, MeanInfo) / element(2, MeanInfo)}
- end
- end.
- getHighestHour(Type, Monitor) ->
- case [{element(1, element(2, DateGet)), ValueGet} ||
- {_, DateGet, TypeGet, ValueGet} <- maps:get(measurements, Monitor), Type == TypeGet] of
- [] -> {error, io_lib:format("There are no measurements of type ~s.~n", [Type])};
- Pairs -> Amounts = [lists:foldl(fun ({_, ValueArg}, {Hour, Sum, Count}) ->
- {Hour, Sum + ValueArg, Count + 1} end, {HourGetOuter, 0, 0}, [{HourGet, ValueGet} ||
- {HourGet, ValueGet} <- Pairs, HourGet == HourGetOuter]) || HourGetOuter <- lists:seq(0, 23)],
- AmountsMeans = [case Count of
- 0 -> {Hour, 0};
- _ -> {Hour, Sum / Count}
- end || {Hour, Sum, Count} <- Amounts],
- AmountsMeansSorted = lists:reverse(lists:keysort(2, AmountsMeans)),
- {ok, element(1, lists:nth(1, AmountsMeansSorted))}
- end.
- prepareMonitorForTesting() ->
- P = createMonitor(),
- P1 = addStation("Wisniewo", {1.2, 3.4}, P),
- P2 = addStation("Zurominek", {3.4, 5.6}, P1),
- P3 = addStation("Podkrajewo", {5.6, 7.8}, P2),
- P4 = addValue("Zurominek", {{2018, 4, 3}, {15, 50, 13}}, "PM10", 8.0, P3),
- P5 = addValue("Wisniewo", {{2018, 4, 4}, {15, 20, 10}}, "PM10", 1.2, P4),
- P6 = addValue({1.2, 3.4}, {{2018, 4, 3}, {15, 12, 45}}, "PM10", 2.4, P5),
- P7 = addValue("Podkrajewo", {{2018, 4, 3}, {15, 20, 10}}, "PM10", 104, P6),
- P8 = removeValue({5.6, 7.8}, {{2018, 4, 3}, {15, 20, 10}}, "PM10", P7),
- P9 = removeValue("Wisniewo", {{2018, 4, 4}, {15, 20, 10}}, "PM10", P8),
- P10 = addValue("Wisniewo", {{2018, 4, 3}, {16, 43, 12}}, "PM10", 4.6, P9),
- P11 = addValue({1.2, 3.4}, {{2018, 4, 4}, {15, 23, 47}}, "PM2.5", 7, P10),
- addValue("Wisniewo", {{2018, 4, 4}, {15, 23, 47}}, "PM10", 2, P11).
- getOneValue_test() ->
- P12 = prepareMonitorForTesting(),
- (getOneValue({3.4, 5.6}, {{2018, 4, 3}, {15, 50, 13}}, "PM10", P12) == 8.0) and
- (getOneValue("Wisniewo", {{2018, 4, 4}, {15, 23, 47}}, "PM2.5", P12) == 7).
- getStationMean_test() ->
- P12 = prepareMonitorForTesting(),
- getStationMean("Wisniewo", "PM10", P12) == 4.0.
- getDailyMean_test() ->
- P12 = prepareMonitorForTesting(),
- getDailyMean({2018, 4, 3}, "PM10", P12) == 5.0.
- getHourlyMean_test() ->
- P12 = prepareMonitorForTesting(),
- getHourlyMean("Wisniewo", 15, "PM10", P12) == 2.2.
- getFromNonexistentStation_test() ->
- P12 = prepareMonitorForTesting(),
- getOneValue("Bogurzyn", {{2018, 4, 3}, {15, 50, 13}}, "PM10", P12) == P12.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement