diff --git a/include/class-MailboxSizeAPI.php b/include/class-MailboxSizeAPI.php index 4b9e9ab..0e3714c 100644 --- a/include/class-MailboxSizeAPI.php +++ b/include/class-MailboxSizeAPI.php @@ -1,89 +1,117 @@ . // load dependend traits class_exists( 'MailboxAPI' ); /** * Methods for a MailboxSizeAPI class */ trait MailboxSizeAPITrait { - use MailboxAPITrait; - /** * Select the MAX Mailbox quota date * * @return self */ public function selectMaxMailboxSizeDate() { return $this->select( 'MAX( mailboxsize_date ) AS max_mailboxsize_date' ) ->groupBy( 'mailbox_ID' ); } + /** + * Limit to a specific date interval + * + * @param $one DateTime + * @param $two DateTime + * @return self + */ + public function whereMailboxSizeBetweenDates( $one, $two ) { + $from = $one->format( 'Y-m-d H:i:s' ); + $to = $two->format( 'Y-m-d H:i:s' ); + return $this->where( sprintf( + "mailboxsize_date BETWEEN '%s' AND '%s'", + $from, + $to + ) ); + } + + /** + * Limit to the last 12 months + * + * @return self + */ + public function whereMailboxSizeInLatestYear() { + $now = new DateTime(); + $past = new DateTime(); + $past->sub( new DateInterval( 'P1Y' ) ); + return $this->whereMailboxSizeBetweenDates( $past, $now ); + } + /** * Assure that this is only the more updated Mailbox quota * * @return self */ public function whereMailboxSizeIsLast() { // subquery with a maximum constraint $max = ( new MailboxSizeAPI( null, false ) ) ->fromCustom( DB::instance()->getTable( 'mailboxsize', 'mailboxsize_sub' ) ) ->equals( 'mailboxsize.mailbox_ID', 'mailboxsize_sub.mailbox_ID' ) ->selectMaxMailboxSizeDate() ->getQuery(); return $this->where( sprintf( 'mailboxsize_date = (%s)', $max ) ); } } /** * MailboxSize API */ class MailboxSizeAPI extends Query { use MailboxSizeAPITrait; + use MailboxAPITrait; /** * Univoque column name to the Mailbox ID */ protected $MAILBOX_ID = 'mailboxsize.mailbox_ID'; /** * Constructor * * @param object $db Database * @param mixed $from Set to false to avoid to use the default FROM */ public function __construct( $db = null, $from = true ) { // set database and class name parent::__construct( $db, MailboxSize::class ); /** * Set database table (sometime the standard alias it's not useful) * * See MailboxSizeAPI class. */ if( $from ) { $this->from( MailboxSize::T ); } } } diff --git a/template/mailbox-stats-size-chart.php b/template/mailbox-stats-size-chart.php new file mode 100644 index 0000000..946ed85 --- /dev/null +++ b/template/mailbox-stats-size-chart.php @@ -0,0 +1,62 @@ +. + +/* + * This is the template for a Mailbox size chart. + * + * Called from: + * template/mailbox-stats.php + * + * Available variables: + * $mailbox object + */ + +// avoid to be load directly +defined( 'BOZ_PHP' ) or die; + +// query one year of data +$stats = ( new MailboxSizeAPI() ) + ->whereMailbox( $mailbox ) + ->whereMailboxSizeInLatestYear() + ->queryGenerator(); + +// prepare the chart +$chart_args = [ + // label used for the y axis + 'ylabel' => __( "Size" ), + + // label used for each y axis data + 'ylabel-format' => function( $bytes ) { + return human_filesize( $bytes ); + }, +]; + +// prepare the chart +$chart = new ASCIILineChart(); +foreach( $stats as $stat ) { + $chart->add( $stat->getMailboxSizeDate(), $stat->getMailboxSizeBytes() ); +} + +// sort by date +$chart->sort(); +?> + + +

+ +
render( $chart_args ) ?>
+ diff --git a/template/mailbox-stats.php b/template/mailbox-stats.php index 5ff9934..4eceda3 100644 --- a/template/mailbox-stats.php +++ b/template/mailbox-stats.php @@ -1,85 +1,91 @@ . /* * This is the template for some mailbox stats * * Called from: * template/mailbox.php * * Available variables: * $mailbox object * $plan object */ // avoid to be load directly defined( 'BOZ_PHP' ) or die; // calculate the remaining Mailbox quota percentage $remaining_quota_percentage = null; if( $plan ) { $remaining_quota_percentage = Plan::percentage( $mailbox->getMailboxLastSizeBytes(), $plan->getPlanMailboxQuota() ); if( $remaining_quota_percentage ) { $remaining_quota_percentage = 100 - $remaining_quota_percentage; } } ?>

getMailboxLastSizeBytes() !== null ): ?> getPlanMailboxQuota() ): ?>
getMailboxLastSizeBytes() ) ?>
getPlanMailboxQuota() ) ?>
%
+ + $mailbox, + ] ) ?> + + diff --git a/www/content/style.css b/www/content/style.css index e69de29..392493b 100644 --- a/www/content/style.css +++ b/www/content/style.css @@ -0,0 +1,4 @@ +.ascii-chart { + overflow-y:auto; + line-height:1; +}