GraphQL - How to add/manipulate response columns - graphql

Is there a way to pass a static variable to a GraphQL endpoint in order to be returned with the response?
In my case I'm pulling timesheets for a specific userId. Unfortunately the userId isn't returned in the response.
Types
type Query {
# Returns a timesheet for user id and given date.
#
# Arguments
# userId: User's id.
# date: Date.
timesheet(userId: ID, date: Date!): Timesheet
}
type Timesheet {
# Id.
id: ID
# Date.
date: Date
# Expected time based on work schedule in seconds.
expectedTime: Int
# Tracked time in seconds.
trackedTime: Int
# Time off time in seconds.
timeOffTime: Int
# Holiday time in seconds.
holidayTime: Int
# Sum of tracked, holiday, time off time minus break time.
totalTime: Int
# Break time in seconds.
breakTime: Int
}
Request Body
{"query":"{
timesheets(userId: \"10608\", dateFrom: \"2022-01-01\", dateTo: \"2022-12-31\") {
items { id date trackedTime timeOffTime holidayTime totalTime breakTime }
}
}"}
Example Response
data.timesheets.items.id
data.timesheets.items.date
data.timesheets.items.trackedTime
data.timesheets.items.timeOffTime
data.timesheets.items.holidayTime
data.timesheets.items.totalTime
data.timesheets.items.breakTime
3646982
2022-01-01
0
0
0
0
3495676
2022-01-02
18000
0
0
18000
3500068
2022-01-03
35100
0
0
35100
Desired Response
userId
data.timesheets.items.id
data.timesheets.items.date
data.timesheets.items.trackedTime
data.timesheets.items.timeOffTime
data.timesheets.items.holidayTime
data.timesheets.items.totalTime
data.timesheets.items.breakTime
10608
3646982
2022-01-01
0
0
0
0
10608
3495676
2022-01-02
18000
0
0
18000
10608
3500068
2022-01-03
35100
0
0
35100

Related

AWS Elasticsearch Aggregation

# Transaction Id Amount Status
1 AA001 100 pending
2 AA001 100 success
3 AA002 200 pending
On above data AA001 - having both pending & success AA002 have only pending
So expected to get as below
# Transaction Id Amount Status
2 AA001 100 success
3 AA002 200 pending
How to apply Aggregation?
Condition:
- if a transaction have both pending and success status record return only success record
- if a transaction have only pending return pending record
In theory you can sort the way you want , and a rank to each row. Pick the rank = 1 , so you pick only one row for a unique combination of transaction id and amount.

How to get time difference in ('h:m:s') between two times which are already in this format

