-
Notifications
You must be signed in to change notification settings - Fork 2
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 #59 from Giveth/add-rpgf3-projects
Add rpgf3 projects
- Loading branch information
Showing
12 changed files
with
228 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
export const GIVETH_API_URL = | ||
process.env.GIVETH_API_URL || "https://mainnet.serve.giveth.io/graphql"; | ||
|
||
export const givethSourceConfig: SourceConfig = { | ||
source: "giveth", | ||
idField: "id", | ||
titleField: "title", | ||
descriptionField: "descriptionSummary", | ||
slugField: "slug", | ||
imageField: "image", | ||
}; |
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,92 +1,12 @@ | ||
import { DataSource } from "typeorm"; | ||
import { getDataSource } from "../../../helpers/db"; | ||
import { Project } from "../../../model"; | ||
import { fetchGivethProjectsBatch } from "./service"; | ||
import { GivethProjectInfo } from "./type"; | ||
import { type GivethProjectInfo } from "./type"; | ||
import { updateOrCreateProject } from "../helpers"; | ||
import { givethSourceConfig } from "./constants"; | ||
|
||
const updateOrCreateProject = async ( | ||
dataSource: DataSource, | ||
project: GivethProjectInfo | ||
export const processProjectsBatch = async ( | ||
projectsBatch: GivethProjectInfo[] | ||
) => { | ||
const existingProject = await dataSource | ||
.getRepository(Project) | ||
.createQueryBuilder("project") | ||
.where("project.id = :id", { id: `giveth-${project.id}` }) | ||
.getOne(); | ||
|
||
if (existingProject) { | ||
const isUpdated = | ||
existingProject.title !== project.title || | ||
existingProject.description !== project.descriptionSummary || | ||
existingProject.slug !== project.slug || | ||
existingProject.image !== project.image; | ||
|
||
if (isUpdated) { | ||
const updatedProject = new Project({ | ||
...existingProject, | ||
title: project.title, | ||
description: project.descriptionSummary, | ||
image: project.image, | ||
slug: project.slug, | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.update(Project) | ||
.set(updatedProject) | ||
.where("id = :id", { id: `giveth-${project.id}` }) | ||
.execute(); | ||
} | ||
} else { | ||
const newProject = new Project({ | ||
id: `giveth-${project.id}`, | ||
title: project.title, | ||
description: project.descriptionSummary, | ||
image: project.image, | ||
slug: project.slug, | ||
projectId: project.id, | ||
source: "giveth", | ||
totalVouches: 0, | ||
totalFlags: 0, | ||
totalAttests: 0, | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(Project) | ||
.values([newProject]) | ||
.execute(); | ||
} | ||
}; | ||
|
||
const processProjectsBatch = async (projectsBatch: GivethProjectInfo[]) => { | ||
const dataSource = await getDataSource(); | ||
if (!dataSource) return; | ||
for (const project of projectsBatch) { | ||
console.log("Processing Project: Giveth", project.id); | ||
await updateOrCreateProject(dataSource, project); | ||
} | ||
}; | ||
|
||
export const fetchAndProcessGivethProjects = async () => { | ||
try { | ||
let hasMoreProjects = true; | ||
let skip = 0; | ||
const limit = 10; | ||
|
||
while (hasMoreProjects) { | ||
const projectsBatch = await fetchGivethProjectsBatch(limit, skip); | ||
if (projectsBatch.length > 0) { | ||
await processProjectsBatch(projectsBatch); | ||
skip += limit; | ||
} else { | ||
hasMoreProjects = false; | ||
} | ||
} | ||
} catch (error: any) { | ||
console.log("error on fetching giveth projects", error.message); | ||
await updateOrCreateProject(project, givethSourceConfig); | ||
} | ||
}; |
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,22 @@ | ||
import { processProjectsBatch } from "./helpers"; | ||
import { fetchGivethProjectsBatch } from "./service"; | ||
|
||
export const fetchAndProcessGivethProjects = async () => { | ||
try { | ||
let hasMoreProjects = true; | ||
let skip = 0; | ||
const limit = 10; | ||
|
||
while (hasMoreProjects) { | ||
const projectsBatch = await fetchGivethProjectsBatch(limit, skip); | ||
if (projectsBatch.length > 0) { | ||
await processProjectsBatch(projectsBatch); | ||
skip += limit; | ||
} else { | ||
hasMoreProjects = false; | ||
} | ||
} | ||
} catch (error: any) { | ||
console.log("error on fetchAndProcessGivethProjects", error.message); | ||
} | ||
}; |
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,88 @@ | ||
import { type DataSource } from "typeorm"; | ||
import { Project } from "../../model"; | ||
import { getDataSource } from "../../helpers/db"; | ||
|
||
export const updateOrCreateProject = async ( | ||
project: any, | ||
sourConfig: SourceConfig | ||
) => { | ||
const { | ||
source, | ||
idField, | ||
titleField, | ||
descriptionField, | ||
slugField, | ||
imageField, | ||
} = sourConfig; | ||
|
||
const id = `${source}-${project[idField]}`; | ||
|
||
const dataSource = await getDataSource(); | ||
if (!dataSource) { | ||
console.log( | ||
`[${new Date().toISOString()}] - ERROR: Failed to UPSERT project. Data source not found. Project ID: ${id}` | ||
); | ||
return; | ||
} | ||
|
||
const existingProject = await dataSource | ||
.getRepository(Project) | ||
.createQueryBuilder("project") | ||
.where("project.id = :id", { id }) | ||
.getOne(); | ||
|
||
if (existingProject) { | ||
const isUpdated = | ||
existingProject.title !== project[titleField] || | ||
existingProject.description !== project[descriptionField] || | ||
existingProject.slug !== project[slugField] || | ||
existingProject.image !== project[imageField]; | ||
|
||
if (isUpdated) { | ||
const updatedProject = new Project({ | ||
...existingProject, | ||
title: project[titleField], | ||
description: project[descriptionField], | ||
image: project[imageField], | ||
slug: project[slugField], | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.update(Project) | ||
.set(updatedProject) | ||
.where("id = :id", { id }) | ||
.execute(); | ||
|
||
console.log( | ||
`[${new Date().toISOString()}] - INFO: Project Updated. Project ID: ${id}` | ||
); | ||
} | ||
} else { | ||
const newProject = new Project({ | ||
id, | ||
title: project[titleField], | ||
description: project[descriptionField], | ||
image: project[imageField], | ||
slug: project[slugField], | ||
projectId: project[idField], | ||
source: source, | ||
totalVouches: 0, | ||
totalFlags: 0, | ||
totalAttests: 0, | ||
lastUpdatedTimestamp: new Date(), | ||
}); | ||
|
||
await dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(Project) | ||
.values([newProject]) | ||
.execute(); | ||
|
||
console.log( | ||
`[${new Date().toISOString()}] - INFO: Project Created. Project ID: ${id}` | ||
); | ||
} | ||
}; |
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,11 @@ | ||
export const RPGF3_API_URL = | ||
process.env.RPGF3_API_URL || "https://backend.pairwise.vote/mock/projects"; | ||
|
||
export const rpgf3SourceConfig: SourceConfig = { | ||
source: "rpgf3", | ||
idField: "RPGF3Id", | ||
titleField: "name", | ||
descriptionField: "impactDescription", | ||
slugField: "url", | ||
imageField: "image", | ||
}; |
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,17 @@ | ||
import { getDataSource } from "../../../helpers/db"; | ||
import { updateOrCreateProject } from "../helpers"; | ||
import { rpgf3SourceConfig } from "./constants"; | ||
import { fetchRpgf3Projects } from "./service"; | ||
|
||
export const fetchAndProcessRpgf3Projects = async () => { | ||
try { | ||
const data = await fetchRpgf3Projects(); | ||
if (!data) return; | ||
for (const project of data) { | ||
if (project.type.toLowerCase() !== "project") continue; | ||
await updateOrCreateProject(project, rpgf3SourceConfig); | ||
} | ||
} catch (error: any) { | ||
console.log("error on fetchAndProcessRpgf3Projects", error.message); | ||
} | ||
}; |
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,19 @@ | ||
import { RPGF3_API_URL } from "./constants"; | ||
import { Rpgf3ProjectInfo } from "./type"; | ||
|
||
export const fetchRpgf3Projects = async () => { | ||
try { | ||
const res = await fetch(RPGF3_API_URL, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
const data: Rpgf3ProjectInfo[] = await res.json(); | ||
|
||
return data; | ||
} catch (error: any) { | ||
console.log("error on fetching fetchRpgf3Projects", error.message); | ||
return null; | ||
} | ||
}; |
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,14 @@ | ||
export type Rpgf3ProjectInfo = { | ||
id: string; | ||
name: string; | ||
poll_id: string; | ||
url: string; | ||
impactDescription: string; | ||
contributionDescription: string; | ||
RPGF3Id: string; | ||
parentId: string; | ||
image: string; | ||
metadataUrl: string; | ||
created_at: string; | ||
type: string; | ||
}; |
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,8 @@ | ||
interface SourceConfig { | ||
source: string; | ||
idField: string; | ||
titleField: string; | ||
descriptionField: string; | ||
slugField: string; | ||
imageField: string; | ||
} |