|
1 | | -package com.nevmem.moneysaver.app.data |
| 1 | +import axios from "axios"; |
| 2 | +import { apiUrl } from "../utils/constants"; |
| 3 | +import { Environment, Measurement } from "../types"; |
2 | 4 |
|
3 | | -import android.util.Log |
4 | | -import com.nevmem.moneysaver.app.room.AppDatabase |
5 | | -import com.nevmem.moneysaver.app.room.entity.Feature |
6 | | -import kotlinx.coroutines.CoroutineScope |
7 | | -import kotlinx.coroutines.Dispatchers |
8 | | -import kotlinx.coroutines.Job |
9 | | -import kotlinx.coroutines.launch |
10 | | -import java.lang.ref.WeakReference |
11 | | -import javax.inject.Inject |
| 5 | +async function exportCsv(url: string, filename: string) { |
| 6 | + try { |
| 7 | + const response = await axios({ |
| 8 | + url, |
| 9 | + method: "GET", |
| 10 | + responseType: "blob", |
| 11 | + }); |
| 12 | + const href = URL.createObjectURL(response.data); |
12 | 13 |
|
13 | | -class SettingsManagerImpl @Inject constructor( |
14 | | - private val appDatabase: AppDatabase |
15 | | -) : SettingsManager { |
16 | | - private val enabledFeatures = HashSet<Feature>() |
17 | | - private val tag = "SETTINGS_MANAGER_IMPL" |
| 14 | + const link = document.createElement("a"); |
| 15 | + link.href = href; |
| 16 | + link.setAttribute("download", filename); |
| 17 | + document.body.appendChild(link); |
| 18 | + link.click(); |
18 | 19 |
|
19 | | - private val listeners = ArrayList<WeakReference<SettingsManagerListener>>() |
20 | | - private var initialization: Job? = null |
21 | | - |
22 | | - init { |
23 | | - Log.d(tag, "Initialization") |
24 | | - } |
25 | | - |
26 | | - override fun initialize(): Job { |
27 | | - if (initialization == null) { |
28 | | - initialization = CoroutineScope(Dispatchers.IO).launch { |
29 | | - appDatabase.featuresDao().loadAll().forEach { |
30 | | - Log.d(tag, "Feature: ${it.featureName}") |
31 | | - enabledFeatures.add(it) |
32 | | - } |
33 | | - } |
34 | | - } |
35 | | - return initialization as Job |
36 | | - } |
| 20 | + document.body.removeChild(link); |
| 21 | + URL.revokeObjectURL(href); |
| 22 | + } catch (error) { |
| 23 | + throw new Error(error); |
| 24 | + } |
| 25 | +} |
37 | 26 |
|
38 | | - override fun isFeatureEnabled(featureName: String): Boolean { |
39 | | - return enabledFeatures.contains(Feature(featureName)) |
40 | | - } |
| 27 | +export async function dataFetcher<T>(url: string) { |
| 28 | + return await axios |
| 29 | + .get<T>(url) |
| 30 | + .then((res) => res.data) |
| 31 | + .catch((error) => { |
| 32 | + throw new Error(error); |
| 33 | + }); |
| 34 | +} |
41 | 35 |
|
42 | | - override fun enableFeature(featureName: String) { |
43 | | - if (!isFeatureEnabled(featureName)) { |
44 | | - val feature = Feature(featureName) |
45 | | - enabledFeatures.add(feature) |
46 | | - insertFeature(feature) |
47 | | - notifyFeaturesChanged() |
48 | | - } |
49 | | - } |
| 36 | +export async function postData<T>(url: string, payload: any) { |
| 37 | + return await axios |
| 38 | + .post<T>(url, payload) |
| 39 | + .then((res) => res.data) |
| 40 | + .catch((error) => { |
| 41 | + throw new Error(error); |
| 42 | + }); |
| 43 | +} |
50 | 44 |
|
51 | | - override fun disableFeature(featureName: String) { |
52 | | - if (isFeatureEnabled(featureName)) { |
53 | | - val featureToDelete = enabledFeatures.find { |
54 | | - it.featureName == featureName |
55 | | - } |
56 | | - enabledFeatures.remove(Feature(featureName)) |
57 | | - if (featureToDelete != null) { |
58 | | - deleteFeature(featureToDelete) |
59 | | - } |
60 | | - notifyFeaturesChanged() |
61 | | - } |
62 | | - } |
| 45 | +export async function deleteData(url: string) { |
| 46 | + return await axios.delete(url).catch((error) => { |
| 47 | + throw new Error(error); |
| 48 | + }); |
| 49 | +} |
63 | 50 |
|
64 | | - override fun toggleFeature(featureName: String) { |
65 | | - if (isFeatureEnabled(featureName)) { |
66 | | - disableFeature(featureName) |
67 | | - } else { |
68 | | - enableFeature(featureName) |
69 | | - } |
70 | | - } |
| 51 | +export async function exportMeasurementsCsv(benchmarkId: string) { |
| 52 | + const url = `${apiUrl}/api/v2/export/measurements/csv?benchmarkId=${benchmarkId}`; |
| 53 | + await exportCsv(url, "measurements.csv"); |
| 54 | +} |
71 | 55 |
|
72 | | - override fun subscribe(listener: WeakReference<SettingsManagerListener>) { |
73 | | - listeners.add(listener) |
74 | | - } |
| 56 | +export async function exportEnvironmentsCsv(filters: Object) { |
| 57 | + const withValues = Object.entries(filters) |
| 58 | + .filter(([_key, value]) => Boolean(value)) |
| 59 | + .map(([key, value]) => [key, value]); |
| 60 | + const params = new URLSearchParams(withValues).toString(); |
| 61 | + const url = `${apiUrl}/api/v2/export/environments/csv?${params}`; |
| 62 | + await exportCsv(url, "environments.csv"); |
| 63 | +} |
75 | 64 |
|
76 | | - override fun unsubscribe(listener: WeakReference<SettingsManagerListener>) { |
77 | | - listeners.remove(listener) |
78 | | - } |
| 65 | +export async function exportBenchmarksCsv() { |
| 66 | + const url = `${apiUrl}/api/v2/export/benchmarks/csv`; |
| 67 | + await exportCsv(url, "benchmarks.csv"); |
| 68 | +} |
79 | 69 |
|
80 | | - private fun notifyFeaturesChanged() { |
81 | | - val iterator = listeners.iterator() |
82 | | - while (iterator.hasNext()) { |
83 | | - val ref = iterator.next().get() |
84 | | - if (ref != null) { |
85 | | - ref.onFeaturesUpdated() |
86 | | - } else { |
87 | | - iterator.remove() |
88 | | - } |
89 | | - } |
90 | | - } |
| 70 | +export async function getMeasurementsForBechmark(benchmarkId: string) { |
| 71 | + const url = `${apiUrl}/api/v2/measurement/find?benchmarkId=${benchmarkId}`; |
| 72 | + const data = await dataFetcher<Array<Measurement>>(url); |
| 73 | + return data?.length; |
| 74 | +} |
91 | 75 |
|
92 | | - private fun insertFeature(feature: Feature) { |
93 | | - CoroutineScope(Dispatchers.IO).launch { |
94 | | - appDatabase.featuresDao().insert(feature) |
95 | | - } |
96 | | - } |
| 76 | +export async function saveEnvironment(payload: Object) { |
| 77 | + return await postData<Array<Environment>>( |
| 78 | + `${apiUrl}/api/v2/environment`, |
| 79 | + payload |
| 80 | + ); |
| 81 | +} |
97 | 82 |
|
98 | | - private fun deleteFeature(feature: Feature) { |
99 | | - CoroutineScope(Dispatchers.IO).launch { |
100 | | - appDatabase.featuresDao().delete(feature) |
101 | | - } |
102 | | - } |
| 83 | +export async function deleteEnvironment(id: string) { |
| 84 | + return await deleteData(`${apiUrl}/api/v2/environment?id=${id}`); |
103 | 85 | } |
0 commit comments