tsdb_sample.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief TSDB samples.
  9. *
  10. * Time series log (like TSDB) feature samples source file.
  11. *
  12. * TSL is time series log, the TSDB saved many TSLs.
  13. */
  14. #include <flashdb.h>
  15. #include <string.h>
  16. #ifdef FDB_USING_TSDB
  17. #define FDB_LOG_TAG "[sample][tsdb]"
  18. #ifdef FDB_USING_TIMESTAMP_64BIT
  19. #define __PRITS "lld"
  20. #else
  21. #define __PRITS "d"
  22. #endif
  23. struct env_status {
  24. int temp;
  25. int humi;
  26. };
  27. static bool query_cb(fdb_tsl_t tsl, void *arg);
  28. static bool query_by_time_cb(fdb_tsl_t tsl, void *arg);
  29. static bool set_status_cb(fdb_tsl_t tsl, void *arg);
  30. void tsdb_sample(fdb_tsdb_t tsdb)
  31. {
  32. struct fdb_blob blob;
  33. FDB_INFO("==================== tsdb_sample ====================\n");
  34. { /* APPEND new TSL (time series log) */
  35. struct env_status status;
  36. /* append new log to TSDB */
  37. status.temp = 36;
  38. status.humi = 85;
  39. fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
  40. FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi);
  41. status.temp = 38;
  42. status.humi = 90;
  43. fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
  44. FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n", status.temp, status.humi);
  45. }
  46. { /* QUERY the TSDB */
  47. /* query all TSL in TSDB by iterator */
  48. fdb_tsl_iter(tsdb, query_cb, tsdb);
  49. }
  50. { /* QUERY the TSDB by time */
  51. /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00) */
  52. struct tm tm_from = { .tm_year = 1970 - 1900, .tm_mon = 0, .tm_mday = 1, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  53. struct tm tm_to = { .tm_year = 2020 - 1900, .tm_mon = 4, .tm_mday = 5, .tm_hour = 0, .tm_min = 0, .tm_sec = 0 };
  54. time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  55. size_t count;
  56. /* query all TSL in TSDB by time */
  57. fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  58. /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  59. count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  60. FDB_INFO("query count is: %zu\n", count);
  61. }
  62. { /* SET the TSL status */
  63. /* Change the TSL status by iterator or time iterator
  64. * set_status_cb: the change operation will in this callback
  65. *
  66. * NOTE: The actions to modify the state must be in order.
  67. * like: FDB_TSL_WRITE -> FDB_TSL_USER_STATUS1 -> FDB_TSL_DELETED -> FDB_TSL_USER_STATUS2
  68. * The intermediate states can also be ignored.
  69. * such as: FDB_TSL_WRITE -> FDB_TSL_DELETED
  70. */
  71. fdb_tsl_iter(tsdb, set_status_cb, tsdb);
  72. }
  73. FDB_INFO("===========================================================\n");
  74. }
  75. static bool query_cb(fdb_tsl_t tsl, void *arg)
  76. {
  77. struct fdb_blob blob;
  78. struct env_status status;
  79. fdb_tsdb_t db = arg;
  80. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  81. FDB_INFO("[query_cb] queried a TSL: time: %" __PRITS ", temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
  82. return false;
  83. }
  84. static bool query_by_time_cb(fdb_tsl_t tsl, void *arg)
  85. {
  86. struct fdb_blob blob;
  87. struct env_status status;
  88. fdb_tsdb_t db = arg;
  89. fdb_blob_read((fdb_db_t) db, fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  90. FDB_INFO("[query_by_time_cb] queried a TSL: time: %" __PRITS ", temp: %d, humi: %d\n", tsl->time, status.temp, status.humi);
  91. return false;
  92. }
  93. static bool set_status_cb(fdb_tsl_t tsl, void *arg)
  94. {
  95. fdb_tsdb_t db = arg;
  96. FDB_INFO("set the TSL (time %" __PRITS ") status from %d to %d\n", tsl->time, tsl->status, FDB_TSL_USER_STATUS1);
  97. fdb_tsl_set_status(db, tsl, FDB_TSL_USER_STATUS1);
  98. return false;
  99. }
  100. #endif /* FDB_USING_TSDB */