-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
192 additions
and
64 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
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
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,79 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/unkeyed/unkey/apps/agent/pkg/errors" | ||
) | ||
|
||
type GetKeyStatsRequest struct { | ||
KeyId string `validate:"required"` | ||
} | ||
|
||
type usageRecord struct { | ||
Time int64 `json:"time"` | ||
Value int64 `json:"value"` | ||
} | ||
|
||
type GetKeyStatsResponse struct { | ||
Usage []usageRecord `json:"usage"` | ||
} | ||
|
||
func (s *Server) getKeyStats(c *fiber.Ctx) error { | ||
ctx, span := s.tracer.Start(c.UserContext(), "server.getKey") | ||
defer span.End() | ||
req := GetKeyStatsRequest{ | ||
KeyId: c.Params("keyId"), | ||
} | ||
|
||
err := s.validator.Struct(req) | ||
if err != nil { | ||
return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) | ||
} | ||
|
||
authHash, err := getKeyHash(c.Get("Authorization")) | ||
if err != nil { | ||
return errors.NewHttpError(c, errors.UNAUTHORIZED, err.Error()) | ||
} | ||
|
||
authKey, found, err := s.db.FindKeyByHash(ctx, authHash) | ||
if err != nil { | ||
return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, fmt.Sprintf("unable to find key: %s", err.Error())) | ||
} | ||
if !found { | ||
return errors.NewHttpError(c, errors.NOT_FOUND, fmt.Sprintf("unable to find key by hash: %s", authHash)) | ||
} | ||
|
||
if authKey.ForWorkspaceId == "" { | ||
return errors.NewHttpError(c, errors.BAD_REQUEST, "wrong key type") | ||
} | ||
|
||
key, found, err := s.db.FindKeyById(ctx, req.KeyId) | ||
if err != nil { | ||
return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, err.Error()) | ||
} | ||
if !found { | ||
return errors.NewHttpError(c, errors.NOT_FOUND, fmt.Sprintf("key %s not found", req.KeyId)) | ||
} | ||
if key.WorkspaceId != authKey.ForWorkspaceId { | ||
return errors.NewHttpError(c, errors.UNAUTHORIZED, "workspace access denied") | ||
} | ||
|
||
keyStats, err := s.tinybird.GetKeyStats(ctx, key.Id) | ||
if err != nil { | ||
return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, "unable to load stats") | ||
} | ||
|
||
res := GetKeyStatsResponse{ | ||
Usage: make([]usageRecord, len(keyStats.Usage)), | ||
} | ||
for i, day := range keyStats.Usage { | ||
res.Usage[i] = usageRecord{ | ||
Time: day.Time, | ||
Value: day.Value, | ||
} | ||
} | ||
|
||
return c.JSON(res) | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ package tracing | |
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
|
Oops, something went wrong.
aa6d1e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
unkey – ./
unkey-unkey.vercel.app
unkey.dev
www.unkey.dev
unkey-git-main-unkey.vercel.app
unkey.vercel.app