Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

SOLVED cPanel plugin - add entries to statistics

Discussion in 'cPanel Developers' started by CloudNexus, Jun 11, 2018.

  1. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    Hi there,

    I'm starting to grow fond of the way to develop for cPanel! Now that I'm slowly getting the hang of it I'd like to create some UI that will help the end-user use my plugin. Part of this is custom pages in the cPanel dashboard (this is done), but the second part is adding to the "statistics" sidebar of the user on his cPanel start page.

    I'd like to add a number of extra 'counters' there for some resources that my plugin is using (similar to MySQL). Is there a way I can extend this statistics template without changing the core template? I want it to integrate as neatly as possible.

    I see it's using the angular statisticsController to foreach through a list of ::statistics, which I suppose are apps and/or packages that toggle this behaviour. How can I added my 'custom' app statistics to this list?
     
  2. cPanelKenneth

    cPanelKenneth cPanel Development
    Staff Member

    Joined:
    Apr 7, 2006
    Messages:
    4,563
    Likes Received:
    43
    Trophy Points:
    308
    cPanel Access Level:
    Root Administrator
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    You guys are awesome, seriously.

    I do find it (sometimes) hard to find the right information in your developers documentation, but I believe this is being worked on! Thanks so much and thanks again!
     
  4. cPanelKenneth

    cPanelKenneth cPanel Development
    Staff Member

    Joined:
    Apr 7, 2006
    Messages:
    4,563
    Likes Received:
    43
    Trophy Points:
    308
    cPanel Access Level:
    Root Administrator
    Thank you for the praise :) Yes we are working on improving our documentation. Its navigation and discoverability are two areas of concern, especially with our developer documentation.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    I've got this working properly now, but only one last thing I found:

    When you set the "before" or "after", CPanel's api ([server]:2083/cpsess[number]/execute/ResourceUsage/get_usages) returns all of the statistics items in the correct order (the json order is correct), so the API adheres to the given order of things.

    However, on the front-end, the angular code that is used to generate the actual items, does not adhere to the order in the JSON.

    E.g. With my plugin set "before" mysql_databases it renders before the MySQL disk stats (cachedmysqldiskusage). When I set to to "After" mysql_databases, it renders at the start of the list.

    Is this an issue on my side? I am thinking its not (because the json is in the correct order, when it comes from the cpanel api). Is this a bug?
     
  6. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,802
    Likes Received:
    1,895
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hi @CloudNexus,

    Can you post the specific code you are using for the custom module so we can attempt to reproduce it on a test environment?

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    Yes of course - it's quite simple really:

    Code:
    package Cpanel::ResourceUsage::Custom::NexuscoreStats;
     
    use strict;
    use warnings;
    
    use Cpanel                   ();
    use Cpanel::API              ();
    use Cpanel::Locale           ();
    use Cpanel::Logger           ();
    
    # This feature expects each custom module to contain a "get_usages" function
    # that returns an array of hashes, with each hash matching the parameters below:
    sub get_usages {
        my ($username) = @_;
        my $enabled = 'y';
        # this will return a number (positive)
        my $maxEntries = _get_package_variable($username,"some_metric_1");
        # this will return a number (positive)
        my $currentEntryCount = get_package_variable($username,"some_metric_2");
        if($sqlEnabled eq 'y') {
            return (
                {
                    id          => 'nexuscore_entry_limit',
                    description => 'My package description',
                    usage       => $currentEntryCount,
                    maximum     => $maxEntries,
                    before      => 'mysql_databases',
                    url          => 'myplugin/index.live.php',        
           
                },
            );
        }
        return;
    }
    
    sub _get_package_variable {
       return 5;
    }
     
    1;
    In this case it will appear before the "mysql disk stats". If I change "before" to "after", my plugin ends up in the start of the list. JSON returned from the api (viewed in the chrome network tab) is in the correct order. See attached picture.

    Returned json (https://cpanel.dev.local:2083/cpsess123456789/execute/ResourceUsage/get_usages)
    Code:
    {
       "data":[
          {
             "usage":2666496,
             "id":"disk_usage",
             "formatter":"format_bytes",
             "maximum":null,
             "description":"Disk Usage",
             "url":"diskusage/index.html"
          },
          {
             "url":null,
             "maximum":null,
             "description":"MySQL® Disk Usage",
             "formatter":"format_bytes",
             "id":"cachedmysqldiskusage",
             "usage":0
          },
          {
             "url":"stats/bandwidth.html",
             "formatter":"format_bytes",
             "maximum":null,
             "description":"Bandwidth",
             "id":"bandwidth",
             "usage":"428201"
          },
          {
             "id":"addon_domains",
             "usage":0,
             "url":"addon/index.html",
             "maximum":"0",
             "formatter":null,
             "description":"Addon Domains"
          },
          {
             "description":"Subdomains",
             "maximum":null,
             "formatter":null,
             "url":"subdomain/index.html",
             "usage":0,
             "id":"subdomains"
          },
          {
             "url":"park/index.html",
             "formatter":null,
             "maximum":"0",
             "description":"Aliases",
             "id":"aliases",
             "usage":0
          },
          {
             "usage":0,
             "id":"email_accounts",
             "maximum":null,
             "formatter":null,
             "description":"Email Accounts",
             "url":"mail/pops/index.html"
          },
          {
             "url":"mail/lists.html",
             "formatter":null,
             "maximum":null,
             "description":"Mailing Lists",
             "id":"mailing_lists",
             "usage":0
          },
          {
             "id":"autoresponders",
             "usage":0,
             "url":"mail/autores.html",
             "formatter":null,
             "maximum":null,
             "description":"Autoresponders"
          },
          {
             "usage":0,
             "id":"forwarders",
             "maximum":null,
             "formatter":null,
             "description":"Forwarders",
             "url":"mail/fwds.html"
          },
          {
             "usage":0,
             "id":"email_filters",
             "maximum":null,
             "description":"Email Filters",
             "formatter":null,
             "url":"mail/filters/managefilters.html"
          },
          {
             "maximum":null,
             "formatter":null,
             "description":"FTP Accounts",
             "url":"ftp/accounts.html",
             "usage":"0",
             "id":"ftp_accounts"
          },
    <!!!!!!!!!!!!!!!!!!!!!!!!!!>
          {
             "formatter":null,
             "description":"My package description",
             "maximum":"5",
             "url":"myplugin/index.live.php",
             "usage":5,
             "id":"nexuscore_entry_limit"
          },
    <!!!!!!!!!!!!!!!!!!!!!!!!!!>
          {
             "usage":4,
             "id":"mysql_databases",
             "description":"MySQL® Databases",
             "maximum":null,
             "formatter":null,
             "url":"sql/index.html"
          }
       ],
       "status":1,
       "errors":null,
       "messages":null,
       "metadata":{
          "transformed":1
       },
       "warnings":null
    }
    
    [CODE
     

    Attached Files:

    #7 CloudNexus, Jun 19, 2018
    Last edited: Jun 20, 2018
  8. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,802
    Likes Received:
    1,895
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

    Thank you for providing the requested information.

    Upon testing, I believe the issue you are experiencing relates to the notice found in the Sort Order category under Additional Information on the How to Create a Pluggable Statistics Module document:

    This is only applicable to the cPanel UI, and thus it's not something you will notice in the JSON output. To verify if this is the case, try temporarily removing the following line from your code to see if it then works as expected:

    Code:
    maximum     => $maxEntries,
    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    Dear Michael,

    Thanks again for your help. You are completely right. When removing the entry (maximum) it does show on the correct place.

    THing is however, that even if I set it to 1/100 (1% usage) it will end up above the mysql statistics in my case, meaning that basically anything with a limit other than unlimited will be shown 'higher' than unlimited items.

    THanks for the fast help once again, this at least explains most of this. I should rtfm! Thanks!
     
  10. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,802
    Likes Received:
    1,895
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

    You're very welcome!

    That's currently the designed behavior, as any setting with an unlimited value technically means it's usage is at 0%.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. CloudNexus

    CloudNexus Member

    Joined:
    May 18, 2018
    Messages:
    16
    Likes Received:
    2
    Trophy Points:
    3
    Location:
    Netherlands
    cPanel Access Level:
    Root Administrator
    Thank you!
     
    cPanelMichael likes this.
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice