-
Notifications
You must be signed in to change notification settings - Fork 55
/
uploader_service.js
134 lines (126 loc) · 4.82 KB
/
uploader_service.js
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
/**
* :copyright (c) 2014 - 2020, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved.
* :author
*/
// uploader services
angular.module('BE.seed.service.uploader', []).factory('uploader_service', [
'$http',
'$q',
'$timeout',
'user_service',
function ($http, $q, $timeout, user_service) {
var uploader_factory = {};
/**
* create_dataset: AJAX request to create a new dataset/import record
* should have a response like:
* {
* "status": "success",
* "import_record_id": 4,
* "import_record_name": "2013 city compliance dataset"
* }
* or
* {
* "status": "error",
* "message": "name already in use"
* }
*/
uploader_factory.create_dataset = function (dataset_name) {
// timeout here for testing
return $http.post('/api/v3/datasets/', {
name: dataset_name
}, {
params: {
organization_id: user_service.get_organization().id
}
}).then(function (response) {
return response.data;
});
};
/**
* validate_use_cases
* This service call will simply call a view on the backend to validate
* BuildingSync files with use cases
* @param file_id: the pk of a ImportFile object we're going to save raw.
*/
uploader_factory.validate_use_cases = function (file_id) {
var org_id = user_service.get_organization().id;
return $http.post('/api/v3/import_files/' + file_id + '/validate_use_cases/?organization_id=' + org_id.toString())
.then(function (response) {
return response.data;
})
.catch(function (err) {
if (err.data.status === 'error') {
return err.data;
}
// something unexpected happened... throw it
throw err;
});
};
/**
* save_raw_data
* This service call will simply call a view on the backend to save raw
* data into BuildingSnapshot instances.
* @param file_id: the pk of a ImportFile object we're going to save raw.
*/
uploader_factory.save_raw_data = function (file_id, cycle_id) {
return $http.post('/api/v3/import_files/' + file_id + '/start_save_data/', {
cycle_id: cycle_id,
}, {
params: { organization_id: user_service.get_organization().id }
}).then(function (response) {
return response.data;
});
};
/**
* check_progress: gets the progress for saves, maps, and matches
* @param progress_key: progress_key to grab the progress
*/
uploader_factory.check_progress = function (progress_key) {
return $http.get('/api/v3/progress/' + progress_key + '/').then(function (response) {
if (response.data.status === 'error') return $q.reject(response);
else return response.data;
});
};
/**
* check_progress_loop: check loop to update the progress bar
*
* @param {string} progress_key: key
* @param {number} offset: where to start the progress bar
* @param {number} multiplier: multiplier for progress val
* @param {fn} success_fn: function to call when progress is done
* @param {fn} failure_fn: function to call when progress is done and the result was not success
* @param {obj} progress_bar_obj: progress bar object, attr 'progress'
* is set with the progress
*/
uploader_factory.check_progress_loop = function (progress_key, offset, multiplier, success_fn, failure_fn, progress_bar_obj, debug) {
debug = !_.isUndefined(debug);
uploader_factory.check_progress(progress_key).then(function (data) {
$timeout(function () {
progress_bar_obj.progress = _.clamp((data.progress * multiplier) + offset, 0, 100);
progress_bar_obj.status_message = data.status_message;
if (data.progress < 100) {
uploader_factory.check_progress_loop(progress_key, offset, multiplier, success_fn, failure_fn, progress_bar_obj, debug);
} else {
success_fn(data);
}
}, 750);
}, failure_fn);
};
uploader_factory.pm_meters_preview = function (file_id, org_id) {
return $http.get(
'/api/v3/import_files/' + file_id + '/pm_meters_preview/',
{ params: { organization_id: org_id } }
).then(function (response) {
return response.data;
});
};
uploader_factory.greenbutton_meters_preview = function (file_id, org_id, view_id) {
return $http.get(
'/api/v3/import_files/' + file_id + '/greenbutton_meters_preview/',
{ params: { organization_id: org_id, view_id } }
).then(function (response) {
return response.data;
});
};
return uploader_factory;
}]);