From: Eric Bollengier Date: Thu, 17 Mar 2022 15:22:14 +0000 (+0100) Subject: Modify list metadata to return only the last occurence of the data by default X-Git-Tag: Beta-15.0.0~619 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19ea027b9d763e84f6f0971038c4b7d4cc52a29f;p=thirdparty%2Fbacula.git Modify list metadata to return only the last occurence of the data by default --- diff --git a/bacula/src/cats/cats.h b/bacula/src/cats/cats.h index 6974f99e1..17153255c 100644 --- a/bacula/src/cats/cats.h +++ b/bacula/src/cats/cats.h @@ -656,6 +656,7 @@ public: int order; int orderby; // 0: JobId, FileIndex 1: EmailTime bool all; + bool alljobs; // query all jobs, else return only the last version char *JobIds; char Id[MAX_SEARCH_LENGTH]; @@ -680,7 +681,7 @@ public: char errmsg[MAX_NAME_LENGTH]; META_DBR(): MinSize(-1), MaxSize(-1), HasAttachment(-1), isDraft(-1), isRead(-1), isInline(-1), offset(0), limit(512), - order(0), orderby(0), all(false) + order(0), orderby(0), all(false),alljobs(false) { JobIds = NULL; *Id = *Tenant = *Owner = 0; diff --git a/bacula/src/cats/sql_list.c b/bacula/src/cats/sql_list.c index d0454b87a..2fa7b33cf 100644 --- a/bacula/src/cats/sql_list.c +++ b/bacula/src/cats/sql_list.c @@ -1301,7 +1301,14 @@ void BDB::bdb_list_metadata_owner_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HA POOL_MEM esc(PM_MESSAGE), tmp(PM_MESSAGE), where(PM_MESSAGE), join(PM_MESSAGE); bdb_lock(); + /* We use the command line to generate the SQL query with what the user + * want to filter + */ meta_r->create_db_filter(jcr, this, where.handle()); + + /* We also apply ACL via SQL (here on the Client name and the Job name) if we + * are in a restricted console + */ const char *where_filter = get_acls(DB_ACL_BIT(DB_ACL_JOB) | DB_ACL_BIT(DB_ACL_CLIENT), strcmp(where.c_str(), "") == 0); @@ -1373,7 +1380,7 @@ void BDB::bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER bdb_list_metadata_owner_records(jcr, meta_r, sendit, ctx, type); return; } - + const char *k1=""; // set to Attachment if it is appropriate POOL_MEM esc(PM_MESSAGE), tmp(PM_MESSAGE), where(PM_MESSAGE), join(PM_MESSAGE); bdb_lock(); //TODO add ACL part @@ -1394,6 +1401,7 @@ void BDB::bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER } if (strcmp(meta_r->Type, "Attachment") == 0) { + k1 = "Attachment"; pm_strcat(join, " JOIN MetaEmail ON (EmailId = AttachmentEmailId AND MetaEmail.JobId = MetaAttachment.JobId) "); } @@ -1404,6 +1412,23 @@ void BDB::bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER if (join_filter && *join_filter) { pm_strcat(join, join_filter); } + + if (!meta_r->alljobs) { + /* The idea is to get only the last occurence of each email. The key differentiator is + * the EmailId, and we use the Job table via the StartTime to select the latest version + * + * should give + * AND MetaEmail.JobId = (SELECT JobId FROM Job JOIN MetaEmail AS B USING (JobId) + * WHERE MetaEmail.EmailId=B.EmailId ORDER BY StartTime DESC LIMIT 1 + * or + * AND MetaAttachment.JobId = (SELECT JobId FROM Job JOIN MetaAttachment AS B USING (JobId) + * WHERE MetaAttachment.AttachmentEmailId=B.AttachmentEmailId ORDER BY StartTime DESC LIMIT 1 + */ + Mmsg(tmp, + " AND Meta%s.JobId = (SELECT JobId FROM Job JOIN Meta%s AS B USING (JobId) %s WHERE Meta%s.%sEmailId=B.%sEmailId %s ORDER BY StartTime DESC LIMIT 1) ", + meta_r->Type, meta_r->Type, join_filter, meta_r->Type, k1, k1, where_filter); + pm_strcat(where, tmp.c_str()); + } if (meta_r->orderby == 1) { Mmsg(tmp, " ORDER BY EmailTime %s ", meta_r->order ? "DESC" : "ASC"); diff --git a/bacula/src/dird/ua_cmds.c b/bacula/src/dird/ua_cmds.c index fc9859bdc..e790cebe7 100644 --- a/bacula/src/dird/ua_cmds.c +++ b/bacula/src/dird/ua_cmds.c @@ -139,7 +139,7 @@ static struct cmdstruct commands[] = { /* C "\t\t source= | code= | type= ]\n" "\tobjects [jobid= client= type= | category= | status= | limit= | order= ]\n" "\tmetadata type= tenant= [owner=]\n" - "\t [jobid= client= order= limit= orderby=