emqx_bridge_pgsql.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%--------------------------------------------------------------------
  4. -module(emqx_bridge_pgsql).
  5. -include_lib("emqx_connector/include/emqx_connector.hrl").
  6. -include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
  7. -include_lib("typerefl/include/types.hrl").
  8. -include_lib("emqx/include/logger.hrl").
  9. -include_lib("hocon/include/hoconsc.hrl").
  10. -include_lib("epgsql/include/epgsql.hrl").
  11. -include_lib("snabbkaffe/include/snabbkaffe.hrl").
  12. -include_lib("emqx_resource/include/emqx_resource.hrl").
  13. %% `hocon_schema' API
  14. -export([
  15. namespace/0,
  16. roots/0,
  17. fields/1,
  18. desc/1
  19. ]).
  20. %% for sharing with other actions
  21. -export([fields/3]).
  22. %% Examples
  23. -export([
  24. bridge_v2_examples/1,
  25. conn_bridge_examples/1
  26. ]).
  27. %% Exported for timescale and matrix bridges
  28. -export([
  29. values/1,
  30. values_conn_bridge_examples/2
  31. ]).
  32. -define(ACTION_TYPE, pgsql).
  33. %% Hocon Schema Definitions
  34. namespace() -> "bridge_pgsql".
  35. roots() ->
  36. [].
  37. fields("config_connector") ->
  38. emqx_postgresql_connector_schema:fields("config_connector");
  39. fields(config) ->
  40. fields("config_connector") ++
  41. fields(action);
  42. fields(action) ->
  43. {pgsql,
  44. hoconsc:mk(
  45. hoconsc:map(name, hoconsc:ref(emqx_bridge_pgsql, pgsql_action)),
  46. #{
  47. desc => <<"PostgreSQL Action Config">>,
  48. required => false
  49. }
  50. )};
  51. fields(action_parameters) ->
  52. [
  53. {sql,
  54. hoconsc:mk(
  55. binary(),
  56. #{desc => ?DESC("sql_template"), default => default_sql(), format => <<"sql">>}
  57. )}
  58. ];
  59. fields(pgsql_action) ->
  60. emqx_bridge_v2_schema:make_producer_action_schema(
  61. hoconsc:mk(
  62. hoconsc:ref(?MODULE, action_parameters),
  63. #{
  64. required => true,
  65. desc => ?DESC("action_parameters")
  66. }
  67. )
  68. );
  69. fields("put_bridge_v2") ->
  70. fields(pgsql_action);
  71. fields("get_bridge_v2") ->
  72. fields(pgsql_action);
  73. fields("post_bridge_v2") ->
  74. fields("post", pgsql, pgsql_action);
  75. fields("config") ->
  76. [
  77. {enable, hoconsc:mk(boolean(), #{desc => ?DESC("config_enable"), default => true})},
  78. {sql,
  79. hoconsc:mk(
  80. binary(),
  81. #{desc => ?DESC("sql_template"), default => default_sql(), format => <<"sql">>}
  82. )},
  83. {local_topic,
  84. hoconsc:mk(
  85. binary(),
  86. #{desc => ?DESC("local_topic"), default => undefined}
  87. )}
  88. ] ++ emqx_resource_schema:fields("resource_opts") ++
  89. (emqx_postgresql:fields(config) --
  90. emqx_connector_schema_lib:prepare_statement_fields());
  91. fields("post") ->
  92. fields("post", ?ACTION_TYPE, "config");
  93. fields("put") ->
  94. fields("config");
  95. fields("get") ->
  96. emqx_bridge_schema:status_fields() ++ fields("post").
  97. fields("post", Type, StructName) ->
  98. [type_field(Type), name_field() | fields(StructName)].
  99. type_field(Type) ->
  100. {type, hoconsc:mk(hoconsc:enum([Type]), #{required => true, desc => ?DESC("desc_type")})}.
  101. name_field() ->
  102. {name, hoconsc:mk(binary(), #{required => true, desc => ?DESC("desc_name")})}.
  103. desc("config") ->
  104. ?DESC("desc_config");
  105. desc(Method) when Method =:= "get"; Method =:= "put"; Method =:= "post" ->
  106. ["Configuration for PostgreSQL using `", string:to_upper(Method), "` method."];
  107. desc(pgsql_action) ->
  108. ?DESC("pgsql_action");
  109. desc(action_parameters) ->
  110. ?DESC("action_parameters");
  111. desc("config_connector") ->
  112. ?DESC(emqx_postgresql_connector_schema, "config_connector");
  113. desc(_) ->
  114. undefined.
  115. default_sql() ->
  116. <<
  117. "insert into t_mqtt_msg(msgid, topic, qos, payload, arrived) "
  118. "values (${id}, ${topic}, ${qos}, ${payload}, TO_TIMESTAMP((${timestamp} :: bigint)/1000))"
  119. >>.
  120. %% Examples
  121. bridge_v2_examples(Method) ->
  122. [
  123. #{
  124. <<"pgsql">> => #{
  125. summary => <<"PostgreSQL Action">>,
  126. value => values({Method, pgsql})
  127. }
  128. }
  129. ].
  130. conn_bridge_examples(Method) ->
  131. [
  132. #{
  133. <<"pgsql">> => #{
  134. summary => <<"PostgreSQL Bridge">>,
  135. value => values_conn_bridge_examples(Method, pgsql)
  136. }
  137. }
  138. ].
  139. values({get, PostgreSQLType}) ->
  140. maps:merge(
  141. #{
  142. status => <<"connected">>,
  143. node_status => [
  144. #{
  145. node => <<"emqx@localhost">>,
  146. status => <<"connected">>
  147. }
  148. ]
  149. },
  150. values({put, PostgreSQLType})
  151. );
  152. values({post, PostgreSQLType}) ->
  153. values({put, PostgreSQLType});
  154. values({put, PostgreSQLType}) ->
  155. maps:merge(
  156. #{
  157. name => <<"my_action">>,
  158. type => PostgreSQLType,
  159. enable => true,
  160. connector => <<"my_connector">>,
  161. resource_opts => #{
  162. batch_size => 1,
  163. batch_time => <<"50ms">>,
  164. inflight_window => 100,
  165. max_buffer_bytes => <<"256MB">>,
  166. request_ttl => <<"45s">>,
  167. worker_pool_size => 16
  168. }
  169. },
  170. values(parameters)
  171. );
  172. values(parameters) ->
  173. #{
  174. <<"parameters">> => #{
  175. <<"sql">> =>
  176. <<
  177. "INSERT INTO client_events(clientid, event, created_at)"
  178. "VALUES (\n"
  179. " ${clientid},\n"
  180. " ${event},\n"
  181. " TO_TIMESTAMP((${timestamp} :: bigint))\n"
  182. ")"
  183. >>
  184. }
  185. }.
  186. values_conn_bridge_examples(get, Type) ->
  187. maps:merge(
  188. #{
  189. status => <<"connected">>,
  190. node_status => [
  191. #{
  192. node => <<"emqx@localhost">>,
  193. status => <<"connected">>
  194. }
  195. ]
  196. },
  197. values_conn_bridge_examples(post, Type)
  198. );
  199. values_conn_bridge_examples(_Method, Type) ->
  200. #{
  201. enable => true,
  202. type => Type,
  203. name => <<"foo">>,
  204. server => <<"127.0.0.1:5432">>,
  205. database => <<"mqtt">>,
  206. pool_size => 8,
  207. username => <<"root">>,
  208. password => <<"******">>,
  209. sql => default_sql(),
  210. local_topic => <<"local/topic/#">>,
  211. resource_opts => #{
  212. worker_pool_size => 8,
  213. health_check_interval => ?HEALTHCHECK_INTERVAL_RAW,
  214. batch_size => ?DEFAULT_BATCH_SIZE,
  215. batch_time => ?DEFAULT_BATCH_TIME,
  216. query_mode => async,
  217. max_buffer_bytes => ?DEFAULT_BUFFER_BYTES
  218. }
  219. }.