File: FoxLog.cpp

package info (click to toggle)
wsjtx 2.7.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 70,440 kB
  • sloc: cpp: 75,379; f90: 46,460; python: 27,241; ansic: 13,367; fortran: 2,382; makefile: 197; sh: 133
file content (354 lines) | stat: -rwxr-xr-x 11,831 bytes parent folder | download
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
#include "FoxLog.hpp"

#include <stdexcept>
#include <utility>
#include <QString>
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QSqlError>
#include <QSqlQuery>
#include <QTextStream>
#include <QDebug>
#include "Logger.hpp"
#include "Configuration.hpp"
#include "qt_db_helpers.hpp"
#include "pimpl_impl.hpp"

class FoxLog::impl final
  : public QSqlTableModel
{
  Q_OBJECT

public:
  impl (Configuration const * configuration);

  QVariant data (QModelIndex const& index, int role) const
  {
    auto value = QSqlTableModel::data (index, role);
    if (index.column () == fieldIndex ("when") && Qt::DisplayRole == role)
      {
        QLocale locale;
        value = locale.toString (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
      }
    return value;
  }

  Configuration const * configuration_;
  QSqlQuery mutable dupe_query_;
  QSqlQuery mutable export_query_;
  // queries for rates
  QSqlQuery mutable rate_n_query_;
  QSqlQuery mutable rate60m_query_;
  QString rate();
};

#include "FoxLog.moc"

namespace 
{
  QString const fox_log_ddl {
                             "CREATE %1 TABLE fox_log%2 ("
                             "	id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                             "	\"when\" DATETIME NOT NULL,"
                             "	call VARCHAR(20) NOT NULL,"
                             "	grid VARCHAR(4),"
                             "	report_sent VARCHAR(3),"
                             "	report_rcvd VARCHAR(3),"
                             "	band VARCHAR(6) NOT NULL"
                             ")"
  };
}

FoxLog::impl::impl (Configuration const * configuration)
  : configuration_ {configuration}
{
  if (!database ().tables ().contains ("fox_log"))
    {
      QSqlQuery query;
      SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                       fox_log_ddl.arg ("").arg (""));
    }
  else
    {
      QSqlQuery query;
      // query to check if table has a unique constraint
      SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                       "SELECT COUNT(*)"
                       "       FROM sqlite_master"
                       "    WHERE"
                       "       type = 'index' AND tbl_name = 'fox_log'");
      query.next ();
      if (query.value (0).toInt ())
        {
          // update to new schema with no dupe disallowing unique
          // constraint
          database ().transaction ();
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           fox_log_ddl.arg ("TEMPORARY").arg ("_backup"));
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           "INSERT INTO fox_log_backup SELECT * from fox_log");
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           "DROP TABLE fox_log");
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           fox_log_ddl.arg ("").arg (""));
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           "INSERT INTO fox_log SELECT * from fox_log_backup");
          SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
                           "DROP TABLE fox_log_backup");
          database ().commit ();
        }
    }

  SQL_error_check (dupe_query_, &QSqlQuery::prepare,
                   "SELECT "
                   "    COUNT(*) "
                   "  FROM "
                   "    fox_log "
                   "  WHERE "
                   "    call = :call "
                   "    AND band = :band");

  SQL_error_check (export_query_, &QSqlQuery::prepare,
                   "SELECT "
                   "    band"
                   "    , \"when\""
                   "    , call"
                   "    , grid"
                   "    , report_sent"
                   "    , report_rcvd "
                   "  FROM "
                   "    fox_log "
                   "  ORDER BY "
                   "    \"when\"");

  SQL_error_check (rate_n_query_, &QSqlQuery::prepare,
                   "SELECT "
                   " \"when\""
                   "  FROM "
                   "    fox_log "
                   "  ORDER BY "
                   "    \"when\" DESC"
                   "  LIMIT :limitn"
  );

  SQL_error_check (rate60m_query_, &QSqlQuery::prepare,
                   "SELECT "
                   " COUNT() "
                   "  FROM "
                   "    fox_log "
                   " where \"when\" > :one_hour_ago"
                   "  ORDER BY "
                   "    \"when\" DESC"
  );
  setEditStrategy (QSqlTableModel::OnFieldChange);
  setTable ("fox_log");
  setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)"));
  setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call"));
  setHeaderData (fieldIndex ("grid"), Qt::Horizontal, tr ("Grid"));
  setHeaderData (fieldIndex ("report_sent"), Qt::Horizontal, tr ("Sent"));
  setHeaderData (fieldIndex ("report_rcvd"), Qt::Horizontal, tr ("Rcvd"));
  setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band"));

  // This descending order by time is important, it makes the view
  // place the latest row at the top, without this the model/view
  // interactions are both sluggish and unhelpful.
  setSort (fieldIndex ("when"), Qt::DescendingOrder);

  SQL_error_check (*this, &QSqlTableModel::select);
}

QString FoxLog::rate()
{
  return QString("Last 10: %1, Last 100: %2, Last 60m: %3").arg(QString::number(this->rate_last_n(10),'f',0),
                                               QString::number(this->rate_last_n(100),'f',0),
                                               QString::number(this->rate_60m()));
}

FoxLog::FoxLog (Configuration const * configuration)
  : m_ {configuration}
{
}

FoxLog::~FoxLog ()
{
}

QSqlTableModel * FoxLog::model ()
{
  return &*m_;
}

namespace
{
  void set_value_maybe_null (QSqlRecord& record, QString const& name, QString const& value)
  {
    if (value.size ())
      {
        record.setValue (name, value);
      }
    else
      {
        record.setNull (name);
      }
  }
}

