MCPcopy Index your code
hub / github.com/coder/coder / parseInsightsInterval

Function parseInsightsInterval

coderd/insights.go:759–784  ·  view source on GitHub ↗
(ctx context.Context, rw http.ResponseWriter, intervalString string, startTime, endTime time.Time)

Source from the content-addressed store, hash-verified

757}
758
759func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, intervalString string, startTime, endTime time.Time) (codersdk.InsightsReportInterval, bool) {
760 switch v := codersdk.InsightsReportInterval(intervalString); v {
761 case codersdk.InsightsReportIntervalDay, "":
762 return v, true
763 case codersdk.InsightsReportIntervalWeek:
764 if !lastReportIntervalHasAtLeastSixDays(startTime, endTime) {
765 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
766 Message: "Query parameter has invalid value.",
767 Detail: "Last report interval should have at least 6 days.",
768 })
769 return "", false
770 }
771 return v, true
772 default:
773 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
774 Message: "Query parameter has invalid value.",
775 Validations: []codersdk.ValidationError{
776 {
777 Field: "interval",
778 Detail: fmt.Sprintf("must be one of %v", []codersdk.InsightsReportInterval{codersdk.InsightsReportIntervalDay, codersdk.InsightsReportIntervalWeek}),
779 },
780 },
781 })
782 return "", false
783 }
784}
785
786func lastReportIntervalHasAtLeastSixDays(startTime, endTime time.Time) bool {
787 lastReportIntervalDays := endTime.Sub(startTime) % (7 * 24 * time.Hour)

Callers 2

insightsTemplatesMethod · 0.85

Calls 3

InsightsReportIntervalTypeAlias · 0.92
WriteFunction · 0.92

Tested by 1