From 7b2553e82c7a0d8ea34c358cda9474583908c03d Mon Sep 17 00:00:00 2001 From: Dan Pasanen Date: Sun, 5 Nov 2017 09:03:48 -0600 Subject: [PATCH] updater: provide incrementals if any are found for the given version * No functionality change if no incrementals are in the database, but if there are for the user's given version, those are provided rather than full roms. The future is now. --- README.md | 4 ++-- updater/app.py | 10 ++++++++-- updater/database.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ba412f4..a1fc8e5 100644 --- a/README.md +++ b/README.md @@ -70,10 +70,10 @@ Options: Example API Calls: --- Obtaining rom list for a device:
-`GET /api/v1///?after=&version=<14.1>` (incremental can be anything, it is currently unused)
+`GET /api/v1///?after=&version=<14.1>` `` - Name of device. Example: `d2vzw`
`` - Type of rom. Example: `nightly`
-`` - Caller device's incremental ID (ro.build.incr). Can be anything.
+`` - Caller device's incremental ID (ro.build.incr). If any zips are available that updates the given incremental version, those are provided rather than full roms.
`` - Timestamp for current build on device. (optional)
`` - Version of rom. Example: `14.1`(optional)
diff --git a/updater/app.py b/updater/app.py index 817028f..afe5383 100755 --- a/updater/app.py +++ b/updater/app.py @@ -2,7 +2,7 @@ from updater.changelog.gerrit import GerritServer, GerritJSONEncoder from updater.changelog import get_changes, get_timestamp -from updater.database import Rom, ApiKey, Device +from updater.database import Rom, ApiKey, Device, Incremental from flask import Flask, jsonify, request, abort, render_template from flask_mongoengine import MongoEngine @@ -101,7 +101,13 @@ def check_builds(): @cache.memoize(timeout=3600) def get_build_types(device, romtype, after, version): - roms = Rom.get_roms(device=device, romtype=romtype, before=app.config['BUILD_SYNC_TIME']) + roms = [] + + roms = Incremental.get_incrementals(device=device, romtype=romtype, before=app.config['BUILD_SYNC_TIME'], incremental=incrementalversion) + + if not len(roms): + roms = Rom.get_roms(device=device, romtype=romtype, before=app.config['BUILD_SYNC_TIME']) + if after: roms = roms(datetime__gt=after) if version: diff --git a/updater/database.py b/updater/database.py index 1f7592a..37939f6 100755 --- a/updater/database.py +++ b/updater/database.py @@ -52,6 +52,54 @@ def get_device_version(cls, device): return None return cls.objects(device=device).first()['version'] +class Incremental(Document): + filename = StringField(required=True) + datetime = DateTimeField(required=True, default=default_time) + device = StringField(required=True) + version = StringField(required=True) + romtype = StringField(required=True) + md5sum = StringField(required=True) + url = StringField() + from_incremental = StringField(required=True) + to_incremental = StringField(required=True) + + @classmethod + def get_incrementals(cls, device, romtype=None, before=3600, incremental=None): + args = { + 'device': device, + 'romtype': romtype, + 'datetime__lt': datetime.now()-timedelta(seconds=before), + 'from_incremental': incremental + } + if before == 0: + del args['datetime__lt'] + if not romtype: + del args['romtype'] + return cls.objects(**args).order_by('-datetime') + + @classmethod + def get_devices(cls): + #TODO change this to an aggregate + return cls.objects().distinct(field="device") + + @classmethod + def get_types(cls, device): + return cls.objects().distinct(field="romtype") + + @classmethod + def get_current_devices_by_version(cls): + # db.rom.aggregate({'$group': {'_id': '$version', 'device': {'$push': '$device'}}}) + versions = {} + for version in cls.objects().aggregate({'$group': {'_id': '$version', 'devices': {'$push': '$device'}}}): + versions[version['_id']] = version['devices'] + return versions + + @classmethod + def get_device_version(cls, device): + if not device: + return None + return cls.objects(device=device).first()['version'] + class Device(Document): model = StringField(required=True, unique=True) oem = StringField(required=True)