tsdb_sample.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "ld"
  20. #else
  21. #define __PRITS "d"
  22. #endif
  23. struct env_status {
  24. int temp;
  25. int humi;
  26. };
  27. static pika_bool query_cb(fdb_tsl_t tsl, void* arg);
  28. static pika_bool query_by_time_cb(fdb_tsl_t tsl, void* arg);
  29. static pika_bool set_status_cb(fdb_tsl_t tsl, void* arg);
  30. void tsdb_sample(fdb_tsdb_t tsdb) {
  31. struct fdb_blob blob;
  32. FDB_INFO("==================== tsdb_sample ====================\n");
  33. { /* APPEND new TSL (time series log) */
  34. struct env_status status;
  35. /* append new log to TSDB */
  36. status.temp = 36;
  37. status.humi = 85;
  38. fdb_tsl_append(tsdb, fdb_blob_make(&blob, &status, sizeof(status)));
  39. FDB_INFO("append the new status.temp (%d) and status.humi (%d)\n",
  40. 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",
  45. status.temp, status.humi);
  46. }
  47. { /* QUERY the TSDB */
  48. /* query all TSL in TSDB by iterator */
  49. fdb_tsl_iter(tsdb, query_cb, tsdb);
  50. }
  51. { /* QUERY the TSDB by time */
  52. /* prepare query time (from 1970-01-01 00:00:00 to 2020-05-05 00:00:00)
  53. */
  54. struct tm tm_from = {.tm_year = 1970 - 1900,
  55. .tm_mon = 0,
  56. .tm_mday = 1,
  57. .tm_hour = 0,
  58. .tm_min = 0,
  59. .tm_sec = 0};
  60. struct tm tm_to = {.tm_year = 2020 - 1900,
  61. .tm_mon = 4,
  62. .tm_mday = 5,
  63. .tm_hour = 0,
  64. .tm_min = 0,
  65. .tm_sec = 0};
  66. time_t from_time = mktime(&tm_from), to_time = mktime(&tm_to);
  67. size_t count;
  68. /* query all TSL in TSDB by time */
  69. fdb_tsl_iter_by_time(tsdb, from_time, to_time, query_by_time_cb, tsdb);
  70. /* query all FDB_TSL_WRITE status TSL's count in TSDB by time */
  71. count = fdb_tsl_query_count(tsdb, from_time, to_time, FDB_TSL_WRITE);
  72. FDB_INFO("query count is: %zu\n", count);
  73. }
  74. { /* SET the TSL status */
  75. /* Change the TSL status by iterator or time iterator
  76. * set_status_cb: the change operation will in this callback
  77. *
  78. * NOTE: The actions to modify the state must be in order.
  79. * like: FDB_TSL_WRITE -> FDB_TSL_USER_STATUS1 -> FDB_TSL_DELETED
  80. * -> FDB_TSL_USER_STATUS2 The intermediate states can also be ignored.
  81. * such as: FDB_TSL_WRITE -> FDB_TSL_DELETED
  82. */
  83. fdb_tsl_iter(tsdb, set_status_cb, tsdb);
  84. }
  85. FDB_INFO("===========================================================\n");
  86. }
  87. static pika_bool query_cb(fdb_tsl_t tsl, void* arg) {
  88. struct fdb_blob blob;
  89. // rbg/kcf
  90. struct env_status status = {0};
  91. fdb_tsdb_t db = arg;
  92. fdb_blob_read(
  93. (fdb_db_t)db,
  94. fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  95. FDB_INFO("[query_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n",
  96. tsl->time, status.temp, status.humi);
  97. return pika_false;
  98. }
  99. static pika_bool query_by_time_cb(fdb_tsl_t tsl, void* arg) {
  100. struct fdb_blob blob;
  101. // rbg/kcf
  102. struct env_status status = {0};
  103. fdb_tsdb_t db = arg;
  104. fdb_blob_read(
  105. (fdb_db_t)db,
  106. fdb_tsl_to_blob(tsl, fdb_blob_make(&blob, &status, sizeof(status))));
  107. // rbg/kcf
  108. FDB_INFO(
  109. "[query_by_time_cb] queried a TSL: time: %ld, temp: %d, humi: %d\n",
  110. tsl->time, status.temp, status.humi);
  111. return pika_false;
  112. }
  113. static pika_bool set_status_cb(fdb_tsl_t tsl, void* arg) {
  114. fdb_tsdb_t db = arg;
  115. FDB_INFO("set the TSL (time %ld) status from %d to %d\n", tsl->time,
  116. tsl->status, FDB_TSL_USER_STATUS1);
  117. fdb_tsl_set_status(db, tsl, FDB_TSL_USER_STATUS1);
  118. return pika_false;
  119. }
  120. #endif /* FDB_USING_TSDB */