Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
appbase-edge-function
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanjinfei
appbase-edge-function
Commits
9a01df2c
Commit
9a01df2c
authored
Dec 13, 2024
by
duanjinfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix user rank background exec
parent
61c8e9a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
57 deletions
+48
-57
index.ts
supabase/functions/update-rank/index.ts
+48
-57
No files found.
supabase/functions/update-rank/index.ts
View file @
9a01df2c
...
...
@@ -17,6 +17,53 @@ async function count_holders(supabase) {
return
count
}
async
function
cacheUserRank
(
supabase
)
{
const
{
data
:
userData
,
error
:
userError
}
=
await
supabase
.
from
(
'
user
'
).
select
(
'
*
'
).
gt
(
'
rank
'
,
0
).
order
(
'
rank
'
,
{
ascending
:
true
}).
limit
(
100
);
if
(
userError
)
{
console
.
error
(
'
Error fetching top 100 users:
'
,
error
);
return
new
Response
(
JSON
.
stringify
({
error
:
'
Failed to fetch users
'
}),
{
status
:
500
});
}
// 2. 将数据 JSON 序列化
const
jsonData
=
JSON
.
stringify
(
userData
);
// 3. 生成文件名
const
timestamp
=
Math
.
floor
(
Date
.
now
()
/
3600000
)
*
3600
;
// 计算整点的时间戳
const
directory
=
`user-rank/
${
timestamp
}
`
;
const
fileName
=
`
${
directory
}
/user_top_100.json`
;
const
bucketName
=
'
cache
'
;
// 替换为你的存储桶名称
const
{
error
:
uploadError
}
=
await
supabase
.
storage
.
from
(
bucketName
)
.
upload
(
fileName
,
new
Blob
([
jsonData
]),
{
contentType
:
'
application/json
'
,
upsert
:
true
,
// 如果文件已存在则覆盖
});
if
(
uploadError
)
{
console
.
error
(
'
Error uploading file to storage:
'
,
uploadError
);
}
const
holdersCountFileName
=
`
${
directory
}
/user_holders.json`
;
let
holdersCount
=
await
count_holders
(
supabase
);
const
holdersJson
=
JSON
.
stringify
({
holders_count
:
holdersCount
});
const
{
error
:
uploadHolderError
}
=
await
supabase
.
storage
.
from
(
bucketName
)
.
upload
(
holdersCountFileName
,
new
Blob
([
holdersJson
]),
{
contentType
:
'
application/json
'
,
upsert
:
true
,
// 如果文件已存在则覆盖
});
if
(
uploadHolderError
)
{
console
.
error
(
'
Error uploading file to storage:
'
,
uploadHolderError
);
}
}
Deno
.
serve
(
async
(
req
)
=>
{
if
(
req
.
method
===
'
OPTIONS
'
)
{
return
new
Response
(
'
ok
'
,
{
headers
:
corsHeaders
})
...
...
@@ -29,51 +76,7 @@ Deno.serve(async (req) => {
{
global
:
{
headers
:
{
Authorization
:
req
.
headers
.
get
(
'
Authorization
'
)
!
}
}
}
)
const
{
data
:
userData
,
error
:
userError
}
=
await
supabase
.
from
(
'
user
'
).
select
(
'
*
'
).
gt
(
'
rank
'
,
0
).
order
(
'
rank
'
,
{
ascending
:
true
}).
limit
(
100
);
if
(
userError
)
{
console
.
error
(
'
Error fetching top 100 users:
'
,
error
);
return
new
Response
(
JSON
.
stringify
({
error
:
'
Failed to fetch users
'
}),
{
status
:
500
});
}
// 2. 将数据 JSON 序列化
const
jsonData
=
JSON
.
stringify
(
userData
);
// 3. 生成文件名
const
timestamp
=
Math
.
floor
(
Date
.
now
()
/
3600000
)
*
3600
;
// 计算整点的时间戳
const
directory
=
`user-rank/
${
timestamp
}
`
;
const
fileName
=
`
${
directory
}
/user_top_100.json`
;
const
bucketName
=
'
cache
'
;
// 替换为你的存储桶名称
const
{
error
:
uploadError
}
=
await
supabase
.
storage
.
from
(
bucketName
)
.
upload
(
fileName
,
new
Blob
([
jsonData
]),
{
contentType
:
'
application/json
'
,
upsert
:
true
,
// 如果文件已存在则覆盖
});
if
(
uploadError
)
{
console
.
error
(
'
Error uploading file to storage:
'
,
uploadError
);
return
new
Response
(
JSON
.
stringify
({
error
:
'
Failed to upload file
'
}),
{
status
:
500
});
}
const
holdersCountFileName
=
`
${
directory
}
/user_holders.json`
;
let
holdersCount
=
await
count_holders
(
supabase
);
const
holdersJson
=
JSON
.
stringify
({
holders_count
:
holdersCount
});
const
{
error
:
uploadHolderError
}
=
await
supabase
.
storage
.
from
(
bucketName
)
.
upload
(
holdersCountFileName
,
new
Blob
([
holdersJson
]),
{
contentType
:
'
application/json
'
,
upsert
:
true
,
// 如果文件已存在则覆盖
});
if
(
uploadHolderError
)
{
console
.
error
(
'
Error uploading file to storage:
'
,
uploadHolderError
);
return
new
Response
(
JSON
.
stringify
({
error
:
'
Failed to upload file
'
}),
{
status
:
500
});
}
EdgeRuntime
.
waitUntil
(
cacheUserRank
(
supabase
));
return
new
Response
(
JSON
.
stringify
({
code
:
200
,
...
...
@@ -87,15 +90,3 @@ Deno.serve(async (req) => {
return
new
Response
(
String
(
err
?.
message
??
err
),
{
status
:
500
})
}
})
/* To invoke locally:
1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start)
2. Make an HTTP request:
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello-world' \
--header 'Authorization: Bearer ' \
--header 'Content-Type: application/json' \
--data '{"name":"Functions"}'
*/
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment