subFilterService

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Sub Filter Service
app.service('SubFilterService', function($http, $q, $timeout) {
var that = this;
// Shared data
this.filter = {
type: "",
value: "-1",
quarterData: {
pipelines: [],
shippers: [],
agents: [],
points: []
},
primaryFilters: {
pipeline: ['shipper','point','agent', 'rate_schedule', 'contract'],
shipper: ['point','agent', 'rate_schedule']
}
};
// Helper function to search collection
// Returns only those records whose field(field_name) matched field_value
// Params
// - collection: a list to search
// - field_name: string representing the field being checked
// - field_Value: string representing the value of the field for the match
function search(collection, field_name, field_value) {
var matches = [];
// console.log('collection', collection)
collection.forEach(function(data) {
// If the field's value matches to the field_value, push it into the matches list
if (data[field_name] === field_value) {
matches.push(data);
}
});
return matches;
}
// Helper function to calculate aggregate information for each name in the list
// Returns a unique list of objects, each having a unique name_field value
// Params:
// - list: a list of all records whose aggregate information is collected based on names
// - name_field: string representing the field by which the records are aggregated
// - quant_field: string, either "contract" or "point". Point is due to agent points having point_storage_msq
// - defer: promise object via which the result of the aggregation is returned, necessary to prevent the screen from freezing
function collectAggregateData(list, name_field, quant_field, defer) {
var unique_names = [];
var aggregate_data = [];
var contract_storage_msq = quant_field + "_storage_msq"
var contract_transport_mdq = quant_field + "_transport_mdq"
// Loop through the list
list.forEach(function(data) {
var name = data[name_field];
// If record by name_field doesnt exist in unique_names
if (unique_names.indexOf(name) == -1) {
unique_names.push(name);
}
});
// For each unique name
unique_names.forEach(function(name) {
// Object representing the aggregate information
var info = {
name: name,
contract_storage: 0,
volume_storage: 0,
contract_transport: 0,
volume_transport: 0,
rate_schedule: 0
}
// Loop though all the records
list.forEach(function(data) {
// If the record name matches the unique name
if (data[name_field] === name) {
// In case contract_storage_msq is null
data[contract_storage_msq] = data[contract_storage_msq] || '';
//Ig the rate_schedule is not empty
if(data['rate_schedule'] && data['rate_schedule'].trim() !== "") {
info.rate_schedule += 1;
}
// If the contract_storage_msq is not empty
if (data[contract_storage_msq] && data[contract_storage_msq].trim() !== "") {
info.contract_storage += 1 // Add 1 to the appropriate contract number
info.volume_storage += parseInt(data[contract_storage_msq].trim()); // Add the quantity to the appropriate quantity
}
// Same as above but for contract_transport_mdq
if (data[contract_transport_mdq] && data[contract_transport_mdq].trim() !== "") {
info.contract_transport += 1
info.volume_transport += parseInt(data[contract_transport_mdq].trim());
}
}
});
// Push the aggregate information into main list
aggregate_data.push(info);
});
// Resolve promise
defer.resolve(aggregate_data);
};
// Helper function that creates a promise to be resolved by the aggregated data
// Params
// - data_source: function to be executed to get a list of records to be aggregated
// - name_field: string representing the field that holds the name identifier of the records
// - quant_field: string, "contract" or "point"
// - scope: directive's scope object, used to assign an aggregated records list to the scope.aggregated_data
function promiseHelper(data_source, name_field, quant_field, scope) {
// Set the scope.predicate for table sort
// Create a promise
var defer = $q.defer();
// When the promise gets resolved
defer.promise.then(function(records) {
scope.total = records.length;
// scope.aggregate_data = records.slice(scope.start, scope.end);
scope.aggregate_data = records
});
// Delay execution of aggregate data
$timeout(function() {
// Code to search for relevent records and aggregate by name_field
collectAggregateData(
data_source,
name_field,
quant_field,
defer
);
}, 100);
}
// Extract the dataset needed to populate the primary filter table
// Params
// - records: string identifying the collection needed from filter.quarterData
// - fields: array of fields to be extracted from each record
function getDataset(scope, records, fields, start, end) {
var dataset = [];
var quarterData = that.filter.quarterData;
quarterData[records].slice(start,end).forEach(function(record) {
var data = [];
fields.forEach(function(field) {
if(field === 'agent_name_clean') {
if(record.agents[0]) data.push(record.agents[0].agent_name_clean)
else data.push(" - ")
}
else data.push(record[field]);
})
//If record is a pipeline
if(record.pipeline_name_clean) {
data.push(record.pipeline_name_clean);
}
// If record is an agent
if(record.agent_name_clean) {
data.push(record.agent_name_clean);
}
dataset.push(data)
});
return dataset;
}
function getTableData(scope, filterType, start, end, defer) {
if (filterType === "shipper") {
var headers = ["Shipper Name", "Affiliate","Rate Schedule",
"Contract No.", "MDQ", "MSQ", "Start Date",
"End Date","Agents","Points" ];
var fields = ["shipper_name_clean","shipper_affiliate_ind","rate_schedule","contract_number",
"contract_transport_mdq","contract_storage_msq","start_date","end_date",
"agent_name_clean", "point_count"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.shippers.length});
}
else if (filterType === "agent") {
var headers = ["Agent Name", "Affiliate","Shipper Name",
"Contract No.", "MDQ", "MSQ",
"Rate Schedule","Negotiated Rate", "Points","Start Date","End Date"];
var fields = ["agent_name_clean","shipper_affiliate_ind","shipper_name_clean",
"contract_number","contract_transport_mdq","contract_storage_msq",
"rate_schedule","negotiated_rate_ind","points_count","start_date","end_date"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.agents.length});
}
else if (filterType === "point") {
var headers = ["Point Count","Shipper Name", "Affiliate", "Rate Schedule",
"Contract No.", "MDQ", "MSQ", "Start Date",
"End Date", "Negotiated Rate", "Agents"];
var fields = ["point_count","shipper_name_clean","shipper_affiliate_ind","rate_schedule","contract_number",
"contract_transport_mdq","contract_storage_msq","start_date","end_date",
"negotiated_rate_ind", "agent_name_clean"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.points.length});
}
else if (filterType === "rate_schedule") {
var headers = ["Rate Schedule","Shipper Name", "Agents",
"Contract No.", "MDQ", "MSQ", "Negotiated Rate", "Start Date","End Date" ];
var fields = ["rate_schedule","shipper_name_clean","agent_count","contract_number",
"contract_transport_mdq","contract_storage_msq","negotiated_rate_ind","start_date","end_date"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.shippers.length});
}
else if (filterType === "contract") {
var headers = ["Shipper Name", "Affiliate", "Rate Schedule",
"Contract No.", "MDQ", "MSQ", "Start Date","End Date",
"Negotiated Rate","Agents"];
var fields = ["shipper_name_clean","shipper_affiliate_ind","rate_schedule",
"contract_number","contract_transport_mdq","contract_storage_msq","start_date","end_date",
"negotiated_rate_ind","agent_name_clean"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.shippers.length});
}
//If select new pipeline Name and filterType isn't set
else {
var headers = ["Rate Schedule","Shipper Name", "Agents",
"Contract No.", "MDQ", "MSQ", "Negotiated Rate", "Start Date","End Date" ];
var fields = ["rate_schedule","shipper_name_clean","agent_count","contract_number",
"contract_transport_mdq","contract_storage_msq","negotiated_rate_ind","start_date","end_date"];
var dataset = getDataset(scope, "shippers", fields, start, end);
defer.resolve({headers: headers, dataset: dataset, total: that.filter.quarterData.shippers.length});
}
}
// Main ugly function that checks the primay and sub filter and popluates the sub filter tables based on them
// Params:
// - scope: direcitve's scope, used to set the directive scope.aggregate_data value
// - element: direcitve's angular element, used to empty the element's table's body
this.getFilterValue = function(scope, element) {
that.filter.value = scope.filter.value;
}
this.getFilterData = function(scope, element) {
var subFilter = that.filter.type;
var quarterData = that.filter.quarterData;
console.log('scope.primaryFilter.value is',scope.primaryFilter.value)
var tbody = element.find('tbody');
tbody.find('td').remove();
// Sub filter is SHIPPER
if (subFilter === 'shipper') {
promiseHelper(search(quarterData.shippers, "parent_pipeline_id", scope.primaryFilter.value), "shipper_name_clean", "contract", scope);
}
// Sub filter is AGENT
else if (subFilter === 'agent') {
promiseHelper(search(quarterData.agents, "parent_pipeline_id", scope.primaryFilter.value), "agent_name_clean", "contract", scope);
}
// Sub filter is POINT
else if (subFilter === 'point') {
promiseHelper(search(quarterData.points, "parent_pipeline_id", scope.primaryFilter.value), "point_name_clean", "point", scope);
}
else if (subFilter === 'rate_schedule') {
promiseHelper(search(quarterData.shippers, "parent_pipeline_id", scope.primaryFilter.value), "rate_schedule", "contract", scope);
}
else if (subFilter === 'contract') {
promiseHelper(search(quarterData.shippers, "parent_pipeline_id", scope.primaryFilter.value), "contract_number", "contract", scope);
}
}
this.redrawTable = function(element,scope) {
var filterType = that.filter.type;
var defer = $q.defer();
defer.promise.then(function(data) {
scope.length = data.total;
if(data.total != 0 && scope.isBigTable){
scope.isShow = true;
scope.countOfPages = Math.ceil(data.total/10);
}
// Populate table header
var headers = data.headers;
var header_tags = "<tr>"
var pointHeaders = ["Shipper Name", "Point Type","Point Name",
"Point ID", "Point ID type", "Point Zone", "Point MDQ",
"Point MSQ"];
headers.forEach(function(header) {
header_tags += "<th>" + header + "</th>";
});
header_tags += "</tr>"
//element.find("thead").html(header_tags);
scope.headers = headers;
scope.pointHeaders = pointHeaders;
// Populate table rows
var row_tags = ""
var dataset = data.dataset;
dataset.forEach(function(data) {
var row = "<tr>";
data.forEach(function(field) {
row += "<td>" + field + "</td>";
});
row += "</tr>";
row_tags += row;
});
scope.rows = dataset
});
$timeout(function() {
getTableData(scope ,filterType, scope.start, scope.end, defer);
}, 100);
}
this.getQuarterData = function(scope, element) {
$(".loading-quarter-message").html("Loading data .... ");
// Get the new quarter value
var nameValue = scope.filter.value;
console.log('nameValue',nameValue)
// HTTP requests to be resolved as promises
var pipelinePromise = $http.get('/pipeline/all?pipe_id=' + nameValue);
// Resolve above promises in the order of the list argument
var allPromise = $q.all([pipelinePromise]);
// Set the quarter data to the appropriate result
allPromise.then(function(result) {
console.log("allPromise", result);
that.filter.quarterData.pipelines = result[0].data.pipelines;
that.filter.quarterData.agents = [];
that.filter.quarterData.points = [];
that.filter.quarterData.shippers = [];
that.filter.quarterData.pipelines.forEach(function(item){
item.shippers.forEach(function(shipper){
that.filter.quarterData.shippers.push(shipper);
that.filter.quarterData.agents = that.filter.quarterData.agents.concat(shipper.agents)
that.filter.quarterData.points = that.filter.quarterData.points.concat(shipper.points)
});
});
// console.log('totalList is', that.filter.quarterData)
// Hide loading quarter label
$(".loading-quarter-message").html("");
that.redrawTable(scope,element);
});
}
});