-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from PromptPal/annatarhe/pp-19-prompt-tracking
feat(metric): add prompt metrics in last 7 days
- Loading branch information
Showing
6 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package ent | ||
|
||
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/upsert ./schema | ||
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/upsert,sql/modifier ./schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package schema | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"entgo.io/ent/dialect/sql" | ||
"github.com/PromptPal/PromptPal/ent/prompt" | ||
"github.com/PromptPal/PromptPal/ent/promptcall" | ||
"github.com/PromptPal/PromptPal/service" | ||
) | ||
|
||
type promptCallMetricBySQL struct { | ||
P50 float32 `json:"p50"` | ||
P90 float32 `json:"p90"` | ||
P99 float32 `json:"p99"` | ||
} | ||
|
||
type promptMetrics struct { | ||
data *promptCallMetricBySQL | ||
} | ||
|
||
func (p promptResponse) Metrics(ctx context.Context) (res promptMetrics, err error) { | ||
|
||
var temp []promptCallMetricBySQL | ||
|
||
err = service. | ||
EntClient. | ||
PromptCall. | ||
Query(). | ||
Where(promptcall.HasPromptWith(prompt.ID(int(p.prompt.ID)))). | ||
Where(promptcall.CreateTimeGT(time.Now().AddDate(-1, 0, 0))). | ||
Aggregate(func(s *sql.Selector) string { | ||
return sql.As("percentile_cont(0.5) within group (order by duration asc)", "p50") | ||
}, func(s *sql.Selector) string { | ||
return sql.As("percentile_cont(0.9) within group (order by duration asc)", "p90") | ||
}, func(s *sql.Selector) string { | ||
return sql.As("percentile_cont(0.99) within group (order by duration asc)", "p99") | ||
}). | ||
Scan(ctx, &temp) | ||
|
||
if err != nil { | ||
err = NewGraphQLHttpError(http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
if len(temp) > 0 { | ||
res.data = &temp[0] | ||
} | ||
return | ||
} | ||
|
||
func (p promptMetrics) P50() float64 { | ||
return float64(p.data.P50) | ||
} | ||
func (p promptMetrics) P90() float64 { | ||
return float64(p.data.P90) | ||
} | ||
func (p promptMetrics) P99() float64 { | ||
return float64(p.data.P99) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters