How to show next time slots from the current time - laravel

I am trying to show next time slots from the current time.
Table contains the time slots 'time' = 12:00 AM, 10:00 AM, 12:00 PM, 02:00 PM, 05:00 PM, 08:00 PM, 10:00 PM
I just want to show the upcoming time slots from the current time. The time slots remaining for today will show in today array and the next time slots in tomorrow array.
$now = Carbon::now()->format('g:i A');
$time = TimeSlot::where('time', '<', $now)->get();
$data = array('message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_DATA_FOUND), 'timeSlots'=>$time, 'current_time'=>$now);
return $this->sendSuccessResponse($data);
Output is :
{
"message": "Data retrieve Successfully",
"timeSlots": [
{
"id": 1,
"time": "12:00 AM"
},
{
"id": 2,
"time": "10:00 AM"
},
{
"id": 3,
"time": "12:00 PM"
},
{
"id": 4,
"time": "02:00 PM"
},
{
"id": 5,
"time": "05:00 PM"
},
{
"id": 6,
"time": "08:00 PM"
},
{
"id": 7,
"time": "10:00 PM"
}
],
"current_time": "5:16 PM",
"status": true
}
Reuired Output will be something like this:
{
"message": "Data retrieve Successfully",
"timeSlots": [
{
"today": [
{
"id": 6,
"time": "08:00 PM"
},
{
"id": 7,
"time": "10:00 PM"
}
],
"tomorrow": [
{
"id": 1,
"time": "12:00 AM"
},
{
"id": 2,
"time": "10:00 AM"
},
{
"id": 3,
"time": "12:00 PM"
},
{
"id": 4,
"time": "02:00 PM"
},
{
"id": 5,
"time": "05:00 PM"
}
],
}
],
"current_time": "5:16 PM",
"status": true
}
Please help me out,

In case you can't modify your table to store info in a more useful way (as suggested in my comment), you can do at the collection level. For this, I'm making use of the map() and partition() collection methods. Given that the first method returns the same collection instance, it allows us to chain the methods.
list($tomorrow, $today) = TimeSlot::all()
// Here we create a new field that will contain a carbon instance of the time
->map(function ($slot) {
$slot['timeTemp'] = Carbon::createFromFormat('g:i A', $slot['time']);
return $slot;
})
// Then we partition the collection using the current time,
// this will return two collections
->partition(function ($slot) {
return $slot['timeTemp'] < now();
});
$timeSlots = [
'today' => $today,
'tomorrow' => $tomorrow,
];
$data = [
'message' => ResponseMessage::statusResponses(ResponseMessage::_STATUS_DATA_FOUND),
'timeSlots' => $timeSlots,
'current_time' => Carbon::now()->format('g:i A'),
];
return $this->sendSuccessResponse($data);
Of course, this will also return the 'timeTemp' value that we inserted in each time slot, but you can ignore them or remove them by mapping through each slot again. Up to you.

Related

Laravel collection sum of columns group by month

I'm working on Laravel project where I need to fetch some data, sum the columns and group the results in M-y (Jan-19) format to show in a monthly bar graph chart.
I was able to successfully get the data sum'ed and grouped but the problem is, for the months without any records, I still want to show up in the graph with a total equals to 0.
This is my current approach:
$data = $this->user->income()
->whereBetween('game_date', ['2019-01-01', '2019-04-30'])
->selectRaw('sum(total_score) as score,
sum(total_stars) as stars,
MONTH(game_date) as month,
DATE_FORMAT(game_date,"%b") as month_name
')
->groupBy('month', 'month_name')
->get();
This gives me the following result (missing months without any records):
[
{
"score": "707",
"stars": "64",
"month": 1,
"month_name": "Jan"
},
{
"score": "200",
"stars": "29",
"month": 3,
"month_name": "Mar"
}
]
And the expected result is including missing months to making visually clear chart as:
[
{
"score": "707",
"stars": "64",
"month": 1,
"month_name": "Jan"
},
{
"score": "0",
"stars": "0",
"month": 2,
"month_name": "Feb"
},
{
"score": "200",
"stars": "29",
"month": 3,
"month_name": "Mar"
},
]
Please note that the ->whereBetween('game_date', []) will aways have dates from start of month and end of month (covering full monthly records).
you have to use COALESCE() function. It's because at the month of Feb you get null at sum(total_score) as score instead of 0. look at
here and here
hop it help

Problem with the interpretation of "Now" as #Sys.Time in Dialogflow

Currently i am creating a chatbot for skype using Dialogflow the main problem is when i use the command "Now" in a skype message it uses my current time +1 hour, but when i ask for the time "Now" from the IOS Application it use the current correct TimeZone, someone knows from "where" exactly dialogflow takes the current time zone for the word "Now" because from my app-IOS because from the IOS_Application it gets one value (Correct timezone value) and from skype it gets a another(timezone + 1 hour value)
Raw Interaction Log(Dialogflow - Skype):
{
"queryText": "what time is now?",
"parameters": {
"time": "StiDate [Thu Oct 18 12:38:16 CDT 2018]"
},
"fulfillmentText": "the time is 12:38:16",
"fulfillmentMessages": [
{
"text": {
"text": [
"[{\"type\":0,\"speech\":\"the time is 12:38:16\"}]"
]
}
}
],
"intent": {
"id": "37524c80-a15a-4c04-aa9b-38986ff38993",
"displayName": "A_Test_EventTime"
},
"languageCode": "en",
"sentimentAnalysisResult": {},
"id": "93ce9408-4b73-4f18-9ae0-b947a906afc8",
"sessionId": "6b69769b-1ce7-4359-9018-c88d017485bf",
"timestamp": "2018-10-18T17:38:16.164Z",
"source": "agent"
}
Raw Interaction Log(Dialogflow - AppIOS):
{
"queryText": "What time is now?",
"parameters": {
"time": "StiDate [Thu Oct 18 11:38:00 CST 2018]"
},
"fulfillmentText": "the time is 11:38:00",
"fulfillmentMessages": [
{
"text": {
"text": [
"[{\"type\":0,\"speech\":\"the time is 11:38:00\"}]"
]
}
}
],
"outputContexts": [
{
"name": "fa75fc39-7c68-47ac-bea5-12394f425855",
"lifespanCount": 4,
"parameters": {
"time.original": "now?",
"time": "StiDate [Thu Oct 18 11:38:00 CST 2018]"
}
}
],
"intent": {
"id": "37524c80-a15a-4c04-aa9b-38986ff38993",
"displayName": "A_Test_EventTime"
},
"languageCode": "en",
"sentimentAnalysisResult": {},
"id": "58ade82b-c842-44b6-b0a2-d6cced4d6648",
"sessionId": "dfe0efda53d11aa3d8d43e92a726f9e4",
"timestamp": "2018-10-18T17:38:00.695Z",
"source": "agent"
}
Dialogflow agents have a default time zone. You can change this time zone in your Dialogflow's agents settings in the console: https://dialogflow.com/docs/agents/create-manage#general

Grafana table that shows top 5 objects from an Array

I'm trying to create a table that shows the top 5 nested objects in an array.
My documents look something like this:
{
"_id": 1,
"workers": [
{
"worker_id": 1,
"units": [
{
"unit_id": 1,
"time": 100
},
{
"unit_id": 2,
"time": 200
},
{
"unit_id": 3,
"time": 300
},
{
"unit_id": 4,
"time": 400
}
]
},
{
"worker_id": 2,
"units": [
{
"unit_id": 11,
"time": 1000
},
{
"unit_id": 12,
"time": 200
},
{
"unit_id": 13,
"time": 300
},
{
"unit_id": 14,
"time": 350
}
]
}
]
}
I would like to have two columns in the table. One column with the _id of the document and the other with the unit_id. In the second column should be only the top five units that have the highest time.
Is this possible with Grafana?
For computing the top 5 entries from the nested Object, you can use simple-json-datasource plugin. You can create a simple Webapp which does the JSON Parsing logic and return the data in the format you want

How to join output of models in Laravel?

I have the following code:
$orders = OrderProduct::where(function ($query) use ($request) {
})->with('order_products')->orderBy('status', 'desc')->paginate(50)->toArray();
And order_products function is:
public function order_products()
{
return $this->hasMany("App\Order", "order_id", "order_id");
}
It gives me output result:
{
"user_id": "1",
"created_at": "2016-12-18 14:06:11",
"status": "2",
"note": "34535345",
"order_id": "2",
"address": "Kiev",
"courier_id": null,
"payment_type": "0",
"order_products": [
{
"id": 73,
"product_id": "1265",
"amount": "1"
},
{
"id": 74,
"product_id": "1266",
"amount": "1"
}
]
I need to join order_products with products_details, that to give title of product for each order like as:
"order_products": [
{
"id": 73,
"product_id": "1265",
"amount": "1",
"title": "Product name"
},
{
"id": 74,
"product_id": "1266",
"amount": "1",
"title": "Product name"
}
]
Instead this I get a new model output in response:
"products_details": [
{}, {}
]
How can I join two with models?
without joining, just using your first code:
in order_products, override the toArray() method.
function toArray() {
return [
id => $this->id,
product_id => $this->product_id,
amount => $this->amount,
title => $this->details->name
];
}
wich $this->details->name is the relation between order_products and products_details

Django query data btween date and time

How do I query data between two date and time from django model.
I am using below code but it is not working. Here In Db for every second data is populated and from browser every second or minute or hours or current date or week, a Ajax is request sent.
However currently I am not filtering the data based upon they date+ time because of this server takes time to respond. Please guide me how do I query data model between date+time
tempresult=Realtimekwhrtu1.objects.filter(datetime_published__range=(
datetime.datetime.combine('2008-03-27',datetime.time.min),
datetime.datetime.combine('2008-03-27',datetime.time.max)))
JSON example:
{
"data": [{
"date": "11/25/2016 08:59:58",
"energy": 29940935080,
"power": 6815.7056798623,
"time": 217781943
}, {
"date": "11/25/2016 09:29:59",
"energy": 29940981851,
"power": 6803.7187250996,
"time": 217783743
}, {
"date": "11/25/2016 09:59:59",
"energy": 29941028913,
"power": 6841.5804195804,
"time": 217785544
}, {
"date": "11/25/2016 10:29:59",
"energy": 29941075952,
"power": 6845.9247648903,
"time": 217787343
}, {
"date": "11/25/2016 10:59:58",
"energy": 29941123228,
"power": 6877.2764478764,
"time": 217789143
}]
}

Resources