140 lines
4.1 KiB
Erlang
140 lines
4.1 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @author aresei
|
|
%%% @copyright (C) 2023, <COMPANY>
|
|
%%% @doc
|
|
%%%
|
|
%%% @end
|
|
%%% Created : 04. 7月 2023 12:31
|
|
%%%-------------------------------------------------------------------
|
|
-module(service_model).
|
|
-author("aresei").
|
|
-include("efka_tables.hrl").
|
|
-include_lib("stdlib/include/qlc.hrl").
|
|
|
|
-define(TAB, service).
|
|
|
|
%% API
|
|
-export([create_table/0]).
|
|
-export([insert/1, get_all_services/0, get_all_service_ids/0]).
|
|
-export([get_metrics/1, get_params/1, set_metrics/2, set_params/2, get_service/1, get_status/1, change_status/2]).
|
|
|
|
create_table() ->
|
|
%% id生成器
|
|
mnesia:create_table(service, [
|
|
{attributes, record_info(fields, service)},
|
|
{record_name, service},
|
|
{disc_copies, [node()]},
|
|
{type, ordered_set}
|
|
]).
|
|
|
|
insert(Service = #service{}) ->
|
|
case mnesia:transaction(fun() -> mnesia:write(?TAB, Service, write) end) of
|
|
{'atomic', Res} ->
|
|
Res;
|
|
{'aborted', Reason} ->
|
|
{error, Reason}
|
|
end.
|
|
|
|
change_status(ServiceId, NewStatus) when is_binary(ServiceId), is_integer(NewStatus) ->
|
|
Fun = fun() ->
|
|
case mnesia:read(?TAB, ServiceId, write) of
|
|
[] ->
|
|
mnesia:abort(<<"service not found">>);
|
|
[Service] ->
|
|
mnesia:write(?TAB, Service#service{status = NewStatus}, write)
|
|
end
|
|
end,
|
|
case mnesia:transaction(Fun) of
|
|
{'atomic', ok} ->
|
|
ok;
|
|
{'aborted', Reason} ->
|
|
{error, Reason}
|
|
end.
|
|
|
|
-spec set_params(ServiceId :: binary(), Params :: binary()) -> ok | {error, Reason :: any()}.
|
|
set_params(ServiceId, Params) when is_binary(ServiceId), is_binary(Params) ->
|
|
Fun = fun() ->
|
|
case mnesia:read(?TAB, ServiceId, write) of
|
|
[] ->
|
|
mnesia:abort(<<"service not found">>);
|
|
[S] ->
|
|
mnesia:write(?TAB, S#service{params = Params}, write)
|
|
end
|
|
end,
|
|
case mnesia:transaction(Fun) of
|
|
{'atomic', ok} ->
|
|
ok;
|
|
{'aborted', Reason} ->
|
|
{error, Reason}
|
|
end.
|
|
|
|
-spec set_metrics(ServiceId :: binary(), Metrics :: binary()) -> ok | {error, Reason :: any()}.
|
|
set_metrics(ServiceId, Metrics) when is_binary(ServiceId), is_binary(Metrics) ->
|
|
Fun = fun() ->
|
|
case mnesia:read(?TAB, ServiceId, write) of
|
|
[] ->
|
|
mnesia:abort(<<"service not found">>);
|
|
[S] ->
|
|
mnesia:write(?TAB, S#service{metrics = Metrics}, write)
|
|
end
|
|
end,
|
|
case mnesia:transaction(Fun) of
|
|
{'atomic', ok} ->
|
|
ok;
|
|
{'aborted', Reason} ->
|
|
{error, Reason}
|
|
end.
|
|
|
|
-spec get_params(ServiceId :: binary()) -> Params :: binary().
|
|
get_params(ServiceId) when is_binary(ServiceId) ->
|
|
case mnesia:dirty_read(?TAB, ServiceId) of
|
|
[] ->
|
|
<<"">>;
|
|
[#service{params = Params}] ->
|
|
Params
|
|
end.
|
|
|
|
-spec get_metrics(ServiceId :: binary()) -> Params :: binary().
|
|
get_metrics(ServiceId) when is_binary(ServiceId) ->
|
|
case mnesia:dirty_read(?TAB, ServiceId) of
|
|
[] ->
|
|
<<"">>;
|
|
[#service{metrics = Metrics}] ->
|
|
Metrics
|
|
end.
|
|
|
|
-spec get_status(ServiceId :: binary()) -> Status :: integer().
|
|
get_status(ServiceId) when is_binary(ServiceId) ->
|
|
case mnesia:dirty_read(?TAB, ServiceId) of
|
|
[] ->
|
|
0;
|
|
[#service{status = Status}] ->
|
|
Status
|
|
end.
|
|
|
|
-spec get_service(ServiceId :: binary()) -> error | {ok, Service :: #service{}}.
|
|
get_service(ServiceId) when is_binary(ServiceId) ->
|
|
case mnesia:dirty_read(?TAB, ServiceId) of
|
|
[] ->
|
|
error;
|
|
[Service] ->
|
|
{ok, Service}
|
|
end.
|
|
|
|
-spec get_all_services() -> [#service{}].
|
|
get_all_services() ->
|
|
Fun = fun() ->
|
|
Q = qlc:q([E || E <- mnesia:table(?TAB)]),
|
|
qlc:e(Q)
|
|
end,
|
|
|
|
case mnesia:transaction(Fun) of
|
|
{'atomic', Res} ->
|
|
Res;
|
|
{'aborted', _} ->
|
|
[]
|
|
end.
|
|
|
|
-spec get_all_service_ids() -> [ServiceId :: binary()].
|
|
get_all_service_ids() ->
|
|
mnesia:dirty_all_keys(?TAB). |