Skip to content

Instantly share code, notes, and snippets.

@Liareth
Last active November 24, 2017 12:17
Show Gist options
  • Save Liareth/e2d2cdc77da67f0407b11a83de20123d to your computer and use it in GitHub Desktop.
Save Liareth/e2d2cdc77da67f0407b11a83de20123d to your computer and use it in GitHub Desktop.
void Metrics::Update(ViewPtr<Tasks> tasks)
{
for (auto& resampler : m_resamplers)
{
ResamplerData* data = resampler.second.get();
if (data->m_isWorkingAsynchronously)
{
continue;
}
using namespace std::chrono;
system_clock::time_point now = system_clock::now();
const auto passed = duration_cast<nanoseconds>(now - data->m_lastFlush);
if (passed >= data->m_interval)
{
std::swap(data->m_unsampled, data->m_processing);
data->m_isWorkingAsynchronously = true;
tasks->QueueOnAsyncThread(
[this, data, now, tasks]
{
auto targetTimepoint = now;
if (data->m_interval != seconds(0))
{
// With a target interval of 1000ms, if we pushed at 1200ms, this would set the last flush to be 1000ms.
const auto lastFlushAsNs = duration_cast<nanoseconds>(now.time_since_epoch());
const auto targetTimestamp = (lastFlushAsNs / data->m_interval) * data->m_interval;
targetTimepoint -= lastFlushAsNs - targetTimestamp;
}
auto resampledData = data->m_resampler(std::move(data->m_processing));
for (auto& entry : resampledData)
{
entry.m_timestamp = targetTimepoint;
}
data->m_lastFlush = targetTimepoint;
data->m_processing.clear();
tasks->QueueOnMainThread(
[this, resampledData = std::move(resampledData)]() mutable
{
this->Push(std::move(resampledData));
}
);
data->m_isWorkingAsynchronously = false;
}
);
}
}
for (const auto& callback : m_callbacks)
{
callback.second(m_data);
}
m_data.clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment