Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format mgr:5min and mgr:60min as YAML #1796

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/mgr/IntervalAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* DEBUG: section 16 Cache Manager API */

#include "squid.h"
#include "base/PackableStream.h"
#include "base/TextException.h"
#include "ipc/Messages.h"
#include "ipc/TypedMsgHdr.h"
Expand All @@ -17,8 +18,10 @@
#include "Store.h"
#include "tools.h"

#include <iosfwd>

void GetAvgStat(Mgr::IntervalActionData& stats, int minutes, int hours);
void DumpAvgStat(Mgr::IntervalActionData& stats, StoreEntry* sentry);
void DumpAvgStat(Mgr::IntervalActionData& stats, std::ostream&);

Mgr::IntervalActionData::IntervalActionData()
{
Expand Down Expand Up @@ -149,7 +152,8 @@ Mgr::IntervalAction::dump(StoreEntry* entry)
{
debugs(16, 5, MYNAME);
Must(entry != nullptr);
DumpAvgStat(data, entry);
PackableStream yaml(*entry);
DumpAvgStat(data, yaml);
}

void
Expand Down
264 changes: 103 additions & 161 deletions src/stat.cc

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/tests/stub_libtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "tests/STUB.h"

#include "time/Engine.h"
#include "time/YamlDateTime.h"

#include <iosfwd>
void Time::Engine::tick() STUB

#include "time/gadgets.h"
Expand All @@ -33,5 +36,6 @@ const char *FormatRfc1123(time_t) STUB_RETVAL("")
time_t ParseRfc1123(const char *) STUB_RETVAL(0)
const char *FormatStrf(time_t) STUB_RETVAL("")
const char *FormatHttpd(time_t) STUB_RETVAL("")
void YamlDateTime::print(std::ostream &) const STUB
}

2 changes: 2 additions & 0 deletions src/time/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ noinst_LTLIBRARIES = libtime.la
libtime_la_SOURCES = \
Engine.cc \
Engine.h \
YamlDateTime.cc \
YamlDateTime.h \
forward.h \
gadgets.cc \
gadgets.h \
Expand Down
24 changes: 24 additions & 0 deletions src/time/YamlDateTime.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 1996-2023 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/

#include "squid.h"
#include "YamlDateTime.h"

#include <ctime>
#include <iomanip>
#include <ostream>

void
Time::YamlDateTime::print(std::ostream &os) const
{
// need to add fractions and timezone on top of this
static const char *yaml_time_format = "%Y-%m-%d %H:%M:%S";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see why we should name this format, but if you insist on naming it, please do not make the variable static and modifiable (there is no reason for any of that).

Suggested change
static const char *yaml_time_format = "%Y-%m-%d %H:%M:%S";
const auto yaml_time_format = "%Y-%m-%d %H:%M:%S";

const auto tm = gmtime(&tv_.tv_sec);
os << std::put_time(tm, yaml_time_format);
os << '.' << std::setw(2) << (tv_.tv_usec / 10000) << 'Z';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why limit time to two digits?? Debugging often requires higher precision. Squid does not use two digits in most contexts IIRC. The chosen format does not specify precision AFAICT:

(\.[0-9]*[1-9])? # (fraction)

Please use all available digits.

}
38 changes: 38 additions & 0 deletions src/time/YamlDateTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 1996-2023 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/

#ifndef SQUID_SRC_TIME_YAMLDATETIME_H
#define SQUID_SRC_TIME_YAMLDATETIME_H

#include <iosfwd>
#include <sys/time.h>

namespace Time {

/// Output onto an ostream a yaml-formatted datetime string (UTC)
/// see https://yaml.org/type/timestamp.html
class YamlDateTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to put YamlDateTime inside Time namespace? The class name already has "Time". And Yaml. And Date. Why pick Time out of those three? The namespace itself is highly questionable. I would keep this class outside that namespace.

{
public:
YamlDateTime(const struct timeval &tv) : tv_(tv) {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid implicit conversions by default:

Suggested change
YamlDateTime(const struct timeval &tv) : tv_(tv) {};
explicit YamlDateTime(const struct timeval &tv): tv_(tv) {};

void print(std::ostream &) const;

private:
const struct timeval tv_;
};

} // namespace Time

inline auto &
operator<<(std::ostream &os, const Time::YamlDateTime &dt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foo::X operators should be declared in Foo. See commit 25ecffe.

{
dt.print(os);
return os;
}

#endif /* SQUID_SRC_TIME_YAMLDATETIME_H */
1 change: 1 addition & 0 deletions src/time/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Time
{

class Engine;
class YamlDateTime;

} // namespace Time

Expand Down