From 523cdbc8367ba670889ac16e15a98f6bb2e22dcc Mon Sep 17 00:00:00 2001
From: JuanPi Carbajal <ajuanpi+dev@gmail.com>
Date: Fri, 29 Oct 2021 17:15:02 +0200
Subject: [PATCH] htmlui: samples: when no data, manual sample triggers
 retrieval of sample

---
 dataloggerui/htmlui.py                        | 19 +++++++++++--------
 dataloggerui/templates/logger_ui_samples.html |  2 +-
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/dataloggerui/htmlui.py b/dataloggerui/htmlui.py
index dc366a7..f50fef1 100644
--- a/dataloggerui/htmlui.py
+++ b/dataloggerui/htmlui.py
@@ -579,6 +579,7 @@ def html_ui_samples():
     total, new = sui.get_sample_count()
     if 'n_samples' in request.args:
         n_samples = int(request.args['n_samples'])
+        n_samples = max(1, n_samples)  # discard 0 samples
     else:
         n_samples = min(15, total)
 
@@ -587,20 +588,22 @@ def html_ui_samples():
     for i in index:
         samples.append(sample_to_record(sui.get_sample(sample=i)))
 
-
     sample_df = pd.DataFrame().from_records(samples)
     sample_df.index = index
-    dt_col = sample_df.columns[sample_df.columns.str.startswith('date')][0]
-    sample_df.sort_values(by=dt_col, ascending=False, inplace=True)
-    col_oder = [dt_col] + sample_df.columns[sample_df.columns != dt_col].to_list()
-    sample_df = sample_df[col_oder]
-
-    #sample_df = sample_df.set_index(dt_col, drop=True)
+    if not sample_df.empty:
+        # Only do this if there is data in the df, i.e. there are samples
+        dt_col = sample_df.columns[sample_df.columns.str.startswith('date')][0]
+        sample_df.sort_values(by=dt_col, ascending=False, inplace=True)
+        col_oder = [dt_col] + sample_df.columns[sample_df.columns != dt_col].to_list()
+        sample_df = sample_df[col_oder]
+        #sample_df = sample_df.set_index(dt_col, drop=True)
 
     interval, units = sui.get_sampling_interval()
     interval = f"{interval} {'min' if units == 'T' else units}"
 
-    return render_template('logger_ui_samples.html', sample=sample_df.to_html(),
+    df_html = '<p>There is no data in the loger.<br>Wait approx. sampling interval and reload the page.</p>'\
+              if sample_df.empty else sample_df.to_html()
+    return render_template('logger_ui_samples.html', sample=df_html,
                                                 sample_count=(total, new),
                                                 auto_sampleStatus=sui.get_auto_sampling(),
                                                 sampling_interval=interval,
diff --git a/dataloggerui/templates/logger_ui_samples.html b/dataloggerui/templates/logger_ui_samples.html
index 2eefb65..c90b409 100644
--- a/dataloggerui/templates/logger_ui_samples.html
+++ b/dataloggerui/templates/logger_ui_samples.html
@@ -12,7 +12,7 @@
         title="Get the latest sample form the logger's internal storage">Get samples</button></label>
     <input type="number"
                name="n_samples"
-               min="1"
+               min="0"
                step="1"
                max="{{ sample_count[0] }}"
                size="6"
-- 
GitLab