Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Delete All Measurements #185

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
907 changes: 456 additions & 451 deletions data/lang-en.csv

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@
"Settings.SendEmail.Error": "Please send an email to bugs@openobservatory.org with information on the app and iOS version. Tap \"Copy to clipboard\" below to copy our email address.",
"Settings.Language.Current": "Current app language is {lang}",
"Settings.Language.Label": "Language",
"Settings.Storage.Label": "Storage Usage",
"Settings.Storage.Usage.Label": "Total storage usage",
"Settings.Storage.Delete": "Delete all uploaded results",
"Settings.Storage.Confirmation.Title": "Do you want to delete all uploaded results?",
"Settings.Storage.Confirmation.Paragraph": "You are about to delete all uploaded OONI measurements from your device. They will still be available on [OONI Explorer](https://explorer.ooni.org)",
"Notification.FinishedRunning": "Finished running",
"OONIBrowser.TryMirror": "Try mirror",
"OONIBrowser.Loading": "Loading...",
Expand Down
39 changes: 39 additions & 0 deletions main/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,48 @@ const showMeasurement = (msmtID) => {
})
}

const deleteResult = (resultID = 0) => {
const ooni = new Ooniprobe()
// let measurement = {}

return new Promise((resolve, reject) => {
ooni.on('data', (data) => {
if (data.level === 'error') {
debug('error: ', data.message)
Sentry.addBreadcrumb({
category: 'actions',
message: data.message,
level: Sentry.Severity.Error
})
reject(data.message)
return
}

switch(data.fields.type) {
default:
log.error('deleteResult: extra data.fields', data.fields)
debug('extra data.fields', data.fields)
break
}
})

const idToDelete = resultID > 0 ? resultID : '--all'

ooni
.call(['rm', '--yes', idToDelete])
.then(() => {
debug(`Deleted result: ${idToDelete}`)
resolve()
})
.catch(err => reject(err))
})

}

module.exports = {
listResults,
listMeasurements,
showMeasurement,
deleteResult,
hardReset
}
28 changes: 27 additions & 1 deletion main/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Useful issue on non-ascii path problems on windows:
// https://github.com/nodejs/node/issues/17586
const path = require('path')
const fs = require('fs-extra')
const { is } = require('electron-util')

const electron = require('electron')
Expand Down Expand Up @@ -72,10 +73,35 @@ const debugGetAllPaths = () => ({
'logFile': log.transports.file.findLogPath(),
})

const getDirectorySize = (dir) => {
var size = 0

var contents = fs.readdirSync(dir)

contents.map(file => {
const filePath = `${dir}/${file}`
const fsStat = fs.statSync(filePath)
if (fsStat.isDirectory()) {
const dirSize = getDirectorySize(filePath)
size += dirSize
} else {
size += fsStat.size
}
})
return size
}

const getHomeDirSize = () => {
const homeDir = getHomeDir()
return getDirectorySize(homeDir)
}

module.exports = {
getBinaryPath,
getBinaryDirectory,
getBinarySuffix,
getHomeDir,
debugGetAllPaths
debugGetAllPaths,
getDirectorySize,
getHomeDirSize
}
67 changes: 67 additions & 0 deletions renderer/components/settings/StorageManagement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useState, useMemo, useCallback } from 'react'
import { FormattedMessage } from 'react-intl'
import {
Flex,
Button,
Text
} from 'ooni-components'
import electron from 'electron'
import humanize from 'humanize'
import { RemoveScroll } from 'react-remove-scroll'
import Raven from 'raven-js'

import ConfirmationModal from '../ConfirmationModal'
import FormattedMarkdownMessage from '../FormattedMarkdownMessage'

export const StorageManagement = () => {
const [showModal, setShowModal] = useState(false)
const [refreshSizeCounter, setRefreshSizeCounter] = useState(0)

const onDelete = useCallback(() => {
const remote = electron.remote
const { deleteResult } = remote.require('./actions')
deleteResult().then(() => {
setRefreshSizeCounter(prev => prev + 1)
}).catch(err => {
Raven.captureException(err, {extra: {scope: 'renderer.deleteAllMeasurements'}})
})
}, [setRefreshSizeCounter])

const homeDirSize = useMemo(() => {
const remote = electron.remote
const { getHomeDirSize } = remote.require('./utils/paths')
const homeDirSize = humanize.filesize(getHomeDirSize())
return homeDirSize
}, [refreshSizeCounter]) // dependency to auto update size when counter changes

return (
<Flex flexDirection='column'>
<Flex my={2}>
<Text><FormattedMessage id='Settings.Storage.Usage.Label' /></Text>
<Text color='gray6' ml={4}>{homeDirSize}</Text>
</Flex>
<Flex my={2}>
<Button onClick={() => setShowModal(true)}>
<FormattedMessage id='Settings.Storage.Delete'/>
</Button>
</Flex>
<RemoveScroll enabled={showModal}>
<ConfirmationModal
show={showModal}
title={<FormattedMessage id='Settings.Storage.Confirmation.Title' />}
body={<FormattedMarkdownMessage id='Settings.Storage.Confirmation.Paragraph' />}
confirmLabel={<FormattedMessage id='Modal.Delete' />}
cancelLabel={<FormattedMessage id='Modal.Cancel' />}
onConfirm={() => {
onDelete()
setShowModal(false)
}}
onCancel={() => setShowModal(false)}
/>
</RemoveScroll>

</Flex>
)
}

export default StorageManagement
6 changes: 6 additions & 0 deletions renderer/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ConfigProvider } from '../components/settings/useConfig'
import { BooleanOption, NumberOption } from '../components/settings/widgets'
import { LanguageSelector } from '../components/settings/LanguageSelector'
import { WebsiteCategoriesSelector } from '../components/settings/WebsiteCategoriesSelector'
import { StorageManagement } from '../components/settings/StorageManagement'
import Layout from '../components/Layout'
import Sidebar from '../components/Sidebar'
import { default as pkgJson } from '../../package.json'
Expand Down Expand Up @@ -79,6 +80,11 @@ const Settings = () => {
/>
</Section>

{/* Stoage Usage */}
<Section title={<FormattedMessage id='Settings.Storage.Label' />}>
<StorageManagement />
</Section>

{/* Privacy */}
<Section title={<FormattedMessage id='Settings.Privacy.Label' />}>
<BooleanOption
Expand Down
2 changes: 1 addition & 1 deletion renderer/static/translations.js

Large diffs are not rendered by default.