2020-05-24 18:39:09 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief TSDB samples.
|
|
|
|
*
|
|
|
|
* Time series log (like TSDB) feature samples source file.
|
|
|
|
*
|
|
|
|
* TSL is time series log, the TSDB saved many TSLs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <flashdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-05-31 19:02:13 +08:00
|
|
|
#ifdef FDB_USING_TSDB
|
|
|
|
|
|
|
|
#define FDB_LOG_TAG "[sample][tsdb]"
|
|
|
|
|
2020-05-24 18:39:09 +08:00
|
|
|
struct env_status {
|
|
|
|
int temp;
|
|
|
|
int humi;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool query_cb(fdb_tsl_t tsl, void *arg);
|
|
|
|
static bool set_status_cb(fdb_tsl_t tsl, void *arg);
|
|
|
|
|
2020-05-31 19:02:13 +08:00
|
|
|
void tsdb_sample(fdb_tsdb_t tsdb)
|
2020-05-24 18:39:09 +08:00
|
|
|
{
|
|
|
|
struct fdb_blob blob;
|
|
|
|
|
2020-10-04 16:41:52 +08:00
|
|
|
FDB_INFO("==================== tsdb_sample ====================\n");
|
|
|
|
|
2020-05-24 18:39:09 +08:00
|
|
|
{ /* APPEND new TSL (time series log) */
|
|
|
|
struct env_status status;
|
|
|
|
|
|
|
|
/* append new log to TSDB */
|
|
|
|
status.temp = 36;
|
|
|
|
status.humi = 85;
|
2020-05-31 19:02:13 +08:00
|
|
|
fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
|
|
|
|
FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi);
|
2020-05-24 18:39:09 +08:00
|
|
|
|
|
|
|
status.temp = 38;
|
|
|
|
status.humi = 90;
|
2020-05-31 19:02:13 +08:00
|
|
|
fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
|
|
|
|
FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi);
|
2020-05-24 18:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{ /* QUERY the TSDB */
|
|
|
|
/* query all TSL in TSDB by iterator */
|
2020-05-31 19:02:13 +08:00
|
|
|
fdb_tsl_iter(tsdb, query_cb, tsdb);
|
2020-05-24 18:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{ /* QUERY the TSDB by time */
|
|
|
|
/* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
|
|
|
|
struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
|
|
|
|
struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
|
|
|
|
time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
|
|
|
|
size_t count;
|
|
|
|
/* query all TSL in TSDB by time */
|
2020-05-31 19:02:13 +08:00
|
|
|
fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_cb, tsdb);
|
2020-05-24 18:39:09 +08:00
|
|
|
/* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
|
2020-05-31 19:02:13 +08:00
|
|
|
count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
|
|
|
|
FDB_INFO("query count is: %u\n", count);
|
2020-05-24 18:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{ /* SET the TSL status */
|
|
|
|
/* Change the TSL status by iterator or time iterator
|
|
|
|
* set_status_cb: the change operation will in this callback
|
|
|
|
*
|
|
|
|
* NOTE: The actions to modify the state must be in order.
|
|
|
|
* FDB_TSL_WRITE -> FDB_TSL_USER_STATUS1 -> FDB_TSL_DELETED -> FDB_TSL_USER_STATUS2
|
|
|
|
*/
|
2020-05-31 19:02:13 +08:00
|
|
|
fdb_tsl_iter(tsdb, set_status_cb, tsdb);
|
2020-05-24 18:39:09 +08:00
|
|
|
}
|
2020-10-04 16:41:52 +08:00
|
|
|
|
|
|
|
FDB_INFO("===========================================================\n");
|
2020-05-24 18:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool query_cb(fdb_tsl_t tsl, void *arg)
|
|
|
|
{
|
|
|
|
struct fdb_blob blob;
|
|
|
|
struct env_status status;
|
|
|
|
fdb_tsdb_t db = arg;
|
|
|
|
|
|
|
|
fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
|
2020-05-31 19:02:13 +08:00
|
|
|
FDB_INFO("queried a TSL: time: %ld, temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
|
2020-05-24 18:39:09 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool set_status_cb(fdb_tsl_t tsl, void *arg)
|
|
|
|
{
|
|
|
|
fdb_tsdb_t db = arg;
|
|
|
|
|
2020-05-31 19:02:13 +08:00
|
|
|
FDB_INFO("set the TSL (time %ld) status from %d to %d\n", tsl->time, tsl->status, FDB_TSL_USER_STATUS1);
|
2020-05-24 18:39:09 +08:00
|
|
|
fdb_tsl_set_status(db, tsl, FDB_TSL_USER_STATUS1);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-31 19:02:13 +08:00
|
|
|
|
|
|
|
#endif /* FDB_USING_TSDB */
|