I have this sql query
Ticket.joins([assigned_user::departament],:ticket_status).group("ticket_statuses.name","departaments.name").where("ticket_statuses.status = ?", 1).count
what does this give me back
[ "Open", "BROWSER" ] => 2,
[ "Open", "MARKETING" ] => 5,
[ "Open", "MONITORING" ] => 2,
[ "Open", "SALES" ] => 7,
[ "Open", "ADMINISTRATION" ] => 8,
[ "Open", "COLLECTIONS" ] => 1,
[ "Open", "EMPLOYEE" ] => 8,
[ "Open", "SYSTEMS" ] => 6,
[ "Open", "QUALITY" ] => 4,
[ "Open", "TECH SUPPORT" ] => 5,
[ "Open", "STORE" ] => 2,
[ "Closed", "SYSTEMS" ] => 11,
[ "Closed", "MONITORING" ] => 7,
[ "Closed", "ADMINISTRATION" ] => 4,
[ "Closed", "QUALITY" ] => 6,
[ "Closed", "STORE" ] => 6,
[ "Closed", "EMPLOYEE" ] => 2,
[ "Closed", "SALES" ] => 3,
[ "Closed", "TECHNICAL SUPPORT" ] => 4,
[ "Closed", "BROWSER" ] => 1,
[ "Closed", "COLLECTIONS" ] => 2,
[ "Closed", "MARKETING" ] => 1,
[ "Attended", "QUALITY" ] => 3,
[ "Served", "EMPLOYEE" ] => 3,
[ "Served", "WAREHOUSE" ] => 5,
[ "Attended", "COLLECTIONS" ] => 3,
[ "Served", "SYSTEMS" ] => 8,
[ "Served", "SALES" ] => 5,
[ "Attended", "TECHNICAL SUPPORT" ] => 3,
[ "Attended", "BROWSER" ] => 8,
[ "Attended", "ADMINISTRATION" ] => 7,
[ "Attended", "MONITORING" ] => 3,
[ "Served", "MARKETING" ] => 2,
[ "In process", "EMPLOYEE" ] => 4,
[ "In process", "COLLECTIONS" ] => 2,
[ "In process", "QUALITY" ] => 7,
[ "In process", "MONITORING" ] => 4,
[ "In process", "BROWSER" ] => 4,
[ "In progress", "MARKETING" ] => 4,
[ "In process", "SYSTEMS" ] => 3,
[ "In process", "ADMINISTRATION" ] => 4,
[ "In process", "TECHNICAL SUPPORT" ] => 3,
[ "In process", "WAREHOUSE" ] => 3,
[ "In process", "SALES" ] => 1
I am using this query with the 'chartkick' gem
that groups them in a graph by area, where when passing the mouse in each area it shows me this
Marketing
Open:5
Close:1
Attended:2
In process:4
Monitoring
Open:2
Close:7
Attended:3
In process:4
I would like to add to that information the total amount
and what is displayed like this
Marketing
Open:5
Close:1
Attended:2
In process:4
Total:12
Monitoring
Open:2
Close:7
Attended:3
In process:4
Total:16
I was looking for the solution but I don't know how to achieve this
Given your current output as h
h = {[ "Open", "BROWSER" ] => 2,[ "Open", "MARKETING" ] => 5,[ "Open", "MONITORING" ] => 2,[ "Open", "SALES" ] => 7,[ "Open", "ADMINISTRATION" ] => 8,[ "Open", "COLLECTIONS" ] => 1,[ "Open", "EMPLOYEE" ] => 8,[ "Open", "SYSTEMS" ] => 6,[ "Open", "QUALITY" ] => 4,[ "Open", "TECH SUPPORT" ] => 5,[ "Open", "STORE" ] => 2,[ "Closed", "SYSTEMS" ] => 11, [ "Closed", "MONITORING" ] => 7,[ "Closed", "ADMINISTRATION" ] => 4,[ "Closed", "QUALITY" ] => 6,[ "Closed", "STORE" ] => 6,[ "Closed", "EMPLOYEE" ] => 2,[ "Closed", "SALES" ] => 3,[ "Closed", "TECHNICAL SUPPORT" ] => 4,[ "Closed", "BROWSER" ] => 1,[ "Closed", "COLLECTIONS" ] => 2,[ "Closed", "MARKETING" ] => 1,[ "Attended", "QUALITY" ] => 3,[ "Served", "EMPLOYEE" ] => 3,[ "Served", "WAREHOUSE" ] => 5,[ "Attended", "COLLECTIONS" ] => 3,[ "Served", "SYSTEMS" ] => 8,[ "Served", "SALES" ] => 5,[ "Attended", "TECHNICAL SUPPORT" ] => 3,[ "Attended", "BROWSER" ] => 8,[ "Attended", "ADMINISTRATION" ] => 7,[ "Attended", "MONITORING" ] => 3,[ "Served", "MARKETING" ] => 2,[ "In process", "EMPLOYEE" ] => 4,[ "In process", "COLLECTIONS" ] => 2,[ "In process", "QUALITY" ] => 7,[ "In process", "MONITORING" ] => 4,[ "In process", "BROWSER" ] => 4,[ "In progress", "MARKETING" ] => 4,[ "In process", "SYSTEMS" ] => 3,[ "In process", "ADMINISTRATION" ] => 4,[ "In process", "TECHNICAL SUPPORT" ] => 3,[ "In process", "WAREHOUSE" ] => 3,[ "In process", "SALES" ] => 1}
The following will produce a Hash with the desired data structure:
h.each_with_object(Hash.new {|h,k| h[k] = {"Total"=> 0}}) do |((status,department),v),obj|
obj[department][status] = v
obj[department]["Total"] += v
end
# {"BROWSER"=>{"Total"=>15, "Open"=>2, "Closed"=>1, "Attended"=>8, "In process"=>4},
# "MARKETING"=>{"Total"=>12, "Open"=>5, "Closed"=>1, "Served"=>2, "In progress"=>4},
# "MONITORING"=>{"Total"=>16, "Open"=>2, "Closed"=>7, "Attended"=>3, "In process"=>4},
# "SALES"=>{"Total"=>16, "Open"=>7, "Closed"=>3, "Served"=>5, "In process"=>1},
# "ADMINISTRATION"=>{"Total"=>23, "Open"=>8, "Closed"=>4, "Attended"=>7, "In process"=>4},
# "COLLECTIONS"=>{"Total"=>8, "Open"=>1, "Closed"=>2, "Attended"=>3, "In process"=>2},
# "EMPLOYEE"=>{"Total"=>17, "Open"=>8, "Closed"=>2, "Served"=>3, "In process"=>4},
# "SYSTEMS"=>{"Total"=>28, "Open"=>6, "Closed"=>11, "Served"=>8, "In process"=>3},
# "QUALITY"=>{"Total"=>20, "Open"=>4, "Closed"=>6, "Attended"=>3, "In process"=>7},
# "TECH SUPPORT"=>{"Total"=>5, "Open"=>5}, "STORE"=>{"Total"=>8, "Open"=>2, "Closed"=>6},
# "TECHNICAL SUPPORT"=>{"Total"=>10, "Closed"=>4, "Attended"=>3, "In process"=>3},
# "WAREHOUSE"=>{"Total"=>8, "Served"=>5, "In process"=>3}}
Suppose the given hash were as follows.
h = {
["Open", "BROWSER"] =>2, ["Open", "MARKETING"] =>5,
["Open", "MONITORING"] =>2, ["Closed", "MARKETING"]=>1,
["Closed", "MONITORING"] =>7, ["Attended", "BROWSER"]=>8,
["Attended", "MONITORING"]=>3, ["Closed", "BROWSER"] =>1,
["Served", "MARKETING"] =>2
}
We may compute the desired hash as follows.
h.each_with_object({}) do |((status, dept), v), g|
g.update(dept=>{ status=>v }) { |_k,o,n| o.merge(n) }
end.transform_values { |f| f.update("Total"=>f.values.sum) }
#=> { "BROWSER"=>{"Open"=>2, "Attended"=>8, "Closed"=>1, "Total"=>11},
# "MARKETING"=>{"Open"=>5, "Closed"=>1, "Served"=>2, "Total"=>8},
# "MONITORING"=>{"Open"=>2, "Closed"=>7, "Attended"=>3, "Total"=>12}}
See:
array decomposition to better understand the way each_with_object's block variables are written;
the form of Hash#update (a.k.a. Hash#merge!) that takes a block (here { |_k,o,n| o.merge(n) }) that returns the values of keys that are present in both hashes being merged. _k holds the common key, which here is not used in the block calculation (as I've indicated by the underscore). o and n respectively hold the values of the "old" and "new" values of _k. o is the value in the hash being constructed, n is the value in the hash being merged; and
Hash#transform_values.
Note the intermediate hash that is computed:
h.each_with_object({}) do |((status, dept), v), g|
g.update(dept=>{ status=>v }) { |_k,o,n| o.merge(n) }
end
#=> { "BROWSER"=>{"Open"=>2, "Attended"=>8, "Closed"=>1},
# "MARKETING"=>{"Open"=>5, "Closed"=>1, "Served"=>2},
# "MONITORING"=>{"Open"=>2, "Closed"=>7, "Attended"=>3}}
I chose to compute the values of "Total" as a separate (second) step, to both simplify the calculations and facilitate testing. If desired, however, one could modify the code above to compute the desired hash in a single pass through the elements of h (as #engineersmnky did) as follows.
h.each_with_object({}) do |((status, dept), v), g|
g.update(dept=>{ status=>v, "Total"=>v }) do |_k,o,n|
n["Total"] += o["Total"]
o.merge(n)
end
end
Here is a second way to perform the calculation, using Enumerable#group_by.
h.group_by { |(status, dept), _v| dept }
.transform_values do |arr|
arr.each_with_object({ "Total"=>0 }) do |((status, _dept), v), g|
g.update(status=>v, "Total"=>g["Total"] + v)
end
end
#=> {"BROWSER"=>{"Total"=>11, "Open"=>2, "Attended"=>8, "Closed"=>1},
# "MARKETING"=>{"Total"=>8, "Open"=>5, "Closed"=>1, "Served"=>2},
# "MONITORING"=>{"Total"=>12, "Open"=>2, "Closed"=>7, "Attended"=>3}}
Note the intermediate calculation.
h.group_by { |(status, dept), _v| dept }
#=> { "BROWSER"=>[
# [["Open", "BROWSER"], 2], [["Attended", "BROWSER"], 8],
# [["Closed", "BROWSER"], 1]
# ],
# "MARKETING"=>[
# [["Open", "MARKETING"], 5], [["Closed", "MARKETING"], 1],
# [["Served", "MARKETING"], 2]
# ],
# "MONITORING"=>[
# [["Open", "MONITORING"], 2], [["Closed", "MONITORING"], 7],
# [["Attended", "MONITORING"], 3]
# ]
# }
You should be able to build this structure directly in the database using grouping sets. I'm assuming postgresql here (read more here https://www.postgresql.org/docs/15/queries-table-expressions.html#QUERIES-GROUPING-SETS) but this should also work for other Databases.
Ticket
.select("ticket_statuses.name as status_name, departments.name as department_name, count(*) as count")
.joins([assigned_user: :departament],:ticket_status)
.group("grouping sets ((ticket_statuses.name, departments.name), (departments.name), ())")
.where("ticket_statuses.status = ?", 1)
You will get:
Count grouped by status/department
Count grouped by department
Total Count
But you will get it as instances of your model (Ticket).
Total Count will be the one with nil for status/department and grouped by department will be the ones with nil for status.
You can tweak the order or extract the values.
I have installed the highcharts package in my project. Now I am trying to view a sample chart but I am getting an error.
use miloschuman\highcharts\Highcharts;
<?=
Highcharts::widget([
'options' => [
'title' => ['text' => 'Fruit Consumption'],
'xAxis' => [
'categories' => ['Apples', 'Bananas', 'Oranges']
],
'yAxis' => [
'title' => ['text' => 'Fruit eaten']
],
'series' => [
['name' => 'Jane', 'data' => [1, 0, 4]],
['name' => 'John', 'data' => [5, 7, 3]]
]
]
]);
?>
Error
Invalid Argument – yii\base\InvalidArgumentException
The file or directory to be published does not exist: F:\xampp\htdocs\mdc/vendor\bower/highcharts
Update 1
Below is my stack trace
Update 2
Below is my composer.json
{
"name": "yiisoft/yii2-app-advanced",
"description": "Yii 2 Advanced Project Template",
"keywords": ["yii2", "framework", "advanced", "project template"],
"homepage": "http://www.yiiframework.com/",
"type": "project",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "~2.0.13",
"yiisoft/yii2-bootstrap": "~2.0.0",
"yiisoft/yii2-swiftmailer": "~2.0.0",
"luoxiaojun1992/yii2-tinker": "*",
"kartik-v/yii2-grid": "#dev",
"kartik-v/yii2-widget-select2": "#dev",
"kartik-v/yii2-mpdf": "dev-master",
"moonlandsoft/yii2-phpexcel": "*",
"nirvana-msu/yii2-showloading": "1.0.*#dev",
"kartik-v/yii2-krajee-base": "^2.0",
"kartik-v/yii2-widget-spinner": "#dev",
"edwinhaq/yii2-simple-loading": "*",
"c006/yii2-submit-spinner": "*",
"kartik-v/yii2-detail-view": "*",
"fedemotta/yii2-cronjob": "*",
"kartik-v/yii2-widget-datetimepicker": "*",
"kartik-v/yii2-date-range": "dev-master",
"phpoffice/phpspreadsheet": "^1.2",
"linslin/yii2-curl": "*",
"kartik-v/yii2-widget-fileinput": "#dev",
"kartik-v/yii2-dialog": "*",
"google/apiclient": "^2.0",
"delight-im/str": "^2.4",
"mongosoft/yii2-soap-client": "dev-master",
"kartik-v/yii2-export": "#dev",
"ext-json": "*",
"miloschuman/yii2-highcharts-widget": "*",
"simialbi/yii2-widget-chart": "^1.0#dev",
"onmotion/yii2-widget-apexcharts": "dev-master",
"2amigos/yii2-chartjs-widget": "~2.0",
"kartik-v/yii2-bootstrap4-dropdown": "#dev",
"practically/yii2-chartjs": "dev-master",
"php-parallel-lint/php-console-color": "dev-master",
"nnnick/chartjs": "v2.9.3"
},
"require-dev": {
"yiisoft/yii2-debug": "~2.0.0",
"yiisoft/yii2-gii": "~2.0.0",
"yiisoft/yii2-faker": "~2.0.0",
"codeception/base": "^2.2.3",
"codeception/verify": "~0.3.1"
},
"config": {
"process-timeout": 1800,
"fxp-asset": {
"enabled": false
}
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
},
"scripts": {
"post-install-cmd": "php init --env=Development --overwrite=n"
},
"autoload": {
"psr-4": {
"kartik\\spinner\\": "",
"nirvana\\showloading\\": ""
}
},
"repositories": [{
"type": "composer",
"url": "https://asset-packagist.org"
}]
}
Update 3
Removed highcharts package from composer.json and then executed composer update
F:\xampp\htdocs\mdc>composer require --prefer-dist miloschuman/yii2-highcharts-widget "*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing bower-asset/highcharts (v7.2.1): Loading from cache
- Installing miloschuman/yii2-highcharts-widget (dev-master 260b946): Loading from cache
Package codeception/base is abandoned, you should avoid using it. No replacement was suggested.
Package jakub-onderka/php-console-color is abandoned, you should avoid using it. Use php-parallel-lint/php-console-color instead.
Package jakub-onderka/php-console-highlighter is abandoned, you should avoid using it. Use php-parallel-lint/php-console-highlighter instead.
Writing lock file
Generating autoload files
14 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Getting same error
Invalid Argument – yii\base\InvalidArgumentException
The file or directory to be published does not exist: F:\xampp\htdocs\mdc/vendor\bower/highcharts
Update 4
As per #Taron 1st solution I have backend/config/main.php
<?PHP
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
// require(__DIR__ .'/main-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module'
],
'api' => [
'class' => 'backend\modules\api\Api',
],
],
'timeZone' => 'Asia/Karachi',
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
'enableCookieValidation' => false,
'enableCsrfValidation' => false,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
],
'params' => $params,
];
How can I get rid of this error?
Any help would be highly appreciated
Looks like bower assets stored in path /vendor/bower-assets, but your script tries to load assets from vendor/bower (bower assets path error). Try solution bellow, I hope one of them will help you.
Solution first:
Just add aliases bellow to your config file/files (config/web.php)
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
Solution second:
Remove composer.lock file
Add package fxp: composer globalrequire "fxp/composer-asset-plugin:~1.1.1"
Reinstall composer: composer --prefer-dist install
array = [
[ 1, "name1" ],
[ 2, "name2" ],
[ 3, "name3" ],
[ 4, "name4" ]
]
I want to make this as an array of hashes like this:
array_hash = [{ "id" => 1, "name" => "name1" },
{ "id" => 2, "name" => "name2" },
{ "id" => 3, "name" => "name3" },
{ "id" => 4, "name" => "name4" }]
array = [
[ 1, "name1" ],
[ 2, "name2" ],
[ 3, "name3" ],
[ 4, "name4" ]
]
array.map { |e| ['id', 'name'].zip(e).to_h }
#⇒ [
# {"id"=>1, "name"=>"name1"},
# {"id"=>2, "name"=>"name2"},
# {"id"=>3, "name"=>"name3"},
# {"id"=>4, "name"=>"name4"}
# ]
The only interesting here is Enumerable#zip, that “merges” arrays.
I'd use:
array.map { |id, name| { 'id' => id, 'name' => name } }
#=> [{"id"=>1, "name"=>"name1"},
# {"id"=>2, "name"=>"name2"},
# {"id"=>3, "name"=>"name3"},
# {"id"=>4, "name"=>"name4"}]
The .to_h method is new to Ruby 2.x. Here is an alternative for anyone on 1.9.x or lower.
array = [[ 1, "name1" ], [ 2, "name2" ], [ 3, "name3" ], [ 4, "name4" ]]
array.inject([]) { |a, r| a << { id: r[0], name: r[1] } }