@@ -39,19 +39,34 @@ func GetMetrics(w http.ResponseWriter, r *http.Request) {
39
39
}
40
40
41
41
ctx := workloads .CurrentContext (appName )
42
+ err := workloads .PopulateWorkloadIDs (ctx )
43
+ if err != nil {
44
+ RespondError (w , err )
45
+ return
46
+ }
47
+
42
48
apiName , err := getRequiredQueryParam ("apiName" , r )
43
49
if err != nil {
44
50
RespondError (w , err )
45
51
return
46
52
}
47
53
54
+ api := ctx .APIs [apiName ]
55
+ apiSavedStatus , err := workloads .GetAPISavedStatus (api .ID , api .WorkloadID , appName )
56
+ if err != nil {
57
+ RespondError (w , err )
58
+ return
59
+ }
60
+
48
61
endTime := time .Now ()
62
+ twoWeeksAgo := endTime .Add (- 14 * 24 * time .Hour )
63
+ metricsStartTime := apiSavedStatus .Start
64
+ if twoWeeksAgo
65
+
49
66
period := int64 (60 * 60 )
50
67
51
- //GetClassificationMetrics(appName, apiName, ctx.APIs[apiName].ID, period, &endTime)
52
68
if * ctx .APIs [apiName ].Tracker .ModelType == "classification" {
53
69
metrics := GetClassificationMetrics (appName , apiName , ctx .APIs [apiName ].ID , period , & endTime )
54
- // GetNetworkStats(appName, apiName, ctx.APIs[apiName].ID, stats)
55
70
Respond (w , metrics )
56
71
} else {
57
72
metrics := GetRegressionMetrics (appName , apiName , ctx .APIs [apiName ].ID , period , & endTime )
@@ -104,15 +119,12 @@ func Max(floats ...*float64) *float64 {
104
119
}
105
120
106
121
func Avg (values []* float64 , counts []* float64 ) * float64 {
107
- debug .Pp (values )
108
- debug .Pp (counts )
109
122
if len (values ) == 0 || len (counts ) == 0 {
110
123
return nil
111
124
}
112
125
113
126
total := float64 (* SumInt (counts ... ))
114
127
avg := 0.0
115
- debug .Pp (total )
116
128
for idx , valPtr := range values {
117
129
weight := * counts [idx ]
118
130
value := * valPtr
@@ -167,6 +179,10 @@ func GetClassificationMetrics(appName string, apiName string, apiID string, peri
167
179
return & apiMetrics
168
180
}
169
181
182
+ func GetLatencyMetricsDef (routeName string , period int64 , endTime * time.Time ) {
183
+
184
+ }
185
+
170
186
func GetRegressionMetrics (appName string , apiName string , apiID string , period int64 , endTime * time.Time ) * schema.APIMetrics {
171
187
startTime := endTime .Add (- 7 * 24 * 60 * 60 * time .Second )
172
188
@@ -281,29 +297,6 @@ func GetRegressionMetricDef(appName string, apiName string, apiID string, period
281
297
282
298
return regressionMetric
283
299
}
284
- // input := cloudwatch.GetMetricStatisticsInput{
285
- // EndTime: &endTime,
286
- // StartTime: &startTime,
287
- // Namespace: aws.String("cortex"),
288
- // MetricName: aws.String("PREDICTION"),
289
- // Dimensions: []*cloudwatch.Dimension{
290
- // &cloudwatch.Dimension{
291
- // Name: aws.String("AppName"),
292
- // Value: aws.String(appName),
293
- // },
294
- // &cloudwatch.Dimension{
295
- // Name: aws.String("APIName"),
296
- // Value: aws.String(apiName),
297
- // },
298
- // &cloudwatch.Dimension{
299
- // Name: aws.String("APIID"),
300
- // Value: aws.String(apiID),
301
- // },
302
- // },
303
- // Period: aws.Int64(5 * 60),
304
- // Statistics: []*string{aws.String("SampleCount"), aws.String("Minimum"), aws.String("Maximum"), aws.String("Average")},
305
- // }
306
- //}
307
300
308
301
func GetNetworkStatsDef (appName string , apiName string , apiID string , period int64 ) []* cloudwatch.MetricDataQuery {
309
302
dimensions := []* cloudwatch.Dimension {
@@ -438,144 +431,4 @@ func GetClassesMetricDef(appName string, apiName string, apiID string, period in
438
431
}
439
432
debug .Pp (classMetricQueries )
440
433
return classMetricQueries , nil
441
- }
442
-
443
- // func GetNetworkStatsMetricDef(appName string, apiName string, apiID string) ([]*cloudwatch.Metric, error) {
444
-
445
- // }
446
-
447
- // func GetClassificationMetrics(appName string, apiName string, apiID string) (*schema.PredictionMetrics, error) {
448
- // var predictionMetrics schema.PredictionMetrics
449
-
450
- // classMetricQuery, err := GetClassesMetricDef(appName, apiName, apiID)
451
- // if err != nil {
452
- // return err
453
- // }
454
-
455
- // startTime := time.Now().Add(-5 * 60 * time.Second)
456
- // endTime := time.Now()
457
- // classMapping := make(map[string]float64, len(metricsList))
458
-
459
- // for i := 0; i < metricDataQueryBatchCount; i++ {
460
- // var batchSize int
461
- // if i == metricDataQueryBatchCount - 1{
462
- // batchSize = numClasses % 100
463
- // } else {
464
- // batchSize = 100
465
- // }
466
- // queries := make([]*cloudwatch.MetricDataQuery, batchSize)
467
-
468
- // prevPage := i * 100
469
- // for metricID := prevPage; metricID < prevPage + batchSize; metricID++ {
470
- // queries[metricID-prevPage] = &cloudwatch.MetricDataQuery{
471
- // Id: aws.String(fmt.Sprintf("id_%d", metricID)),
472
- // MetricStat: &cloudwatch.MetricStat{
473
- // Metric: metricsList[metricID],
474
- // Period: aws.Int64(5 * 60),
475
- // Stat: aws.String("Sum"),
476
- // },
477
- // }
478
- // }
479
-
480
- // metricsDataQuery := cloudwatch.GetMetricDataInput{
481
- // EndTime: &endTime,
482
- // StartTime: &startTime,
483
- // MetricDataQueries: queries,
484
- // }
485
-
486
- // output, _ := config.AWS.CloudWatchMetrics.GetMetricData(&metricsDataQuery)
487
- // debug.Pp(output)
488
-
489
- // for _, result := range output.MetricDataResults {
490
- // idList := strings.Split(*result.Id, "_")
491
- // id, _ := strconv.Atoi(idList[1])
492
- // dims := metricsList[id].Dimensions
493
- // var metricName string
494
- // for _, dim := range dims {
495
- // if *dim.Name == "CLASS" {
496
- // metricName = *dim.Value
497
- // }
498
- // }
499
- // if len(result.Values) == 0 {
500
- // classMapping[metricName] = 0
501
- // } else {
502
- // classMapping[metricName] = *result.Values[0]
503
- // }
504
- // }
505
- // }
506
-
507
- // startTimeSecond := startTime.Round(time.Minute)
508
- // if startTimeSecond.After(startTime) {
509
- // startTimeSecond.Add(-1 * time.Minute)
510
- // }
511
- // endTimeSecond := startTimeSecond.Add(5 * time.Minute)
512
-
513
- // debug.Pp(startTimeSecond)
514
-
515
- // predictionMetrics.StartTime = &startTimeSecond
516
- // predictionMetrics.EndTime = &endTimeSecond
517
- // predictionMetrics.ClassDistribution = classMapping
518
- // return &predictionMetrics
519
- // }
520
-
521
- // func GetRegressionMetrics(appName string, apiName string, apiID string) *schema.PredictionMetrics {
522
- // fmt.Println("GetRegressionMetrics")
523
- // var predictionMetrics schema.PredictionMetrics
524
- // startTime := time.Now().Add(-5 * 60 * time.Second)
525
- // endTime := time.Now()
526
- // input := cloudwatch.GetMetricStatisticsInput{
527
- // EndTime: &endTime,
528
- // StartTime: &startTime,
529
- // Namespace: aws.String("cortex"),
530
- // MetricName: aws.String("PREDICTION"),
531
- // Dimensions: []*cloudwatch.Dimension{
532
- // &cloudwatch.Dimension{
533
- // Name: aws.String("AppName"),
534
- // Value: aws.String(appName),
535
- // },
536
- // &cloudwatch.Dimension{
537
- // Name: aws.String("APIName"),
538
- // Value: aws.String(apiName),
539
- // },
540
- // &cloudwatch.Dimension{
541
- // Name: aws.String("APIID"),
542
- // Value: aws.String(apiID),
543
- // },
544
- // },
545
- // Period: aws.Int64(5 * 60),
546
- // Statistics: []*string{aws.String("SampleCount"), aws.String("Minimum"), aws.String("Maximum"), aws.String("Average")},
547
- // }
548
-
549
- // debug.Pp(input)
550
- // response, _ := config.AWS.CloudWatchMetrics.GetMetricStatistics(&input)
551
- // debug.Pp(response)
552
- // if response.Datapoints == nil || len(response.Datapoints) == 0 {
553
- // return &predictionMetrics
554
- // }
555
-
556
- // predictionMetrics.StartTime = response.Datapoints[0].Timestamp
557
- // dataPointStartTime := *response.Datapoints[0].Timestamp
558
- // dataPointEndTime := dataPointStartTime.Add(5 * 60 * time.Second)
559
-
560
- // predictionMetrics.StartTime = &dataPointStartTime
561
- // predictionMetrics.EndTime = &dataPointEndTime
562
-
563
- // basicStats := map[string]float64{}
564
- // basicStats["SampleCount"] = *response.Datapoints[0].SampleCount
565
- // basicStats["Minimum"] = *response.Datapoints[0].Minimum
566
- // basicStats["Maximum"] = *response.Datapoints[0].Maximum
567
- // basicStats["Average"] = *response.Datapoints[0].Average
568
- // predictionMetrics.Statistics = basicStats
569
-
570
- // if *response.Datapoints[0].Minimum > 0 {
571
- // percentiles := map[string]float64{}
572
- // input.Statistics = nil
573
- // input.ExtendedStatistics = []*string{aws.String("p25.0"), aws.String("p50.0"), aws.String("p75.0")}
574
- // response, _ := config.AWS.CloudWatchMetrics.GetMetricStatistics(&input)
575
- // for name, val := range response.Datapoints[0].ExtendedStatistics{
576
- // percentiles[name] = *val
577
- // }
578
- // predictionMetrics.Percentiles = percentiles
579
- // }
580
- // return &predictionMetrics
581
- // }
434
+ }
0 commit comments