Influxdb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php namespace Dever\Store;
  2. use Dever;
  3. use Dever\Debug;
  4. use Dever\Output;
  5. use Dever\Sql;
  6. #https://docs.influxdata.com/influxdb/v2/api-guide/api_intro/
  7. class Influxdb extends Base
  8. {
  9. public function __construct($setting)
  10. {
  11. $this->type = $setting['type'];
  12. $this->read = $setting;
  13. }
  14. public function query($param = array(), $type = 1)
  15. {
  16. $header['Authorization'] = 'Token ' . $this->read['token'];
  17. $header['Content-Type'] = 'text/plain; charset=utf-8';
  18. $header['Accept'] = 'application/json';
  19. if ($type == 1) {
  20. $type = 'get';
  21. $host = $this->read['host'] . '/query?db='.$this->read['name'];
  22. $param = array('q' => $param);
  23. $json = false;
  24. } else {
  25. $type = 'post';
  26. $host = $this->read['host'] . '/api/v2/write?org='.$this->read['user'].'&bucket='.$this->read['name'].'&precision=' . $this->read['precision'];
  27. $json = true;
  28. }
  29. /*
  30. //$curl = 'curl --get '.$this->read['host'].'/query?db=api --header "Authorization: '.$header['Authorization'].'" --data-urlencode "q='.$param['q'].'"';
  31. //$curl = 'curl --request POST "'.$host.'" --header "Authorization: '.$header['Authorization'].'" --header "Content-Type: '.$header['Content-Type'].'" --header "Accept: '.$header['Accept'].'" --data-binary \''.$param.'\'';
  32. //echo $curl;die;
  33. */
  34. $result = Dever::curl($host, $param, $type, $json, $header)->result();
  35. if (Debug::$shell) {
  36. $this->log(array('param' => $param, 'result' => $result));
  37. }
  38. return $result;
  39. }
  40. public function struct($config, $state = 0)
  41. {
  42. if (!$state) {
  43. $this->query('DELETE FROM ' . $config['table']);
  44. }
  45. }
  46. public function load($table, $param, $set, $field, $lock)
  47. {
  48. $bind = array();
  49. $sql = Sql::select($table, $param, $bind, $set, $field, $lock, $this->type);
  50. $data = $this->query($sql);
  51. $data = Dever::json_decode($data);
  52. if (isset($data['results'][0]['series'][0]['values'])) {
  53. return $data['results'][0]['series'][0];
  54. } else {
  55. return array();
  56. }
  57. }
  58. public function select($table, $param, $set, $field, $lock)
  59. {
  60. $data = $this->load($table, $param, $set, $field, $lock);
  61. if ($data) {
  62. $columns = $data['columns'];
  63. $values = $data['values'];
  64. $result = array();
  65. foreach ($values as $k => $v) {
  66. foreach ($columns as $k1 => $v1) {
  67. $result[$k][$v1] = $this->set($v[$k1], $v1);
  68. }
  69. }
  70. return $result;
  71. }
  72. return $data;
  73. }
  74. public function find($table, $param, $set, $field, $lock)
  75. {
  76. $data = $this->load($table, $param, $set, $field, $lock);
  77. if ($data) {
  78. $columns = $data['columns'];
  79. $values = $data['values'];
  80. $result = array();
  81. foreach ($columns as $k => $v) {
  82. $result[$v] = $this->set($values[0][$k], $v);
  83. }
  84. return $result;
  85. }
  86. return $data;
  87. }
  88. public function count($table, $param, $field)
  89. {
  90. $result = $this->load($table, $param, array('col'=>'count(*)'), $field, false);
  91. if ($result && isset($result['values'][0][1])) {
  92. return $result['values'][0][1];
  93. }
  94. return 0;
  95. }
  96. public function insert($table, $data, $field)
  97. {
  98. $param = $table;
  99. $time = $data['cdate'];
  100. $tags = $fields = array();
  101. if (isset($data['id'])) {
  102. if (!$data['id']) {
  103. $data['id'] = 1;
  104. }
  105. $tags[] = 'id=' . $data['id'];
  106. }
  107. foreach ($field as $k => $v) {
  108. $value = $v['default'] ?? "null";
  109. if (isset($data[$k])) {
  110. $value = $data[$k];
  111. }
  112. if (!$value) {
  113. $value = 'null';
  114. }
  115. if (!strstr($v['type'], 'char') && !strstr($v['type'], 'text') && $value == 'null') {
  116. $value = 0;
  117. }
  118. if (isset($v['base64']) && $v['base64']) {
  119. $value = 'base64_' . base64_encode($value);
  120. }
  121. if (isset($v['fields']) && $v['fields']) {
  122. if (strstr($v['type'], 'char') || strstr($v['type'], 'text')) {
  123. $value = '"' . $value . '"';
  124. }
  125. $fields[] = $k . '=' . $value;
  126. } else {
  127. $tags[] = $k . '=' . $value;
  128. }
  129. }
  130. if ($tags) {
  131. $param .= ',' . implode(',', $tags);
  132. } else {
  133. Dever::error('influxdb tags not null');
  134. }
  135. if ($fields) {
  136. $param .= ' ' . implode(',', $fields);
  137. }
  138. $param .= ' ' . $time;
  139. $this->query($param, 2);
  140. return $time;
  141. }
  142. public function update($table, $param, $data, $field)
  143. {
  144. $data = array_merge($param, $data);
  145. return $this->insert($table, $data, $field);
  146. }
  147. private function set($k, &$v)
  148. {
  149. if ($v == 'time') {
  150. $v = 'cdate';
  151. $k = strtotime($k);
  152. }
  153. if (strstr($k, 'base64_')) {
  154. $k = base64_decode(str_replace('base64_', '', $k));
  155. }
  156. if ($k == 'null') {
  157. $k = '';
  158. }
  159. return $k;
  160. }
  161. public function delete($table, $param, $field){}
  162. public function index($config, $state = 0){}
  163. public function partition($config, $partition){}
  164. public function begin(){}
  165. public function commit(){}
  166. public function rollback(){}
  167. public function transaction(){}
  168. }