Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: LineageOS device and version mapping #8

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const getDevices = async ({
paginatedBaseQuery.andWhere({ 'roms_devices_mapping.status': status });

if (searchTerm.length >= 3) {
const searchTermNormalized = `*${searchTerm.split(/[ ]/).join('*')}*`;
paginatedBaseQuery.andWhere(function () {
this.whereRaw(
'roms_devices_mapping.id in ?',
Expand All @@ -152,15 +153,22 @@ const getDevices = async ({
FROM
roms_search_index
WHERE
keywords Match '${searchTerm}')`
keywords Match ?)`,
searchTermNormalized
)
);
});
}

const countData = await db('roms_devices_mapping').count(
'roms_devices_mapping.id'
);
const countData = await paginatedBaseQuery
.clone()
.clearSelect()
.clearOrder()
.clearCounters()
.clear('limit')
.clear('offset')
.count('roms_devices_mapping.id')
.first();

const q = db
.from(paginatedBaseQuery)
Expand Down Expand Up @@ -194,7 +202,7 @@ const getDevices = async ({

return {
deviceList: [...normalized.values()],
count: countData[0]['count(`roms_devices_mapping`.`id`)'],
count: countData['count(`roms_devices_mapping`.`id`)'],
};
};

Expand Down
72 changes: 70 additions & 2 deletions scripts/sync-lineage-os.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,88 @@
#!/usr/bin/env node
const got = require('got');
const _got = require('got');
const kluer = require('kleur');
const { logcons } = require('logcons');
const { STATUS_ENUM } = require('../db/status_enum');
const { upsertDevice } = require('../lib/sdk');
const { parse } = require('yaml');
const { dateStringToDate } = require('../lib/date-utils');

const success = kluer.green().bold;

const versionMap = {
'13.0': 6,
'14.1': 7.1,
'15.1': 8.1,
'16.0': 9,
'17.1': 10,
'18.1': 11,
'19.1': 12,
'20': 13,
'21': 14,
};

const URL =
'https://raw.githubusercontent.com/LineageOS/hudson/main/updater/devices.json';

const getDeviceDetailsUrl = deviceId =>
`https://ungh.cc/repos/LineageOS/lineage_wiki/files/main/_data/devices/${deviceId}`;
const getAllDevices = async () => {
const data = await _got(
'https://ungh.cc/repos/LineageOS/lineage_wiki/files/main'
).json();

const devicePromises = data.files
.filter(d => {
return d.path.startsWith('_data/devices');
})
.map(async d => {
const [_, _a, name] = d.path.split('/');

const deviceDetails = (await got(getDeviceDetailsUrl(name)).json()).file
.contents;
const parsedDetails = parse(deviceDetails);
const codename = name.replace(/_variant(\d+)/, '').replace(/.yml$/, '');
let releaseDate = '';
if (typeof parsedDetails.release === 'object') {
// const [firstKey] = Object.keys(parsedDetails.release);
// releaseDate = dateStringToDate(parsedDetails.release[firstKey]);
} else {
releaseDate = dateStringToDate(parsedDetails.release);
}

const versions = (parsedDetails.versions ?? []).map(d => {
return versionMap[d] ?? 'N/A';
});
return {
codename,
release: releaseDate,
versions,
};
});

const result = await Promise.all(devicePromises);
return result.reduce((acc, item) => {
acc[item.codename] = item;
return acc;
}, {});
};

async function main() {
const deviceDetails = await getAllDevices();

const response = await got(URL);

const promises = (JSON.parse(response.body) || []).map(async deviceItem => {
const codename = deviceItem.model;
const details = deviceDetails[codename];
const deviceName = `${deviceItem.oem} ${deviceItem.name}`;

await upsertDevice({
deviceName,
codename,
rom: {
status: STATUS_ENUM.unknown,
androidVersion: ['N/A'],
androidVersion: details?.versions || ['N/A'],
links: [`https://download.lineageos.org/${codename}`],
name: 'LineageOS',
},
Expand All @@ -34,6 +94,14 @@ async function main() {
console.log(success(`${logcons.tick()} Done, Syncing Lineage OS`));
}

function got(url) {
return _got(url, {
headers: {
Authorization: `Bearer ${process.env.GH_TOKEN}`,
},
});
}

exports.syncLineageOS = main;

if (require.main === module) {
Expand Down
40 changes: 23 additions & 17 deletions scripts/sync-search-index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
const {conch} = require("@barelyreaper/conch");
const { db } = require("../db/db");
const { conch } = require('@barelyreaper/conch');
const { db } = require('../db/db');

async function main() {
await db("roms_search_index").del();
const romData = await db("roms_devices_mapping")
.leftJoin("roms", "roms_devices_mapping.rom_id", "roms.id")
.leftJoin("devices", "roms_devices_mapping.device_id", "devices.id")
await db('roms_search_index').del();
const romData = await db('roms_devices_mapping')
.leftJoin('roms', 'roms_devices_mapping.rom_id', 'roms.id')
.leftJoin('devices', 'roms_devices_mapping.device_id', 'devices.id')
.select(
"roms_devices_mapping.*",
"roms.name as rom_name",
"devices.basename as device_name",
"devices.codename as device_codename",
'roms_devices_mapping.*',
'roms.name as rom_name',
'devices.basename as device_name',
'devices.codename as device_codename'
);

await conch(romData, async (row) => {
const execChain = await db("roms_search_index").insert({
rom_mapping_id: row.id,
keywords: [row.rom_name, row.device_name, row.device_codename].join(", "),
});
}, { limit: 20 });
await conch(
romData,
async row => {
await db('roms_search_index').insert({
rom_mapping_id: row.id,
keywords: [row.rom_name, row.device_name, row.device_codename].join(
', '
),
});
},
{ limit: 20 }
);
}

exports.syncSearchIndex = main
exports.syncSearchIndex = main;

if (require.main === module) main().then(() => process.exit(0));