bool FoxLog::add_QSO (QDateTime const& when, QString const& call, QString const& grid
                      , QString const& report_sent, QString const& report_received
                      , QString const& band)
{
  auto record = m_->record ();
  if (!when.isNull ())
    {
      record.setValue ("when", when.toMSecsSinceEpoch () / 1000);
    }
  else
    {
      record.setNull ("when");
    }
  set_value_maybe_null (record, "call", call);
  set_value_maybe_null (record, "grid", grid);
  set_value_maybe_null (record, "report_sent", report_sent);
  set_value_maybe_null (record, "report_rcvd", report_received);
  set_value_maybe_null (record, "band", band);
  if (m_->isDirty ())
    {
      m_->revert ();            // discard any uncommitted changes
    }
  m_->setEditStrategy (QSqlTableModel::OnManualSubmit);
  ConditionalTransaction transaction {*m_};
  auto ok = m_->insertRecord (-1, record);
  if (ok)
    {
      ok = transaction.submit (false);
    }
  m_->setEditStrategy (QSqlTableModel::OnFieldChange);
  return ok;
}

bool FoxLog::dupe (QString const& call, QString const& band) const
{
  m_->dupe_query_.bindValue (":call", call);
  m_->dupe_query_.bindValue (":band", band);
  SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
  m_->dupe_query_.next ();
  return m_->dupe_query_.value (0).toInt ();
}

void FoxLog::reset ()
{
  // synchronize model
  while (m_->canFetchMore ()) m_->fetchMore ();
  if (m_->rowCount ())
    {
      m_->setEditStrategy (QSqlTableModel::OnManualSubmit);
      ConditionalTransaction transaction {*m_};
      SQL_error_check (*m_, &QSqlTableModel::removeRows, 0, m_->rowCount (), QModelIndex {});
      transaction.submit ();
      m_->select ();            // to refresh views
      m_->setEditStrategy (QSqlTableModel::OnFieldChange);
    }
}

int FoxLog::rate_60m()
{
  int rate60m = 0;
  qlonglong const& one_hour_ago = QDateTime::currentDateTime().addSecs(-3600).toMSecsSinceEpoch () / 1000;

  // query the 60m rate
  m_->rate60m_query_.bindValue (":one_hour_ago", one_hour_ago);
  SQL_error_check (m_->rate60m_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
  m_->rate60m_query_.next ();
  rate60m = m_->rate60m_query_.value (0).toLongLong();
  return rate60m;
  //
}

double FoxLog::rate_last_n(int n)
{
  double rate_interval = 0;

  qlonglong const& secs_now = QDateTime::currentDateTime().toMSecsSinceEpoch () / 1000;

  // get last n or up to n
  m_->rate_n_query_.bindValue (":limitn", n);
  SQL_error_check (m_->rate_n_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));

  m_->rate_n_query_.next();
  if (!m_->rate_n_query_.isValid()) {
    LOG_ERROR(QString("rate_n result is not valid. Last error %1").arg(m_->rate_n_query_.lastError().text()));
    return 0.0;
  }
  // size / (time_now - time_of_first)
  m_->rate_n_query_.last();
  rate_interval = secs_now - m_->rate_n_query_.value (0).toLongLong ();
  if (rate_interval == 0) return 0.0;

  m_->rate_n_query_.first(); // count the records

  int size = 1;
  while (m_->rate_n_query_.next() && m_->rate_n_query_.isValid()) size++;
  return (size/rate_interval) * 3600;
}

namespace
{
  struct ADIF_field
  {
    explicit ADIF_field (QString const& name, QString const& value)
      : name_ {name}
      , value_ {value}
    {
    }

    QString name_;
    QString value_;
  };

  QTextStream& operator << (QTextStream& os, ADIF_field const& field)
  {
    if (field.value_.size ())
      {
        os << QString {"<%1:%2>%3 "}.arg (field.name_).arg (field.value_.size ()).arg (field.value_);
      }
    return os;
  }
}

void FoxLog::export_qsos (QTextStream& out) const
{
  out << "WSJT-X FT8 DXpedition Mode Fox Log\n<eoh>";

  SQL_error_check (m_->export_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
  auto record = m_->export_query_.record ();
  auto band_index = record.indexOf ("band");
  auto when_index = record.indexOf ("when");
  auto call_index = record.indexOf ("call");
  auto grid_index = record.indexOf ("grid");
  auto sent_index = record.indexOf ("report_sent");
  auto rcvd_index = record.indexOf ("report_rcvd");
  while (m_->export_query_.next ())
    {
      auto when = QDateTime::fromMSecsSinceEpoch (m_->export_query_.value (when_index).toULongLong () * 1000ull, Qt::UTC);
      out << '\n'
          << ADIF_field {"band", m_->export_query_.value (band_index).toString ()}
          << ADIF_field {"mode", "FT8"}
          << ADIF_field {"qso_date", when.toString ("yyyyMMdd")}
          << ADIF_field {"time_on", when.toString ("hhmmss")}
          << ADIF_field {"call", m_->export_query_.value (call_index).toString ()}
          << ADIF_field {"gridsquare", m_->export_query_.value (grid_index).toString ()}
          << ADIF_field {"rst_sent", m_->export_query_.value (sent_index).toString ()}
          << ADIF_field {"rst_rcvd", m_->export_query_.value (rcvd_index).toString ()}
          << ADIF_field {"station_callsign", m_->configuration_->my_callsign ()}
          << ADIF_field {"my_gridsquare", m_->configuration_->my_grid ()}
          << ADIF_field {"operator", m_->configuration_->opCall ()}
          << "<eor>";
    }
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
  out << endl;
#else
  out << Qt::endl;
#endif
}