import Widget from "App/mstore/types/widget"; import APIClient from 'App/api_client'; import { fetchErrorCheck } from "App/utils"; export default class MetricService { private client: APIClient; constructor(client?: APIClient) { this.client = client ? client : new APIClient(); } initClient(client?: APIClient) { this.client = client || new APIClient(); } /** * Get all metrics. * @returns {Promise} */ getMetrics(): Promise { return this.client.get('/metrics') .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data || []); } /** * Get a metric by metricId. * @param metricId * @returns {Promise} */ getMetric(metricId: string): Promise { return this.client.get('/metrics/' + metricId) .then(fetchErrorCheck) .then((response: { data: any; }) => response.data || {}); } /** * Save a metric. * @param metric * @returns */ saveMetric(metric: Widget, dashboardId?: string): Promise { const data = metric.toJson() const isCreating = !data[Widget.ID_KEY]; const method = isCreating ? 'post' : 'put'; const url = isCreating ? '/metrics' : '/metrics/' + data[Widget.ID_KEY]; return this.client[method](url, data) .then(fetchErrorCheck) .then((response: { data: any; }) => response.data || {}) } /** * Delete a metric. * @param metricId * @returns {Promise} */ deleteMetric(metricId: string): Promise { return this.client.delete('/metrics/' + metricId) .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data); } /** * Get all templates. * @returns {Promise} */ getTemplates(): Promise { return this.client.get('/metrics/templates') .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data || []); } getMetricChartData(metric: Widget, data: any, isWidget: boolean = false): Promise { const path = isWidget ? `/metrics/${metric.metricId}/chart` : `/metrics/try`; return this.client.post(path, data) .then(fetchErrorCheck) .then((response: { data: any; }) => response.data || {}); } /** * Fetch sessions from the server. * @param filter * @returns */ fetchSessions(metricId: string, filter: any): Promise { return this.client.post(metricId ? `/metrics/${metricId}/sessions` : '/metrics/try/sessions', filter) .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data || []); } fetchIssues(filter: string): Promise { return this.client.post(`/metrics/try/issues`, filter) .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data || {}); } fetchIssue(metricId: string, issueId: string, params: any): Promise { return this.client.post(`/custom_metrics/${metricId}/issues/${issueId}/sessions`, params) .then((response: { json: () => any; }) => response.json()) .then((response: { data: any; }) => response.data || {}); } }