1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

use async_std::task;
use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD as b64_std_engine, Engine};
use chrono::{NaiveDateTime, SecondsFormat};
use futures::prelude::*;
use influxdb2::api::buckets::ListBucketsRequest;
use influxdb2::models::Query;
use influxdb2::models::{DataPoint, PostBucketRequest};
use influxdb2::Client;
use influxdb2::FromDataPoint;
use zenoh::buffers::ZBuf;
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin};

use std::convert::{TryFrom, TryInto};
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant, UNIX_EPOCH};
use uuid::Uuid;
use zenoh::buffers::buffer::SplitBuffer;
use zenoh::prelude::*;
use zenoh::properties::Properties;
use zenoh::selector::TimeExpr;
use zenoh::time::Timestamp;
use zenoh::Result as ZResult;
use zenoh_backend_traits::config::{
    PrivacyGetResult, PrivacyTransparentGet, StorageConfig, VolumeConfig,
};
use zenoh_backend_traits::StorageInsertionResult;
use zenoh_backend_traits::*;
use zenoh_core::{bail, zerror};
use zenoh_util::{Timed, TimedEvent, TimedHandle, Timer};

// Properties used by the Backend
pub const PROP_BACKEND_URL: &str = "url";
pub const PROP_BACKEND_ORG_ID: &str = "org_id";
pub const PROP_TOKEN: &str = "token";

// Properties used by the Storage
pub const PROP_STORAGE_DB: &str = "db";
pub const PROP_STORAGE_CREATE_DB: &str = "create_db";
pub const PROP_STORAGE_ON_CLOSURE: &str = "on_closure";

// Special key for None (when the prefix being stripped exactly matches the key)
pub const NONE_KEY: &str = "@@none_key@@";

// delay after deletion to drop a measurement
const DROP_MEASUREMENT_TIMEOUT_MS: u64 = 5000;

lazy_static::lazy_static!(
    static ref INFLUX_REGEX_ALL: String = key_exprs_to_influx_regex(&["**".try_into().unwrap()]);
);

type Config<'a> = &'a serde_json::Map<String, serde_json::Value>;

fn get_private_conf<'a>(config: Config<'a>, credit: &str) -> ZResult<Option<&'a String>> {
    match config.get_private(credit) {
        PrivacyGetResult::NotFound => Ok(None),
        PrivacyGetResult::Private(serde_json::Value::String(v)) => Ok(Some(v)),
        PrivacyGetResult::Public(serde_json::Value::String(v)) => {
            tracing::warn!(
                r#"Value "{}" is given for `{}` publicly (i.e. is visible by anyone who can fetch the router configuration). 
                You may want to replace `{}: "{}"` with `private: {{{}: "{}"}}`"#,
                v,
                credit,
                credit,
                v,
                credit,
                v
            );
            Ok(Some(v))
        }
        PrivacyGetResult::Both {
            public: serde_json::Value::String(public),
            private: serde_json::Value::String(private),
        } => {
            tracing::warn!(
                r#"Value "{}" is given for `{}` publicly, but a private value also exists. 
                The private value will be used, 
                but the public value, which is {} the same as the private one, will still be visible in configurations."#,
                public,
                credit,
                if public == private { "" } else { "not " }
            );
            Ok(Some(private))
        }
        _ => {
            bail!("Optional property `{}` must be a string", credit)
        }
    }
}

fn extract_credentials(config: Config) -> ZResult<Option<InfluxDbCredentials>> {
    match (
        get_private_conf(config, PROP_BACKEND_ORG_ID)?,
        get_private_conf(config, PROP_TOKEN)?,
    ) {
        // (Some(org_id), Some(token)) => Ok(Some(InfluxDbCredentials::Creds(
        //     org_id.clone(),
        //     token.clone(),
        // ))),
        (Some(org_id), Some(token)) => Ok(Some(InfluxDbCredentials {
            org_id: org_id.clone(),
            token: token.clone(),
        })),
        _ => {
            tracing::error!("Couldn't get token and org");
            bail!(
                "Properties `{}` and `{}` must exist",
                PROP_BACKEND_ORG_ID,
                PROP_TOKEN
            );
        }
    }
}

pub struct InfluxDbBackend {}

#[cfg(feature = "dynamic_plugin")]
zenoh_plugin_trait::declare_plugin!(InfluxDbBackend);

