Merge lp:~martin-borho/ubuntu-weather-app/backend-upgrade into lp:ubuntu-weather-app/obsolete.trunk

Proposed by Martin Borho
Status: Merged
Approved by: Raúl Yeguas
Approved revision: 40
Merged at revision: 41
Proposed branch: lp:~martin-borho/ubuntu-weather-app/backend-upgrade
Merge into: lp:ubuntu-weather-app/obsolete.trunk
Diff against target: 275 lines (+93/-43)
5 files modified
components/LocationTab.qml (+8/-7)
components/SettingsDialog.qml (+1/-1)
components/Storage.qml (+1/-0)
components/WeatherApi.js (+66/-26)
ubuntu-weather-app.qml (+17/-9)
To merge this branch: bzr merge lp:~martin-borho/ubuntu-weather-app/backend-upgrade
Reviewer Review Type Date Requested Status
Raúl Yeguas Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+169423@code.launchpad.net

Commit message

Improvements to API-Worker to reduce HTTP calls against weather api.

Description of the change

- Weather data will not get refreshed unless stored data is older than 30min or an refresh is forced.
- Metric and imperial values are getting precalculated in API-Worker now. Refresh of weather data isn't neccessary anymore after changing scale in settings.

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Raúl Yeguas (neokore) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'components/LocationTab.qml'
--- components/LocationTab.qml 2013-06-12 17:44:34 +0000
+++ components/LocationTab.qml 2013-06-14 13:37:21 +0000
@@ -19,7 +19,8 @@
1919
20 Component.onCompleted: {20 Component.onCompleted: {
21 var locData = mainView.locationsList[locationIndex],21 var locData = mainView.locationsList[locationIndex],
22 currentData = locData.current.results;22 currentData = locData.current.results,
23 units = (mainView.settings.units === 'imperial') ? 'imperial' : 'metric';
2324
24 // set location data as property25 // set location data as property
25 locationData = locData;26 locationData = locData;
@@ -32,9 +33,9 @@
32 // set current Condition33 // set current Condition
33 currentCondition.condition = currentData.condition.id;34 currentCondition.condition = currentData.condition.id;
34 currentCondition.icon = currentData.condition.icon;35 currentCondition.icon = currentData.condition.icon;
35 currentCondition.currentTemp = currentData.temp;36 currentCondition.currentTemp = currentData[units].temp;
36 currentCondition.minTemp = currentData.temp_min;37 currentCondition.minTemp = currentData[units].temp_min;
37 currentCondition.maxTemp = currentData.temp_max;38 currentCondition.maxTemp = currentData[units].temp_max;
3839
39 // set daily forecasts40 // set daily forecasts
40 var dailyForecasts = locationData.daily.results,41 var dailyForecasts = locationData.daily.results,
@@ -43,8 +44,8 @@
43 dayForecastModel.append({44 dayForecastModel.append({
44 dateRel: "",//Tomorrow",45 dateRel: "",//Tomorrow",
45 date: formatTimestamp(dailyForecasts[x].timestamp, 'dddd, dd MMMM yyyy'),46 date: formatTimestamp(dailyForecasts[x].timestamp, 'dddd, dd MMMM yyyy'),
46 temp: dailyForecasts[x].max,47 temp: dailyForecasts[x][units].max,
47 tempMin: dailyForecasts[x].min,48 tempMin: dailyForecasts[x][units].min,
48 cond: dailyForecasts[x].condition.id,49 cond: dailyForecasts[x].condition.id,
49 condIcon: dailyForecasts[x].condition.icon50 condIcon: dailyForecasts[x].condition.icon
50 });51 });
@@ -61,7 +62,7 @@
61 text: i18n.tr("Refresh")62 text: i18n.tr("Refresh")
6263
63 onTriggered: {64 onTriggered: {
64 mainView.refreshData();65 mainView.refreshData(false, true);
65 }66 }
66 }67 }
67 Action {68 Action {
6869
=== modified file 'components/SettingsDialog.qml'
--- components/SettingsDialog.qml 2013-06-12 17:13:32 +0000
+++ components/SettingsDialog.qml 2013-06-14 13:37:21 +0000
@@ -52,7 +52,7 @@
52 for(var settingName in storedSettings) {52 for(var settingName in storedSettings) {
53 settings[settingName] = storedSettings[settingName];53 settings[settingName] = storedSettings[settingName];
54 }54 }
55 refreshData();55 refreshData(true);
56 });56 });
57 }57 }
58 }58 }
5959
=== modified file 'components/Storage.qml'
--- components/Storage.qml 2013-06-09 15:42:41 +0000
+++ components/Storage.qml 2013-06-14 13:37:21 +0000
@@ -83,6 +83,7 @@
83 for(var i = 0; i < rs.rows.length; i++) {83 for(var i = 0; i < rs.rows.length; i++) {
84 var row = rs.rows.item(i),84 var row = rs.rows.item(i),
85 locData = JSON.parse(row.data);85 locData = JSON.parse(row.data);
86 locData["updated"] = parseInt(row.date, 10);
86 locData["db"] = {id: row.id, updated: new Date(parseInt(row.date, 10))};87 locData["db"] = {id: row.id, updated: new Date(parseInt(row.date, 10))};
87 locations.push(locData);88 locations.push(locData);
88 }89 }
8990
=== modified file 'components/WeatherApi.js'
--- components/WeatherApi.js 2013-06-12 16:41:07 +0000
+++ components/WeatherApi.js 2013-06-14 13:37:21 +0000
@@ -1,11 +1,25 @@
1.pragma library1.pragma library
2/**
3* Version of the response data format.
4* Increase this number to force a refresh.
5*/
6var RESPONSE_DATA_VERSION = 1;
7
8/**
9* Helper functions
10*/
11function calcFahrenheit(celsius) {
12 return celsius * 1.8 + 32;
13}
14function calcMph(kmh) {
15 return kmh*0.62137;
16}
217
3var OpenWeatherMapApi = (function() {18var OpenWeatherMapApi = (function() {
4 /**19 /**
5 provides neccessary methods for requesting and preparing data from OpenWeatherMap.org20 provides neccessary methods for requesting and preparing data from OpenWeatherMap.org
6 */21 */
7 var _baseUrl = "http://api.openweathermap.org/data/2.5/";22 var _baseUrl = "http://api.openweathermap.org/data/2.5/";
8
9 //23 //
10 function _buildSearchResult(request, data) {24 function _buildSearchResult(request, data) {
11 var searchResult = {25 var searchResult = {
@@ -31,12 +45,20 @@
31 var result = {45 var result = {
32 timestamp: data.dt,46 timestamp: data.dt,
33 date: (data.date !== undefined) ? data.date : ((data.dt_txt !== undefined) ? data.dt_txt: null),47 date: (data.date !== undefined) ? data.date : ((data.dt_txt !== undefined) ? data.dt_txt: null),
34 temp: data.main.temp,48 metric: {
35 temp_min: data.main.temp_min,49 temp:data.main.temp,
36 temp_max: data.main.temp_max,50 temp_min: data.main.temp_min,
51 temp_max: data.main.temp_max
52 },
53 imperial: {
54 temp: calcFahrenheit(data.main.temp),
55 temp_min: calcFahrenheit(data.main.temp_min),
56 temp_max: calcFahrenheit(data.main.temp_max)
57 },
37 humidity: data.main.humidity,58 humidity: data.main.humidity,
38 pressure: data.main.pressure,59 pressure: data.main.pressure,
39 wind_speed: data.wind.speed,60 wind_speed_kmh: data.wind.speed,
61 wind_speed_mph: calcMph(data.wind.speed),
40 wind_deg: data.wind.deg,62 wind_deg: data.wind.deg,
41 condition: data.weather[0]63 condition: data.weather[0]
42 };64 };
@@ -76,12 +98,22 @@
76 // TODO date_txt98 // TODO date_txt
77 forecastResult.results.push({99 forecastResult.results.push({
78 timestamp: f.dt,100 timestamp: f.dt,
79 day: f.temp.day,101 metric: {
80 min: f.temp.min,102 day: f.temp.day,
81 max: f.temp.max,103 min: f.temp.min,
82 night: f.temp.night,104 max: f.temp.max,
83 eve: f.temp.eve,105 night: f.temp.night,
84 morn: f.temp.morn,106 eve: f.temp.eve,
107 morn: f.temp.morn
108 },
109 imperial: {
110 day: calcFahrenheit(f.temp.day),
111 min: calcFahrenheit(f.temp.min),
112 max: calcFahrenheit(f.temp.max),
113 night: calcFahrenheit(f.temp.night),
114 eve: calcFahrenheit(f.temp.eve),
115 morn: calcFahrenheit(f.temp.morn)
116 },
85 pressure: f.pressure,117 pressure: f.pressure,
86 humidity: f.humidity,118 humidity: f.humidity,
87 condition: f.weather[0],119 condition: f.weather[0],
@@ -228,7 +260,8 @@
228 },260 },
229 response = {261 response = {
230 location: params.location,262 location: params.location,
231 db: (params.db) ? params.db : null263 db: (params.db) ? params.db : null,
264 format: RESPONSE_DATA_VERSION
232 },265 },
233 addDataToResponse = (function(data) {266 addDataToResponse = (function(data) {
234 response[data.request.type] = data;267 response[data.request.type] = data;
@@ -256,16 +289,15 @@
256 "openweathermap": OpenWeatherMapApi289 "openweathermap": OpenWeatherMapApi
257});290});
258291
259292/**
260/*293* following WorkerScript handles the data requests against the weather API.
261 following WorkerScript handles the data requests against the weather API.294* "message" requires a "params" property with the required params to perform
262 "message" requires a "params" property with the required params to perform295* the API call and an "action" property, which will be added also to the response.
263 the API call and an "action" property, which will be added also to the response.
264*/296*/
265if(typeof WorkerScript != "undefined") {297if(typeof WorkerScript != "undefined") {
266 WorkerScript.onMessage = function(message) {298 WorkerScript.onMessage = function(message) {
267 // handles the response data299 // handles the response data
268 var finished = function(result) {300 var finished = function(result) {
269 WorkerScript.sendMessage({301 WorkerScript.sendMessage({
270 action: message.action,302 action: message.action,
271 result: result303 result: result
@@ -288,25 +320,33 @@
288 } else if(message.action === "updateData") {320 } else if(message.action === "updateData") {
289 var locLength = message.params.locations.length,321 var locLength = message.params.locations.length,
290 locUpdated = 0,322 locUpdated = 0,
291 result = [];323 result = [],
324 now = new Date().getTime();
292 if(locLength > 0) {325 if(locLength > 0) {
293 message.params.locations.forEach(function(loc) {326 message.params.locations.forEach(function(loc) {
294 var updatedHnd = function (newData) {327 var updatedHnd = function (newData, cached) {
295 locUpdated += 1;328 locUpdated += 1;
329 newData["save"] = (cached === true) ? false : true;
296 result.push(newData);330 result.push(newData);
297 if(locUpdated === locLength) {331 if(locUpdated === locLength) {
298 result.sort(sortDataResults);332 result.sort(sortDataResults);
299 finished(result);333 finished(result);
300 }334 }
301 },335 },
302 units = (message.params.units === "metric" || message.params.units === "imperial")
303 ? message.params.units : "metric",
304 params = {336 params = {
305 location:loc.location,337 location:loc.location,
306 db: loc.db,338 db: loc.db,
307 units: units339 units: 'metric'
308 };340 },
309 WeatherApi.getLocationData(params, updatedHnd, onError);341 secsFromLastFetch = (now-loc.updated)/1000;
342 //
343 if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > 1800){
344 // data older than 30min, location is new or data format is deprecated
345 WeatherApi.getLocationData(params, updatedHnd, onError);
346 } else {
347 console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
348 updatedHnd(loc, true);
349 }
310 })350 })
311 } else {351 } else {
312 finished(result);352 finished(result);
313353
=== modified file 'ubuntu-weather-app.qml'
--- ubuntu-weather-app.qml 2013-06-12 17:44:34 +0000
+++ ubuntu-weather-app.qml 2013-06-14 13:37:21 +0000
@@ -25,7 +25,9 @@
25 if(!messageObject.error) {25 if(!messageObject.error) {
26 if(messageObject.action === "updateData") {26 if(messageObject.action === "updateData") {
27 messageObject.result.forEach(function(loc) {27 messageObject.result.forEach(function(loc) {
28 storage.updateLocation(loc.db.id, loc);28 if(loc["save"] === true) {
29 storage.updateLocation(loc.db.id, loc);
30 }
29 });31 });
30 buildTabs(messageObject.result);32 buildTabs(messageObject.result);
31 }33 }
@@ -67,15 +69,21 @@
67 tabsObject.selectedTabIndex = locLength -1;69 tabsObject.selectedTabIndex = locLength -1;
68 }70 }
6971
70 function refreshData() {72 function refreshData(from_storage, force_refresh) {
71 if(bigLoading === null)73 if(bigLoading === null) {
72 loading.running = true;74 loading.running = true;
73 storage.getLocations(function(locations) {75 }
74 locationDataWorker.sendMessage({76 //
75 action: "updateData",77 if(from_storage === true && force_refresh !== true) {
76 params: {locations:locations, units:settings["units"]}78 storage.getLocations(buildTabs);
77 })79 } else {
78 });80 storage.getLocations(function(locations) {
81 locationDataWorker.sendMessage({
82 action: "updateData",
83 params: {locations:locations, force: force_refresh}
84 })
85 });
86 }
79 }87 }
8088
81 function showLocationManager() {89 function showLocationManager() {

Subscribers

People subscribed via source and target branches