Unique counts in a callback - rethinkdb

I have a list of users, and I'm trying to get the result of each unique domain in users' email addresses and their totals.
So, let's say I have these 5 users:
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| id | email | firstname | lastname | something |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00c0f0db-87d0-45b2-8ed2-aa94d1e3e659 | shane.conte#jourrapide.com | Shane | Conte | iew9anap0L |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 0114360a-3ef8-49d6-8c51-02392bc51e10 | michelle.guitierrez#dayrep.com | Michelle | Guitierrez | eeNgiev3foh |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e8e2f2-2130-4f65-8914-b93d5b029d75 | terri.hebert#rhyta.com | Terri | Hebert | vahMoKiuCh0 |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00e1578b-cf6d-46b8-92e3-2388a80105f7 | richard.copeland#dayrep.com | Richard | Copeland | Iem4mohng |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
| 00f1be34-d60e-4b2f-b3ae-610c67151f2d | elsie.fuhrman#rhyta.com | Elsie | Fuhrman | aPie6piD6ae |
+--------------------------------------+--------------------------------+-------------+------------+-------------+
After running the query, I'd like to see this result:
+-------+----------------+
| count | domain |
+-------+----------------+
| 1 | jourrapide.com |
+-------+----------------+
| 2 | dayrep.com |
+-------+----------------+
| 2 | rhyta.com |
+-------+----------------+
I'm currently running this query below to get the unique domains but if I try to run count() in it, it dramatically fails after 300 seconds, which I expect to happen since I have A LOT more than 5 users :)
r.db('helloworld').table('users').pluck('email').map(function(user) {
return user('email').split('#').nth(1).downcase()
}).distinct().map(function(domain) {
return {
count: '???', // <--- this is where I need help
domain: domain
}
})
And as you can imagine, it perfectly returns this result:
+-------+----------------+
| count | domain |
+-------+----------------+
| ??? | jourrapide.com |
+-------+----------------+
| ??? | dayrep.com |
+-------+----------------+
| ??? | rhyta.com |
+-------+----------------+
I hope this makes sense. If you think I'm on a wrong path, feel free to suggest any other way.
Thanks in advance!

You can't distinct() to count(). Instead you want to group():
r.db('helloworld').table('users').pluck('email').map(function(user) {
// wrap the result in an object for grouping purposes
return { domain: user('email').split('#').nth(1).downcase() };
})
// this groups all domains together in a [{ group, reduction }] list of objects
.group('domain')
// after group(), calls are scoped to each reduction: count each one
.count()
// let's ungroup to scope the following calls to the whole sequence
.ungroup()
// let's be compliant with the format you expect
.map(function(doc) {
return {
domain: doc('group'),
count: doc('reduction')
};
});

Related

Laravel | Return rows in order based off pivot

I have the following setup:
stages:
| id | name | order |
commands:
| id | name | body |
------------------------------=
| 1 | test | phpunit |
| 2 | style | echo "style" |
| 3 | deploy | deploy |
command_stage:
| command_id | stage_id | order |
---------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
Basically, I would like to create a method on the stage model which allows me to get all of the commands back based off of the order, but commands have a specific order and so do the stages.
So each command is stored under a stage so we know which part to run but each command also has an order in that stage. Now I know I can do something like the following:
$inOrder = collect();
Stage::get()->each(function ($stage) {
$commands = $stage->commands()->orderByPivot('order')->get();
$inOrder->push($commands);
});
return $inOrder;
But I was wondering if there is a nicer way to do this? Or even a way to do this solely on one database hit?
Pre-load and sort the relationship:
$stages = Stage::with(['commands' => function ($subQuery) {
$subQuery->orderBy('command_stage', 'order');
}])->get();
foreach($stages as $stage) {
$inOrder->push($stage->commands);
}
This method of Eager-Loading will reduce the N+1 query issue of doing $stage->commands() inside the foreach() loop.

Index Route missing in Resource Routes Laravel

