summaryrefslogtreecommitdiff
path: root/src/serverinfo.cpp
blob: 4d99f6db1da6bd749f9ca07791e00e30d34f73e3 (plain)
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
#include "serverinfo.h"
#include "log.h"

using namespace MOBase;

const std::size_t MaxDownloadCount = 5;

ServerInfo::ServerInfo() : ServerInfo({}, false, {}, 0, {}) {}

ServerInfo::ServerInfo(QString name, bool premium, QDate last, int preferred,
                       SpeedList lastDownloads)
    : m_name(std::move(name)), m_premium(premium), m_lastSeen(std::move(last)),
      m_preferred(preferred), m_lastDownloads(std::move(lastDownloads))
{
  if (m_lastDownloads.size() > MaxDownloadCount) {
    m_lastDownloads.resize(MaxDownloadCount);
  }
}

const QString& ServerInfo::name() const
{
  return m_name;
}

bool ServerInfo::isPremium() const
{
  return m_premium;
}

void ServerInfo::setPremium(bool b)
{
  m_premium = b;
}

const QDate& ServerInfo::lastSeen() const
{
  return m_lastSeen;
}

void ServerInfo::updateLastSeen()
{
  m_lastSeen = QDate::currentDate();
}

int ServerInfo::preferred() const
{
  return m_preferred;
}

void ServerInfo::setPreferred(int i)
{
  m_preferred = i;
}

const ServerInfo::SpeedList& ServerInfo::lastDownloads() const
{
  return m_lastDownloads;
}

int ServerInfo::averageSpeed() const
{
  int count = 0;
  int total = 0;

  for (const auto& s : m_lastDownloads) {
    if (s > 0) {
      ++count;
      total += s;
    }
  }

  if (count > 0) {
    return static_cast<double>(total) / count;
  }

  return 0;
}

void ServerInfo::addDownload(int bytesPerSecond)
{
  if (bytesPerSecond <= 0) {
    log::error("trying to add download with {} B/s to server '{}'; ignoring",
               bytesPerSecond, m_name);

    return;
  }

  if (m_lastDownloads.size() == MaxDownloadCount) {
    std::rotate(m_lastDownloads.begin(), m_lastDownloads.begin() + 1,
                m_lastDownloads.end());

    m_lastDownloads.back() = bytesPerSecond;
  } else {
    m_lastDownloads.push_back(bytesPerSecond);
  }

  log::debug("added download at {} B/s to server '{}'", bytesPerSecond, m_name);
}

void ServerList::add(ServerInfo s)
{
  m_servers.push_back(std::move(s));

  std::sort(m_servers.begin(), m_servers.end(), [](auto&& a, auto&& b) {
    return (a.preferred() < b.preferred());
  });
}

ServerList::iterator ServerList::begin()
{
  return m_servers.begin();
}

ServerList::const_iterator ServerList::begin() const
{
  return m_servers.begin();
}

ServerList::iterator ServerList::end()
{
  return m_servers.end();
}

ServerList::const_iterator ServerList::end() const
{
  return m_servers.end();
}

std::size_t ServerList::size() const
{
  return m_servers.size();
}

bool ServerList::empty() const
{
  return m_servers.empty();
}

ServerList::container ServerList::getPreferred() const
{
  container v;

  for (const auto& server : m_servers) {
    if (server.preferred() > 0) {
      v.push_back(server);
    }
  }

  return v;
}

void ServerList::cleanup()
{
  QDate now = QDate::currentDate();

  for (auto itor = m_servers.begin(); itor != m_servers.end();) {
    const QDate lastSeen = itor->lastSeen();

    if (lastSeen.daysTo(now) > 30) {
      log::debug("removing server {} since it hasn't been available for downloads "
                 "in over a month",
                 itor->name());

      itor = m_servers.erase(itor);
    } else {
      ++itor;
    }
  }
}