-
Notifications
You must be signed in to change notification settings - Fork 8
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 #2153 from ujh/refactor-clustering-app
Refactor clustering app
- Loading branch information
Showing
18 changed files
with
339 additions
and
100 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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 @@ | ||
import _ from "lodash"; | ||
|
||
export const groupedInks = (collectedInks) => | ||
_.values( | ||
_.mapValues( | ||
_.groupBy(collectedInks, (ci) => | ||
["brand_name", "line_name", "ink_name"].map((n) => ci[n]).join(",") | ||
), | ||
(cis) => cis[0] | ||
) | ||
); |
15 changes: 15 additions & 0 deletions
15
app/javascript/src/admin/micro-clusters/groupedInks.spec.js
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,15 @@ | ||
import { groupedInks } from "./groupedInks"; | ||
|
||
describe("groupedInks", () => { | ||
it("returns only unique inks", () => { | ||
const inks = [ | ||
{ brand_name: "brand1", line_name: "line1", ink_name: "ink1" }, | ||
{ brand_name: "brand1", line_name: "line1", ink_name: "ink1" }, | ||
{ brand_name: "brand2", line_name: "line1", ink_name: "ink1" } | ||
]; | ||
expect(groupedInks(inks)).toStrictEqual([ | ||
{ brand_name: "brand1", line_name: "line1", ink_name: "ink1" }, | ||
{ brand_name: "brand2", line_name: "line1", ink_name: "ink1" } | ||
]); | ||
}); | ||
}); |
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,75 @@ | ||
import Jsona from "jsona"; | ||
|
||
import { getRequest } from "../../fetch"; | ||
import { | ||
SET_LOADING_PERCENTAGE, | ||
SET_MACRO_CLUSTERS, | ||
SET_MICRO_CLUSTERS | ||
} from "../components/clustering/actions"; | ||
import { groupedInks } from "./groupedInks"; | ||
|
||
export const loadMicroClusters = (dispatch) => { | ||
const formatter = new Jsona(); | ||
let data = []; | ||
function run(page = 1) { | ||
loadMicroClusterPage(page).then((json) => { | ||
const next_page = json.meta.pagination.next_page; | ||
// Remove clusters without collected inks | ||
// Group collected inks | ||
const pageData = formatter | ||
.deserialize(json) | ||
.filter((c) => c.collected_inks.length > 0) | ||
.map((c) => { | ||
const grouped_collected_inks = groupedInks(c.collected_inks); | ||
return { ...c, grouped_collected_inks }; | ||
}); | ||
data = [...data, ...pageData]; | ||
if (next_page) { | ||
run(next_page); | ||
} else { | ||
dispatch({ type: SET_MICRO_CLUSTERS, payload: data }); | ||
} | ||
}); | ||
} | ||
run(); | ||
}; | ||
|
||
const loadMicroClusterPage = (page) => { | ||
return getRequest( | ||
`/admins/micro_clusters.json?unassigned=true&without_ignored=true&page=${page}` | ||
).then((response) => response.json()); | ||
}; | ||
|
||
export const loadMacroClusters = (dispatch) => { | ||
let data = []; | ||
const formatter = new Jsona(); | ||
function run(page = 1) { | ||
loadMacroClusterPage(page).then((json) => { | ||
const pagination = json.meta.pagination; | ||
dispatch({ | ||
type: SET_LOADING_PERCENTAGE, | ||
payload: (pagination.current_page * 100) / pagination.total_pages | ||
}); | ||
const next_page = json.meta.pagination.next_page; | ||
const pageData = formatter.deserialize(json).map((c) => { | ||
const grouped_collected_inks = groupedInks( | ||
c.micro_clusters.map((c) => c.collected_inks).flat() | ||
); | ||
return { ...c, grouped_collected_inks }; | ||
}); | ||
data = [...data, ...pageData]; | ||
if (next_page) { | ||
run(next_page); | ||
} else { | ||
dispatch({ type: SET_MACRO_CLUSTERS, payload: data }); | ||
} | ||
}); | ||
} | ||
run(); | ||
}; | ||
|
||
const loadMacroClusterPage = (page) => { | ||
return getRequest(`/admins/macro_clusters.json?page=${page}`).then( | ||
(response) => response.json() | ||
); | ||
}; |
Oops, something went wrong.