Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fixed CSV exports (#394)

Andrii Romasiun93072337fabb
diff --git a/app/pages/Captcha/View/ViewCaptcha.helpers.tsx b/app/pages/Captcha/View/ViewCaptcha.helpers.tsx
index 6fc4f04b5..d88a26cab 100644
--- a/app/pages/Captcha/View/ViewCaptcha.helpers.tsx
+++ b/app/pages/Captcha/View/ViewCaptcha.helpers.tsx
@@ -1,6 +1,5 @@
import React from 'react'
// @ts-ignore
-import { saveAs } from 'file-saver'
import {
GlobeEuropeAfricaIcon,
LanguageIcon,
@@ -12,15 +11,10 @@ import {
} from '@heroicons/react/24/outline'
import dayjs from 'dayjs'
import { area, bar } from 'billboard.js'
-import _forEach from 'lodash/forEach'
import _map from 'lodash/map'
import _split from 'lodash/split'
import _replace from 'lodash/replace'
-import _isEmpty from 'lodash/isEmpty'
-import _keys from 'lodash/keys'
import _size from 'lodash/size'
-import _round from 'lodash/round'
-import JSZip from 'jszip'
import {
TimeFormat,
@@ -30,7 +24,6 @@ import {
tbsFormatMapperTooltip,
tbsFormatMapperTooltip24h,
} from 'redux/constants'
-import countries from 'utils/isoCountries'
// @ts-ignore
import * as d3 from 'd3'
import { nFormatter } from 'utils/generic'
@@ -41,68 +34,6 @@ const getExportFilename = (prefix: string) => {
return `${prefix}-${date}.zip`
}
-const convertToCSV = (array: any[]) => {
- let str = 'name,value,perc\r\n'
-
- for (let i = 0; i < _size(array); ++i) {
- let line = ''
-
- _forEach(array[i], (index) => {
- if (line !== '') line += ','
- line += index
- })
-
- str += `${line}\r\n`
- }
-
- return str
-}
-
-const onCSVExportClick = (
- data: {
- data: any
- types: any
- },
- pid: string,
- tnMapping: {
- [key: string]: string
- },
- language: string,
-) => {
- const { data: rowData, types } = data
- const zip = new JSZip()
-
- _forEach(types, (item) => {
- if (_isEmpty(rowData[item])) {
- return
- }
-
- const rowKeys = _keys(rowData[item])
- let total = 0
-
- _forEach(rowKeys, (e) => {
- total += rowData[item][e]
- })
-
- const csvData = _map(rowKeys, (e) => {
- const perc = _round((rowData[item][e] / total) * 100 || 0, 2)
-
- if (item === 'cc') {
- const name = countries.getName(e, language)
- return [name, rowData[item][e], `${perc}%`]
- }
-
- return [e, rowData[item][e], `${perc}%`]
- })
-
- zip.file(`${tnMapping[item]}.csv`, convertToCSV(csvData))
- })
-
- zip.generateAsync({ type: 'blob' }).then((content) => {
- saveAs(content, getExportFilename(`swetrix-${pid}`))
- })
-}
-
const CHART_METRICS_MAPPING = {
results: 'results',
}
@@ -312,6 +243,5 @@ export {
getSettings,
getExportFilename,
getColumns,
- onCSVExportClick,
CHART_METRICS_MAPPING,
}
diff --git a/app/pages/Captcha/View/ViewCaptcha.tsx b/app/pages/Captcha/View/ViewCaptcha.tsx
index 1e8bb4c93..c392e0593 100644
--- a/app/pages/Captcha/View/ViewCaptcha.tsx
+++ b/app/pages/Captcha/View/ViewCaptcha.tsx
@@ -50,7 +50,6 @@ import routes from 'routesPath'
import { getProject, getCaptchaData } from 'api'
import { Panel, CustomEvents } from './Panels'
import {
- onCSVExportClick,
getFormatDate,
panelIconMapping,
typeNameMapping,
@@ -62,6 +61,7 @@ import {
CHART_METRICS_MAPPING,
getColumns,
} from './ViewCaptcha.helpers'
+import { onCSVExportClick } from 'pages/Project/View/ViewProject.helpers'
import TBPeriodSelector from 'pages/Project/View/components/TBPeriodSelector'
import CCRow from '../../Project/View/components/CCRow'
import RefRow from './components/RefRow'
diff --git a/app/pages/Project/View/ViewProject.helpers.tsx b/app/pages/Project/View/ViewProject.helpers.tsx
index 3259fe9df..8bca8af70 100644
--- a/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/app/pages/Project/View/ViewProject.helpers.tsx
@@ -42,6 +42,7 @@ import {
PROJECT_TABS,
isSelfhosted,
} from 'redux/constants'
+import { IEntry } from 'redux/models/IEntry'
import { getTimeFromSeconds, getStringFromTime, sumArrays, nFormatter } from 'utils/generic'
import countries from 'utils/isoCountries'
import { IAnalyticsFunnel } from 'redux/models/IProject'
@@ -124,7 +125,7 @@ const getExportFilename = (prefix: string) => {
}
const convertToCSV = (array: any[]) => {
- let str = 'name,value,perc\r\n'
+ let str = 'name,value,percentage\r\n'
for (let i = 0; i < _size(array); ++i) {
let lines = ''
@@ -156,25 +157,20 @@ const onCSVExportClick = (
_forEach(types, (item) => {
if (_isEmpty(rowData[item])) {
- return
+ return null
}
- const rowKeys = _keys(rowData[item])
- let total = 0
+ const total = _reduce(rowData[item], (acc, { count }) => acc + count, 0)
- _forEach(rowKeys, (e) => {
- total += rowData[item][e]
- })
-
- const csvData = _map(rowKeys, (e) => {
- const perc = _round((rowData[item][e] / total) * 100 || 0, 2)
+ const csvData = _map(rowData[item], (entry: IEntry) => {
+ const perc = _round((entry.count / total) * 100 || 0, 2)
if (item === 'cc') {
- const name = countries.getName(e, language)
- return [name, rowData[item][e], `${perc}%`]
+ const name = countries.getName(entry.name, language)
+ return [`"${name}"`, entry.count, `${perc}%`]
}
- return [e, rowData[item][e], `${perc}%`]
+ return [`"${entry.name}"`, entry.count, `${perc}%`]
})
zip.file(`${tnMapping[item]}.csv`, convertToCSV(csvData))