完成除数据查询前端功能的EDataCapture开发
This commit is contained in:
@@ -2,7 +2,31 @@
|
||||
namespace EdgeManager\EDataCapture;
|
||||
|
||||
class EDataCapture {
|
||||
// 我不主张在构造函数内实现类型检查,因为在请求过于密集、记录较多时会严重影响性能
|
||||
// 请使用SCADA系统的开发人员自觉遵守文档内规范
|
||||
function __construct(
|
||||
protected $dbconn,
|
||||
protected $post = NULL,
|
||||
protected $get = NULL
|
||||
) {}
|
||||
|
||||
private function set_data() {
|
||||
|
||||
foreach (array_chunk($this -> post -> param, 6507524, true) as $chunk) {
|
||||
$sql_cmd[] = [sprintf(
|
||||
"INSERT INTO hf_mes_scada_data_capture_node_data_%s",
|
||||
$chunk[0] -> working_subclass
|
||||
)];
|
||||
foreach ($chunk as $row) {
|
||||
$sql_cmd[] = sprintf(
|
||||
"(code, v_%s, device_code, batch) VALUES('%s', %s, %s, %s)",
|
||||
$row -> type,
|
||||
$row -> code,
|
||||
$row -> value,
|
||||
$row -> device_code ?? NULL,
|
||||
$row -> batch ?? NULL
|
||||
);
|
||||
}
|
||||
return pg_query($this -> dbconn, implode(' ', $sql_cmd));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,20 @@ class ENodeConfigure {
|
||||
) {}
|
||||
|
||||
function add_node() {
|
||||
$table_name = "hf_mes_scada_data_capture_node_data_" . $this -> post['process_code'];
|
||||
$exists = pg_query(sprintf(
|
||||
"SELECT EXISTS(
|
||||
SELECT 1 FROM hf_mes_scada_data_capture_node_configure WHERE code='%s'
|
||||
)", $this -> post['code']
|
||||
));
|
||||
if (pg_fetch_assoc($exists)['exists'] == 't') {
|
||||
return "REPLICATED";
|
||||
}
|
||||
|
||||
$table_name = "hf_mes_scada_data_capture_node_data_" . $this -> post['working_subclass'];
|
||||
|
||||
pg_query($this -> dbconn, "BEGIN");
|
||||
$res1 = pg_query($this -> dbconn, sprintf(
|
||||
"CREATE TABLE IF NOT EXISTS %s(
|
||||
$res[] = pg_query($this -> dbconn, sprintf(
|
||||
"CREATE TABLE IF NOT EXISTS %s (
|
||||
id serial8,
|
||||
code text references hf_mes_scada_data_capture_node_configure(code),
|
||||
v_string text,
|
||||
@@ -22,35 +31,70 @@ class ENodeConfigure {
|
||||
v_bool bool,
|
||||
device_code text,
|
||||
batch text,
|
||||
create_date timestamp
|
||||
capture_time timestamp NOT NULL DEFAULT NOW()
|
||||
)", $table_name
|
||||
));
|
||||
$res2 = pg_insert(
|
||||
$res[] = pg_insert(
|
||||
$this -> dbconn,
|
||||
'hf_mes_scada_data_capture_node_configure',
|
||||
$this -> post
|
||||
);
|
||||
|
||||
if ($res1 and $res2)
|
||||
pg_query($this -> dbconn, "COMMIT");
|
||||
else
|
||||
pg_query($this -> dbconn, "ROLLBACK");
|
||||
|
||||
pg_query($this -> dbconn, sprintf("SELECT create_hypertable('%s','create_date')", $table_name));
|
||||
pg_query($this -> dbconn, sprintf(
|
||||
"CREATE INDEX ON %s (v_string, v_int, v_float, v_bool, create_date DESC)
|
||||
WHERE COALESCE(v_string, v_int::text, v_float::text, v_bool::text) IS NOT NULL
|
||||
// 检查一下,如果超表已经有了就不要尝试重复创建了
|
||||
$table_exists = pg_query(sprintf(
|
||||
"SELECT *
|
||||
FROM timescaledb_information.hypertables
|
||||
WHERE hypertable_name = '%s'
|
||||
", $table_name
|
||||
));
|
||||
if (!in_array($table_name, pg_fetch_all_columns($table_exists, 1))) {
|
||||
$res[] = pg_query($this -> dbconn, sprintf("SELECT create_hypertable('%s', 'capture_time')", $table_name));
|
||||
$res[] = pg_query($this -> dbconn, sprintf(
|
||||
"CREATE INDEX ON %s (v_string, v_int, v_float, v_bool, capture_time DESC)
|
||||
WHERE COALESCE(v_string, v_int::text, v_float::text, v_bool::text) IS NOT NULL
|
||||
", $table_name
|
||||
));
|
||||
}
|
||||
|
||||
if (in_array(false, $res)) {
|
||||
pg_query($this -> dbconn, "ROLLBACK");
|
||||
return false;
|
||||
} else {
|
||||
pg_query($this -> dbconn, "COMMIT");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function remove_node() {
|
||||
pg_query($this -> dbconn, "BEGIN");
|
||||
$res[] = pg_delete(
|
||||
$this -> dbconn,
|
||||
'hf_mes_scada_data_capture_node_configure',
|
||||
$this -> post,
|
||||
);
|
||||
$exists = pg_query(sprintf(
|
||||
"SELECT EXISTS(
|
||||
SELECT 1 FROM hf_mes_scada_data_capture_node_configure WHERE code='%s'
|
||||
)", $this -> post['working_subclass']
|
||||
));
|
||||
if (pg_fetch_assoc($exists)['exists'] == 'f') {
|
||||
$res[] = pg_query(sprintf(
|
||||
"DROP TABLE hf_mes_scada_data_capture_node_data_%s",
|
||||
$this -> post['working_subclass']
|
||||
));
|
||||
}
|
||||
|
||||
if (in_array(false, $res)) {
|
||||
pg_query($this -> dbconn, "ROLLBACK");
|
||||
return false;
|
||||
} else {
|
||||
pg_query($this -> dbconn, "COMMIT");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function update_node() {
|
||||
return pg_update(
|
||||
$this -> dbconn,
|
||||
$this -> dbconn,
|
||||
'hf_mes_scada_data_capture_node_configure',
|
||||
$this -> post,
|
||||
['code' => $this -> post['code']]
|
||||
@@ -61,4 +105,4 @@ class ENodeConfigure {
|
||||
$res = pg_query($this -> dbconn, "SELECT * FROM hf_mes_scada_data_capture_node_configure");
|
||||
return pg_fetch_all($res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user