commit 7342d916e1466e5cb95fb749391a16ecaeb3f501
parent 9102772f09695845876d76bbb14c7b08f999e057
Author: René Wagner <rwa@clttr.info>
Date: Wed, 21 Jun 2023 20:57:55 +0200
rework time-based grouping, add dayofweek and dayofmonth grouping
We now allow multiple time-based group params as comma-separated values
to the -g param.
Instead of assuming a Year/Month group when giving `month`, we now
group only by the given param. To achieve the previous behaviour
one must specify a combination of grouping params.
Diffstat:
4 files changed, 43 insertions(+), 27 deletions(-)
diff --git a/phosta b/phosta
@@ -18,7 +18,7 @@ use Data::Dumper;
$Getopt::Std::STANDARD_HELP_VERSION = 'true';
-my $VERSION = '1.2';
+my $VERSION = '1.3';
my $PROGRAM = 'phosta';
my $configfile = catfile(File::HomeDir->my_home, '.phosta.conf');
@@ -69,9 +69,13 @@ exit 0;
sub validate
{
- my @group_params = ('year', 'month', 'week', 'hour');
- !defined($opt_g) || grep /$opt_g/, @group_params or return 0;
-
+ my @group_params = ('year', 'month', 'week', 'dayofmonth', 'dayofweek', 'hour');
+ if (defined($opt_g)) {
+ foreach (split (/,/, $opt_g)) {
+ my $val = $_;
+ grep /$val/, @group_params or return 0;
+ }
+ }
my @select_params = qw(file maker model lensmaker lens aperture exposuretime iso focallength focallength35mm);
if (defined($opt_s)) {
foreach (split (/,/, $opt_s)) {
@@ -237,25 +241,34 @@ sub get_sql
my @grouparray;
my $orderlist = '';
my @wherearray;
- given ($opt_g) {
- when ('month') {
- $fieldlist = "IFNULL(strftime('%Y/%m', datetimeoriginal), '-') as month,";
- push @grouparray, "strftime('%Y/%m', datetimeoriginal)";
- }
- when ('week') {
- $fieldlist = "IFNULL(strftime('%Y/%W', datetimeoriginal), '-') as week,";
- push @grouparray, "strftime('%Y/%W', datetimeoriginal)";
- }
- when ('year') {
- $fieldlist = "IFNULL(strftime('%Y', datetimeoriginal), '-') as year,";
- push @grouparray, "strftime('%Y', datetimeoriginal)";
- }
- when ('hour') {
- $fieldlist = "IFNULL(strftime('%H', datetimeoriginal), '-') as hour,";
- push @grouparray, "strftime('%H', datetimeoriginal)";
+ foreach (split (/,/, $opt_g)) {
+ given ($_) {
+ when ('month') {
+ $fieldlist = $fieldlist . "IFNULL(strftime('%m', datetimeoriginal), '-') as month,";
+ push @grouparray, "strftime('%m', datetimeoriginal)";
+ }
+ when ('week') {
+ $fieldlist = $fieldlist . "IFNULL(strftime('%W', datetimeoriginal), '-') as week,";
+ push @grouparray, "strftime('%W', datetimeoriginal)";
+ }
+ when ('year') {
+ $fieldlist = $fieldlist . "IFNULL(strftime('%Y', datetimeoriginal), '-') as year,";
+ push @grouparray, "strftime('%Y', datetimeoriginal)";
+ }
+ when ('hour') {
+ $fieldlist = $fieldlist . "IFNULL(strftime('%H', datetimeoriginal), '-') as hour,";
+ push @grouparray, "strftime('%H', datetimeoriginal)";
+ }
+ when ('dayofmonth') {
+ $fieldlist = $fieldlist . "IFNULL(strftime('%d', datetimeoriginal), '-') as dayofmonth,";
+ push @grouparray, "strftime('%d', datetimeoriginal)";
+ }
+ when ('dayofweek') {
+ $fieldlist = $fieldlist . "substr('SunMonTueWedThuFriSat-', 1+ 3 * IFNULL(strftime('%w', datetimeoriginal), 7), 3) as dayofweek,";
+ push @grouparray, "strftime('%w', datetimeoriginal)";
+ }
}
}
-
if (defined($opt_s)) {
$fieldlist = $fieldlist . $opt_s. ', ';
push @grouparray, $opt_s;
@@ -456,7 +469,7 @@ sub main::HELP_MESSAGE
say '';
say 'stats querying:';
say ' -g <period> : group by a time period, defaults to total (which means no grouping by period)';
- say ' allowed values: year, month, week, hour';
+ say ' allowed values: year, month, week, dayofmonth, dayofweek, hour';
say ' -s <fields> : specify the information you want to select, defaults to none (just show number of images)';
say ' allowed values: file, maker, model, lensmaker, lens, aperture, exposuretime, iso, focallength, focallength35mm';
say ' multiple fields should be listed comma-separated without whitespaces';
diff --git a/phosta-completion.sh b/phosta-completion.sh
@@ -4,12 +4,12 @@ _phosta_completions()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
+ local suggestions=""
local prefix=""
if [[ ${cur} =~ "," ]]; then
prefix="${cur%,*},"
fi
- local suggestions=""
case "${prev}" in
"-o")
suggestions=($(compgen -W "${prefix}year ${prefix}month ${prefix}week ${prefix}hour ${prefix}file ${prefix}maker ${prefix}model ${prefix}lensmaker ${prefix}lens ${prefix}aperture ${prefix}exposuretime ${prefix}iso ${prefix}focallength ${prefix}focallength35mm" "${cur}"))
@@ -18,13 +18,13 @@ _phosta_completions()
suggestions=($(compgen -W "${prefix}file ${prefix}maker ${prefix}model ${prefix}lensmaker ${prefix}lens ${prefix}aperture ${prefix}exposuretime ${prefix}iso ${prefix}focallength ${prefix}focallength35mm" "${cur}"))
;;
"-g")
- suggestions=($(compgen -W "${prefix}year ${prefix}month ${prefix}week ${prefix}hour" "${cur}"))
+ suggestions=($(compgen -W "${prefix}year ${prefix}month ${prefix}week ${prefix}dayofmonth ${prefix}dayofweek ${prefix}hour" "${cur}"))
;;
"-T")
suggestions=($(compgen -W "auto on off" "${cur}"))
;;
"-n")
- suggestions=($(compgen -W "10 20 50 100" "${cur}"))
+ suggestions=($(compgen -W "1 3 5 10 20 50 100" "${cur}"))
;;
"-W")
suggestions=($(compgen -W "80 100 120" "${cur}"))
@@ -42,6 +42,9 @@ _phosta_completions()
"-D")
suggestions=($(compgen -f "${cur}"))
;;
+ *)
+ suggestions=($(compgen -W "-o -s -g -T -n -W -t -p -D -c -E -e -f -n"))
+ ;;
esac
if [ "${#suggestions[@]}" == "1" ]; then
diff --git a/phosta-examples.7.scd b/phosta-examples.7.scd
@@ -17,7 +17,7 @@ phosta -E jpg,jpeg,tiff -D ~/Documents/stats.db -p ~/Pictures
## QUERY DATA
- show image count by _model_ in 2019 grouped by month, sorted by newest month first
```
-phosta -s model -g month -t 20190101-20121231 -o month,count
+phosta -s model -g month -t 20190101-20191231 -o month,count
```
- show image count by _model_ for _make_ FUJIFILM grouped by year, ordered by year & count descending
diff --git a/phosta.1.scd b/phosta.1.scd
@@ -84,7 +84,7 @@ Allowed values: _file_, _maker_, _model_, _lensmake_, _lens_, _aperture_, _expos
*-g* <period>
Group result set by a time period.++
Default: total (which means no grouping by period)++
-Allowed values: _year_, _month_, _week_, _hour_
+Allowed values: _year_, _month_, _week_, _dayofmonth_, _dayofweek_, _hour_
*-n* <number>
Limit the resultset to <number> of lines.