status subscription: extend output#1312
Conversation
arogge
left a comment
There was a problem hiding this comment.
Pretty good, but we can probably do better :)
core/src/cats/sql_list.cc
Outdated
| if (verbose) { | ||
| Mmsg(errmsg, _("Query failed: %s\n"), sql_strerror()); | ||
| sendit->Decoration(errmsg); | ||
| } |
There was a problem hiding this comment.
This is a functional change that may not be desired.
The caller could call us with verbose set to false, resulting in errmsg still being set and using other means of displaying the message to the user.
core/src/lib/crypto_openssl.cc
Outdated
| std::string compute_hash(const std::string& unhashed, | ||
| const std::string& digestname) | ||
| { | ||
| std::stringstream string; |
There was a problem hiding this comment.
please don't name variables like a type, especially don't name a variable like a different type (i.e. this is a stringstream and you named it string)
core/src/lib/crypto_openssl.cc
Outdated
| EVP_MD_CTX_destroy(context); | ||
| } | ||
| } | ||
| return string.str(); |
There was a problem hiding this comment.
so on any error, you'll just get an empty string.
That doesn't feel like great error handling.
Maybe we should use an std::optional to handle errors nicer.
core/src/lib/crypto_openssl.cc
Outdated
| unsigned char hash[EVP_MAX_MD_SIZE]; | ||
| unsigned int lengthOfHash = 0; | ||
| if (EVP_DigestFinal_ex(context, hash, &lengthOfHash)) { | ||
| string << "{" << digestname << "}"; |
There was a problem hiding this comment.
we should probably get the digest name back from OpenSSL, so the string value is fixed, even if you pass digestname as "ShA512" or something like that.
core/src/lib/crypto_openssl.cc
Outdated
| string << "{" << digestname << "}"; | ||
| string << std::hex << std::setw(2) << std::setfill('0'); | ||
| for (unsigned int i = 0; i < lengthOfHash; ++i) { | ||
| string << (int)hash[i]; |
There was a problem hiding this comment.
we should prefer C++-style casts like static_cast<int>() in this case over C style casts to avoid casting const away accidentally.
core/src/dird/ua_status.cc
Outdated
| PoolMem subscriptions(PM_MESSAGE); | ||
| PoolMem query(PM_MESSAGE); | ||
| OutputFormatter output_text | ||
| = OutputFormatter(pm_append, (void*)&subscriptions, nullptr, nullptr); |
There was a problem hiding this comment.
casts to void* are implicit and c-style casts are evil.
| = OutputFormatter(pm_append, (void*)&subscriptions, nullptr, nullptr); | |
| = OutputFormatter(pm_append, &subscriptions, nullptr, nullptr); |
core/src/dird/ua_status.cc
Outdated
| return true; | ||
| } | ||
|
|
||
| std::string get_subscription_status_signature_source_text(UaContext* ua, |
There was a problem hiding this comment.
you're only using ua->db, so maybe consider BareosDb* instead of UaContext* or maybe BareosDb&.
There was a problem hiding this comment.
I'm also using ua->jcr. So either I leave it like this, or I've to add another parameter jcr. I opt for leave it like this.
core/src/cats/sql_list.cc
Outdated
| description, verbose); | ||
| } | ||
|
|
||
| bool BareosDb::ListOneRowSqlQuery(JobControlRecord* jcr, |
There was a problem hiding this comment.
instead of adding a whole new function, we could consider adding another defaulted parameter to ListSqlQuery() that will make it use ObjectStart()/ObjectEnd() if SqlNumRows() == 1.
Optimal would be a custom enum class with reasonable symbolic names so that you're passing something like CollapseMode::Collapse and default it to CollapseMode::NoCollapse instead of just adding a bool where you have to look up what it does.
core/src/dird/ua_status.cc
Outdated
| std::string signature_source | ||
| = get_subscription_status_signature_source_text(ua, now); | ||
| std::string hash = compute_hash(signature_source.c_str()); | ||
| ua->send->ObjectKeyValue("signature", "Signature: ", hash.c_str(), "%s\n"); |
There was a problem hiding this comment.
I really wouldn't call it a signature, it is just a hash or checksum, so we should probably name it as such.
There was a problem hiding this comment.
I named it Checksum now.
core/src/dird/ua_status.cc
Outdated
| ua->send->ObjectKeyValue("binary-info", | ||
| "Binary info: ", kBareosVersionStrings.BinaryInfo, | ||
| "%s\n"); | ||
| ua->send->ObjectKeyValue("date", "Date: ", now, "%s\n"); |
There was a problem hiding this comment.
It is called date, but contains the current time.
Maybe something like "report-time" and "Report time:" is more reasonable.
|
I addressed all mentioned suggestions. Without new feedback, the only I maybe can address is replacing |
arogge
left a comment
There was a problem hiding this comment.
I made a few observations, but no objections. Looks good to me now!
core/src/dird/ua_status.cc
Outdated
| std::optional<std::string> checksum = compute_hash(checksum_source.c_str()); | ||
| if (checksum) { | ||
| ua->send->ObjectKeyValue("checksum", | ||
| "Checksum: ", checksum.value().c_str(), "%s\n"); | ||
| } |
There was a problem hiding this comment.
- i would probably have used
autoinstead ofstd::optional<std::string>here. compute_hash()takes aconst std::string&as first parameter. You're passing the underlyingconst char*of your existingstd::stringto it, which will lead to an implicit conversion to std::string() which implies another copy. Just passing thestd::stringwould be better- there are two ways to get the value out of a
std::optional: callingvalue()oroperator*/operator->. Withvalue()you get a check that there is a value in your optional (and an exception if there is none), while with the operators behaviour is undefined if there is no value. As you checked that there is a value, you can safely use the operators, simplifyingchecksum.value().c_str()to justchecksum->c_str().
Instead of the command ACL "configure", unrestriced access to Clients, Jobs and Filesets is required, as these are the information that the command really uses.
This can result is much better read- and parseable results.
6b752d9 to
be7080d
Compare
In case the calculation of subscription will change in the future, adding version information will help to determine how the subscription counting is done at that time.
2614462 to
592c5db
Compare
While working on #1280 some improvements to the
status subscriptioncommand did come up and are now handled in this separate PR.Please check
If you have any questions or problems, please give a comment in the PR.
Helpful documentation and best practices
Checklist for the reviewer of the PR (will be processed by the Bareos team)
General
Separate commit for CHANGELOG.md ("update CHANGELOG.md"). The PR number is correct.Source code quality
bareos-check-sources --since-mergedoes not report any problemsTests