impl Plugin for InfluxDbBackend {
    type StartArgs = VolumeConfig;
    type Instance = VolumeInstance;

    const DEFAULT_NAME: &'static str = "influxdb_backend2";
    const PLUGIN_VERSION: &'static str = plugin_version!();
    const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

    fn start(_name: &str, config: &Self::StartArgs) -> ZResult<Self::Instance> {
        zenoh_util::try_init_log_from_env();

        tracing::debug!("InfluxDBv2 backend {}", Self::PLUGIN_VERSION);

        let mut config = config.clone();
        config
            .rest
            .insert("version".into(), Self::PLUGIN_VERSION.into());

        let url = match config.rest.get(PROP_BACKEND_URL) {
            Some(serde_json::Value::String(url)) => url.clone(),
            _ => {
                bail!(
                    "Mandatory property `{}` for InfluxDbv2 Backend must be a string",
                    PROP_BACKEND_URL
                )
            }
        };

        // The InfluxDB "admin" client that will be used for creating and dropping buckets (create/drop databases)
        #[allow(unused_mut)]
        let mut admin_client: Client;

        match extract_credentials(&config.rest)? {
            Some(creds) => {
                admin_client = match std::panic::catch_unwind(|| {
                    Client::new(url.clone(), creds.org_id.clone(), creds.token.clone())
                }) {
                    Ok(client) => client,
                    Err(e) => bail!("Error in creating client for InfluxDBv2 volume: {:?}", e),
                };
                match async_std::task::block_on(async { admin_client.ready().await }) {
                    Ok(res) => {
                        if !res {
                            bail!("InfluxDBv2 server is not ready! ")
                        }
                    }
                    Err(e) => bail!("Failed to create InfluxDBv2 Volume : {:?}", e),
                }

                Ok(Box::new(InfluxDbVolume {
                    admin_status: config,
                    admin_client,
                    credentials: Some(creds),
                }))
            }
            None => {
                bail!("Admin creds not provided. Can't proceed without them.");
            }
        }
    }
}

// Struct for credentials
#[derive(Debug)]
struct InfluxDbCredentials {
    org_id: String,
    token: String,
}

pub struct InfluxDbVolume {
    admin_status: VolumeConfig,
    admin_client: Client,
    credentials: Option<InfluxDbCredentials>,
}

#[async_trait]
impl Volume for InfluxDbVolume {
    fn get_admin_status(&self) -> serde_json::Value {
        self.admin_status.to_json_value()
    }

    fn get_capability(&self) -> Capability {
        Capability {
            persistence: Persistence::Durable,
            history: History::All,
            read_cost: 1,
        }
    }

