Skip to content

Conversation

@SharonIV0x86
Copy link
Contributor

@SharonIV0x86 SharonIV0x86 commented Mar 7, 2025

This closes #2817.

This PR introduces a new parameter to the LASTSAVE command to return the time of last save to disk in a human readable format.

Usage
LASTSAVE ISO8601
LASTSAVE iso8601

LASTSAVE also works for backward compatibility.

127.0.0.1:6666> LASTSAVE
(integer) 1741363334
127.0.0.1:6666> LASTSAVE iso8601
"2025-03-07T21:32:14+0530"
127.0.0.1:6666> LASTSAVE ISO8601
"2025-03-07T21:32:14+0530"
127.0.0.1:6666> 

@SharonIV0x86 SharonIV0x86 changed the title enhance: Added iso8601 time format parameter to LASTSAVE command. feat: Added iso8601 time format parameter to LASTSAVE command. Mar 7, 2025
return {Status::RedisParseErr, "unknown extra subcommand"};
}
if (args.size() == 2) {
std::string fmt_arg = args[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

You can try to use EqualICase (common->string_util.h).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can try to use EqualICase (common->string_util.h).

Initially i came across parser.EatEqICase() but then i dropped the idea as i didnt know its correct usage so i though i'll take some feedback on its usage.

Thanks for the review i'll use EqualICase as it seems a better fit in this case.

Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

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

It doesn't seem to be a Redis feature. So I'm wondering if it will be useful in production envs?

@SharonIV0x86
Copy link
Contributor Author

It doesn't seem to be a Redis feature. So I'm wondering if it will be useful in production envs?

You're right this isn't a redis feature.

But instead of converting the UNIX timestamp manually, users can directly retrieve an easily interpretable format.

@PragmaTwice
Copy link
Member

You're right this isn't a redis feature.

But instead of converting the UNIX timestamp manually, users can directly retrieve an easily interpretable format.

I've checked the issue you opened. I'm fine with this change.

public:
Status Parse(const std::vector<std::string> &args) override {
if (args.size() > 2) {
return {Status::RedisParseErr, "unknown extra subcommand"};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return {Status::RedisParseErr, "unknown extra subcommand"};
return {Status::RedisParseErr, "unknown extra arguments"};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for review, i was just working on the changes requested by torwig right now, ill make this change as well.

if (fmt_arg == "iso8601") {
format_spec_ = true;
} else {
return {Status::RedisParseErr, "unknown subcommand"};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return {Status::RedisParseErr, "unknown subcommand"};
return {Status::RedisParseErr, "unknown arguments"};

Comment on lines 1016 to 1018
std::string fmt_arg = args[1];
std::transform(fmt_arg.begin(), fmt_arg.end(), fmt_arg.begin(), ::tolower);
if (fmt_arg == "iso8601") {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
std::string fmt_arg = args[1];
std::transform(fmt_arg.begin(), fmt_arg.end(), fmt_arg.begin(), ::tolower);
if (fmt_arg == "iso8601") {
if (util::EqualICase(args[1], "iso8601")) {

*output = redis::Integer(unix_sec);
if (format_spec_) {
time_t raw_time = static_cast<time_t>(unix_sec);
struct tm *local_time = localtime(&raw_time);
Copy link
Member

Choose a reason for hiding this comment

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

We can use the thread-safe version localtime_r.

strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", local_time);
*output = redis::BulkString(buf);
} else {
*output = redis::BulkString("Error converting time");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
*output = redis::BulkString("Error converting time");
*output = redis::BulkString("Failed to convert timestamp to local time");

@PragmaTwice PragmaTwice changed the title feat: Added iso8601 time format parameter to LASTSAVE command. feat(cmd): add iso8601 time format parameter to LASTSAVE command Mar 10, 2025
Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

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

Looks good. Could you add a go test case for your changes?

@SharonIV0x86
Copy link
Contributor Author

Looks good. Could you add a go test case for your changes?

I am a bit confused about where exactly to add the test for LASTSAVE command, i guess it will make sense to add it in the gocase/unit/server/poll_updates_test.go as the LASTSAVE command is listed under the server section in the docs?

@PragmaTwice
Copy link
Member

You can add a new go source file under tests/gocase/unit/server/.

Update src/commands/cmd_server.cc

Co-authored-by: hulk <[email protected]>

Update src/commands/cmd_server.cc

Update src/commands/cmd_server.cc

Update src/commands/cmd_server.cc

Co-authored-by: Twice <[email protected]>

Update src/commands/cmd_server.cc

Co-authored-by: Twice <[email protected]>
@git-hulk
Copy link
Member

Looks good. @SharonIV0x86 You could use the gofmt to format Go files to pass the CI.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 50%)

See analysis details on SonarQube Cloud

@PragmaTwice PragmaTwice merged commit b8db0e7 into apache:unstable Mar 11, 2025
35 of 36 checks passed
@SharonIV0x86 SharonIV0x86 deleted the enhance/LASTSAVE branch March 13, 2025 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LASTSAVE Command to Return Human-Readable Timestamp.

4 participants