55// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
77import { isNotNil } from '@/common/helpers' ;
8- import { IBackendKind , IBackendMetric , IKind , IMetric , ITag } from '../models/metric' ;
8+ import { IBackendKind , IKind , IMetric , ITag } from '../models/metric' ;
99import { freeKeyPrefix } from '@/url2' ;
10-
11- export function saveMetric ( metric : IMetric ) {
12- const body : IBackendMetric = {
13- description : metric . description ,
14- kind : ( metric . kind + ( metric . withPercentiles ? '_p' : '' ) ) as IBackendKind ,
15- name : metric . name ,
16- metric_id : metric . id ,
17- string_top_name : metric . stringTopName ,
18- string_top_description : metric . stringTopDescription ,
19- weight : metric . weight ,
20- resolution : metric . resolution ,
21- visible : metric . visible ,
22- disable : metric . disable ,
23- tags : metric . tags . map ( ( tag ) => ( {
24- name : tag . name ,
25- description : tag . alias ,
26- raw : tag . raw_kind != null ,
27- raw_kind : tag . raw_kind ,
28- value_comments :
29- tag . customMapping . length > 0
30- ? tag . customMapping . reduce (
31- ( acc , map ) => {
32- acc [ map . from ] = map . to ;
33- return acc ;
34- } ,
35- { } as Record < string , string >
36- )
37- : undefined ,
38- } ) ) ,
39- tags_draft : Object . fromEntries ( metric . tags_draft . map ( ( t ) => [ t . name , t ] ) ) ,
40- pre_key_tag_id : metric . pre_key_tag_id ,
41- pre_key_from : metric . pre_key_from ? metric . pre_key_from : 0 ,
42- skip_max_host : ! ! metric . skip_max_host ,
43- skip_min_host : ! ! metric . skip_min_host ,
44- skip_sum_square : ! ! metric . skip_sum_square ,
45- pre_key_only : ! ! metric . pre_key_only ,
46- metric_type : metric . metric_type ,
47- version : metric . version ,
48- group_id : metric . group_id ,
49- fair_key_tag_ids : metric . fair_key_tag_ids ,
50- } ;
51-
52- return fetch ( `/api/metric${ metric . id ? `?s=${ metric . name } ` : '' } ` , {
53- method : 'POST' ,
54- body : JSON . stringify ( { metric : body } ) ,
55- } )
56- . then ( ( res ) => res . json ( ) )
57- . catch ( ( ) => {
58- throw new Error ( 'Unknown error' ) ;
59- } )
60- . then ( ( parsed ) => {
61- if ( 'error' in parsed ) {
62- throw new Error ( parsed . error ) ;
63- }
64- return parsed ;
65- } ) ;
66- }
10+ import { ApiMetric , PostMetricMetaValue } from '@/api/metric' ;
6711
6812export function resetMetricFlood ( metricName : string ) {
6913 return fetch ( `/api/reset-flood?s=${ metricName } ` , {
@@ -81,19 +25,7 @@ export function resetMetricFlood(metricName: string) {
8125 } ) ;
8226}
8327
84- export const fetchMetric = async ( url : string ) => {
85- const response = await fetch ( url ) ;
86- if ( ! response . ok ) {
87- throw new Error ( `Failed to fetch: ${ url } ` ) ;
88- }
89- return response . json ( ) ;
90- } ;
91-
92- export const fetchAndProcessMetric = async ( url : string ) => {
93- const {
94- data : { metric } ,
95- } = await fetchMetric ( url ) ;
96-
28+ export function mapMetricToEdit ( { data : { metric } } : ApiMetric ) : IMetric {
9729 const tags_draft : ITag [ ] = Object . entries ( metric . tags_draft ?? { } )
9830 . map ( ( [ , t ] ) => t as ITag )
9931 . filter ( isNotNil ) ;
@@ -102,7 +34,7 @@ export const fetchAndProcessMetric = async (url: string) => {
10234 return {
10335 id : metric . metric_id === undefined ? 0 : metric . metric_id ,
10436 name : metric . name ,
105- description : metric . description ,
37+ description : metric . description ?? '' ,
10638 kind : ( metric . kind . endsWith ( '_p' ) ? metric . kind . replace ( '_p' , '' ) : metric . kind ) as IKind ,
10739 stringTopName : metric . string_top_name === undefined ? '' : metric . string_top_name ,
10840 stringTopDescription : metric . string_top_description === undefined ? '' : metric . string_top_description ,
@@ -111,20 +43,21 @@ export const fetchAndProcessMetric = async (url: string) => {
11143 visible : metric . visible === undefined ? false : metric . visible ,
11244 disable : metric . disable === undefined ? false : metric . disable ,
11345 withPercentiles : metric . kind . endsWith ( '_p' ) ,
114- tags : metric . tags . map ( ( tag : ITag , index : number ) => ( {
115- name : tag . name === undefined || tag . name === `key${ index } ` ? '' : tag . name ,
116- alias : tag . description === undefined ? '' : tag . description ,
117- customMapping : tag . value_comments
118- ? Object . entries ( tag . value_comments ) . map ( ( [ from , to ] ) => ( {
119- from,
120- to,
121- } ) )
122- : [ ] ,
123- isRaw : tag . raw || tag . raw_kind != null ,
124- raw_kind : tag . raw_kind ,
125- } ) ) ,
46+ tags :
47+ metric . tags ?. map ( ( tag , index ) => ( {
48+ name : tag . name === undefined || tag . name === `key${ index } ` ? '' : tag . name ,
49+ alias : tag . description === undefined ? '' : tag . description ,
50+ customMapping : tag . value_comments
51+ ? Object . entries ( tag . value_comments ) . map ( ( [ from , to ] ) => ( {
52+ from,
53+ to,
54+ } ) )
55+ : [ ] ,
56+ isRaw : tag . raw || tag . raw_kind != null ,
57+ raw_kind : tag . raw_kind ,
58+ } ) ) ?? [ ] ,
12659 tags_draft,
127- tagsSize : metric . tags . length ,
60+ tagsSize : metric . tags ? .length ?? 0 ,
12861 pre_key_tag_id : metric . pre_key_tag_id && freeKeyPrefix ( metric . pre_key_tag_id ) ,
12962 pre_key_from : metric . pre_key_from ,
13063 metric_type : metric . metric_type ,
@@ -135,4 +68,46 @@ export const fetchAndProcessMetric = async (url: string) => {
13568 skip_min_host : ! ! metric . skip_min_host ,
13669 skip_sum_square : ! ! metric . skip_sum_square ,
13770 } ;
138- } ;
71+ }
72+
73+ export function mapEditToMetric ( metric : IMetric ) : PostMetricMetaValue {
74+ return {
75+ description : metric . description ,
76+ kind : ( metric . kind + ( metric . withPercentiles ? '_p' : '' ) ) as IBackendKind ,
77+ name : metric . name ,
78+ metric_id : metric . id ,
79+ string_top_name : metric . stringTopName ,
80+ string_top_description : metric . stringTopDescription ,
81+ weight : metric . weight ,
82+ resolution : metric . resolution ,
83+ visible : metric . visible ,
84+ disable : metric . disable ,
85+ tags : metric . tags . map ( ( tag ) => ( {
86+ name : tag . name ,
87+ description : tag . alias ,
88+ raw : tag . raw_kind != null ,
89+ raw_kind : tag . raw_kind ,
90+ value_comments :
91+ tag . customMapping . length > 0
92+ ? tag . customMapping . reduce (
93+ ( acc , map ) => {
94+ acc [ map . from ] = map . to ;
95+ return acc ;
96+ } ,
97+ { } as Record < string , string >
98+ )
99+ : undefined ,
100+ } ) ) ,
101+ tags_draft : Object . fromEntries ( metric . tags_draft . map ( ( t ) => [ t . name , t ] ) ) ,
102+ pre_key_tag_id : metric . pre_key_tag_id ,
103+ pre_key_from : metric . pre_key_from ? metric . pre_key_from : 0 ,
104+ skip_max_host : ! ! metric . skip_max_host ,
105+ skip_min_host : ! ! metric . skip_min_host ,
106+ skip_sum_square : ! ! metric . skip_sum_square ,
107+ pre_key_only : ! ! metric . pre_key_only ,
108+ metric_type : metric . metric_type ,
109+ version : metric . version ,
110+ group_id : metric . group_id ,
111+ fair_key_tag_ids : metric . fair_key_tag_ids ,
112+ } ;
113+ }
0 commit comments