I am unable to get the index route of my resource when using some prefix,
when my route is this:
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
i got this route listing:
| subjects | subjects.index
| subjects | subjects.store
| subjects/create | subjects.create
| subjects/{subject} | subjects.update
| subjects/{subject} | subjects.destroy
| subjects/{subject} | subjects.show
| subjects/{subject}/edit | subjects.edit
But when i add a prefix like this:
Route::prefix('admin')->group(function () {
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
});
The index route disappears, and routes list become this:
| admin/subjects | subjects.store
| admin/subjects/create | subjects.create
| admin/subjects/{subject} | subjects.update
| admin/subjects/{subject} | subjects.destroy
| admin/subjects/{subject} | subjects.show
| admin/subjects/{subject}/edit | subjects.edit
Got the solution. actually there was another route ('subjects') in my web.php. I needed to remove this, since they both were colliding.

Use a non-unique route key name?

We have rows which are sequenced and scoped to each organisation. So:
Processes tbl:
| id | org_id | sequence | name |
|---- |-------- |---------- |------------------- |
| 23 | 1 | 1 | New person |
| 56 | 1 | 2 | Person leaves |
| 84 | 1 | 3 | Person absent |
| 26 | 2 | 1 | Something happens |
| 34 | 2 | 2 | Something else |
| 77 | 2 | 3 | And another |
We do this so they can have a simple numbering system for their own records rather than a UUID, hash or something else. We prefer to have this in the url as well, so:
example.com/processes/1
Once logged in, we know their org_id, so for each query we can use org_id along with the sequence from the url.
Is there a clean laravel-esque kind of way to achieve this, maybe using the Model's getRouteKeyName?
I got the answer with some help from the laracasts forum:
/**
* Retrieve the model for a bound value.
*
* #param mixed $value
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('sequence', $value)
->where('org_id', /* however you get your org_id */)
->first() ?? abort(404);
}

laravel 5 Relationship has one

User table
id | name | user_type | email | password
1 | John | 1 | john#gmail.com | something
2 | Roy | 1 | roy#gmail.com | something
3 | Rax | 1 | Rax#gmail.com | something
4 | Ren | 1 | ren#gmail.com | something
AssignUser Table
id | user_id | assign_to_math | assign_to_chemistry
1 | 3 | 2 | 1
2 | 4 | 1 | 2
So, here is users table where four users are avail
Now in assign table first row Rax is assign tuitoin for math to Roy and chemistry to John
I want to get the detail of john and roy with relation
I tried in User models
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'id', 'assign_to_math');
}
It return for me null value don't know why please help
Thanks in advance
try:
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'assign_to_math', 'id');
}

Display record count in listbox using multiple tables and fields

i need help with a query, can't get it to work correctly. What i'm trying to achieve is to have a select box displaying the number of records associated with a particular theme, for some theme it works well for some it displays (0) when infact there are 2 records, I'm wondering if someone could help me on this, your help would be greatly appreciated, please see below my actual query + table structure :
SELECT theme.id_theme, theme.theme, calender.start_date,
calender.id_theme1,calender.id_theme2, calender.id_theme3, COUNT(*) AS total
FROM theme, calender
WHERE (YEAR(calender.start_date) = YEAR(CURDATE())
AND MONTH(calender.start_date) > MONTH(CURDATE()) )
AND (theme.id_theme=calender.id_theme1)
OR (theme.id_theme=calender.id_theme2)
OR (theme.id_theme=calender.id_theme3)
GROUP BY theme.id_theme
ORDER BY theme.theme ASC
THEME table
|---------------------|
| id_theme | theme |
|----------|----------|
| 1 | Yoga |
| 2 | Music |
| 3 | Taichi |
| 4 | Dance |
| 5 | Coaching |
|---------------------|
CALENDAR table
|---------------------------------------------------------------------------|
| id_calender | id_theme1 | id_theme2 | id_theme3 | start_date | end_date |
|-------------|-----------|-----------|-----------|------------|------------|
| 1 | 2 | 4 | | 2015-07-24 | 2015-08-02 |
| 2 | 4 | 1 | 5 | 2015-08-06 | 2015-08-22 |
| 3 | 1 | 3 | 2 | 2014-10-11 | 2015-10-28 |
|---------------------------------------------------------------------------|
LISTBOX
|----------------|
| |
| Yoga (1) |
| Music (1) |
| Taichi (0) |
| Dance (2) |
| Coaching (1) |
|----------------|
Thanking you in advance
I think that themes conditions should be into brackets
((theme.id_theme=calender.id_theme1)
OR (theme.id_theme=calender.id_theme2)
OR (theme.id_theme=calender.id_theme3))
Hope this help

Resources