1717-include (" poller.hrl" ).
1818-include (" parser.hrl" ).
1919
20- -export ([get_resources_using_polling /0 ,
21- json_to_record_resources /1 ,
22- json_to_record_resource /1 ,
23- get_parsers_by_id /1 ,
20+ -export ([get_streams_using_polling /0 ,
21+ json_to_record_streams /1 ,
22+ json_to_record_stream /1 ,
23+ get_parser_by_id /1 ,
2424 post_datapoint /2 ,
2525 get_datapoint /1 ]).
2626
@@ -44,7 +44,7 @@ get_datapoint(StreamId)->
4444 _ ->
4545 Id = StreamId
4646 end ,
47- case erlastic_search :search_limit (? ES_INDEX , " datapoint" , " streamid :" ++ Id , 100 ) of
47+ case erlastic_search :search_limit (? ES_INDEX , " datapoint" , " stream_id :" ++ Id , 100 ) of
4848 {ok , Result } ->
4949 FinalJson = lib_json :get_list_and_add_id (Result , data ),
5050 lib_json :get_field (FinalJson , " data" );
@@ -63,9 +63,9 @@ get_datapoint(StreamId)->
6363post_datapoint (StreamId , Value )->
6464 case is_integer (StreamId ) of
6565 true ->
66- FieldValue1 = {" streamid " ,integer_to_binary (StreamId )};
66+ FieldValue1 = {" stream_id " ,integer_to_binary (StreamId )};
6767 _ ->
68- FieldValue1 = {" streamid " ,list_to_binary (StreamId )}
68+ FieldValue1 = {" stream_id " ,list_to_binary (StreamId )}
6969 end ,
7070 case is_integer (Value ) of
7171 true ->
@@ -99,82 +99,91 @@ post_datapoint(StreamId, Value)->
9999 end .
100100
101101% % @doc
102- % % Function: get_parsers_by_id /1
103- % % Purpose: get parsers list according to specific resource id
104- % % Returns: {error, ErrMsg} | [ #parser .....]
102+ % % Function: get_parser_by_id /1
103+ % % Purpose: get parser according to specific stream id
104+ % % Returns: {error, ErrMsg} | #parser
105105% % @end
106- -spec get_parsers_by_id (string ()) -> tuple () | list ().
107- get_parsers_by_id ( ResourceId )->
108- case erlastic_search :search_limit (? ES_INDEX , " parser" , " resource_id :" ++ ResourceId , 100 ) of
106+ -spec get_parser_by_id (string ()) -> tuple () | tuple ().
107+ get_parser_by_id ( StreamId )->
108+ case erlastic_search :search_limit (? ES_INDEX , " parser" , " stream_id :" ++ StreamId , 100 ) of
109109 {ok , Result } ->
110110 EncodedResult = lib_json :encode (Result ),
111111 case re :run (EncodedResult , " \" max_score\" :null" , [{capture , first , list }]) of
112- {match , _ } -> {error , " parsers not found" };
112+ {match , _ } -> {error , " the parser not found" };
113113 nomatch -> FinalJsonList = lib_json :get_field (lib_json :get_list_and_add_id (Result ), " hits" ),
114-
115- % %transform this json list to record list
116- jsonToRecord (FinalJsonList , [])
114+ case length (FinalJsonList ) of
115+ 1 ->
116+ Item = lists :nth (1 , FinalJsonList ),
117+ # parser {stream_id = lib_json :get_field (Item , " stream_id" ),
118+ input_parser = binary_to_list (lib_json :get_field (Item , " input_parser" )),
119+ input_type = binary_to_list (lib_json :get_field (Item , " input_type" ))
120+ };
121+ _ ->
122+ erlang :display (" multiple parsers exist for this stream id" ),
123+ {error , " multiple parsers exit for this stream id" }
124+ end
125+ % % transform this json list to record list
126+ % % jsonToRecord(FinalJsonList, [])
117127 end ;
118128 _ ->
119- erlang :display (" an error happens: parsers not found" ),
129+ erlang :display (" an error happens: the parser not found" ),
120130 {error , " parsers not found!!" }
121131 end .
122132
123133% % @doc
124- % % Function: get_resources_using_polling /0
125- % % Purpose: Retrieves all resources from Elastic Search that are using polling.
126- % % Returns: [] | [Resource , ... ] | {error, Reason}.
134+ % % Function: get_streams_using_polling /0
135+ % % Purpose: Retrieves all streams from Elastic Search that are using polling.
136+ % % Returns: [] | [Stream , ... ] | {error, Reason}.
127137% % @end
128- -spec get_resources_using_polling () -> [] | [string ()] | {error , term ()}.
129- get_resources_using_polling () ->
130- % % TODO : Post a query to ES to retrieve resources that have a polling-URL.
131-
138+ -spec get_streams_using_polling () -> [] | [string ()] | {error , term ()}.
139+ get_streams_using_polling () ->
132140 JsonQuery = " {\" query\" : {\" filtered\" : " ++
133- " { \" filter\" : {\" exists\" : {\" field\" : \" url \" }}}}}" ,
141+ " { \" filter\" : {\" exists\" : {\" field\" : \" uri \" }}}}}" ,
134142
135143 case erlastic_search :search_json (# erls_params {},
136144 ? ES_INDEX ,
137- " resource " ,
145+ " stream " ,
138146 JsonQuery ) of
139147 {error , Reason } -> {error , Reason };
140148 {ok , Result } ->
141149 lib_json :get_field (Result , " hits.hits" )
142150 end .
151+
143152
144153
145154
146155% % @doc
147- % % Function: json_to_record_resources /1
148- % % Purpose: Converts a list of resource Jsons to a list of pollerInfo records
149- % % Returns: [] | [Resource , ...]
156+ % % Function: json_to_record_streams /1
157+ % % Purpose: Converts a list of stream Jsons to a list of pollerInfo records
158+ % % Returns: [] | [Stream , ...]
150159% % @end
151- -spec json_to_record_resources ([json ()]) -> [] | [record ()].
152- json_to_record_resources ([]) -> [];
153- json_to_record_resources ([H |T ]) ->
154- [json_to_record_resource (H ) | json_to_record_resources (T )].
160+ -spec json_to_record_streams ([json ()]) -> [] | [record ()].
161+ json_to_record_streams ([]) -> [];
162+ json_to_record_streams ([H |T ]) ->
163+ [json_to_record_stream (H ) | json_to_record_streams (T )].
155164
156165
157166
158167
159168% % @doc
160- % % Function: json_to_record_resource /1
161- % % Purpose: Converts a resource Json to a pollerInfo record
162- % % Returns: Resource
169+ % % Function: json_to_record_stream /1
170+ % % Purpose: Converts a stream Json to a pollerInfo record
171+ % % Returns: #pollerInfo{}
163172% % @end
164- -spec json_to_record_resource ( Resource :: json ()) -> record ().
165- json_to_record_resource ( Resource ) ->
166- Name = case lib_json :get_field (Resource , " _source.name" ) of
173+ -spec json_to_record_stream ( Stream :: json ()) -> record ().
174+ json_to_record_stream ( Stream ) ->
175+ Name = case lib_json :get_field (Stream , " _source.name" ) of
167176 undefined -> undefined ;
168177 N -> binary_to_list (N )
169178 end ,
170- Url = case lib_json :get_field (Resource , " _source.url " ) of
179+ Uri = case lib_json :get_field (Stream , " _source.uri " ) of
171180 undefined -> undefined ;
172181 U -> binary_to_list (U )
173182 end ,
174- # pollerInfo {resourceid = binary_to_list (lib_json :get_field (Resource , " _id" )),
183+ # pollerInfo {stream_id = binary_to_list (lib_json :get_field (Stream , " _id" )),
175184 name = Name ,
176- url = Url ,
177- frequency = lib_json :get_field (Resource , " _source.polling_freq" )}.
185+ uri = Uri ,
186+ frequency = lib_json :get_field (Stream , " _source.polling_freq" )}.
178187
179188
180189% % ====================================================================
@@ -191,8 +200,7 @@ json_to_record_resource(Resource) ->
191200jsonToRecord ([], Res )->
192201 Res ;
193202jsonToRecord ([Item |Tail ], Res )->
194- Tmp = # parser {resource_id = lib_json :get_field (Item , " resource_id" ),
195- stream_id = lib_json :get_field (Item , " stream_id" ),
203+ Tmp = # parser {stream_id = lib_json :get_field (Item , " stream_id" ),
196204 input_parser = binary_to_list (lib_json :get_field (Item , " input_parser" )),
197205 input_type = binary_to_list (lib_json :get_field (Item , " input_type" ))
198206 },
0 commit comments