I want to get difference between two times in hour:min:sec format. For Example starting time 02:00:00 and ending time 02:30:00 Difference should be 00:30:00
I tried to use Carbon but i think it support different format
<td> {{ \Carbon\Carbon::now()->diff(\Carbon\Carbon::parse($sales->created_at))}}</td>
I want to get difference in hour:min:sec input would also be in same format
Your code is working fine,
$diff = \Carbon\Carbon::now()->diff(\Carbon\Carbon::parse('12:50:35'));
This will return DateInterval object,
DateInterval {#242 ▼
interval: + 11:18:14.006875
+"y": 0
+"m": 0
+"d": 0
+"h": 11
+"i": 18
+"s": 14
+"f": 0.006875
+"weekday": 0
+"weekday_behavior": 0
+"first_last_day_of": 0
+"invert": 0
+"days": 0
+"special_type": 0
+"special_amount": 0
+"have_weekday_relative": 0
+"have_special_relative": 0
}
Then you can get diff in hours, seconds, minutes, days etc.
$diff->h, $diff->i, $diff->s, $diff->days
So your input would be,
$diff->h .':'. $diff->i .':'. $diff->s
Otherwise you can use,
$diffInhours = \Carbon\Carbon::now()->diffInHours(\Carbon\Carbon::parse('12:50:35');
$diffInMinutes = \Carbon\Carbon::now()->diffInMinutes(\Carbon\Carbon::parse('12:50:35'));
Or many other ways.
Carbon diff() function returns a DateInterval Object.
Your expected result can be retrieve from DateInterval object by calling format() function.
<td>
{{ $timeDifference = now()->diff($sales->created_at)->format('%h:%i:%s') }}
</td>

Qlik Sense. Time Filter

Same issue I posted Friday but I will be more specific this time. I have this data:
UserId Action Id Date
1 1 1/1/2018
1 2 1/1/2018
1 2 2/1/2018
2 3 3/1/2018
2 4 4/1/2018
And I want a filter that will yield the following:
Count Instances from FirstDate to 2/1/2018
UserId ActionCount
1 3
2 0
In the data load editor you want to group by the User in order to get that first date:
GroupedUserData:
Load
UserId
min(Date) as FirstDate
resident [The name of your original table];
And then you want to use set analysis chart-side:
sum({<FirstDate = {'<=2/1/2018'}>} ActionCount)

Please suggest a linq query for my requirement

Can anyone suggest a linq query for the below requirement.
There is a Checkbox on the form..when we click on it...As per the below datatable it has to be grouped according to ItemCode,Sum(SoldQty), StockInHand,LatestRecordValueOfSales, Amount, Description.
You can't group. the following columns
solddate - show the latest sold date
department
category
ItemCode Description UOM SoldQty Stock in Hand SellPrice Amount
---------------------------------------------------------------
100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/21/2013 MEAT INDIAN BEAF
100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/21/2013 MEAT INDIAN BEAF
200 frozen meat Kilograms 0.005 88.19 4 4.01 0 200 1/21/2013 OTHERS INDIAN BEAF
200 frozen meat Kilograms 0.044 88.19 4 4.04 0 200 1/21/2013 OTHERS INDIAN BEAF
100 Paracetamol 200MG UOM1 5 -5 3 8 0 100 1/22/2013 MEAT INDIAN BEAF
200 frozen meat Kilograms 0.054 88.19 4 4.05 0 200 1/22/2013 OTHERS INDIAN BEAF
200 frozen meat Kilograms 0.055 88.19 4 4.06 0 200 1/22/2013 OTHERS INDIAN BEAF
========================================================================
General query
var resQuery = from i in someQueryable
group i by new {i.groupProperty1, i.groupProperty2} into g
select new
{
Property1 = g.Key.Property1,
Property2 = g.Key.Property2
Total = g.Sum(p => p.SumProperty),
/// other properties
};
For your example data it could be like:
var resQuery = from i in dbContext.Items
group i by new{ i.ItemCode, i.Description, i.UOM} into g
select new
{
ItemCode = g.Key.ItemCode,
TotalSold = g.Sum(p => p.SoldQty),
Description = g.Key.Description,
UOM =g.Key.UOM
/// other properties
};
Try example on Ideone: http://ideone.com/xXwgoG
Similar questions asked on SO many times:
Linq Objects Group By & Sum
LINQ Lambda Group By with Sum
Multiple group by and Sum LINQ
Below is my code and it works fine but only for the first row the soldqty and Amount values are getting doubled.while other rows data is fine.I am not able to understand why only the first row data Sum(SoldQty) is getting doubled.
decimal? SoldQty, stockinhand,SellPrice,Amount,CostPrice;
string ItemCode, Description,UOM,BarCode,SoldDate,Department,Category,User;
var resQuery = from row in dtFilter.AsEnumerable()
group row by row.Field<string>("Item Code") into g
select dtFilter.LoadDataRow(new object[]
{
ItemCode=g.Key,
Description=g.Select(r=>r.Field<string>("Description")).First<string>(),
UOM=g.Select(r=>r.Field<string>("UOM")).First<string>(),
SoldQty = g.Sum(r => r.Field<decimal?>("Sold Qty")).Value,
stockinhand=g.Select(r=>r.Field<decimal?>("Stock in Hand")).First<decimal?>(),
SellPrice=g.Select(r=>r.Field<decimal?>("Sell Price")).First<decimal?>(),
Amount = g.Sum(r => r.Field<decimal?>("Amount")).Value,
CostPrice = g.Sum(r => r.Field<decimal?>("Cost Price")).Value,
BarCode=g.Select(r=>r.Field<string>("Barcode")).First<string>(),
SoldDate=g.Select(r=>r.Field<string>("SoldDate")).Last<string>(),
Department=g.Select(r=>r.Field<string>("Department")).First<string>(),
Category=g.Select(r=>r.Field<string>("Category")).First<string>(),
User=g.Select(r=>r.Field<string>("User")).First<string>(), }, false);

EF Linq query comparing data from multiple rows

I would like to create a Linq query that compares date from multiple rows in a single table.
The table consists of data that polls a web-services for balance data for account. Unfortunately the polling interval is not a 100% deterministic which means there can be 0-1-more entries for each account per day.
For the application i would need this data to be reformatted in a certain formatted (see below under output).
I included sample data and descriptions of the table.
Can anybody help me with a EF Linq query that will produce the required output?
table:
id The account id
balance The available credits in the account at the time of the measurement
create_date The datetime when the data was retrieved
Table name:Balances
Field: id (int)
Field: balance (bigint)
Field: create_date (datetime)
sample data:
id balance create_date
3 40 2012-04-02 07:01:00.627
1 55 2012-04-02 13:41:50.427
2 9 2012-04-02 03:41:50.727
1 40 2012-04-02 16:21:50.027
1 49 2012-04-02 16:55:50.127
1 74 2012-04-02 23:41:50.627
1 90 2012-04-02 23:44:50.427
3 3 2012-04-02 23:51:50.827
3 -10 2012-04-03 07:01:00.627
1 0 2012-04-03 13:41:50.427
2 999 2012-04-03 03:41:50.727
1 50 2012-04-03 15:21:50.027
1 49 2012-04-03 16:55:50.127
1 74 2012-04-03 23:41:50.627
2 -10 2012-04-03 07:41:50.727
1 100 2012-04-03 23:44:50.427
3 0 2012-04-03 23:51:50.827
expected output:
id The account id
date The data component which was used to produce the date in the row
balance_last_measurement The balance at the last measurement of the date
difference The difference in balance between the first- and last measurement of the date
On 2012-04-02 id 2 only has 1 measurement which sets the difference value equal to the last(and only) measurement.
id date balance_last_measurement difference
1 2012-04-02 90 35
1 2012-04-03 100 10
2 2012-04-02 9 9
2 2012-04-03 -10 -19
3 2012-04-02 3 -37
3 2012-04-03 0 37
update 2012-04-10 20:06
The answer from Raphaël Althaus is really good but i did make a small mistake in the original request. The difference field in the 'expected output' should be either:
the difference between the last measurement of the previous day and the last measurement of the day
if there is no previous day then first measurement of the day should be used and the last measurement
Is this possible at all? It seems to be quite complex?
I would try something like that.
var query = db.Balances
.OrderBy(m => m.Id)
.ThenBy(m => m.CreationDate)
.GroupBy(m => new
{
id = m.Id,
year = SqlFunctions.DatePart("mm", m.CreationDate),
month = SqlFunctions.DatePart("dd", m.CreationDate),
day = SqlFunctions.DatePart("yyyy", m.CreationDate)
}).ToList()//enumerate there, this is what we need from db
.Select(g => new
{
id = g.Key.id,
date = new DateTime(g.Key.year, g.Key.month, g.Key.day),
last_balance = g.Select(m => m.BalanceValue).LastOrDefault(),
difference = (g.Count() == 1 ? g.First().BalanceValue : g.Last().BalanceValue - g.First().BalanceValue)
});
Well, a probable not optimized solution, but just see if it seems to work.
First, we create a result class
public class BalanceResult
{
public int Id { get; set; }
public DateTime CreationDate { get; set; }
public IList<int> BalanceResults { get; set; }
public int Difference { get; set; }
public int LastBalanecResultOfDay {get { return BalanceResults.Last(); }}
public bool HasManyResults {get { return BalanceResults != null && BalanceResults.Count > 1; }}
public int DailyDifference { get { return HasManyResults ? BalanceResults.Last() - BalanceResults.First() : BalanceResults.First(); } }
}
then we change a little bit our query
var query = db.Balances
.GroupBy(m => new
{
id = m.Id,
year = SqlFunctions.DatePart("mm", m.CreationDate),
month = SqlFunctions.DatePart("dd", m.CreationDate),
day = SqlFunctions.DatePart("yyyy", m.CreationDate)
}).ToList()//enumerate there, this is what we need from db
.Select(g => new BalanceResult
{
Id = g.Key.id,
CreationDate = new DateTime(g.Key.year, g.Key.month, g.Key.day),
BalanceResults = g.OrderBy(l => l.CreationDate).Select(l => l.BalanceValue).ToList()
}).ToList();
and finally
foreach (var balanceResult in balanceResults.ToList())
{
var previousDayBalanceResult = balanceResults.FirstOrDefault(m => m.Id == balanceResult.Id && m.CreationDate == balanceResult.CreationDate.AddDays(-1));
balanceResult.Difference = previousDayBalanceResult != null ? balanceResult.LastBalanecResultOfDay - previousDayBalanceResult.LastBalanecResultOfDay : balanceResult.DailyDifference;
}
as indicated, performance (use of dictionaries, for example), code readability should of course be improved, but... that's the idea !

Resources