Remove strftime from statements and use unixepoch instead (#13250)
Stelios Fragkakis committed
Jul 6, 2022 at 09:47 UTC
36280fc2cfbc637038ddad004d26d55ac12e5f03
7 files changed
+28
-28
database/sqlite/sqlite_aclk.c
+5
-5
@@ -23,11 +23,11 @@ const char *aclk_sync_config[] = {
23
24
"CREATE TRIGGER IF NOT EXISTS tr_dim_del AFTER DELETE ON dimension BEGIN INSERT INTO dimension_delete "
25
"(dimension_id, dimension_name, chart_type_id, dim_id, chart_id, host_id, date_created)"
26
- " select old.id, old.name, c.type||\".\"||c.id, old.dim_id, old.chart_id, c.host_id, strftime('%s') FROM"
26
+ " select old.id, old.name, c.type||\".\"||c.id, old.dim_id, old.chart_id, c.host_id, unixepoch() FROM"
27
" chart c WHERE c.chart_id = old.chart_id; END;",
28
29
"DELETE FROM dimension_delete WHERE host_id NOT IN"
30
- " (SELECT host_id FROM host) OR strftime('%s') - date_created > 604800;",
30
+ " (SELECT host_id FROM host) OR unixepoch() - date_created > 604800;",
31
32
NULL,
33
};
@@ -773,7 +773,7 @@ void sql_maint_aclk_sync_database(struct aclk_database_worker_config *wc, struct
773
BUFFER *sql = buffer_create(ACLK_SYNC_QUERY_SIZE);
774
775
buffer_sprintf(sql,"DELETE FROM aclk_chart_%s WHERE date_submitted IS NOT NULL AND "
776
- "date_updated < strftime('%%s','now','-%d seconds');", wc->uuid_str, ACLK_DELETE_ACK_INTERNAL);
776
+ "date_updated < unixepoch()-%d;", wc->uuid_str, ACLK_DELETE_ACK_INTERNAL);
777
db_execute(buffer_tostring(sql));
778
buffer_flush(sql);
779
@@ -784,7 +784,7 @@ void sql_maint_aclk_sync_database(struct aclk_database_worker_config *wc, struct
784
buffer_flush(sql);
785
786
buffer_sprintf(sql,"DELETE FROM aclk_alert_%s WHERE date_submitted IS NOT NULL AND "
787
- "date_cloud_ack < strftime('%%s','now','-%d seconds');", wc->uuid_str, ACLK_DELETE_ACK_ALERTS_INTERNAL);
787
+ "date_cloud_ack < unixepoch()-%d;", wc->uuid_str, ACLK_DELETE_ACK_ALERTS_INTERNAL);
788
db_execute(buffer_tostring(sql));
789
790
buffer_free(sql);
@@ -910,7 +910,7 @@ void sql_check_aclk_table_list(struct aclk_database_worker_config *wc)
910
sqlite3_free(err_msg);
911
}
912
db_execute("DELETE FROM dimension_delete WHERE host_id NOT IN (SELECT host_id FROM host) "
913
- " OR strftime('%s') - date_created > 604800;");
913
+ " OR unixepoch() - date_created > 604800;");
914
return;
915
}
916
database/sqlite/sqlite_aclk.h
+1
-1
@@ -98,7 +98,7 @@ static inline char *get_str_from_uuid(uuid_t *uuid)
98
#define TRIGGER_ACLK_CHART_PAYLOAD "CREATE TRIGGER IF NOT EXISTS aclk_tr_chart_payload_%s " \
99
"after insert on aclk_chart_payload_%s " \
100
"begin insert into aclk_chart_%s (uuid, unique_id, type, status, date_created) values " \
101
- " (new.uuid, new.unique_id, new.type, 'pending', strftime('%%s')) on conflict(uuid, status) " \
101
+ " (new.uuid, new.unique_id, new.type, 'pending', unixepoch()) on conflict(uuid, status) " \
102
" do update set unique_id = new.unique_id, update_count = update_count + 1; " \
103
"end;"
104
database/sqlite/sqlite_aclk_alert.c
+7
-7
@@ -149,7 +149,7 @@ int sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, int skip_filter)
149
buffer_sprintf(
150
sql,
151
"INSERT INTO aclk_alert_%s (alert_unique_id, date_created) "
152
- "VALUES (@alert_unique_id, strftime('%%s')) on conflict (alert_unique_id) do nothing; ",
152
+ "VALUES (@alert_unique_id, unixepoch()) on conflict (alert_unique_id) do nothing; ",
153
uuid_str);
154
155
rc = sqlite3_prepare_v2(db_meta, buffer_tostring(sql), -1, &res_alert, 0);
@@ -239,9 +239,9 @@ void aclk_push_alert_event(struct aclk_database_worker_config *wc, struct aclk_d
239
buffer_sprintf(
240
sql,
241
"UPDATE aclk_alert_%s SET date_submitted = NULL, date_cloud_ack = NULL WHERE sequence_id >= %"PRIu64
242
- "; UPDATE aclk_alert_%s SET date_cloud_ack = strftime('%%s','now') WHERE sequence_id < %"PRIu64
242
+ "; UPDATE aclk_alert_%s SET date_cloud_ack = unixepoch() WHERE sequence_id < %"PRIu64
243
" and date_cloud_ack is null "
244
- "; UPDATE aclk_alert_%s SET date_submitted = strftime('%%s','now') WHERE sequence_id < %"PRIu64
244
+ "; UPDATE aclk_alert_%s SET date_submitted = unixepoch() WHERE sequence_id < %"PRIu64
245
" and date_submitted is null",
246
wc->uuid_str,
247
wc->alerts_start_seq_id,
@@ -361,7 +361,7 @@ void aclk_push_alert_event(struct aclk_database_worker_config *wc, struct aclk_d
361
362
if (first_sequence_id) {
363
buffer_flush(sql);
364
- buffer_sprintf(sql, "UPDATE aclk_alert_%s SET date_submitted=strftime('%%s') "
364
+ buffer_sprintf(sql, "UPDATE aclk_alert_%s SET date_submitted=unixepoch() "
365
"WHERE date_submitted IS NULL AND sequence_id BETWEEN %" PRIu64 " AND %" PRIu64 ";",
366
wc->uuid_str, first_sequence_id, last_sequence_id);
367
db_execute(buffer_tostring(sql));
@@ -397,7 +397,7 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host)
397
BUFFER *sql = buffer_create(1024);
398
399
buffer_sprintf(sql,"insert into aclk_alert_%s (alert_unique_id, date_created) " \
400
- "select unique_id alert_unique_id, strftime('%%s') date_created from health_log_%s " \
400
+ "select unique_id alert_unique_id, unixepoch() from health_log_%s " \
401
"where new_status <> 0 and new_status <> -2 and config_hash_id is not null and updated_by_id = 0 " \
402
"order by unique_id asc on conflict (alert_unique_id) do nothing;", uuid_str, uuid_str);
403
@@ -737,7 +737,7 @@ void sql_process_queue_removed_alerts_to_aclk(struct aclk_database_worker_config
737
BUFFER *sql = buffer_create(1024);
738
739
buffer_sprintf(sql,"insert into aclk_alert_%s (alert_unique_id, date_created) " \
740
- "select unique_id alert_unique_id, strftime('%%s') date_created from health_log_%s " \
740
+ "select unique_id alert_unique_id, unixepoch() from health_log_%s " \
741
"where new_status = -2 and updated_by_id = 0 and unique_id not in " \
742
"(select alert_unique_id from aclk_alert_%s) order by unique_id asc " \
743
"on conflict (alert_unique_id) do nothing;", wc->uuid_str, wc->uuid_str, wc->uuid_str);
@@ -819,7 +819,7 @@ void aclk_mark_alert_cloud_ack(char *uuid_str, uint64_t alerts_ack_sequence_id)
819
if (alerts_ack_sequence_id != 0) {
820
buffer_sprintf(
821
sql,
822
- "UPDATE aclk_alert_%s SET date_cloud_ack = strftime('%%s','now') WHERE sequence_id <= %" PRIu64 "",
822
+ "UPDATE aclk_alert_%s SET date_cloud_ack = unixepoch() WHERE sequence_id <= %" PRIu64 "",
823
uuid_str,
824
alerts_ack_sequence_id);
825
db_execute(buffer_tostring(sql));
database/sqlite/sqlite_aclk_chart.c
+3
-3
@@ -87,7 +87,7 @@ static int aclk_add_chart_payload(
87
char sql[ACLK_SYNC_QUERY_SIZE];
88
snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1,
89
"INSERT INTO aclk_chart_payload_%s (unique_id, uuid, claim_id, date_created, type, payload) " \
90
- "VALUES (@unique_id, @uuid, @claim_id, strftime('%%s','now'), @type, @payload);", wc->uuid_str);
90
+ "VALUES (@unique_id, @uuid, @claim_id, unixepoch(), @type, @payload);", wc->uuid_str);
91
rc = prepare_statement(db_meta, sql, &res_chart);
92
if (rc != SQLITE_OK) {
93
error_report("Failed to prepare statement to store chart payload data");
@@ -398,7 +398,7 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
398
if (likely(first_sequence)) {
399
400
db_lock();
401
- snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1, "UPDATE aclk_chart_%s SET status = NULL, date_submitted=strftime('%%s','now') "
401
+ snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1, "UPDATE aclk_chart_%s SET status = NULL, date_submitted=unixepoch() "
402
"WHERE date_submitted IS NULL AND sequence_id BETWEEN %" PRIu64 " AND %" PRIu64 ";",
403
wc->uuid_str, first_sequence, last_sequence);
404
db_execute(sql);
@@ -540,7 +540,7 @@ void aclk_receive_chart_ack(struct aclk_database_worker_config *wc, struct aclk_
540
541
char sql[ACLK_SYNC_QUERY_SIZE];
542
543
- snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1,"UPDATE aclk_chart_%s SET date_updated=strftime('%%s','now') WHERE sequence_id <= @sequence_id "
543
+ snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1,"UPDATE aclk_chart_%s SET date_updated=unixepoch() WHERE sequence_id <= @sequence_id "
544
"AND date_submitted IS NOT NULL AND date_updated IS NULL;", wc->uuid_str);
545
546
rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
database/sqlite/sqlite_functions.c
+7
-7
@@ -42,14 +42,14 @@ const char *database_config[] = {
42
"WHERE ch.hash_id = chm.hash_id;",
43
44
"CREATE TRIGGER IF NOT EXISTS ins_host AFTER INSERT ON host BEGIN INSERT INTO node_instance (host_id, date_created)"
45
- " SELECT new.host_id, strftime(\"%s\") WHERE new.host_id NOT IN (SELECT host_id FROM node_instance); END;",
45
+ " SELECT new.host_id, unixepoch() WHERE new.host_id NOT IN (SELECT host_id FROM node_instance); END;",
46
47
"CREATE TRIGGER IF NOT EXISTS tr_v_chart_hash INSTEAD OF INSERT on v_chart_hash BEGIN "
48
"INSERT INTO chart_hash (hash_id, type, id, name, family, context, title, unit, plugin, "
49
"module, priority, chart_type, last_used) "
50
"values (new.hash_id, new.type, new.id, new.name, new.family, new.context, new.title, new.unit, new.plugin, "
51
- "new.module, new.priority, new.chart_type, strftime('%s')) "
52
- "ON CONFLICT (hash_id) DO UPDATE SET last_used = strftime('%s'); "
51
+ "new.module, new.priority, new.chart_type, unixepoch()) "
52
+ "ON CONFLICT (hash_id) DO UPDATE SET last_used = unixepoch(); "
53
"INSERT INTO chart_hash_map (chart_id, hash_id) values (new.chart_id, new.hash_id) "
54
"on conflict (chart_id, hash_id) do nothing; END; ",
55
@@ -1444,7 +1444,7 @@ int file_is_migrated(char *path)
1444
}
1445
1446
#define STORE_MIGRATED_FILE "insert or replace into metadata_migration (filename, file_size, date_created) " \
1447
- "values (@file, @size, strftime('%s'));"
1447
+ "values (@file, @size, unixepoch());"
1448
1449
void add_migrated_file(char *path, uint64_t file_size)
1450
{
@@ -1481,7 +1481,7 @@ void add_migrated_file(char *path, uint64_t file_size)
1481
1482
#define SQL_INS_CHART_LABEL "insert or replace into chart_label " \
1483
"(chart_id, source_type, label_key, label_value, date_created) " \
1484
- "values (@chart, @source, @label, @value, strftime('%s'));"
1484
+ "values (@chart, @source, @label, @value, unixepoch());"
1485
1486
void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value)
1487
{
@@ -1740,7 +1740,7 @@ failed:
1740
1741
#define SQL_STORE_CHART_HASH "insert into v_chart_hash (hash_id, type, id, " \
1742
"name, family, context, title, unit, plugin, module, priority, chart_type, last_used, chart_id) " \
1743
- "values (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11, ?12, strftime('%s'), ?13);"
1743
+ "values (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11, ?12, unixepoch(), ?13);"
1744
1745
int sql_store_chart_hash(
1746
uuid_t *hash_id, uuid_t *chart_id, const char *type, const char *id, const char *name, const char *family,
@@ -1911,7 +1911,7 @@ void compute_chart_hash(RRDSET *st)
1911
}
1912
1913
#define SQL_STORE_CLAIM_ID "insert into node_instance " \
1914
- "(host_id, claim_id, date_created) values (@host_id, @claim_id, strftime('%s')) " \
1914
+ "(host_id, claim_id, date_created) values (@host_id, @claim_id, unixepoch()) " \
1915
"on conflict(host_id) do update set claim_id = excluded.claim_id;"
1916
1917
void store_claim_id(uuid_t *host_id, uuid_t *claim_id)
database/sqlite/sqlite_functions.h
+2
-2
@@ -38,7 +38,7 @@ typedef enum db_check_action_type {
38
"select chart_id from chart where host_id = @host and type=@type and id=@id and (name is null or name=@name);"
39
40
#define SQL_STORE_ACTIVE_CHART \
41
- "insert or replace into chart_active (chart_id, date_created) values (@id, strftime('%s'));"
41
+ "insert or replace into chart_active (chart_id, date_created) values (@id, unixepoch());"
42
43
#define SQL_STORE_DIMENSION \
44
"INSERT OR REPLACE into dimension (dim_id, chart_id, id, name, multiplier, divisor , algorithm) values (?0001,?0002,?0003,?0004,?0005,?0006,?0007);"
@@ -47,7 +47,7 @@ typedef enum db_check_action_type {
47
"select dim_id from dimension where chart_id=@chart and id=@id and name=@name and length(dim_id)=16;"
48
49
#define SQL_STORE_ACTIVE_DIMENSION \
50
- "insert or replace into dimension_active (dim_id, date_created) values (@id, strftime('%s'));"
50
+ "insert or replace into dimension_active (dim_id, date_created) values (@id, unixepoch());"
51
52
#define CHECK_SQLITE_CONNECTION(db_meta) \
53
if (unlikely(!db_meta)) { \
database/sqlite/sqlite_health.c
+3
-3
@@ -435,8 +435,8 @@ void sql_health_alarm_log_count(RRDHOST *host) {
435
436
#define SQL_INJECT_REMOVED(guid, guid2) "insert into health_log_%s (hostname, unique_id, alarm_id, alarm_event_id, config_hash_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, " \
437
"delay_up_to_timestamp, name, chart, family, exec, recipient, source, units, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, class, component, type) " \
438
-"select hostname, ?1, ?2, ?3, config_hash_id, 0, ?4, strftime('%%s'), 0, 0, flags, exec_run_timestamp, " \
439
-"strftime('%%s'), name, chart, family, exec, recipient, source, units, info, exec_code, -2, new_status, delay, NULL, new_value, 0, class, component, type " \
438
+"select hostname, ?1, ?2, ?3, config_hash_id, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, " \
439
+"unixepoch(), name, chart, family, exec, recipient, source, units, info, exec_code, -2, new_status, delay, NULL, new_value, 0, class, component, type " \
440
"from health_log_%s where unique_id = ?5", guid, guid2
441
#define SQL_INJECT_REMOVED_UPDATE(guid) "update health_log_%s set flags = flags | ?1, updated_by_id = ?2 where unique_id = ?3; ", guid
442
void sql_inject_removed_status(char *uuid_str, uint32_t alarm_id, uint32_t alarm_event_id, uint32_t unique_id, uint32_t max_unique_id)
@@ -814,7 +814,7 @@ void sql_health_alarm_log_load(RRDHOST *host) {
814
"on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
815
"charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
816
"p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
817
- "p_db_lookup_before, p_update_every) values (?1,strftime('%s'),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
817
+ "p_db_lookup_before, p_update_every) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
818
"?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34);"
819
820
int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)