    async fn create_storage(&self, mut config: StorageConfig) -> ZResult<Box<dyn Storage>> {
        let volume_cfg = match config.volume_cfg.as_object() {
            Some(v) => v,
            None => bail!("InfluxDBv2 backed storages need some volume-specific configuration"),
        };

        let on_closure = match volume_cfg.get(PROP_STORAGE_ON_CLOSURE) {
            Some(serde_json::Value::String(x)) if x == "drop_series" => OnClosure::DropSeries,
            Some(serde_json::Value::String(x)) if x == "drop_db" => OnClosure::DropDb,
            Some(serde_json::Value::String(x)) if x == "do_nothing" => OnClosure::DoNothing,
            None => OnClosure::DoNothing,
            Some(_) => {
                bail!(
                    r#"`{}` property of InfluxDBv2 storage `{}` must be one of "do_nothing" (default), "drop_db" and "drop_series""#,
                    PROP_STORAGE_ON_CLOSURE,
                    &config.name
                )
            }
        };
        let (db, createdb) = match volume_cfg.get(PROP_STORAGE_DB) {
            Some(serde_json::Value::String(s)) => (
                s.clone(),
                match volume_cfg.get(PROP_STORAGE_CREATE_DB) {
                    None | Some(serde_json::Value::Bool(false)) => false,
                    Some(serde_json::Value::Bool(true)) => true,
                    Some(_) => todo!(),
                },
            ),
            None => (generate_db_name(), true),
            Some(v) => bail!("Invalid value for ${PROP_STORAGE_DB} config property: ${v}"),
        };

        // The Influx client on database used to write/query on this storage
        let url = match &self.admin_status.rest.get(PROP_BACKEND_URL) {
            Some(serde_json::Value::String(url)) => url.clone(),
            _ => {
                bail!(
                    "Mandatory property `{}` for InfluxDBv2 Backend must be a string",
                    PROP_BACKEND_URL
                )
            }
        };

        let creds = match extract_credentials(volume_cfg)? {
            Some(creds) => creds,
            _ => bail!("No credentials specified to access database '{}'", db),
        };
        let client = match std::panic::catch_unwind(|| {
            Client::new(url.clone(), creds.org_id.clone(), creds.token.clone())
        }) {
            Ok(client) => client,
            Err(e) => bail!("Error in creating client for InfluxDBv2 storage: {:?}", e),
        };

        //check if db exists, if it doesn't create one if user has set createdb=true in config
        match async_std::task::block_on(async { is_db_existing(&client, &db).await }) {
            Ok(res) => {
                if !res && createdb {
                    // try to create db using user credentials
                    match async_std::task::block_on(async {
                        create_db(&self.admin_client, &creds.org_id, &db).await
                    }) {
                        Ok(res) => {
                            if !res {
                                bail!("Database '{}' wasnt't created in InfluxDBv2 storage", db)
                            }
                        }
                        Err(e) => bail!("Failed to create InfluxDBv2 Storage : {:?}", e),
                    }
                }
            }
            Err(e) => bail!("Failed to create InfluxDBv2 Storage : {:?}", e),
        }

        config
            .volume_cfg
            .as_object_mut()
            .unwrap()
            .entry(PROP_STORAGE_DB)
            .or_insert(db.clone().into());

        // The Influx client on database with backend's credentials (admin), to drop measurements and database
        let admin_creds = match &self.credentials {
            Some(creds) => creds,
            None => bail!("No credentials specified to access database '{}'", db),
        };
        let admin_client = match std::panic::catch_unwind(|| {
            Client::new(url.clone(), &admin_creds.org_id, &admin_creds.token)
        }) {
            Ok(client) => client,
            Err(e) => bail!("Error in creating client for InfluxDBv2 volume: {:?}", e),
        };

        Ok(Box::new(InfluxDbStorage {
            config,
            admin_client,
            client,
            on_closure,
            timer: Timer::default(),
        }))
    }

    fn incoming_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>> {
        None
    }

    fn outgoing_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>> {
        None
    }
}

enum OnClosure {
    DropDb,
    DropSeries,
    DoNothing,
}

impl TryFrom<&Properties> for OnClosure {
    type Error = zenoh_core::Error;
    fn try_from(p: &Properties) -> ZResult<OnClosure> {
        match p.get(PROP_STORAGE_ON_CLOSURE) {
            Some(s) => {
                if s == "drop_db" {
                    Ok(OnClosure::DropDb)
                } else if s == "drop_series" {
                    Ok(OnClosure::DropSeries)
                } else {
                    bail!("Unsupported value for 'on_closure' property: {}", s)
                }
            }
            None => Ok(OnClosure::DoNothing),
        }
    }
}

struct InfluxDbStorage {
    config: StorageConfig,
    admin_client: Client,
    client: Client,
    on_closure: OnClosure,
    timer: Timer,
}

