Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 74 lines (59 sloc) 2.085 kb
4b31419 @archaelus Adds a module for dealing with pairs of rrd datafiles and erlang info…
authored
1 %%%-------------------------------------------------------------------
2 %% @copyright Geoff Cant
3 %% @author Geoff Cant <nem@erlang.geek.nz>
4 %% @version {@vsn}, {@date} {@time}
5 %% @doc ERRD DB API
6 %% @end
7 %%%-------------------------------------------------------------------
8 -module(errdb).
9
10 -include_lib("eunit.hrl").
11
12 %% API
13 -export([create/1,
14 create/2,
15 read_config/1,
16 write_config/2,
17 config_name/1,
18 rrd_name/1,
19 read/1,
20 delete/1]).
21
22 %% Format:
23 %% errd_db_name/
24 %% data.rrd -- rrdtool file
25 %% config.erl -- config parameters in consult format.
26
27 %%====================================================================
28 %% API
29 %%====================================================================
30
31 create(PathName) ->
32 create(PathName, []).
33
34 create(PathName, Options) when is_list(Options) ->
35 ok = file:make_dir(PathName),
36 write_config(PathName, Options).
37
38 write_config(PathName, Terms) when is_list(Terms) ->
39 file:write_file(config_name(PathName),
40 iolist_to_binary([io_lib:format("~p.~n", [Term])
41 ||Term<-Terms])).
42
43 read_config(PathName) ->
44 file:consult(config_name(PathName)).
45
46 config_name(PathName) ->
47 filename:join([PathName, "config.erl"]).
48
49 rrd_name(PathName) ->
50 filename:join([PathName, "data.rrd"]).
51
52 read(PathName) ->
53 true = filelib:is_dir(PathName),
54 {ok, Config} = read_config(PathName),
55 {ok, {rrd_name(PathName),
56 Config}}.
57
58 delete(PathName) ->
59 file:delete(config_name(PathName)),
60 file:delete(rrd_name(PathName)),
61 file:del_dir(PathName).
62
63 %%====================================================================
64 %% Internal functions
65 %%====================================================================
66
67 errdb_test() ->
68 ?assertMatch(false, filelib:is_dir("priv/test")),
69 ?assertMatch(ok, create("priv/test", [test])),
70 ?assertMatch({ok, {"priv/test/data.rrd",
71 [test]}},
72 read("priv/test")),
73 ?assertMatch(ok, delete("priv/test")).
Something went wrong with that request. Please try again.