-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Store history data each hour (#109)
* Update .gitignore * Implement translation package & translate ErrorScreen * Finish translations setup * Minor lint fixes * Possible dark video background issue fix * Update App/localization/languages/en.json Co-Authored-By: lucienbl <lucien.blunk.lallet@gmail.com> * Update .gitignore * Remove unused string * Add history manager * Minor lint fixes * Store lat & lng in history * Store coords with the history * Expo version bump to 33.0.0 * refactor: Use eslint instead of semistandard (#72) * Add eslint * Run eslint on files * Update contributing guide * Update README * Typo * chore: Use correct GPLv3 header (#77) * chore: Update README.md * Spanish translation (#79) * Spanish translation * correction * improvement spanish translation * fix: Add missing translations (#81) * Add spanish * Add missing translations * feat: Spanish Translations added (#83) * Spanish translation * correction * improvement spanish translation * missing spanish translations added * chore: Update README.md with translations (#84) * Update README.md * Update README.md * feat: Add button to change language (#85) * cherry-pick * Add button on picker * Add button to change language * fix: removes undefined style (#87) * chore: Update README.md (#88) * chore: Update axios (#90) * chore(deps-dev): Bump jest from 24.5.0 to 24.8.0 (#91) Bumps [jest](https://github.com/facebook/jest) from 24.5.0 to 24.8.0. - [Release notes](https://github.com/facebook/jest/releases) - [Changelog](https://github.com/facebook/jest/blob/master/CHANGELOG.md) - [Commits](jestjs/jest@v24.5.0...v24.8.0) * chore(deps): Bump sentry-expo from 1.11.2 to 1.12.0 (#92) Bumps sentry-expo from 1.11.2 to 1.12.0. * chore(deps-dev): Bump eslint-plugin-import from 2.17.2 to 2.17.3 (#95) Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.17.2 to 2.17.3. - [Release notes](https://github.com/benmosher/eslint-plugin-import/releases) - [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md) - [Commits](import-js/eslint-plugin-import@v2.17.2...v2.17.3) * chore(deps): Bump truncate from 2.0.1 to 2.1.0 (#94) Bumps [truncate](https://github.com/FGRibreau/node-truncate) from 2.0.1 to 2.1.0. - [Release notes](https://github.com/FGRibreau/node-truncate/releases) - [Changelog](https://github.com/FGRibreau/node-truncate/blob/master/CHANGELOG.md) - [Commits](FGRibreau/node-truncate@v2.0.1...v2.1.0) * Expo version bump to 33.0.0 * Minor type changes * Migrate expo packages * Minor fixes * Update Loading.js * Remove unused package import * ESLint fixes * Import TaskManager * Remove TS * Auto increment SQL rows * Remove debug console.log * Factorize apiCall * Test getData * Revert "Am feature history"
- Loading branch information
1 parent
8f3043b
commit 079ab7c
Showing
4 changed files
with
210 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class AqiHistory { | ||
_id; | ||
_location; | ||
_lat; | ||
_lng; | ||
_rawPm25; | ||
_creationTime; | ||
|
||
constructor ({ id, location, lat, lng, rawPm25, creationTime }) { | ||
this._id = id; | ||
this._location = location; | ||
this._lat = lat; | ||
this._lng = lng; | ||
this._rawPm25 = rawPm25; | ||
this._creationTime = creationTime; | ||
} | ||
|
||
get id () { | ||
return this._id; | ||
} | ||
|
||
get location () { | ||
return this._location; | ||
} | ||
|
||
get lat () { | ||
return this._lat; | ||
} | ||
|
||
get lng () { | ||
return this._lng; | ||
} | ||
|
||
get rawPm25 () { | ||
return this._rawPm25; | ||
} | ||
|
||
get creationTime () { | ||
return this._creationTime; | ||
} | ||
} | ||
|
||
export default AqiHistory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { SQLite } from 'expo-sqlite'; | ||
import AqiHistory from './AqiHistory'; | ||
|
||
export const SAVE_DATA_INTERVAL = 3600000; // 1 hour | ||
const DB_AQI_HISTORY = 'aqi-history'; | ||
|
||
export const initDb = () => { | ||
return SQLite.openDatabase(DB_AQI_HISTORY, '1.0', 'Aqi History', 5 * 1024 * 1024); | ||
}; | ||
|
||
export const init = async () => { | ||
const db = await initDb(); | ||
|
||
await db.transaction((tx) => { | ||
tx.executeSql( | ||
'create table if not exists history(id integer primary key autoincrement, location varchar(255) not null, lat numeric not null, lng numeric not null, rawPm25 decimal not null, creationTime timestamp not null)', | ||
[], | ||
() => {}, | ||
(transaction, error) => console.log('DB init error', error) | ||
); | ||
}); | ||
|
||
return db; | ||
}; | ||
|
||
export const isSaveNeeded = async () => { | ||
const db = await init(); | ||
|
||
const promise = new Promise((resolve, reject) => { | ||
db.readTransaction((tx) => { | ||
tx.executeSql( | ||
'select * from history order by id desc limit 1', | ||
[], | ||
(transaction, resultSet) => { | ||
if (resultSet.rows.length === 0) { | ||
resolve(true); | ||
} else if ((resultSet.rows.item(0).creationTime + SAVE_DATA_INTERVAL) < Date.now()) { | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
}, | ||
(transaction, error) => reject(error) | ||
); | ||
}); | ||
}); | ||
|
||
return promise; | ||
}; | ||
|
||
export const saveData = async (location, rawPm25, { latitude, longitude }) => { | ||
const db = await init(); | ||
|
||
db.transaction((tx) => { | ||
tx.executeSql( | ||
'insert into history (location, lat, lng, rawPm25, creationTime) values (?, ?, ?, ?, ?)', | ||
[location, latitude, longitude, rawPm25, Date.now()], | ||
() => {}, | ||
(transaction, error) => console.log('DB insert error', error) | ||
); | ||
}); | ||
}; | ||
|
||
export const getData = async (limit) => { | ||
const db = await init(); | ||
|
||
const promise = new Promise((resolve, reject) => { | ||
db.readTransaction((tx) => { | ||
tx.executeSql( | ||
'select * from history order by creationTime desc limit ' + limit, | ||
[], | ||
(transaction, resultSet) => { | ||
let data = []; | ||
|
||
for (let i = 0; i < resultSet.rows.length; i++) { | ||
const result = new AqiHistory(resultSet.rows.item(i)); | ||
data.push(result); | ||
} | ||
|
||
resolve(data); | ||
}, | ||
(transaction, error) => reject(error) | ||
); | ||
}); | ||
}); | ||
|
||
return promise; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as AqiHistoryDb from './AqiHistoryDb'; | ||
|
||
export { | ||
AqiHistoryDb | ||
}; |