impl InfluxDbStorage {
    async fn get_deletion_timestamp(&self, measurement: &str) -> ZResult<Option<Timestamp>> {
        let db = get_db_name(self.config.clone())?;
        let qs = format!(
            "from(bucket: \"{}\")
                                    |> range(start: {})
                                    |> filter(fn: (r) => r._measurement == \"{}\")
                                    |> filter(fn: (r) => r[\"kind\"] == \"DEL\")
                                    |> last()
                                ",
            db, 0, measurement
        );

        // get the value and if it exists then extract the timestamp from it

        #[derive(Debug, Default, FromDataPoint)]
        struct ZenohPoint {
            #[allow(dead_code)]
            // NOTE: "kind" is present within InfluxDB and used in query clauses, but not read in Rust...
            kind: String,
            timestamp: String,
            encoding_prefix: i64, //should be u8 but not supported in v2.x so using a workaround
            encoding_suffix: String,
            base64: bool,
            value: String,
        }

        let query = Query::new(qs);
        let mut query_result: Vec<ZenohPoint> = vec![];

        match async_std::task::block_on(async {
            self.client.query::<ZenohPoint>(Some(query)).await
        }) {
            Ok(result) => {
                query_result = result;
            }
            Err(e) => {
                tracing::error!(
                    "Couldn't get data from InfluxDBv2 database {} with error: {} ",
                    db,
                    e
                );
            }
        };

        if query_result.is_empty() {
            return Ok(None);
        }

        match Timestamp::from_str(&query_result[0].timestamp) {
            Ok(ts) => Ok(Some(ts)),
            Err(e) => {
                bail!("Couldn't parse the deletion timestamp {:?}", e)
            }
        }
    }

    async fn schedule_measurement_drop(&self, measurement: &str) -> TimedHandle {
        let event = TimedEvent::once(
            Instant::now() + Duration::from_millis(DROP_MEASUREMENT_TIMEOUT_MS),
            TimedMeasurementDrop {
                client: self.admin_client.clone(),
                measurement: measurement.to_string(),
            },
        );
        let handle = event.get_handle();
        self.timer.add_async(event).await;
        handle
    }

    // fn keyexpr_from_serie(&self, serie_name: &str) -> ZResult<Option<OwnedKeyExpr>> {
    //     if serie_name.eq(NONE_KEY) {
    //         Ok(None)
    //     } else {
    //         match OwnedKeyExpr::from_str(serie_name) {
    //             Ok(key) => Ok(Some(key)),
    //             Err(e) => Err(format!("{}", e).into()),
    //         }
    //     }
    // }
}

#[async_trait]
impl Storage for InfluxDbStorage {
    fn get_admin_status(&self) -> serde_json::Value {
        // TODO: possibly add more properties in returned Value for more information about this storage
        self.config.to_json_value()
    }

    async fn put(
        &mut self,
        key: Option<OwnedKeyExpr>,
        value: Value,
        timestamp: Timestamp,
    ) -> ZResult<StorageInsertionResult> {
        let measurement = key.unwrap_or_else(|| OwnedKeyExpr::from_str(NONE_KEY).unwrap());
        let influx_time = timestamp.get_time().to_duration().as_nanos() as i64;

        if let Some(del_time) = self.get_deletion_timestamp(measurement.as_str()).await? {
            // ignore sample if oldest than the deletion
            if timestamp < del_time {
                tracing::debug!(
                    "Received a value for {:?} with timestamp older than its deletion in InfluxDBv2 database; Ignore it",
                    measurement
                );
                return Ok(StorageInsertionResult::Outdated);
            }
        }

        // Note: assume that uhlc timestamp was generated by a clock using UNIX_EPOCH (that's the case by default)
        // encode the value as a string to be stored in InfluxDB, converting to base64 if the buffer is not a UTF-8 string
        let (base64, strvalue) = match String::from_utf8(value.payload.contiguous().into_owned()) {
            Ok(s) => (false, s),
            Err(err) => (true, b64_std_engine.encode(err.into_bytes())),
        };

        let db = get_db_name(self.config.clone())?;

        // Note: tags are stored as strings in InfluxDB, while fileds are typed.
        // For simpler/faster deserialization, we store encoding, timestamp and base64 as fields.
        // while the kind is stored as a tag to be indexed by InfluxDB and have faster queries on it.
        let zenoh_point = vec![DataPoint::builder(measurement.clone())
            .tag("kind", "PUT")
            .field("timestamp", timestamp.to_string())
            .field("encoding_prefix", u8::from(*value.encoding.prefix()) as i64) //should be u8 but not supported in v2.x so using a workaround
            .field("encoding_suffix", value.encoding.suffix())
            .field("base64", base64)
            .field("value", strvalue)
            .timestamp(influx_time) //converted timestamp to i64
            .build()?];

        match async_std::task::block_on(async {
            self.client.write(&db, stream::iter(zenoh_point)).await
        }) {
            Ok(_) => Ok(StorageInsertionResult::Inserted),
            Err(e) => bail!(
                "Failed to put Value for {:?} in InfluxDBv2 storage : {}",
                measurement,
                e
            ),
        }
    }

    async fn delete(
        &mut self,
        key: Option<OwnedKeyExpr>,
        timestamp: Timestamp,
    ) -> ZResult<StorageInsertionResult> {
        let measurement = key.unwrap_or_else(|| OwnedKeyExpr::from_str(NONE_KEY).unwrap());
        // Note: assume that uhlc timestamp was generated by a clock using UNIX_EPOCH (that's the case by default)
        // delete all points from the measurement that are older than this DELETE message
        // (in case more recent PUT have been recevived un-ordered)

        let db = get_db_name(self.config.clone())?;

        let start_timestamp = NaiveDateTime::UNIX_EPOCH;
        let stop_timestamp = NaiveDateTime::from_timestamp_opt(
            timestamp.get_time().as_secs() as i64,
            timestamp.get_time().subsec_nanos(),
        )
        .expect("Couldn't convert uhlc timestamp to naivedatetime");

        let predicate = None; //can be specified with tag or field values
        tracing::debug!(
            "Delete {:?} with Influx query in InfluxDBv2 storage",
            measurement
        );
        if let Err(e) = self
            .client
            .delete(&db, start_timestamp, stop_timestamp, predicate)
            .await
        {
            bail!(
                "Failed to delete points for measurement '{}' from InfluxDBv2 storage : {}",
                measurement,
                e
            )
        }
        // store a point (with timestamp) with "delete" tag, thus we don't re-introduce an older point later;
        // filling fields with dummy values
        let influx_time = timestamp.get_time().to_duration().as_nanos() as i64;

        let zenoh_point = vec![DataPoint::builder(measurement.clone())
            .tag("kind", "DEL")
            .field("timestamp", timestamp.to_string())
            .field("encoding_prefix", 0_i64) //should be u8 but not supported in v2.x so using a workaround
            .field("encoding_suffix", "")
            .field("base64", false)
            .field("value", "")
            .timestamp(influx_time) //converted timestamp to i64
            .build()?];

        tracing::debug!(
            "Mark measurement {} as deleted at time {} in InfluxDBv2 storage",
            measurement.clone(),
            stop_timestamp
        );

        if let Err(e) = self.client.write(&db, stream::iter(zenoh_point)).await {
            bail!(
                "Failed to mark measurement {:?} as deleted : {} in InfluxDBv2 storage",
                measurement,
                e
            )
        }
        // schedule_measurement_drop is used to schedule the drop of measurement later in the future, if it's empty
        // influx 2.x doesn't support dropping measurements from the API
        let _ = self.schedule_measurement_drop(measurement.as_str()).await;
        Ok(StorageInsertionResult::Deleted)
    }

    async fn get(
        &mut self,
        key: Option<OwnedKeyExpr>,
        parameters: &str,
    ) -> ZResult<Vec<StoredData>> {
        let measurement = match key.clone() {
            Some(k) => k,
            None => OwnedKeyExpr::from_str(NONE_KEY)?,
        };

        let db = get_db_name(self.config.clone())?;

        // the expected JSon type resulting from the query
        // #[derive(Deserialize, Debug)]
        #[derive(Debug, Default, FromDataPoint)]
        struct ZenohPoint {
            #[allow(dead_code)]
            // NOTE: "kind" is present within InfluxDB and used in query clauses, but not read in Rust...
            kind: String,
            timestamp: String,
            encoding_prefix: i64, //should be u8 but not supported in v2.x so using a workaround
            encoding_suffix: String,
            base64: bool,
            value: String,
        }

        #[allow(unused_assignments)]
        let mut qs: String = String::new();
        match timerange_from_parameters(parameters)? {
            Some(range) => {
                qs = format!(
                    "from(bucket: \"{}\")
                                            |> range({})
                                            |> filter(fn: (r) => r._measurement == \"{}\")
                                            |> filter(fn: (r) => r[\"kind\"] == \"PUT\")
                                        ",
                    db, range, measurement
                );
            }
            None => {
                qs = format!(
                    "from(bucket: \"{}\")
                                            |> range(start:0)
                                            |> filter(fn: (r) => r._measurement == \"{}\")
                                            |> filter(fn: (r) => r[\"kind\"] == \"PUT\")
                                            |> last()
                                        ",
                    db, measurement
                );
            }
        }

        tracing::debug!(
            "Get {:?} with Influx query:{} in InfluxDBv2 storage",
            key,
            qs
        );

        let query = Query::new(qs);
        let mut query_result: Vec<ZenohPoint> = vec![];

        match async_std::task::block_on(async {
            self.client.query::<ZenohPoint>(Some(query)).await
        }) {
            Ok(result) => {
                query_result = result;
            }
            Err(e) => {
                tracing::error!(
                    "Couldn't get data from database {} in InfluxDBv2 storage with error: {} ",
                    db,
                    e
                );
            }
        };
        let mut result: Vec<StoredData> = vec![];

        for zpoint in query_result {
            // get the encoding
            let encoding_prefix =
                (if zpoint.encoding_prefix >= 0 && zpoint.encoding_prefix <= 255 {
                    Ok(zpoint.encoding_prefix as u8)
                } else {
                    Err(zerror!(
                        "Encoding {} is outside possible range of values",
                        zpoint.encoding_prefix
                    ))
                })
                .and_then(|prefix| {
                    KnownEncoding::try_from(prefix)
                        .map_err(|_| zerror!("Unknown encoding {}", zpoint.encoding_prefix))
                })?;
            let encoding = if zpoint.encoding_suffix.is_empty() {
                Encoding::Exact(encoding_prefix)
            } else {
                Encoding::WithSuffix(encoding_prefix, zpoint.encoding_suffix.into())
            };
            // get the payload
            let payload = if zpoint.base64 {
                match b64_std_engine.decode(zpoint.value) {
                    Ok(v) => ZBuf::from(v),
                    Err(e) => {
                        tracing::warn!(
                            r#"Failed to decode zenoh base64 Value from Influx point with timestamp="{}": {}"#,
                            zpoint.timestamp,
                            e
                        );
                        continue;
                    }
                }
            } else {
                ZBuf::from(zpoint.value.into_bytes())
            };
            // get the timestamp
            let timestamp = match Timestamp::from_str(&zpoint.timestamp) {
                Ok(t) => t,
                Err(e) => {
                    tracing::warn!(
                        r#"Failed to decode zenoh Timestamp from Influx point with timestamp="{}": {:?}"#,
                        zpoint.timestamp,
                        e
                    );
                    continue;
                }
            };
            let value = Value::new(payload).encoding(encoding);
            result.push(StoredData { value, timestamp });
        }
        Ok(result)
    }

    //putting a stub here to be implemented later
    async fn get_all_entries(&self) -> ZResult<Vec<(Option<OwnedKeyExpr>, Timestamp)>> {
        tracing::warn!("called get_all_entries in InfluxDBv2 storage");
        let mut result: Vec<(Option<OwnedKeyExpr>, Timestamp)> = Vec::new();
        let curr_time = zenoh::time::new_reception_timestamp();
        result.push((None, curr_time));
        return Ok(result);
    }
}

fn get_db_name(config: StorageConfig) -> ZResult<String> {
    let db = match config.volume_cfg.get(PROP_STORAGE_DB) {
        Some(serde_json::Value::String(s)) => s.to_string(),
        _ => bail!("no db was found"),
    };
    Ok(db)
}

impl Drop for InfluxDbStorage {
    fn drop(&mut self) {
        tracing::debug!("Closing InfluxDBv2 storage");
        let db = match self.config.volume_cfg.get(PROP_STORAGE_DB) {
            Some(serde_json::Value::String(s)) => s.to_string(),
            _ => {
                tracing::error!("no db was found");
                return;
            }
        };

        match self.on_closure {
            OnClosure::DropDb => {
                task::block_on(async move {
                    tracing::debug!("Close InfluxDBv2 storage, dropping database {}", db);
                    if let Err(e) = self.admin_client.delete_bucket(&db).await {
                        tracing::error!("Failed to drop InfluxDbv2 database '{}' : {}", db, e)
                    }
                });
            }
            OnClosure::DropSeries => {
                task::block_on(async move {
                    tracing::debug!(
                        "Close InfluxDBv2 storage, dropping all series from database {}",
                        db
                    );
                    let start = NaiveDateTime::MIN;
                    let stop = NaiveDateTime::MAX;
                    if let Err(e) = self.client.delete(&db, start, stop, None).await {
                        tracing::error!(
                            "Failed to drop all series from InfluxDbv2 database '{}' : {}",
                            db,
                            e
                        )
                    }
                });
            }
            OnClosure::DoNothing => {
                tracing::debug!("Close InfluxDBv2 storage, keeping database {} as it is", db);
            }
        }
    }
}

// Scheduled dropping of a measurement after a timeout, if it's empty
#[allow(dead_code)]
struct TimedMeasurementDrop {
    client: Client,
    measurement: String,
}

#[async_trait]
impl Timed for TimedMeasurementDrop {
    async fn run(&mut self) {
        //keeping the stub here for implemntation in a future time
        //currently the influxDB library doesn't allow dropping a measurement
    }
}

fn generate_db_name() -> String {
    format!("zenoh_db_{}", Uuid::new_v4().simple())
}

async fn is_db_existing(client: &Client, db: &str) -> ZResult<bool> {
    let request = ListBucketsRequest {
        name: Some(db.to_owned()),
        ..ListBucketsRequest::default()
    };

    let dbs = client.list_buckets(Some(request)).await?.buckets;
    if !dbs.is_empty() {
        Ok(true)
    } else {
        Ok(false)
    }
}

async fn create_db(client: &Client, org_id: &str, db: &str) -> ZResult<bool> {
    let result = client
        .create_bucket(Some(PostBucketRequest::new(
            org_id.to_owned(),
            db.to_owned(),
        )))
        .await;
    match result {
        Ok(_) => Ok(true),
        Err(_) => Ok(false), //can post error here
    }
}
// Returns an InfluxDB regex (see https://docs.influxdata.com/influxdb/v1.8/query_language/explore-data/#regular-expressions)
// corresponding to the list of path expressions. I.e.:
// Replace "**" with ".*", "*" with "[^\/]*"  and "/" with "\/".
// Concat each with "|", and surround the result with '/^' and '$/'.
fn key_exprs_to_influx_regex(path_exprs: &[&keyexpr]) -> String {
    let mut result = String::with_capacity(2 * path_exprs[0].len());
    result.push_str("/^");
    for (i, path_expr) in path_exprs.iter().enumerate() {
        if i != 0 {
            result.push('|');
        }
        let mut chars = path_expr.chars().peekable();
        while let Some(c) = chars.next() {
            match c {
                '*' => {
                    if let Some(c2) = chars.peek() {
                        if c2 == &'*' {
                            result.push_str(".*");
                            chars.next();
                        } else {
                            result.push_str(".*")
                        }
                    }
                }
                '/' => result.push_str(r"\/"),
                _ => result.push(c),
            }
        }
    }
    result.push_str("$/");
    result
}

fn timerange_from_parameters(p: &str) -> ZResult<Option<String>> {
    use zenoh::selector::{TimeBound, TimeRange};
    let time_range = p.time_range()?;
    let mut result = String::new();
    match time_range {
        Some(TimeRange(start, stop)) => {
            match start {
                TimeBound::Inclusive(t) => {
                    result.push_str("start:");
                    write_timeexpr(&mut result, t, 0);
                }
                TimeBound::Exclusive(t) => {
                    result.push_str("start:");
                    write_timeexpr(&mut result, t, 1);
                }
                TimeBound::Unbounded => {}
            }
            match stop {
                TimeBound::Inclusive(t) => {
                    result.push_str(", stop:");
                    write_timeexpr(&mut result, t, 1);
                }
                TimeBound::Exclusive(t) => {
                    result.push_str(", stop:");
                    write_timeexpr(&mut result, t, 0);
                }
                TimeBound::Unbounded => {}
            }
        }
        None => return Ok(None),
    }

    Ok(Some(result))
}

fn write_timeexpr(s: &mut String, t: TimeExpr, i: u64) {
    use std::fmt::Write;
    match t {
        TimeExpr::Fixed(t) => {
            // let tm = t +
            let time_duration = t.duration_since(UNIX_EPOCH).expect("Time went backwards")
                + Duration::from_nanos(i); //adding 1ns for inclusive timebinding ;
            let datetime = chrono::DateTime::from_timestamp(
                time_duration
                    .as_secs()
                    .try_into()
                    .expect("Error in converting seconds from u64 to i64"),
                time_duration.subsec_nanos(),
            )
            .expect("Error in converting duration to datetime");
            write!(
                s,
                "{}",
                datetime.to_rfc3339_opts(SecondsFormat::Nanos, true)
            )
        }
        TimeExpr::Now { offset_secs } => {
            let os = offset_secs * 1e9 + i as f64; //adding 1ns for inclusive timebinding
            write!(s, "{}ns", os)
        }
    }
    .unwrap()
}